From 97eb3833201907dcb9ee8fdfbdd128d1f1da69db Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Mon, 19 Nov 2012 10:20:09 +0000 Subject: [PATCH] Restore 1.5.x behaviour of ST_Simplify (#1987) git-svn-id: http://svn.osgeo.org/postgis/trunk@10702 b70326c6-7e19-0410-871a-916f4a2858ee --- liblwgeom/lwcollection.c | 6 +++--- liblwgeom/lwline.c | 3 ++- liblwgeom/lwpoly.c | 25 ++++++++++++------------- regress/simplify.sql | 3 +++ regress/simplify_expected | 5 ++++- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/liblwgeom/lwcollection.c b/liblwgeom/lwcollection.c index dedc02962..fd95b7dbd 100644 --- a/liblwgeom/lwcollection.c +++ b/liblwgeom/lwcollection.c @@ -499,12 +499,12 @@ LWCOLLECTION* lwcollection_simplify(const LWCOLLECTION *igeom, double dist) LWCOLLECTION *out = lwcollection_construct_empty(igeom->type, igeom->srid, FLAGS_GET_Z(igeom->flags), FLAGS_GET_M(igeom->flags)); if( lwcollection_is_empty(igeom) ) - return out; + return out; /* should we return NULL instead ? */ for( i = 0; i < igeom->ngeoms; i++ ) { LWGEOM *ngeom = lwgeom_simplify(igeom->geoms[i], dist); - out = lwcollection_add_lwgeom(out, ngeom); + if ( ngeom ) out = lwcollection_add_lwgeom(out, ngeom); } return out; @@ -553,4 +553,4 @@ lwcollection_startpoint(const LWCOLLECTION* col, POINT4D* pt) return LW_FAILURE; return lwgeom_startpoint(col->geoms[0], pt); -} \ No newline at end of file +} diff --git a/liblwgeom/lwline.c b/liblwgeom/lwline.c index 84cff91f1..30de4ad41 100644 --- a/liblwgeom/lwline.c +++ b/liblwgeom/lwline.c @@ -480,7 +480,8 @@ LWLINE* lwline_simplify(const LWLINE *iline, double dist) if( lwline_is_empty(iline) ) return lwline_clone(iline); - oline = lwline_construct(iline->srid, NULL, ptarray_simplify(iline->points, dist, 2)); + static const int minvertices = 0; /* TODO: allow setting this */ + oline = lwline_construct(iline->srid, NULL, ptarray_simplify(iline->points, dist, minvertices)); oline->type = iline->type; return oline; } diff --git a/liblwgeom/lwpoly.c b/liblwgeom/lwpoly.c index c6b6dcbe0..52c27fad9 100644 --- a/liblwgeom/lwpoly.c +++ b/liblwgeom/lwpoly.c @@ -350,36 +350,35 @@ LWPOLY* lwpoly_simplify(const LWPOLY *ipoly, double dist) LWDEBUGF(2, "simplify_polygon3d: simplifying polygon with %d rings", ipoly->nrings); if( lwpoly_is_empty(ipoly) ) - return opoly; + return opoly; /* should we return NULL instead ? */ for (i = 0; i < ipoly->nrings; i++) { - POINTARRAY *opts = ptarray_simplify(ipoly->rings[i], dist, 3); + static const int minvertices = 0; /* TODO: allow setting this */ + POINTARRAY *opts = ptarray_simplify(ipoly->rings[i], dist, minvertices); + + LWDEBUGF(3, "ring%d simplified from %d to %d points", i, ipoly->rings[i]->npoints, opts->npoints); /* Less points than are needed to form a closed ring, we can't use this */ - if ( i && opts->npoints < 4 ) + if ( opts->npoints < 4 ) { LWDEBUGF(3, "ring%d skipped (% pts)", i, opts->npoints); ptarray_free(opts); - continue; + if ( i ) continue; + else break; /* Don't scan holes if shell is collapsed */ } - LWDEBUGF(3, "ring%d simplified from %d to %d points", i, ipoly->rings[i]->npoints, opts->npoints); - /* Add ring to simplified polygon */ if( lwpoly_add_ring(opoly, opts) == LW_FAILURE ) return NULL; - - /* Don't scan holes if shell is collapsed */ - if ( !i && opts->npoints < 4 ) - { - LWDEBUG(3, "nothing more to do for collapsed shell"); - break; - } } LWDEBUGF(3, "simplified polygon with %d rings", ipoly->nrings); opoly->type = ipoly->type; + + if( lwpoly_is_empty(opoly) ) + return NULL; + return opoly; } diff --git a/regress/simplify.sql b/regress/simplify.sql index 76524587d..c8ba2137c 100644 --- a/regress/simplify.sql +++ b/regress/simplify.sql @@ -9,3 +9,6 @@ SELECT '7', ST_astext(ST_Simplify('POLYGON((0 0 3 2, 0 10 6 1, 0 51 1 6, 50 20 6 SELECT '8', ST_astext(ST_Simplify('POLYGON((0 0 3 2, 0 10 6 1, 0 51 1 6, 50 20 6 7, 30 20 9 9, 7 32 10 5, 0 0 3 2), (1 1 4 3, 1 3 2 3, 18 18 5 30, 1 1 4 3))', 1)); SELECT '9', ST_astext(ST_Simplify('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(5 5, 5 6, 6 6, 8 5, 5 5))', 20)); +SELECT '10', ST_astext(ST_Simplify('LINESTRING(0 0, 0 10)', 20)); +SELECT '11', ST_astext(ST_Simplify('MULTIPOLYGON(((100 100, 100 130, 130 130, 130 100, 100 100)), ((0 0, 10 0, 10 10, 0 10, 0 0),(5 5, 5 6, 6 6, 8 5, 5 5)) )', 20)); +SELECT '12', ST_astext(ST_Simplify('MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0),(5 5, 5 6, 6 6, 8 5, 5 5)),((100 100, 100 130, 130 130, 130 100, 100 100)))', 20)); diff --git a/regress/simplify_expected b/regress/simplify_expected index 52c8de49a..93a5c4622 100644 --- a/regress/simplify_expected +++ b/regress/simplify_expected @@ -6,4 +6,7 @@ 6|MULTILINESTRING ZM ((0 0 3 2,0 51 1 6,50 20 6 7,7 32 10 5),(0 0 4 3,20 20 5 30)) 7|POLYGON ZM ((0 0 3 2,0 51 1 6,50 20 6 7,7 32 10 5,0 0 3 2)) 8|POLYGON ZM ((0 0 3 2,0 51 1 6,50 20 6 7,30 20 9 9,7 32 10 5,0 0 3 2),(1 1 4 3,1 3 2 3,18 18 5 30,1 1 4 3)) -9|POLYGON((0 0,10 10,0 0)) +9| +10|LINESTRING(0 0,0 10) +11|MULTIPOLYGON(((100 100,100 130,130 130,130 100,100 100))) +12|MULTIPOLYGON(((100 100,100 130,130 130,130 100,100 100))) -- 2.40.0