[postgis-tickets] r14949 - Add ST_Normalize function

Sandro Santilli strk at kbt.io
Wed Jun 15 02:49:50 PDT 2016


Author: strk
Date: 2016-06-15 02:49:50 -0700 (Wed, 15 Jun 2016)
New Revision: 14949

Added:
   trunk/regress/normalize.sql
   trunk/regress/normalize_expected
Modified:
   trunk/NEWS
   trunk/doc/reference_editor.xml
   trunk/postgis/lwgeom_functions_basic.c
   trunk/postgis/postgis.sql.in
   trunk/regress/Makefile.in
Log:
Add ST_Normalize function

Includes tests and documentation

Closes #1768

Modified: trunk/NEWS
===================================================================
--- trunk/NEWS	2016-06-15 09:49:39 UTC (rev 14948)
+++ trunk/NEWS	2016-06-15 09:49:50 UTC (rev 14949)
@@ -17,6 +17,7 @@
   - Add parameters for geography ST_Buffer (Thomas Bonfort)
   - TopoGeom_addElement, TopoGeom_remElement (Sandro Santilli)
   - populate_topology_layer (Sandro Santilli)
+  - #1758, ST_Normalize (Sandro Santilli)
   - #2259, ST_Voronoi (Dan Baston)
   - #2991, Enable ST_Transform to use PROJ.4 text (Mike Toews)
   - #3059, Allow passing per-dimension parameters in ST_Expand (Dan Baston)

Modified: trunk/doc/reference_editor.xml
===================================================================
--- trunk/doc/reference_editor.xml	2016-06-15 09:49:39 UTC (rev 14948)
+++ trunk/doc/reference_editor.xml	2016-06-15 09:49:50 UTC (rev 14949)
@@ -897,6 +897,67 @@
 		</refsection>
 	</refentry>
 
+	<refentry id="ST_Normalize">
+		<refnamediv>
+			<refname>ST_Normalize</refname>
+
+			<refpurpose>Returns the geometry in its canonical form.</refpurpose>
+		</refnamediv>
+
+		<refsynopsisdiv>
+			<funcsynopsis>
+			  <funcprototype>
+				<funcdef>geometry <function>ST_Normalized</function></funcdef>
+				<paramdef><type>geometry </type> <parameter>geom</parameter></paramdef>
+			  </funcprototype>
+			</funcsynopsis>
+		</refsynopsisdiv>
+
+		<refsection>
+			<title>Description</title>
+
+			<para>
+        Returns the geometry in its normalized/canonical form.
+        May reorder vertices in polygon rings, rings in a polygon,
+        elements in a multi-geometry complex.
+      </para>
+
+			<para>
+        Mostly only useful for testing purposes (comparing expected
+        and obtained results).
+      </para>
+
+		</refsection>
+
+		<refsection>
+			<title>Examples</title>
+
+			<programlisting>
+SELECT ST_AsText(ST_Normalize(ST_GeomFromText(
+  'GEOMETRYCOLLECTION(
+    POINT(2 3),
+    MULTILINESTRING((0 0, 1 1),(2 2, 3 3)),
+    POLYGON(
+      (0 10,0 0,10 0,10 10,0 10),
+      (4 2,2 2,2 4,4 4,4 2),
+      (6 8,8 8,8 6,6 6,6 8)
+    )
+  )'
+)));
+                                                                     st_astext
+----------------------------------------------------------------------------------------------------------------------------------------------------
+ GEOMETRYCOLLECTION(POLYGON((0 0,0 10,10 10,10 0,0 0),(6 6,8 6,8 8,6 8,6 6),(2 2,4 2,4 4,2 4,2 2)),MULTILINESTRING((2 2,3 3),(0 0,1 1)),POINT(2 3))
+(1 row)
+			</programlisting>
+		</refsection>
+		<refsection>
+			<title>See Also</title>
+			<para>
+        <xref linkend="ST_Equals" />,
+      </para>
+		</refsection>
+	</refentry>
+
 	<refentry id="ST_RemovePoint">
 	  <refnamediv>
 		<refname>ST_RemovePoint</refname>

Modified: trunk/postgis/lwgeom_functions_basic.c
===================================================================
--- trunk/postgis/lwgeom_functions_basic.c	2016-06-15 09:49:39 UTC (rev 14948)
+++ trunk/postgis/lwgeom_functions_basic.c	2016-06-15 09:49:50 UTC (rev 14949)
@@ -1900,6 +1900,33 @@
 	PG_RETURN_POINTER(out);
 }
 
+Datum ST_Normalize(PG_FUNCTION_ARGS);
+PG_FUNCTION_INFO_V1(ST_Normalize);
+Datum ST_Normalize(PG_FUNCTION_ARGS)
+{
+	GSERIALIZED *in, *out;
+	LWGEOM *lwgeom_in, *lwgeom_out;
+
+	POSTGIS_DEBUG(2, "ST_Normalize called");
+
+	in = PG_GETARG_GSERIALIZED_P_COPY(0);
+
+	lwgeom_in = lwgeom_from_gserialized(in);
+	POSTGIS_DEBUGF(3, "Deserialized: %s", lwgeom_summary(lwgeom_in, 0));
+
+	lwgeom_out = lwgeom_normalize(lwgeom_in);
+	POSTGIS_DEBUGF(3, "Normalized: %s", lwgeom_summary(lwgeom_out, 0));
+
+	out = geometry_serialize(lwgeom_out);
+	lwgeom_free(lwgeom_in);
+	lwgeom_free(lwgeom_out);
+
+	PG_FREE_IF_COPY(in, 0);
+
+	PG_RETURN_POINTER(out);
+}
+
+
 /**
  *  @return:
  *   0==2d

Modified: trunk/postgis/postgis.sql.in
===================================================================
--- trunk/postgis/postgis.sql.in	2016-06-15 09:49:39 UTC (rev 14948)
+++ trunk/postgis/postgis.sql.in	2016-06-15 09:49:50 UTC (rev 14949)
@@ -1477,6 +1477,13 @@
 	LANGUAGE 'c' VOLATILE STRICT
 	COST 10;
 
+-- Availability: 2.3.0
+CREATE OR REPLACE FUNCTION ST_Normalize(geom geometry)
+	RETURNS geometry
+	AS 'MODULE_PATHNAME', 'ST_Normalize'
+	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL
+	COST 20; -- 20 as it delegates to GEOS
+
 -- Deprecation in 1.5.0
 CREATE OR REPLACE FUNCTION ST_zmflag(geometry)
 	RETURNS smallint

Modified: trunk/regress/Makefile.in
===================================================================
--- trunk/regress/Makefile.in	2016-06-15 09:49:39 UTC (rev 14948)
+++ trunk/regress/Makefile.in	2016-06-15 09:49:50 UTC (rev 14949)
@@ -98,6 +98,7 @@
 	lwgeom_regress \
 	measures \
 	minimum_bounding_circle \
+	normalize \
 	operators \
 	out_geometry \
 	out_geography \

Added: trunk/regress/normalize.sql
===================================================================
--- trunk/regress/normalize.sql	                        (rev 0)
+++ trunk/regress/normalize.sql	2016-06-15 09:49:50 UTC (rev 14949)
@@ -0,0 +1,7 @@
+select 1, ST_AsText(ST_Normalize(
+'GEOMETRYCOLLECTION(POINT(2 3),MULTILINESTRING((0 0, 1 1),(2 2, 3 3)))'
+::geometry));
+
+select 2, ST_AsText(ST_Normalize(
+'POLYGON((0 10,0 0,10 0,10 10,0 10),(4 2,2 2,2 4,4 4,4 2),(6 8,8 8,8 6,6 6,6 8))'
+::geometry));

Added: trunk/regress/normalize_expected
===================================================================
--- trunk/regress/normalize_expected	                        (rev 0)
+++ trunk/regress/normalize_expected	2016-06-15 09:49:50 UTC (rev 14949)
@@ -0,0 +1,3 @@
+1|GEOMETRYCOLLECTION(MULTILINESTRING((2 2,3 3),(0 0,1 1)),POINT(2 3))
+2|POLYGON((0 0,0 10,10 10,10 0,0 0),(6 6,8 6,8 8,6 8,6 6),(2 2,4 2,4 4,2 4,2 2))
+



More information about the postgis-tickets mailing list