diff --git a/capi/geos_c.cpp b/capi/geos_c.cpp index 04e6c4fd3..d1f820d19 100644 --- a/capi/geos_c.cpp +++ b/capi/geos_c.cpp @@ -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() diff --git a/capi/geos_c.h.in b/capi/geos_c.h.in index 210fd014e..781ff7c13 100644 --- a/capi/geos_c.h.in +++ b/capi/geos_c.h.in @@ -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 */ diff --git a/include/geos/io/WKTWriter.h b/include/geos/io/WKTWriter.h index 09f6184ef..a37973a87 100644 --- a/include/geos/io/WKTWriter.h +++ b/include/geos/io/WKTWriter.h @@ -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; diff --git a/src/io/WKTWriter.cpp b/src/io/WKTWriter.cpp index 77d4b20dc..def315a44 100644 --- a/src/io/WKTWriter.cpp +++ b/src/io/WKTWriter.cpp @@ -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(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; @@ -433,6 +436,14 @@ WKTWriter::writeNumber(double d) const } } +/* protected */ +std::string +WKTWriter::writeNumber(double d) const +{ + uint32_t precision = decimalPlaces >= 0 ? static_cast(decimalPlaces) : 0; + return writeNumber(d, trim, precision); +} + void WKTWriter::appendLineStringText(const LineString& lineString, OrdinateSet outputOrdinates, int p_level, bool doIndent, Writer& writer) const