Skip to content

Commit

Permalink
GeoJSON writing: optimize speed of json_double_with_precision()
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Dec 3, 2024
1 parent ca5b2fa commit fe3f472
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions ogr/ogrlibjsonutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ogrlibjsonutils.h"

#include "cpl_string.h"
#include "ogr_geometry.h"
#include "ogr_p.h"

#include <cmath>
Expand Down Expand Up @@ -132,22 +133,28 @@ static int OGR_json_double_with_precision_to_string(struct json_object *jso,
json_object_get_userdata(jso);
#endif
// Precision is stored as a uintptr_t content casted to void*
const uintptr_t nPrecision = reinterpret_cast<uintptr_t>(userData);
char szBuffer[75] = {};
const uintptr_t nPrecisionIn = reinterpret_cast<uintptr_t>(userData);
const double dfVal = json_object_get_double(jso);
if (fabs(dfVal) > 1e50 && !std::isinf(dfVal))
{
char szBuffer[75] = {};
CPLsnprintf(szBuffer, sizeof(szBuffer), "%.17g", dfVal);
return printbuf_memappend(pb, szBuffer,
static_cast<int>(strlen(szBuffer)));
}
else
{
const bool bPrecisionIsNegative =
(nPrecision >> (8 * sizeof(nPrecision) - 1)) != 0;
OGRFormatDouble(szBuffer, sizeof(szBuffer), dfVal, '.',
bPrecisionIsNegative ? 15
: static_cast<int>(nPrecision));
(nPrecisionIn >> (8 * sizeof(nPrecisionIn) - 1)) != 0;
const int nPrecision =
bPrecisionIsNegative ? 15 : static_cast<int>(nPrecisionIn);
OGRWktOptions opts(nPrecision, /* round = */ true);
opts.format = OGRWktFormat::F;

std::string s = OGRFormatDouble(dfVal, opts, 1);

return printbuf_memappend(pb, s.data(), static_cast<int>(s.size()));
}
return printbuf_memappend(pb, szBuffer, static_cast<int>(strlen(szBuffer)));
}

/************************************************************************/
Expand Down

0 comments on commit fe3f472

Please sign in to comment.