[postgis-tickets] r16857 - St_AsMVTGeom: Drop invalid geometries after simplification

Raul raul at rmr.ninja
Mon Oct 1 06:31:53 PDT 2018


Author: algunenano
Date: 2018-10-01 06:31:52 -0700 (Mon, 01 Oct 2018)
New Revision: 16857

Modified:
   branches/2.5/NEWS
   branches/2.5/postgis/mvt.c
   branches/2.5/regress/mvt.sql
   branches/2.5/regress/mvt_expected
Log:
St_AsMVTGeom: Drop invalid geometries after simplification

References #4183


Modified: branches/2.5/NEWS
===================================================================
--- branches/2.5/NEWS	2018-10-01 12:59:05 UTC (rev 16856)
+++ branches/2.5/NEWS	2018-10-01 13:31:52 UTC (rev 16857)
@@ -1,3 +1,9 @@
+PostGIS 2.5.1
+XXXX/XX/XX
+
+ * Bug fixes *
+  - #4183, St_AsMVTGeom: Drop invalid geometries after simplification (Raúl Marín)
+
 PostGIS 2.5.0
 2018/09/23
 WARNING: If compiling with PostgreSQL+JIT, LLVM >= 6 is required

Modified: branches/2.5/postgis/mvt.c
===================================================================
--- branches/2.5/postgis/mvt.c	2018-10-01 12:59:05 UTC (rev 16856)
+++ branches/2.5/postgis/mvt.c	2018-10-01 13:31:52 UTC (rev 16857)
@@ -822,10 +822,30 @@
 			double y0 = bgbox.ymin;
 			double x1 = bgbox.xmax;
 			double y1 = bgbox.ymax;
-			lwgeom = lwgeom_clip_by_rect(lwgeom, x0, y0, x1, y1);
-			POSTGIS_DEBUG(3, "mvt_geom: no geometry after clip");
-			if (lwgeom == NULL || lwgeom_is_empty(lwgeom))
+			const GBOX pre_clip_box = *lwgeom_get_bbox(lwgeom);
+			LWGEOM *clipped_geom = lwgeom_clip_by_rect(lwgeom, x0, y0, x1, y1);
+			if (clipped_geom == NULL || lwgeom_is_empty(clipped_geom))
+			{
+				POSTGIS_DEBUG(3, "mvt_geom: no geometry after clip");
 				return NULL;
+			}
+			/* For some polygons, the simplify step might have left them
+			 * as invalid, which can cause clipping to return the complementary
+			 * geometry of what it should */
+			if ((lwgeom->type == POLYGONTYPE ||
+				lwgeom->type == MULTIPOLYGONTYPE ||
+				lwgeom->type == COLLECTIONTYPE) &&
+			    !gbox_contains_2d(&pre_clip_box, lwgeom_get_bbox(clipped_geom)))
+			{
+				/* TODO: Adapt this when and if Exception Policies are introduced.
+				 * Other options would be to fix the geometry and retry
+				 * or to calculate the difference between the 2 boxes.
+				 */
+				POSTGIS_DEBUG(3, "mvt_geom: Invalid geometry after clipping");
+				lwgeom_free(clipped_geom);
+				return NULL;
+			}
+			lwgeom = clipped_geom;
 		}
 	}
 

Modified: branches/2.5/regress/mvt.sql
===================================================================
--- branches/2.5/regress/mvt.sql	2018-10-01 12:59:05 UTC (rev 16856)
+++ branches/2.5/regress/mvt.sql	2018-10-01 13:31:52 UTC (rev 16857)
@@ -265,6 +265,14 @@
 	16,
 	true));
 
+-- Invalid geometry after simplification with invalid clipping
+SELECT 'PG45', ST_AsEWKT(ST_AsMVTGeom(
+	'SRID=3857;MULTIPOLYGON(((-8231365.02893734 4980355.83678553,-8231394.82332406 4980186.31880185,-8231367.43081065 4979982.17443372,-8231396.69199339 4980227.59327083,-8231365.02893734 4980355.83678553)))'::geometry,
+	'SRID=3857;POLYGON((-8238115.3789773 4970203.10870116,-8238115.3789773 4980063.48534995,-8228255.00232851 4980063.48534995,-8228255.00232851 4970203.10870116,-8238115.3789773 4970203.10870116))'::geometry,
+	4096,
+	16,
+	true));
+
 -- geometry encoding tests
 SELECT 'TG1', encode(ST_AsMVT(q, 'test', 4096, 'geom'), 'base64') FROM (SELECT 1 AS c1,
 	ST_AsMVTGeom(ST_GeomFromText('POINT(25 17)'),

Modified: branches/2.5/regress/mvt_expected
===================================================================
--- branches/2.5/regress/mvt_expected	2018-10-01 12:59:05 UTC (rev 16856)
+++ branches/2.5/regress/mvt_expected	2018-10-01 13:31:52 UTC (rev 16857)
@@ -47,6 +47,7 @@
 PG43 - ON |MULTIPOLYGON(((5 5,0 0,10 0,5 5)),((0 10,5 5,10 10,0 10)))
 PG43 - OFF|MULTIPOLYGON(((5 5,-1 -1,11 -1,5 5)),((5 5,11 11,-1 11,5 5)))
 PG44|
+PG45|
 TG1|GiEKBHRlc3QSDBICAAAYASIECTLePxoCYzEiAigBKIAgeAI=
 TG2|GiMKBHRlc3QSDhICAAAYASIGETLePwIBGgJjMSICKAEogCB4Ag==
 TG3|GiYKBHRlc3QSERICAAAYAiIJCQCAQArQD88PGgJjMSICKAEogCB4Ag==



More information about the postgis-tickets mailing list