From 6111c39b52cb6d56d1046f36b51cfcfbdae5f29d Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Sat, 25 Jun 2011 21:42:13 +0000 Subject: [PATCH] Convert some BOX3D functions to GBOX git-svn-id: http://svn.osgeo.org/postgis/trunk@7486 b70326c6-7e19-0410-871a-916f4a2858ee --- liblwgeom/cunit/cu_algorithm.c | 6 ++++-- liblwgeom/lwalgorithm.c | 25 ++++++++++++------------ liblwgeom/lwalgorithm.h | 2 +- postgis/lwgeom_functions_basic.c | 33 ++++++++++++++++++++------------ 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/liblwgeom/cunit/cu_algorithm.c b/liblwgeom/cunit/cu_algorithm.c index a795f1878..e03099e3c 100644 --- a/liblwgeom/cunit/cu_algorithm.c +++ b/liblwgeom/cunit/cu_algorithm.c @@ -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; diff --git a/liblwgeom/lwalgorithm.c b/liblwgeom/lwalgorithm.c index d1a6b83cc..7ba077317 100644 --- a/liblwgeom/lwalgorithm.c +++ b/liblwgeom/lwalgorithm.c @@ -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. diff --git a/liblwgeom/lwalgorithm.h b/liblwgeom/lwalgorithm.h index 069c67b79..008d1ef12 100644 --- a/liblwgeom/lwalgorithm.h +++ b/liblwgeom/lwalgorithm.h @@ -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); diff --git a/postgis/lwgeom_functions_basic.c b/postgis/lwgeom_functions_basic.c index 02a587cff..8f2ce0b5c 100644 --- a/postgis/lwgeom_functions_basic.c +++ b/postgis/lwgeom_functions_basic.c @@ -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); } /** -- 2.50.0