]> granicus.if.org Git - postgis/commitdiff
ST_AsText now has second argument to limit decimal digits.
authorDarafei Praliaskouski <me@komzpa.net>
Thu, 18 Jan 2018 12:49:41 +0000 (12:49 +0000)
committerDarafei Praliaskouski <me@komzpa.net>
Thu, 18 Jan 2018 12:49:41 +0000 (12:49 +0000)
Patch by Marc Ducobu, reworked by Darafei Praliaskouski.

Closes #3986
Closes https://github.com/postgis/postgis/pull/194

git-svn-id: http://svn.osgeo.org/postgis/trunk@16329 b70326c6-7e19-0410-871a-916f4a2858ee

NEWS
doc/reference_output.xml
postgis/geography.sql.in
postgis/lwgeom_ogc.c
postgis/postgis.sql.in
regress/out_geography.sql
regress/out_geography_expected
regress/out_geometry.sql
regress/out_geometry_expected

diff --git a/NEWS b/NEWS
index c9b485cbf99a71c8f0bc96791c38bee971b2eb95..30cbacd51c6ca59f6b98e10113636630e4440d3d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -35,6 +35,8 @@ PostGIS 2.5.0
   - #3977, ST_ClusterKMeans is now faster and simpler (Darafei Praliaskouski)
   - #3982, ST_AsEncodedPolyline supports LINESTRING EMPTY and MULTIPOINT EMPTY
            (Darafei Praliaskouski)
+  - #3986, ST_AsText now has second argument to limit decimal digits 
+           (Marc Ducobu, Darafei Praliaskouski)
 
 PostGIS 2.4.0
 2017/09/30
index bb3f8a675ffb117324d8ea987df9cb13b949dc85..e87a7d8a12f538c56f9d62d92576b1f3d30b72bd 100644 (file)
                        <para>Returns the Well-Known Text representation of the geometry prefixed with the SRID.</para>
 
                        <note>
-                         <para>The WKT spec does not include the SRID.  To get the OGC WKT format use ST_AsText</para>
+                         <para>The WKT spec does not include the SRID.  To get the OGC WKT format use ST_AsText.</para>
                        </note>
                        <para><inlinegraphic fileref="images/warning.png" />
                                WKT format does not maintain precision so to prevent floating truncation, use ST_AsBinary or ST_AsEWKB format for transport.
@@ -390,12 +390,12 @@ CIRCULARSTRING(220268 150415 1,220227 150505 2,220227 150406 3)
                                </listitem>
                          </itemizedlist>
                        </para>
-                       <para>Version 1: ST_AsGeoJSON(geom) / precision=15 version=1 options=0</para>
-                       <para>Version 2: ST_AsGeoJSON(geom, precision) / version=1 options=0</para>
-                       <para>Version 3: ST_AsGeoJSON(geom, precision, options) / version=1</para>
-                       <para>Version 4: ST_AsGeoJSON(gj_version, geom) / precision=15 options=0</para>
-                       <para>Version 5: ST_AsGeoJSON(gj_version, geom, precision) /options=0</para>
-                       <para>Version 6: ST_AsGeoJSON(gj_version, geom, precision,options)</para>
+                       <para>Version 1: ST_AsGeoJSON(geom) / maxdecimaldigits=15 version=1 options=0</para>
+                       <para>Version 2: ST_AsGeoJSON(geom, maxdecimaldigits) / version=1 options=0</para>
+                       <para>Version 3: ST_AsGeoJSON(geom, maxdecimaldigits, options) / version=1</para>
+                       <para>Version 4: ST_AsGeoJSON(gj_version, geom) / maxdecimaldigits=15 options=0</para>
+                       <para>Version 5: ST_AsGeoJSON(gj_version, geom, maxdecimaldigits) / options=0</para>
+                       <para>Version 6: ST_AsGeoJSON(gj_version, geom, maxdecimaldigits, options)</para>
 
                        <para>Availability: 1.3.4</para>
                        <para>Availability: 1.5.0 geography support was introduced.</para>
@@ -476,8 +476,8 @@ st_asgeojson
 
                <para>Return the geometry as a Geography Markup Language (GML) element. The version parameter,
                        if specified, may be either 2 or 3. If no version parameter is
-                       specified then the default is assumed to be 2. The precision argument
-                       may be used to reduce the maximum number of decimal places (<varname>maxdecimaldigits</varname>)
+                       specified then the default is assumed to be 2. The <varname>maxdecimaldigits</varname> argument
+                       may be used to reduce the maximum number of decimal places
                        used in output (defaults to 15).</para>
                <para>GML 2 refer to 2.1.2 version, GML 3 to 3.1.1 version</para>
                <para>The 'options' argument is a bitfield. It could be used to define CRS output type
@@ -684,7 +684,7 @@ SELECT ST_AsGML(3, ST_GeomFromEWKT('POLYHEDRALSURFACE( ((0 0 0, 0 0 1, 0 1 1, 0
          <refnamediv>
                <refname>ST_AsKML</refname>
 
-               <refpurpose>Return the geometry as a KML element. Several variants. Default version=2, default precision=15</refpurpose>
+               <refpurpose>Return the geometry as a KML element. Several variants. Default version=2, default maxdecimaldigits=15</refpurpose>
          </refnamediv>
 
          <refsynopsisdiv>
@@ -915,8 +915,7 @@ SELECT (ST_AsLatLonText('POINT (-302.2342342 -792.32498)'));
                M 0 0 L 0 -1 1 -1 1 0 Z</programlisting>
          </refsection>
        </refentry>
-
-               <refentry id="ST_AsText">
+       <refentry id="ST_AsText">
                  <refnamediv>
                        <refname>ST_AsText</refname>
                        <refpurpose>Return the Well-Known Text (WKT) representation of the geometry/geography without SRID metadata.</refpurpose>
@@ -928,17 +927,28 @@ SELECT (ST_AsLatLonText('POINT (-302.2342342 -792.32498)'));
                                <funcdef>text <function>ST_AsText</function></funcdef>
                                <paramdef><type>geometry </type> <parameter>g1</parameter></paramdef>
                          </funcprototype>
+                         <funcprototype>
+                               <funcdef>text <function>ST_AsText</function></funcdef>
+                               <paramdef><type>geometry </type> <parameter>g1</parameter></paramdef>
+                               <paramdef choice="opt"><type>integer </type> <parameter>maxdecimaldigits=15</parameter></paramdef>
+                         </funcprototype>
                          <funcprototype>
                                <funcdef>text <function>ST_AsText</function></funcdef>
                                <paramdef><type>geography </type> <parameter>g1</parameter></paramdef>
                          </funcprototype>
+                         <funcprototype>
+                               <funcdef>text <function>ST_AsText</function></funcdef>
+                               <paramdef><type>geography </type> <parameter>g1</parameter></paramdef>
+                               <paramdef choice="opt"><type>integer </type> <parameter>maxdecimaldigits=15</parameter></paramdef>
+                         </funcprototype>
                        </funcsynopsis>
                  </refsynopsisdiv>
 
                  <refsection>
                        <title>Description</title>
 
-                       <para>Returns the Well-Known Text representation of the geometry/geography.</para>
+                       <para>Returns the Well-Known Text representation of the geometry/geography. Optional argument may be used to reduce the maximum number
+                       of decimal digits after floating point used in output (defaults to 15).</para>
 
                        <note>
                          <para>The WKT spec does not include the SRID.  To get the SRID as part of the data, use the non-standard
@@ -952,12 +962,12 @@ SELECT (ST_AsLatLonText('POINT (-302.2342342 -792.32498)'));
                        </note>
 
                        <para>Availability: 1.5 - support for geography was introduced.</para>
+                       <para>Enhanced: 2.5 - optional parameter precision introduced.</para>
                        <para>&sfs_compliant; s2.1.1.1</para>
                        <para>&sqlmm_compliant; SQL-MM 3: 5.1.25</para>
                        <para>&curve_support;</para>
                  </refsection>
 
-
                  <refsection>
                        <title>Examples</title>
 
@@ -969,6 +979,20 @@ F000000000000000000000000000000000000000000000000');
                   st_astext
 --------------------------------
  POLYGON((0 0,0 1,1 1,1 0,0 0))
+(1 row)</programlisting>
+
+                       <para>Providing the precision is optional.</para>
+
+                       <programlisting>SELECT ST_AsText(GeomFromEWKT('SRID=4326;POINT(111.1111111 1.1111111)'))
+          st_astext
+------------------------------
+ POINT(111.1111111 1.1111111)
+(1 row)</programlisting>
+
+                       <programlisting>SELECT ST_AsText(GeomFromEWKT('SRID=4326;POINT(111.1111111 1.1111111)'),2)
+st_astext
+--------------------
+POINT(111.11 1.11)
 (1 row)</programlisting>
                  </refsection>
 
index eeb205649866ebb58d34c4f3af70b06e9141fb13..bf3298ad270bada55189947682262b4585dc7034 100644 (file)
@@ -102,6 +102,12 @@ CREATE OR REPLACE FUNCTION ST_AsText(geography)
        AS 'MODULE_PATHNAME','LWGEOM_asText'
        LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;
 
+-- Availability: 2.5.0
+CREATE OR REPLACE FUNCTION ST_AsText(geography, int4)
+       RETURNS TEXT
+       AS 'MODULE_PATHNAME','LWGEOM_asText'
+       LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;
+
 -- Availability: 1.5.0 - this is just a hack to prevent unknown from causing ambiguous name because of geography
 CREATE OR REPLACE FUNCTION ST_AsText(text)
        RETURNS text AS
@@ -848,5 +854,3 @@ CREATE OR REPLACE FUNCTION ST_Centroid(text)
        RETURNS geometry AS
        $$ SELECT ST_Centroid($1::@extschema@.geometry);  $$
        LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;
-
------------------------------------------------------------------------------
index 39933495768a2939e86ffa9bb296733e1a86e85a..a3dc74e64bbd6a1dec7d273b55d4fcee9571ac62 100644 (file)
@@ -863,14 +863,17 @@ Datum LWGEOM_asText(PG_FUNCTION_ARGS)
        char *wkt;
        size_t wkt_size;
        text *result;
+       int dbl_dig_for_wkt = DBL_DIG;
 
        POSTGIS_DEBUG(2, "Called.");
 
        geom = PG_GETARG_GSERIALIZED_P(0);
        lwgeom = lwgeom_from_gserialized(geom);
 
+       if (PG_NARGS() > 1) dbl_dig_for_wkt = PG_GETARG_INT32(1);
+
        /* Write to WKT and free the geometry */
-       wkt = lwgeom_to_wkt(lwgeom, WKT_ISO, DBL_DIG, &wkt_size);
+       wkt = lwgeom_to_wkt(lwgeom, WKT_ISO, dbl_dig_for_wkt, &wkt_size);
        lwgeom_free(lwgeom);
        POSTGIS_DEBUGF(3, "WKT size = %u, WKT length = %u", (unsigned int)wkt_size, (unsigned int)strlen(wkt));
 
index ec661386f4a9f0e2f05b2899d80bbeee49fa832f..68ceec2fe33b6b92e7b6c010b6fd8b4b5890a1bf 100644 (file)
@@ -4756,6 +4756,13 @@ CREATE OR REPLACE FUNCTION ST_AsText(geometry)
        LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL
        COST 750; --guess
 
+-- Availability: 2.5.0
+-- PostGIS equivalent function: AsText(geometry, int4)
+CREATE OR REPLACE FUNCTION ST_AsText(geometry, int4)
+    RETURNS TEXT
+    AS 'MODULE_PATHNAME','LWGEOM_asText'
+    LANGUAGE 'c' IMMUTABLE STRICT;
+
 -- Availability: 1.2.2
 CREATE OR REPLACE FUNCTION ST_GeometryFromText(text)
        RETURNS geometry
index c4e2390a91a81688c318660abf5781d89d18d04a..4e5a87dfe6b1d254d348aa220c0dbf2dbf7e20a1 100644 (file)
@@ -139,6 +139,15 @@ SELECT 'geojson_options_14', ST_AsGeoJSON(geography(ST_GeomFromEWKT('SRID=4326;L
 SELECT 'geojson_options_15', ST_AsGeoJSON(geography(ST_GeomFromEWKT('LINESTRING(1 1, 2 2, 3 3, 4 4)')), 0, 7);
 SELECT 'geojson_options_16', ST_AsGeoJSON(geography(ST_GeomFromEWKT('SRID=4326;LINESTRING(1 1, 2 2, 3 3, 4 4)')), 0, 7);
 
+--
+-- Text
+--
+
+-- Precision
+SELECT 'text_precision_01', ST_AsText(geography(GeomFromEWKT('SRID=4326;POINT(111.1111111 1.1111111)')));
+SELECT 'text_precision_02', ST_AsText(geography(GeomFromEWKT('SRID=4326;POINT(111.1111111 1.1111111)')),2);
+
+
 --
 -- Delete inserted spatial data
 --
index c0b0201ad357d882e9d84388141cc5a98959581d..68cfc16053ed8e78d2863b78f215d3e6d45c2b04 100644 (file)
@@ -70,3 +70,5 @@ geojson_options_13|{"type":"LineString","crs":{"type":"name","properties":{"name
 geojson_options_14|{"type":"LineString","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::4326"}},"coordinates":[[1,1],[2,2],[3,3],[4,4]]}
 geojson_options_15|{"type":"LineString","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::4326"}},"bbox":[1,1,4,4],"coordinates":[[1,1],[2,2],[3,3],[4,4]]}
 geojson_options_16|{"type":"LineString","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::4326"}},"bbox":[1,1,4,4],"coordinates":[[1,1],[2,2],[3,3],[4,4]]}
+text_precision_01|POINT(111.1111111 1.1111111)
+text_precision_02|POINT(111.11 1.11)
index ca7a0ba82a51ed9bc6c7e3b164d0cf7d888934c5..6dafe7e70fee61601b50d423d3323724700c80ba 100644 (file)
@@ -173,6 +173,16 @@ SELECT 'pgcast_04','LINESTRING EMPTY'::geometry::path IS NULL;
 SELECT 'pgcast_05','POINT EMPTY'::geometry::point IS NULL;
 SELECT 'pgcast_06',ST_AsText('((0,0),(0,1),(1,1),(1,0))'::polygon::geometry);
 
+
+--
+-- Text
+--
+
+-- Precision
+SELECT 'text_precision_01', ST_AsText(GeomFromEWKT('SRID=4326;POINT(111.1111111 1.1111111)'));
+SELECT 'text_precision_02', ST_AsText(GeomFromEWKT('SRID=4326;POINT(111.1111111 1.1111111)'),2);
+
+
 --
 -- Delete inserted spatial data
 --
index adb3d0c126381ea042d17d040b65edb0989c28aa..6242bc67d810d7bebd923a6239d36564b92b2981 100644 (file)
@@ -92,3 +92,5 @@ pgcast_03|t
 pgcast_04|t
 pgcast_05|t
 pgcast_06|POLYGON((0 0,0 1,1 1,1 0,0 0))
+text_precision_01|POINT(111.1111111 1.1111111)
+text_precision_02|POINT(111.11 1.11)