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;
*/
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.
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);
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);
}
/**