]> granicus.if.org Git - postgis/commitdiff
Convert some BOX3D functions to GBOX
authorPaul Ramsey <pramsey@cleverelephant.ca>
Sat, 25 Jun 2011 21:42:13 +0000 (21:42 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Sat, 25 Jun 2011 21:42:13 +0000 (21:42 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@7486 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/cunit/cu_algorithm.c
liblwgeom/lwalgorithm.c
liblwgeom/lwalgorithm.h
postgis/lwgeom_functions_basic.c

index a795f1878040c2f6db31471804f17ac184696bc9..e03099e3c624c6a9faf711fac15ce662f2a15c6c 100644 (file)
@@ -679,9 +679,11 @@ static void test_lwline_clip_big(void)
 
 static void test_geohash_precision(void)
 {
-       BOX3D bbox;
-       BOX3D bounds;
+       GBOX bbox;
+       GBOX bounds;
        int precision = 0;
+       gbox_init(&bbox);
+       gbox_init(&bounds);
 
        bbox.xmin = 23.0;
        bbox.xmax = 23.0;
index d1a6b83cc88545d3430b4caf634c16b4592cee56..7ba0773174d6ce7272d22a7c742ff370400f2ede 100644 (file)
@@ -734,7 +734,7 @@ char *geohash_point(double longitude, double latitude, int precision)
        return geohash;
 }
 
-int lwgeom_geohash_precision(BOX3D bbox, BOX3D *bounds)
+int lwgeom_geohash_precision(GBOX bbox, GBOX *bounds)
 {
        double minx, miny, maxx, maxy;
        double latmax, latmin, lonmax, lonmin;
@@ -821,33 +821,34 @@ int lwgeom_geohash_precision(BOX3D bbox, BOX3D *bounds)
 */
 char *lwgeom_geohash(const LWGEOM *lwgeom, int precision)
 {
-       BOX3D *bbox = NULL;
-       BOX3D precision_bounds;
+       GBOX gbox;
+       GBOX gbox_bounds;
        double lat, lon;
+       int result;
 
-       bbox = lwgeom_compute_box3d(lwgeom);
-       if ( ! bbox ) return NULL;
+       gbox_init(&gbox);
+       gbox_init(&gbox_bounds);
+
+       result = lwgeom_calculate_gbox(lwgeom, &gbox);  
+       if ( result == LW_FAILURE ) return NULL;
 
        /* Return error if we are being fed something outside our working bounds */
-       if ( bbox->xmin < -180 || bbox->ymin < -90 || bbox->xmax > 180 || bbox->ymax > 90 )
+       if ( gbox.xmin < -180 || gbox.ymin < -90 || gbox.xmax > 180 || gbox.ymax > 90 )
        {
                lwerror("Geohash requires inputs in decimal degrees.");
-               lwfree(bbox);
                return NULL;
        }
 
        /* What is the center of our geometry bounds? We'll use that to
        ** approximate location. */
-       lon = bbox->xmin + (bbox->xmax - bbox->xmin) / 2;
-       lat = bbox->ymin + (bbox->ymax - bbox->ymin) / 2;
+       lon = gbox.xmin + (gbox.xmax - gbox.xmin) / 2;
+       lat = gbox.ymin + (gbox.ymax - gbox.ymin) / 2;
 
        if ( precision <= 0 )
        {
-               precision = lwgeom_geohash_precision(*bbox, &precision_bounds);
+               precision = lwgeom_geohash_precision(gbox, &gbox_bounds);
        }
 
-       lwfree(bbox);
-
        /*
        ** Return the geohash of the center, with a precision determined by the
        ** extent of the bounds.
index 069c67b79520a832a3db4b8b88e651f4f6e7ae9b..008d1ef12896e73b6f8eee08313620ba2b75bbc8 100644 (file)
@@ -45,7 +45,7 @@ int lwpoint_interpolate(const POINT4D *p1, const POINT4D *p2, POINT4D *p, int nd
 LWCOLLECTION *lwline_clip_to_ordinate_range(LWLINE *line, int ordinate, double from, double to);
 LWCOLLECTION *lwmline_clip_to_ordinate_range(LWMLINE *mline, int ordinate, double from, double to);
 
-int lwgeom_geohash_precision(BOX3D bbox, BOX3D *bounds);
+int lwgeom_geohash_precision(GBOX bbox, GBOX *bounds);
 char *lwgeom_geohash(const LWGEOM *lwgeom, int precision);
 char *geohash_point(double longitude, double latitude, int precision);
 
index 02a587cff1f2c88cb533343cd9e6085b448910bc..8f2ce0b5c6b9d9fbe51983d20b7a9ff85dbcd125 100644 (file)
@@ -1639,22 +1639,31 @@ PG_FUNCTION_INFO_V1(LWGEOM_to_BOX);
 Datum LWGEOM_to_BOX(PG_FUNCTION_ARGS)
 {
        PG_LWGEOM *pg_lwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-       BOX3D *box3d = NULL;
-       BOX *result = (BOX *)lwalloc(sizeof(BOX));
        LWGEOM *lwgeom = pglwgeom_deserialize(pg_lwgeom);
+       GBOX gbox;
+       int result;
+       BOX *out = NULL;
+       
+       /* Zero out flags */
+       gbox_init(&gbox);
+
+       /* Calculate the GBOX of the geometry */
+       result = lwgeom_calculate_gbox(lwgeom, &gbox);
 
-       /* Calculate the BOX3D of the geometry */
-       box3d = lwgeom_compute_box3d(lwgeom);
+       /* Clean up memory */
        lwfree(lwgeom);
        PG_FREE_IF_COPY(pg_lwgeom, 0);
-    
-       if ( box3d )
-       {
-           box3d_to_box_p(box3d, result);
-           lwfree(box3d);
-       PG_RETURN_POINTER(result);
-       }
-    PG_RETURN_NULL();
+       
+       /* Null on failure */
+       if ( ! result )
+               PG_RETURN_NULL();
+       
+    out = lwalloc(sizeof(BOX));
+       out->low.x = gbox.xmin;
+       out->low.y = gbox.ymin;
+       out->high.x = gbox.xmax;
+       out->high.y = gbox.ymax;
+       PG_RETURN_POINTER(out);
 }
 
 /**