From: Paul Ramsey Date: Fri, 12 Jun 2015 19:09:27 +0000 (+0000) Subject: #3164, ST_ClipByBox2D a little less brittle X-Git-Tag: 2.2.0rc1~358 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0ec9d8bdd967e86917dae59f043facf05c1b401e;p=postgis #3164, ST_ClipByBox2D a little less brittle git-svn-id: http://svn.osgeo.org/postgis/trunk@13669 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/liblwgeom/lwgeom.c b/liblwgeom/lwgeom.c index 0823f3d1f..017f1c612 100644 --- a/liblwgeom/lwgeom.c +++ b/liblwgeom/lwgeom.c @@ -2042,7 +2042,7 @@ lwgeom_subdivide_recursive(const LWGEOM *geom, int maxvertices, int depth, LWCOL /* efficient way to do this? */ LWGEOM *clipped = lwgeom_clip_by_rect(geom, clip->xmin, clip->ymin, clip->xmax, clip->ymax); /* Hm, clipping left nothing behind, skip it */ - if ( lwgeom_is_empty(clipped) ) + if ( (!clipped) || lwgeom_is_empty(clipped) ) { return 0; } @@ -2077,7 +2077,7 @@ lwgeom_subdivide_recursive(const LWGEOM *geom, int maxvertices, int depth, LWCOL { LWGEOM *clipped = lwgeom_clip_by_rect(geom, clip->xmin, clip->ymin, clip->xmax, clip->ymax); /* Hm, clipping left nothing behind, skip it */ - if ( lwgeom_is_empty(clipped) ) + if ( (!clipped) || lwgeom_is_empty(clipped) ) { return 0; } diff --git a/liblwgeom/lwgeom_geos.c b/liblwgeom/lwgeom_geos.c index 71ff1ca9e..893c8d32c 100644 --- a/liblwgeom/lwgeom_geos.c +++ b/liblwgeom/lwgeom_geos.c @@ -841,9 +841,8 @@ lwgeom_clip_by_rect(const LWGEOM *geom1, double x0, double y0, double x1, double if (g3 == NULL) { - lwerror("Error performing rectangular clipping: %s", - lwgeom_geos_errmsg); - return NULL; /* never get here */ + lwnotice("Error performing rectangular clipping: %s", lwgeom_geos_errmsg); + return NULL; } LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ; @@ -853,8 +852,7 @@ lwgeom_clip_by_rect(const LWGEOM *geom1, double x0, double y0, double x1, double if (result == NULL) { - lwerror("Error performing intersection: GEOS2LWGEOM: %s", - lwgeom_geos_errmsg); + lwerror("Error performing intersection: GEOS2LWGEOM: %s", lwgeom_geos_errmsg); return NULL ; /* never get here */ } diff --git a/postgis/lwgeom_geos.c b/postgis/lwgeom_geos.c index 779f254bd..9e5865c50 100644 --- a/postgis/lwgeom_geos.c +++ b/postgis/lwgeom_geos.c @@ -1634,7 +1634,7 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) GSERIALIZED *result; LWGEOM *lwgeom1, *lwresult ; const GBOX *bbox1; - const GBOX *bbox2; + GBOX *bbox2; geom1 = PG_GETARG_GSERIALIZED_P(0); lwgeom1 = lwgeom_from_gserialized(geom1) ; @@ -1648,10 +1648,10 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) /* WARNING: this is really a BOX2DF, use only xmin and ymin fields */ bbox2 = (GBOX *)PG_GETARG_POINTER(1); + bbox2->flags = 0; /* If bbox1 outside of bbox2, return empty */ - if ( bbox1->xmin > bbox2->xmax || bbox1->xmax < bbox2->xmin || - bbox1->ymin > bbox2->ymax || bbox1->ymax < bbox2->ymin ) + if ( ! gbox_overlaps_2d(bbox1, bbox2) ) { lwresult = lwgeom_construct_empty(lwgeom1->type, lwgeom1->srid, 0, 0); lwgeom_free(lwgeom1); @@ -1662,8 +1662,7 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) } /* if bbox1 is covered by bbox2, return lwgeom1 */ - if ( bbox1->xmax <= bbox2->xmax && bbox1->xmin >= bbox2->xmin && - bbox1->ymax <= bbox2->ymax && bbox1->ymin >= bbox2->ymin ) + if ( gbox_contains_2d(bbox2, bbox1) ) { lwgeom_free(lwgeom1); PG_RETURN_POINTER(geom1); @@ -1671,14 +1670,17 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) lwresult = lwgeom_clip_by_rect(lwgeom1, bbox2->xmin, bbox2->ymin, bbox2->xmax, bbox2->ymax); - lwgeom_free(lwgeom1) ; - - result = geometry_serialize(lwresult) ; - lwgeom_free(lwresult) ; + lwgeom_free(lwgeom1); PG_FREE_IF_COPY(geom1, 0); + if ( lwresult == NULL ) + PG_RETURN_NULL(); + + result = geometry_serialize(lwresult) ; + lwgeom_free(lwresult) ; PG_RETURN_POINTER(result); + #endif /* POSTGIS_GEOS_VERSION >= 35 */ }