Skip to content

Commit

Permalink
Rename pglwgeom_box2d_p
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/postgis/trunk@8065 b70326c6-7e19-0410-871a-916f4a2858ee
  • Loading branch information
pramsey committed Oct 31, 2011
1 parent c9e81d2 commit 2844486
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 65 deletions.
74 changes: 70 additions & 4 deletions liblwgeom/g_serialized.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ char* gserialized_to_string(const GSERIALIZED *g)
return lwgeom_to_wkt(lwgeom_from_gserialized(g), WKT_ISO, 12, 0);
}

int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox)
int gserialized_read_gbox_p(const GSERIALIZED *g, GBOX *gbox)
{

/* Null input! */
Expand All @@ -135,6 +135,7 @@ int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox)
/* Initialize the flags on the box */
gbox->flags = g->flags;

/* Has pre-calculated box */
if ( FLAGS_GET_BBOX(g->flags) )
{
int i = 0;
Expand Down Expand Up @@ -164,10 +165,75 @@ int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox)
}
return LW_SUCCESS;
}
else

/* No pre-calculated box, but for cartesian entries we can do some magic */
if ( ! FLAGS_GET_GEODETIC(g->flags) )
{
return LW_FAILURE;
uint32_t type = gserialized_get_type(g);
/* Boxes of points are easy peasy */
if ( type == POINTTYPE )
{
int i = 1; /* Start past <pointtype><padding> */
double *dptr = (double*)(g->data);
gbox->xmin = gbox->xmax = dptr[i++];
gbox->ymin = gbox->ymax = dptr[i++];
if ( FLAGS_GET_Z(g->flags) )
{
gbox->zmin = gbox->zmax = dptr[i++];
}
if ( FLAGS_GET_M(g->flags) )
{
gbox->mmin = gbox->mmax = dptr[i++];
}
return LW_SUCCESS;
}
/* We can calculate the box of a two-point cartesian line trivially */
else if ( type == LINETYPE )
{
int ndims = FLAGS_NDIMS(g->flags);
int i = 0; /* Start past <linetype><npoints> */
double *dptr = (double*)(g->data);
int *iptr = (int*)(g->data);
int npoints = iptr[1]; /* Read the npoints */

/* This only works with 2-point lines */
if ( npoints != 2 )
return LW_FAILURE;

/* Advance to X */
i++;
gbox->xmin = FP_MIN(dptr[i], dptr[i+ndims]);
gbox->xmax = FP_MAX(dptr[i], dptr[i+ndims]);

/* Advance to Y */
i++;
gbox->ymin = FP_MIN(dptr[i], dptr[i+ndims]);
gbox->ymax = FP_MAX(dptr[i], dptr[i+ndims]);

if ( FLAGS_GET_Z(g->flags) )
{
/* Advance to Z */
i++;
gbox->zmin = FP_MIN(dptr[i], dptr[i+ndims]);
gbox->zmax = FP_MAX(dptr[i], dptr[i+ndims]);
}
if ( FLAGS_GET_M(g->flags) )
{
/* Advance to M */
i++;
gbox->zmin = FP_MIN(dptr[i], dptr[i+ndims]);
gbox->zmax = FP_MAX(dptr[i], dptr[i+ndims]);
}
return LW_SUCCESS;
}
/* We could also do single-entry multi-points */
else if ( type == MULTIPOINTTYPE )
{
/* TODO: Make this actually happen */
return LW_FAILURE;
}
}
return LW_FAILURE;
}


Expand Down Expand Up @@ -1086,7 +1152,7 @@ LWGEOM* lwgeom_from_gserialized(const GSERIALIZED *g)
lwgeom->type = g_type;
lwgeom->flags = g_flags;

if ( gserialized_get_gbox_p(g, &bbox) == LW_SUCCESS )
if ( gserialized_read_gbox_p(g, &bbox) == LW_SUCCESS )
{
lwgeom->bbox = gbox_copy(&bbox);
}
Expand Down
2 changes: 1 addition & 1 deletion liblwgeom/liblwgeom.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ extern LWGEOM* lwgeom_from_gserialized(const GSERIALIZED *g);
* Pull a #GBOX from the header of a #GSERIALIZED, if one is available. If
* it is not, return LW_FAILURE.
*/
extern int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox);
extern int gserialized_read_gbox_p(const GSERIALIZED *g, GBOX *gbox);


/**
Expand Down
6 changes: 3 additions & 3 deletions libpgcommon/lwgeom_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ GSERIALIZED* geometry_serialize(LWGEOM *lwgeom)
return g;
}

int pglwgeom_getbox2d_p(const GSERIALIZED *geom, GBOX *box)
int gserialized_get_gbox_p(const GSERIALIZED *geom, GBOX *box)
{
LWGEOM *lwgeom;
int ret = gserialized_get_gbox_p(geom, box);
int ret = gserialized_read_gbox_p(geom, box);
if ( LW_FAILURE == ret ) {
/* See http://trac.osgeo.org/postgis/ticket/1023 */
lwgeom = lwgeom_from_gserialized(geom);
ret = lwgeom_calculate_gbox(lwgeom, box);
lwgeom_free(lwgeom);
lwgeom_free(lwgeom);
}
return ret;
}
2 changes: 1 addition & 1 deletion libpgcommon/lwgeom_pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ GSERIALIZED* geography_serialize(LWGEOM *lwgeom);
* Use a cached bbox if available, compute it otherwise.
* Return LW_FALSE if the geometry has no bounding box (ie: is empty).
*/
extern int pglwgeom_getbox2d_p(const GSERIALIZED *geom, GBOX *box);
extern int gserialized_get_gbox_p(const GSERIALIZED *geom, GBOX *box);

/**
* Convert cstrings (null-terminated byte array) to textp pointers
Expand Down
2 changes: 1 addition & 1 deletion postgis/geography_measurement.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ Datum geography_point_outside(PG_FUNCTION_ARGS)
g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));

/* We need the bounding box to get an outside point for area algorithm */
if ( gserialized_get_gbox_p(g, &gbox) == LW_FAILURE )
if ( gserialized_read_gbox_p(g, &gbox) == LW_FAILURE )
{
LWGEOM *lwgeom = lwgeom_from_gserialized(g);
POSTGIS_DEBUGF(4,"unable to read gbox from gserialized, calculating from lwgeom (%p)", lwgeom);
Expand Down
4 changes: 2 additions & 2 deletions postgis/lwgeom_box.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ Datum BOX2DFLOAT4_combine(PG_FUNCTION_ARGS)
{
lwgeom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
/* empty geom would make getbox2d_p return NULL */
if ( ! pglwgeom_getbox2d_p(lwgeom, &box) ) PG_RETURN_NULL();
if ( ! gserialized_get_gbox_p(lwgeom, &box) ) PG_RETURN_NULL();
memcpy(result, &box, sizeof(GBOX));
PG_RETURN_POINTER(result);
}
Expand All @@ -395,7 +395,7 @@ Datum BOX2DFLOAT4_combine(PG_FUNCTION_ARGS)
/*combine_bbox(BOX3D, geometry) => union(BOX3D, geometry->bvol) */

lwgeom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
if ( ! pglwgeom_getbox2d_p(lwgeom, &box) )
if ( ! gserialized_get_gbox_p(lwgeom, &box) )
{
/* must be the empty geom */
memcpy(result, (char *)PG_GETARG_DATUM(0), sizeof(GBOX));
Expand Down
24 changes: 12 additions & 12 deletions postgis/lwgeom_btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ Datum lwgeom_lt(PG_FUNCTION_ARGS)

POSTGIS_DEBUG(3, "lwgeom_lt passed getSRID test");

pglwgeom_getbox2d_p(geom1, &box1);
pglwgeom_getbox2d_p(geom2, &box2);
gserialized_get_gbox_p(geom1, &box1);
gserialized_get_gbox_p(geom2, &box2);

PG_FREE_IF_COPY(geom1, 0);
PG_FREE_IF_COPY(geom2, 1);
Expand Down Expand Up @@ -105,8 +105,8 @@ Datum lwgeom_le(PG_FUNCTION_ARGS)
PG_RETURN_NULL();
}

pglwgeom_getbox2d_p(geom1, &box1);
pglwgeom_getbox2d_p(geom2, &box2);
gserialized_get_gbox_p(geom1, &box1);
gserialized_get_gbox_p(geom2, &box2);

PG_FREE_IF_COPY(geom1, 0);
PG_FREE_IF_COPY(geom2, 1);
Expand Down Expand Up @@ -170,8 +170,8 @@ Datum lwgeom_eq(PG_FUNCTION_ARGS)
PG_RETURN_NULL();
}

pglwgeom_getbox2d_p(geom1, &box1);
pglwgeom_getbox2d_p(geom2, &box2);
gserialized_get_gbox_p(geom1, &box1);
gserialized_get_gbox_p(geom2, &box2);
PG_FREE_IF_COPY(geom1, 0);
PG_FREE_IF_COPY(geom2, 1);

Expand Down Expand Up @@ -207,8 +207,8 @@ Datum lwgeom_ge(PG_FUNCTION_ARGS)
PG_RETURN_NULL();
}

pglwgeom_getbox2d_p(geom1, &box1);
pglwgeom_getbox2d_p(geom2, &box2);
gserialized_get_gbox_p(geom1, &box1);
gserialized_get_gbox_p(geom2, &box2);

PG_FREE_IF_COPY(geom1, 0);
PG_FREE_IF_COPY(geom2, 1);
Expand Down Expand Up @@ -271,8 +271,8 @@ Datum lwgeom_gt(PG_FUNCTION_ARGS)
PG_RETURN_NULL();
}

pglwgeom_getbox2d_p(geom1, &box1);
pglwgeom_getbox2d_p(geom2, &box2);
gserialized_get_gbox_p(geom1, &box1);
gserialized_get_gbox_p(geom2, &box2);

PG_FREE_IF_COPY(geom1, 0);
PG_FREE_IF_COPY(geom2, 1);
Expand Down Expand Up @@ -331,8 +331,8 @@ Datum lwgeom_cmp(PG_FUNCTION_ARGS)
PG_RETURN_NULL();
}

pglwgeom_getbox2d_p(geom1, &box1);
pglwgeom_getbox2d_p(geom2, &box2);
gserialized_get_gbox_p(geom1, &box1);
gserialized_get_gbox_p(geom2, &box2);

PG_FREE_IF_COPY(geom1, 0);
PG_FREE_IF_COPY(geom2, 1);
Expand Down
2 changes: 1 addition & 1 deletion postgis/lwgeom_functions_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -2402,7 +2402,7 @@ Datum optimistic_overlap(PG_FUNCTION_ARGS)
}

/*bbox check */
pglwgeom_getbox2d_p(pg_geom1, &g1_bvol );
gserialized_get_gbox_p(pg_geom1, &g1_bvol );

g1_bvol.xmin = g1_bvol.xmin - dist;
g1_bvol.ymin = g1_bvol.ymin - dist;
Expand Down
50 changes: 25 additions & 25 deletions postgis/lwgeom_geos.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ Datum convexhull(PG_FUNCTION_ARGS)
}

/* Copy input bbox if any */
if ( pglwgeom_getbox2d_p(geom1, &bbox) )
if ( gserialized_get_gbox_p(geom1, &bbox) )
{
/* Force the box to have the same dimensionality as the lwgeom */
bbox.flags = lwout->flags;
Expand Down Expand Up @@ -1524,7 +1524,7 @@ Datum isvalid(PG_FUNCTION_ARGS)
#if POSTGIS_GEOS_VERSION < 33
/* Short circuit and return FALSE if we have infinite coordinates */
/* GEOS 3.3+ is supposed to handle this stuff OK */
if ( pglwgeom_getbox2d_p(geom1, &box1) )
if ( gserialized_get_gbox_p(geom1, &box1) )
{
if ( isinf(box1.xmax) || isinf(box1.ymax) || isinf(box1.xmin) || isinf(box1.ymin) ||
isnan(box1.xmax) || isnan(box1.ymax) || isnan(box1.xmin) || isnan(box1.ymin) )
Expand Down Expand Up @@ -1588,7 +1588,7 @@ Datum isvalidreason(PG_FUNCTION_ARGS)
#if POSTGIS_GEOS_VERSION < 33
/* Short circuit and return if we have infinite coordinates */
/* GEOS 3.3+ is supposed to handle this stuff OK */
if ( pglwgeom_getbox2d_p(geom, &box) )
if ( gserialized_get_gbox_p(geom, &box) )
{
if ( isinf(box.xmax) || isinf(box.ymax) || isinf(box.xmin) || isinf(box.ymin) ||
isnan(box.xmax) || isnan(box.ymax) || isnan(box.xmin) || isnan(box.ymin) )
Expand Down Expand Up @@ -1767,8 +1767,8 @@ Datum overlaps(PG_FUNCTION_ARGS)
* geom1 bounding box we can prematurely return FALSE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
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);
Expand Down Expand Up @@ -1845,8 +1845,8 @@ Datum contains(PG_FUNCTION_ARGS)
** geom1 bounding box we can prematurely return FALSE.
** Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
if ( gserialized_get_gbox_p(geom1, &box1) &&
gserialized_get_gbox_p(geom2, &box2) )
{
if ( ( box2.xmin < box1.xmin ) || ( box2.xmax > box1.xmax ) ||
( box2.ymin < box1.ymin ) || ( box2.ymax > box1.ymax ) )
Expand Down Expand Up @@ -1993,8 +1993,8 @@ Datum containsproperly(PG_FUNCTION_ARGS)
* geom1 bounding box we can prematurely return FALSE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
if ( gserialized_get_gbox_p(geom1, &box1) &&
gserialized_get_gbox_p(geom2, &box2) )
{
if (( box2.xmin < box1.xmin ) || ( box2.xmax > box1.xmax ) ||
( box2.ymin < box1.ymin ) || ( box2.ymax > box1.ymax ))
Expand Down Expand Up @@ -2088,8 +2088,8 @@ Datum covers(PG_FUNCTION_ARGS)
* geom1 bounding box we can prematurely return FALSE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
if ( gserialized_get_gbox_p(geom1, &box1) &&
gserialized_get_gbox_p(geom2, &box2) )
{
if (( box2.xmin < box1.xmin ) || ( box2.xmax > box1.xmax ) ||
( box2.ymin < box1.ymin ) || ( box2.ymax > box1.ymax ))
Expand Down Expand Up @@ -2243,8 +2243,8 @@ Datum within(PG_FUNCTION_ARGS)
* geom2 bounding box we can prematurely return FALSE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
if ( gserialized_get_gbox_p(geom1, &box1) &&
gserialized_get_gbox_p(geom2, &box2) )
{
if ( ( box1.xmin < box2.xmin ) || ( box1.xmax > box2.xmax ) ||
( box1.ymin < box2.ymin ) || ( box1.ymax > box2.ymax ) )
Expand Down Expand Up @@ -2378,8 +2378,8 @@ Datum coveredby(PG_FUNCTION_ARGS)
* geom2 bounding box we can prematurely return FALSE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
if ( gserialized_get_gbox_p(geom1, &box1) &&
gserialized_get_gbox_p(geom2, &box2) )
{
if ( ( box1.xmin < box2.xmin ) || ( box1.xmax > box2.xmax ) ||
( box1.ymin < box2.ymin ) || ( box1.ymax > box2.ymax ) )
Expand Down Expand Up @@ -2507,8 +2507,8 @@ Datum crosses(PG_FUNCTION_ARGS)
* geom1 bounding box we can prematurely return FALSE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
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 ) )
Expand Down Expand Up @@ -2584,8 +2584,8 @@ Datum intersects(PG_FUNCTION_ARGS)
* geom1 bounding box we can prematurely return FALSE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
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 ) )
Expand Down Expand Up @@ -2751,8 +2751,8 @@ Datum touches(PG_FUNCTION_ARGS)
* geom1 bounding box we can prematurely return FALSE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
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 ) )
Expand Down Expand Up @@ -2820,8 +2820,8 @@ Datum disjoint(PG_FUNCTION_ARGS)
* geom1 bounding box we can prematurely return TRUE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
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 ) )
Expand Down Expand Up @@ -3044,8 +3044,8 @@ Datum geomequals(PG_FUNCTION_ARGS)
* geom1 bounding box we can prematurely return FALSE.
* Do the test IFF BOUNDING BOX AVAILABLE.
*/
if ( pglwgeom_getbox2d_p(geom1, &box1) &&
pglwgeom_getbox2d_p(geom2, &box2) )
if ( gserialized_get_gbox_p(geom1, &box1) &&
gserialized_get_gbox_p(geom2, &box2) )
{
if ( box2.xmax != box1.xmax ) PG_RETURN_BOOL(FALSE);
if ( box2.xmin != box1.xmin ) PG_RETURN_BOOL(FALSE);
Expand Down
Loading

0 comments on commit 2844486

Please sign in to comment.