From a9e83e2a6b97f0a5e89f50b947d549d33d5e6de5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ra=C3=BAl=20Mar=C3=ADn=20Rodr=C3=ADguez?= Date: Wed, 30 Jan 2019 19:13:47 +0000 Subject: [PATCH] ST_ClipByBox2D: Do not throw when the geometry is invalid References #4314 Closes https://github.com/postgis/postgis/pull/369 git-svn-id: http://svn.osgeo.org/postgis/trunk@17220 b70326c6-7e19-0410-871a-916f4a2858ee --- NEWS | 1 + liblwgeom/cunit/cu_clip_by_rect.c | 7 +++++++ liblwgeom/lwgeom_geos.c | 21 ++++++++++++++++++--- regress/core/clipbybox2d.sql | 2 ++ regress/core/clipbybox2d_expected | 1 + regress/core/mvt.sql | 7 +++++++ regress/core/mvt_expected | 1 + 7 files changed, 37 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index 046051302..d9fb6da7c 100644 --- a/NEWS +++ b/NEWS @@ -66,6 +66,7 @@ PostGIS 3.0.0 - #4275, Avoid passing a NULL pointer to GEOSisEmpty (Raúl Marín) - #4296, Use `server_version_num` instead of parsing `version()` (Raúl Marín) - #4272, Improve notice message when unable to compute stats (Raúl Marín) + - #4314, ST_ClipByBox2D: Do not throw when the geometry is invalid (Raúl Marín) PostGIS 2.5.0 diff --git a/liblwgeom/cunit/cu_clip_by_rect.c b/liblwgeom/cunit/cu_clip_by_rect.c index 95e93ae8a..c4cf8b109 100644 --- a/liblwgeom/cunit/cu_clip_by_rect.c +++ b/liblwgeom/cunit/cu_clip_by_rect.c @@ -64,6 +64,13 @@ static void test_lwgeom_clip_by_rect(void) //tmp = lwgeom_to_ewkt(out); printf("%s\n", tmp); lwfree(tmp); CU_ASSERT(lwgeom_is_empty(out)); lwgeom_free(out); lwgeom_free(in); + + /* Returns NULL with an invalid polygon (line) */ + wkt = "POLYGON((1410 2055, 1410 2056, 1410 2057, 1410 2055))"; + in = lwgeom_from_wkt(wkt, LW_PARSER_CHECK_NONE); + out = lwgeom_clip_by_rect(in, -8.000000, -8.000000, 2056.000000, 2056.000000); + CU_ASSERT_PTR_NULL(out); + lwgeom_free(in); } /* diff --git a/liblwgeom/lwgeom_geos.c b/liblwgeom/lwgeom_geos.c index 96dc8cea8..69a8a3a5e 100644 --- a/liblwgeom/lwgeom_geos.c +++ b/liblwgeom/lwgeom_geos.c @@ -65,12 +65,27 @@ do { \ return NULL; \ } while (0) +/* Pass the latest GEOS error to lwdebug, then return NULL */ +#define GEOS_FAIL_DEBUG() \ + do \ + { \ + lwdebug(1, "%s: GEOS Error: %s", __func__, lwgeom_geos_errmsg); \ + return NULL; \ + } while (0) + #define GEOS_FREE_AND_FAIL(...) \ do { \ GEOS_FREE(__VA_ARGS__); \ GEOS_FAIL(); \ } while (0) +#define GEOS_FREE_AND_FAIL_DEBUG(...) \ + do \ + { \ + GEOS_FREE(__VA_ARGS__); \ + GEOS_FAIL_DEBUG(); \ + } while (0) + /* Return the consistent SRID of all inputs, or call lwerror * in case of SRID mismatch. */ #define RESULT_SRID(...) \ @@ -870,17 +885,17 @@ lwgeom_clip_by_rect(const LWGEOM *geom1, double x1, double y1, double x2, double initGEOS(lwnotice, lwgeom_geos_error); if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) - GEOS_FAIL(); + GEOS_FAIL_DEBUG(); if (!(g3 = GEOSClipByRect(g1, x1, y1, x2, y2))) - GEOS_FREE_AND_FAIL(g1); + GEOS_FREE_AND_FAIL_DEBUG(g1); GEOS_FREE(g1); result = GEOS2LWGEOM(g3, is3d); GEOS_FREE(g3); if (!result) - GEOS_FAIL(); + GEOS_FAIL_DEBUG(); result->srid = geom1->srid; diff --git a/regress/core/clipbybox2d.sql b/regress/core/clipbybox2d.sql index 56fa87cfa..87d774675 100644 --- a/regress/core/clipbybox2d.sql +++ b/regress/core/clipbybox2d.sql @@ -22,3 +22,5 @@ CREATE TEMPORARY TABLE t AS SELECT SELECT '8', ST_AsEWKT(ST_ClipByBox2d(g, ST_MakeEnvelope(-20,-20,-10,-10))) FROM t; -- See http://trac.osgeo.org/postgis/ticket/2954 SELECT '9', ST_AsEWKT(ST_ClipByBox2D('SRID=4326;POINT(0 0)','BOX3D(-1 -1,1 1)'::box3d::box2d)); +-- See https://trac.osgeo.org/postgis/ticket/4314 +SELECT '10', ST_ClipByBox2D('POLYGON((1410 2055, 1410 2056, 1410 2057, 1410 2055))'::geometry, ST_MakeEnvelope(-8.000000, -8.000000, 2056.000000, 2056.000000)); \ No newline at end of file diff --git a/regress/core/clipbybox2d_expected b/regress/core/clipbybox2d_expected index 8986b641e..7588472e1 100644 --- a/regress/core/clipbybox2d_expected +++ b/regress/core/clipbybox2d_expected @@ -7,3 +7,4 @@ 7|POLYGON((2 2,2 5,5 5,5 2,2 2)) 8|SRID=3857;POLYGON EMPTY 9|SRID=4326;POINT(0 0) +10| diff --git a/regress/core/mvt.sql b/regress/core/mvt.sql index 2a21c0375..edbe1548c 100644 --- a/regress/core/mvt.sql +++ b/regress/core/mvt.sql @@ -579,6 +579,13 @@ SELECT '#4294_Vertical', ST_AsText(ST_AsMVTGeom( ST_MakeBox2D(ST_Point(0, 0), ST_Point(10, 10)), 10, 0, true)); +SELECT '#4314', ST_AsMVTGeom( + 'SRID=3857;MULTIPOLYGON(((-8230700.44460474 4970098.60762691,-8230694.76395068 4970080.40480045,-8230692.98226063 4970074.69572152,-8230702.2389602 4970071.78449542,-8230709.99536139 4970096.63875167,-8230700.73864062 4970099.5499925,-8230700.44460474 4970098.60762691)))'::geometry, + 'SRID=3857;POLYGON((-8257645.03970416 5009377.08569731,-8257645.03970416 4970241.3272153,-8218509.28122215 4970241.3272153,-8218509.28122215 5009377.08569731,-8257645.03970416 5009377.08569731))'::geometry, + 2048, + 8, + true); + -- Feature id encoding tests SELECT 'FI1', encode(ST_AsMVT(q, 'test', 4096, 'geom', 'c1'), 'base64') FROM ( SELECT 1::smallint AS c1, 'abcd'::text AS c2, diff --git a/regress/core/mvt_expected b/regress/core/mvt_expected index fe13e2395..6aa2742e5 100644 --- a/regress/core/mvt_expected +++ b/regress/core/mvt_expected @@ -125,6 +125,7 @@ TU3| #3922|6.5 #4294_Horizontal|LINESTRING(0 10,0 5) #4294_Vertical|LINESTRING(0 10,5 10) +#4314| FI1|GicKBHRlc3QSDggBEgIAABgBIgQJMt4/GgJjMiIGCgRhYmNkKIAgeAI= FI2|GicKBHRlc3QSDggBEgIAABgBIgQJMt4/GgJjMiIGCgRhYmNkKIAgeAI= FI3|GicKBHRlc3QSDggBEgIAABgBIgQJMt4/GgJjMiIGCgRhYmNkKIAgeAI= -- 2.40.0