[postgis-tickets] r16329 - ST_AsText now has second argument to limit decimal digits.
Darafei
komzpa at gmail.com
Thu Jan 18 04:49:42 PST 2018
Author: komzpa
Date: 2018-01-18 04:49:41 -0800 (Thu, 18 Jan 2018)
New Revision: 16329
Modified:
trunk/NEWS
trunk/doc/reference_output.xml
trunk/postgis/geography.sql.in
trunk/postgis/lwgeom_ogc.c
trunk/postgis/postgis.sql.in
trunk/regress/out_geography.sql
trunk/regress/out_geography_expected
trunk/regress/out_geometry.sql
trunk/regress/out_geometry_expected
Log:
ST_AsText now has second argument to limit decimal digits.
Patch by Marc Ducobu, reworked by Darafei Praliaskouski.
Closes #3986
Closes https://github.com/postgis/postgis/pull/194
Modified: trunk/NEWS
===================================================================
--- trunk/NEWS 2018-01-17 23:23:15 UTC (rev 16328)
+++ trunk/NEWS 2018-01-18 12:49:41 UTC (rev 16329)
@@ -35,6 +35,8 @@
- #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
Modified: trunk/doc/reference_output.xml
===================================================================
--- trunk/doc/reference_output.xml 2018-01-17 23:23:15 UTC (rev 16328)
+++ trunk/doc/reference_output.xml 2018-01-18 12:49:41 UTC (rev 16329)
@@ -270,7 +270,7 @@
<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 @@
</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 @@
<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 @@
<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 @@
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>
@@ -930,15 +929,26 @@
</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 @@
</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>
@@ -970,6 +980,20 @@
--------------------------------
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>
<!-- Optionally add a "See Also" section -->
Modified: trunk/postgis/geography.sql.in
===================================================================
--- trunk/postgis/geography.sql.in 2018-01-17 23:23:15 UTC (rev 16328)
+++ trunk/postgis/geography.sql.in 2018-01-18 12:49:41 UTC (rev 16329)
@@ -102,6 +102,12 @@
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 @@
RETURNS geometry AS
$$ SELECT ST_Centroid($1::@extschema at .geometry); $$
LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;
-
------------------------------------------------------------------------------
Modified: trunk/postgis/lwgeom_ogc.c
===================================================================
--- trunk/postgis/lwgeom_ogc.c 2018-01-17 23:23:15 UTC (rev 16328)
+++ trunk/postgis/lwgeom_ogc.c 2018-01-18 12:49:41 UTC (rev 16329)
@@ -863,14 +863,17 @@
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));
Modified: trunk/postgis/postgis.sql.in
===================================================================
--- trunk/postgis/postgis.sql.in 2018-01-17 23:23:15 UTC (rev 16328)
+++ trunk/postgis/postgis.sql.in 2018-01-18 12:49:41 UTC (rev 16329)
@@ -4756,6 +4756,13 @@
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
Modified: trunk/regress/out_geography.sql
===================================================================
--- trunk/regress/out_geography.sql 2018-01-17 23:23:15 UTC (rev 16328)
+++ trunk/regress/out_geography.sql 2018-01-18 12:49:41 UTC (rev 16329)
@@ -140,6 +140,15 @@
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
--
DELETE FROM spatial_ref_sys;
Modified: trunk/regress/out_geography_expected
===================================================================
--- trunk/regress/out_geography_expected 2018-01-17 23:23:15 UTC (rev 16328)
+++ trunk/regress/out_geography_expected 2018-01-18 12:49:41 UTC (rev 16329)
@@ -70,3 +70,5 @@
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)
Modified: trunk/regress/out_geometry.sql
===================================================================
--- trunk/regress/out_geometry.sql 2018-01-17 23:23:15 UTC (rev 16328)
+++ trunk/regress/out_geometry.sql 2018-01-18 12:49:41 UTC (rev 16329)
@@ -173,7 +173,17 @@
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
--
DELETE FROM spatial_ref_sys;
Modified: trunk/regress/out_geometry_expected
===================================================================
--- trunk/regress/out_geometry_expected 2018-01-17 23:23:15 UTC (rev 16328)
+++ trunk/regress/out_geometry_expected 2018-01-18 12:49:41 UTC (rev 16329)
@@ -92,3 +92,5 @@
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)
More information about the postgis-tickets
mailing list