Skip to content

Commit

Permalink
Expose geos ryu fork (#919)
Browse files Browse the repository at this point in the history
Makes ryu number formatting accessible to c clients.
  • Loading branch information
Maxxen committed Jun 27, 2023
1 parent 95b1601 commit 54177f2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
5 changes: 5 additions & 0 deletions capi/geos_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,11 @@ extern "C" {
GEOSWKBWriter_setIncludeSRID_r(handle, writer, newIncludeSRID);
}

int
GEOS_printDouble(double d, unsigned int precision, char *result) {
return WKTWriter::writeTrimmedNumber(d, precision, result);
}

/* GeoJSON Reader */
GeoJSONReader*
GEOSGeoJSONReader_create()
Expand Down
13 changes: 13 additions & 0 deletions capi/geos_c.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,19 @@ extern void GEOS_DLL GEOSWKTWriter_setOld3D_r(
GEOSWKTWriter *writer,
int useOld3D);

/** Print the shortest representation of a double using fixed notation.
* Only works for numbers smaller than 1e17 (absolute value).
* \param d The number to format.
* \param precision The desired precision. (max digits after decimal point)
* \param result The buffer to write the result to.
* \return the length of the written string.
*/
extern int GEOS_DLL GEOS_printDouble(
double d,
unsigned int precision,
char *result
);

/* ========== WKB Reader ========== */

/** \see GEOSWKBReader_create */
Expand Down
3 changes: 3 additions & 0 deletions include/geos/io/WKTWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ class GEOS_DLL WKTWriter {
*/
void setOutputDimension(uint8_t newOutputDimension);

static std::string writeNumber(double d, bool trim, uint32_t precision);
static int writeTrimmedNumber(double d, uint32_t precision, char* buf);

protected:

int decimalPlaces;
Expand Down
21 changes: 16 additions & 5 deletions src/io/WKTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,21 @@ WKTWriter::appendSequenceText(const CoordinateSequence& seq,
}
}

/* protected */
std::string
WKTWriter::writeNumber(double d) const
int
WKTWriter::writeTrimmedNumber(double d, uint32_t precision, char* buf)
{
uint32_t precision = decimalPlaces >= 0 ? static_cast<std::uint32_t>(decimalPlaces) : 0;
return geos_d2sfixed_buffered_n(d, precision, buf);
}

std::string
WKTWriter::writeNumber(double d, bool trim, uint32_t precision) {
/*
* For a "trimmed" result, with no trailing zeros we use
* the ryu library.
*/
if (trim) {
char buf[128];
int len = geos_d2sfixed_buffered_n(d, precision, buf);
int len = writeTrimmedNumber(d, precision, buf);
buf[len] = '\0';
std::string s(buf);
return s;
Expand All @@ -433,6 +436,14 @@ WKTWriter::writeNumber(double d) const
}
}

/* protected */
std::string
WKTWriter::writeNumber(double d) const
{
uint32_t precision = decimalPlaces >= 0 ? static_cast<std::uint32_t>(decimalPlaces) : 0;
return writeNumber(d, trim, precision);
}

void
WKTWriter::appendLineStringText(const LineString& lineString, OrdinateSet outputOrdinates, int p_level,
bool doIndent, Writer& writer) const
Expand Down

0 comments on commit 54177f2

Please sign in to comment.