/** * Warning, this function is only good for x/y/z boxes, used * in unit testing of geodetic box generation. */ GBOX* gbox_from_string(const char *str) { const char *ptr = str; char *nextptr; char *gbox_start = strstr(str, "GBOX(("); GBOX *gbox = gbox_new(gflags(0,0,1)); if ( ! gbox_start ) return NULL; /* No header found */ ptr += 6; gbox->xmin = strtod(ptr, &nextptr); if ( ptr == nextptr ) return NULL; /* No double found */ ptr = nextptr + 1; gbox->ymin = strtod(ptr, &nextptr); if ( ptr == nextptr ) return NULL; /* No double found */ ptr = nextptr + 1; gbox->zmin = strtod(ptr, &nextptr); if ( ptr == nextptr ) return NULL; /* No double found */ ptr = nextptr + 3; gbox->xmax = strtod(ptr, &nextptr); if ( ptr == nextptr ) return NULL; /* No double found */ ptr = nextptr + 1; gbox->ymax = strtod(ptr, &nextptr); if ( ptr == nextptr ) return NULL; /* No double found */ ptr = nextptr + 1; gbox->zmax = strtod(ptr, &nextptr); if ( ptr == nextptr ) return NULL; /* No double found */ return gbox; }
void lwgeom_add_bbox_deep(LWGEOM *lwgeom, GBOX *gbox) { if ( lwgeom_is_empty(lwgeom) ) return; FLAGS_SET_BBOX(lwgeom->flags, 1); if ( ! ( gbox || lwgeom->bbox ) ) { lwgeom->bbox = gbox_new(lwgeom->flags); lwgeom_calculate_gbox(lwgeom, lwgeom->bbox); } else if ( gbox && ! lwgeom->bbox ) { lwgeom->bbox = gbox_clone(gbox); } if ( lwgeom_is_collection(lwgeom) ) { int i; LWCOLLECTION *lwcol = (LWCOLLECTION*)lwgeom; for ( i = 0; i < lwcol->ngeoms; i++ ) { lwgeom_add_bbox_deep(lwcol->geoms[i], lwgeom->bbox); } } }
static GBOX* parse_geohash(char *geohash, int precision) { GBOX *box = NULL; double lat[2], lon[2]; POSTGIS_DEBUG(2, "parse_geohash called."); if (NULL == geohash) { geohash_lwpgerror("invalid GeoHash representation", 2); } decode_geohash_bbox(geohash, lat, lon, precision); POSTGIS_DEBUGF(2, "ST_Box2dFromGeoHash sw: %.20f, %.20f", lon[0], lat[0]); POSTGIS_DEBUGF(2, "ST_Box2dFromGeoHash ne: %.20f, %.20f", lon[1], lat[1]); box = gbox_new(gflags(0, 0, 1)); box->xmin = lon[0]; box->ymin = lat[0]; box->xmax = lon[1]; box->ymax = lat[1]; POSTGIS_DEBUG(2, "parse_geohash finished."); return box; }
/** * Ensure there's a box in the LWGEOM. * If the box is already there just return, * else compute it. */ void lwgeom_add_bbox(LWGEOM *lwgeom) { /* an empty LWGEOM has no bbox */ if ( lwgeom_is_empty(lwgeom) ) return; if ( lwgeom->bbox ) return; FLAGS_SET_BBOX(lwgeom->flags, 1); lwgeom->bbox = gbox_new(lwgeom->flags); lwgeom_calculate_gbox(lwgeom, lwgeom->bbox); }
Datum BOX2D_construct(PG_FUNCTION_ARGS) { GSERIALIZED *pgmin = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); GSERIALIZED *pgmax = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); GBOX *result; LWPOINT *minpoint, *maxpoint; double min, max, tmp; minpoint = (LWPOINT*)lwgeom_from_gserialized(pgmin); maxpoint = (LWPOINT*)lwgeom_from_gserialized(pgmax); if ( (minpoint->type != POINTTYPE) || (maxpoint->type != POINTTYPE) ) { elog(ERROR, "GBOX_construct: arguments must be points"); PG_RETURN_NULL(); } error_if_srid_mismatch(minpoint->srid, maxpoint->srid); result = gbox_new(gflags(0, 0, 0)); /* Process X min/max */ min = lwpoint_get_x(minpoint); max = lwpoint_get_x(maxpoint); if ( min > max ) { tmp = min; min = max; max = tmp; } result->xmin = min; result->xmax = max; /* Process Y min/max */ min = lwpoint_get_y(minpoint); max = lwpoint_get_y(maxpoint); if ( min > max ) { tmp = min; min = max; max = tmp; } result->ymin = min; result->ymax = max; PG_RETURN_POINTER(result); }