]> granicus.if.org Git - postgis/commitdiff
#3164, ST_ClipByBox2D a little less brittle
authorPaul Ramsey <pramsey@cleverelephant.ca>
Fri, 12 Jun 2015 19:09:27 +0000 (19:09 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Fri, 12 Jun 2015 19:09:27 +0000 (19:09 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@13669 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/lwgeom.c
liblwgeom/lwgeom_geos.c
postgis/lwgeom_geos.c

index 0823f3d1f4726a732492001f324cfb3ae7397e0e..017f1c6128554202b54f8f4d041b973c9610daaf 100644 (file)
@@ -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;
                                }
index 71ff1ca9ea30a07e3b77ec5c6e1cc27c4bf51114..893c8d32c4c94263ee519715b74ee6b93b32af43 100644 (file)
@@ -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 */
        }
 
index 779f254bdb633ea5b0d63654589a3b6733655dc0..9e5865c50f3e50eba0a02f4fea1276d15015a857 100644 (file)
@@ -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 */
 }