]> granicus.if.org Git - postgis/commitdiff
Add a gbox_overlaps_2d function, use when appropriate (#1357)
authorSandro Santilli <strk@keybit.net>
Fri, 9 Dec 2011 11:18:09 +0000 (11:18 +0000)
committerSandro Santilli <strk@keybit.net>
Fri, 9 Dec 2011 11:18:09 +0000 (11:18 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@8331 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/g_box.c
liblwgeom/liblwgeom.h.in
postgis/lwgeom_geos.c

index f8f13f4083c099c981dde44d232305ce2c1e78f4..d65adc3afb30535a247a9cdb08126ed0c6bcacc8 100644 (file)
@@ -213,6 +213,21 @@ int gbox_overlaps(const GBOX *g1, const GBOX *g2)
        return LW_TRUE;
 }
 
+int gbox_overlaps_2d(const GBOX *g1, const GBOX *g2)
+{
+
+       /* Make sure our boxes are consistent */
+       if ( FLAGS_GET_GEODETIC(g1->flags) != FLAGS_GET_GEODETIC(g2->flags) )
+               lwerror("gbox_overlaps: cannot compare geodetic and non-geodetic boxes");
+
+       /* Check X/Y first */
+       if ( g1->xmax < g2->xmin || g1->ymax < g2->ymin ||
+            g1->xmin > g2->xmax || g1->ymin > g2->ymax )
+               return LW_FALSE;
+               
+       return LW_TRUE;
+}
+
 /**
 * Warning, this function is only good for x/y/z boxes, used
 * in unit testing of geodetic box generation.
index 6606f8c8acd95dbda0b7019d9950d64ff48aa631..1a522853a20645ef14dff1cfae74344dcb9516c2 100644 (file)
@@ -1496,6 +1496,11 @@ extern GBOX* gbox_from_string(const char *str);
 */
 extern int gbox_overlaps(const GBOX *g1, const GBOX *g2);
 
+/**
+* Return #LW_TRUE if the #GBOX overlaps on the 2d plane, #LW_FALSE otherwise. 
+*/
+extern int gbox_overlaps_2d(const GBOX *g1, const GBOX *g2);
+
 /**
 * Copy the values of original #GBOX into duplicate.
 */
index 134b4ed9cd47f54d2a1da85e84d250d7c60d7371..2f4380fad4c290c0766460848539e37ea698199d 100644 (file)
@@ -1770,10 +1770,10 @@ Datum overlaps(PG_FUNCTION_ARGS)
        if ( gserialized_get_gbox_p(geom1, &box1) &&
                gserialized_get_gbox_p(geom2, &box2) )
        {
-               if ( box2.xmax < box1.xmin ) PG_RETURN_BOOL(FALSE);
-               if ( box2.xmin > box1.xmax ) PG_RETURN_BOOL(FALSE);
-               if ( box2.ymax < box1.ymin ) PG_RETURN_BOOL(FALSE);
-               if ( box2.ymin > box2.ymax ) PG_RETURN_BOOL(FALSE);
+               if ( gbox_overlaps_2d(&box1, &box2) == LW_FALSE )
+               {
+                       PG_RETURN_BOOL(FALSE);
+               }
        }
 
        initGEOS(lwnotice, lwgeom_geos_error);
@@ -2510,8 +2510,7 @@ Datum crosses(PG_FUNCTION_ARGS)
        if ( gserialized_get_gbox_p(geom1, &box1) &&
                gserialized_get_gbox_p(geom2, &box2) )
        {
-               if ( ( box2.xmax < box1.xmin ) || ( box2.xmin > box1.xmax ) ||
-                       ( box2.ymax < box1.ymin ) || ( box2.ymin > box2.ymax ) )
+               if ( gbox_overlaps_2d(&box1, &box2) == LW_FALSE )
                {
                        PG_RETURN_BOOL(FALSE);
                }
@@ -2587,8 +2586,7 @@ Datum intersects(PG_FUNCTION_ARGS)
        if ( gserialized_get_gbox_p(geom1, &box1) &&
                gserialized_get_gbox_p(geom2, &box2) )
        {
-               if ( ( box2.xmax < box1.xmin ) || ( box2.xmin > box1.xmax ) ||
-                       ( box2.ymax < box1.ymin ) || ( box2.ymin > box1.ymax ) )
+               if ( gbox_overlaps_2d(&box1, &box2) == LW_FALSE )
                {
                        PG_RETURN_BOOL(FALSE);
                }
@@ -2754,8 +2752,7 @@ Datum touches(PG_FUNCTION_ARGS)
        if ( gserialized_get_gbox_p(geom1, &box1) &&
                gserialized_get_gbox_p(geom2, &box2) )
        {
-               if ( ( box2.xmax < box1.xmin ) || ( box2.xmin > box1.xmax ) ||
-                       ( box2.ymax < box1.ymin ) || ( box2.ymin > box1.ymax ) )
+               if ( gbox_overlaps_2d(&box1, &box2) == LW_FALSE )
                {
                        PG_RETURN_BOOL(FALSE);
                }
@@ -2823,8 +2820,7 @@ Datum disjoint(PG_FUNCTION_ARGS)
        if ( gserialized_get_gbox_p(geom1, &box1) &&
                gserialized_get_gbox_p(geom2, &box2) )
        {
-               if ( ( box2.xmax < box1.xmin ) || ( box2.xmin > box1.xmax ) ||
-                       ( box2.ymax < box1.ymin ) || ( box2.ymin > box1.ymax ) )
+               if ( gbox_overlaps_2d(&box1, &box2) == LW_FALSE )
                {
                        PG_RETURN_BOOL(TRUE);
                }
@@ -3039,14 +3035,13 @@ Datum geomequals(PG_FUNCTION_ARGS)
                PG_RETURN_BOOL(TRUE);
 
        /*
-        * short-circuit: Loose test, if they don't intersect they
-        * sure can't be equal.
+        * short-circuit: Loose test, if geom2 bounding box does not overlap
+        * geom1 bounding box we can prematurely return FALSE.
         */
        if ( gserialized_get_gbox_p(geom1, &box1) &&
             gserialized_get_gbox_p(geom2, &box2) )
        {
-               if ( ( box2.xmax < box1.xmin ) || ( box2.xmin > box1.xmax ) ||
-                    ( box2.ymax < box1.ymin ) || ( box2.ymin > box1.ymax ) )
+               if ( gbox_overlaps_2d(&box1, &box2) == LW_FALSE )
                {
                        PG_RETURN_BOOL(FALSE);
                }