[gdal-dev] Geometry.ExportToWKT : numeric precision ?

Even Rouault even.rouault at mines-paris.org
Wed Apr 6 17:04:30 EDT 2011


Le mercredi 06 avril 2011 22:51:17, Jay Jennings a écrit :
> Hello list,
> Does anyone know of a way to set the numeric precision on a
> Geometry.ExportToWKT() call ?   For the SRS, we are using EPSG:4326
> (WGS-84) so the multipolygon coordinates are in latitude/longitude, and
> most of the coordinate strings seem to carry 15 digits past the decimal
> point !!   For our needs, 7 or 8 digits would be sufficient, so this seems
> wasteful, especially when the string is stored in a database.  I examined
> the OGR documentation but I don't see any variation of ExportToWKT nor any
> other means of setting the numeric precision.
> 
> I estimate using 7 digits of precision instead of 15 could reduce total
> storage by about 40%.  I realize I could post-process the string produced
> by ExportToWKT() to reduce all coordinates to the desired precision.  But
> did I miss anything in OGR capabilities ?
> 
> BTW we are using GDAL/OGR 1.8.0 with C# bindings.

No, there's no way of specifying the number of digits. However if you can 
afford patching and recompiling the code, it is a trivial change.

You could just change the following definition in ogr/ogr_p.h

void OGRFormatDouble( char *pszBuffer, int nBufferLen, double dfVal, char 
chDecimalSep, int nPrecision = 15 );

If you need a value that can be configured, you might instead 
changeOGRMakeWktCoordinate() in ogr/ogrutils.cpp

Index: ogr/ogrutils.cpp
===================================================================
--- ogr/ogrutils.cpp	(révision 22117)
+++ ogr/ogrutils.cpp	(copie de travail)
@@ -176,6 +176,8 @@
 
     int nLenX, nLenY;
 
+    int nPrecision = atoi(CPLGetConfigOption("OGR_WKT_PRECISION", "15"));
+
     if( x == (int) x && y == (int) y )
     {
         snprintf( szX, bufSize, "%d", (int) x );
@@ -183,8 +185,8 @@
     }
     else
     {
-        OGRFormatDouble( szX, bufSize, x, '.' );
-        OGRFormatDouble( szY, bufSize, y, '.' );
+        OGRFormatDouble( szX, bufSize, x, '.', nPrecision );
+        OGRFormatDouble( szY, bufSize, y, '.', nPrecision );
     }
 
     nLenX = strlen(szX);
@@ -198,7 +200,7 @@
         }
         else
         {
-            OGRFormatDouble( szZ, bufSize, z, '.' );
+            OGRFormatDouble( szZ, bufSize, z, '.', nPrecision );
         }
     }



> 
> Jay Jennings
> GeoEye, Inc.


More information about the gdal-dev mailing list