-//Connvert BOX3D to BOX2D
+// Convert BOX3D to BOX2D
+// returned box2d is allocated with 'palloc'
BOX2DFLOAT4 *box3d_to_box2df(BOX3D *box)
{
BOX2DFLOAT4 *result = (BOX2DFLOAT4*) palloc(sizeof(BOX2DFLOAT4));
if (box == NULL)
+ {
+ elog(NOTICE, "box3d_to_box2df got NUL box");
return result;
+ }
result->xmin = nextDown_f(box->xmin);
result->ymin = nextDown_f(box->ymin);
// otherwise we need to compute it.
BOX2DFLOAT4 getbox2d(char *serialized_form)
{
- int type = (unsigned char) serialized_form[0];
- char *loc;
- BOX2DFLOAT4 result;
- BOX3D *box3d;
- BOX2DFLOAT4 *box;
+ int type = (unsigned char) serialized_form[0];
+ char *loc;
+ BOX2DFLOAT4 result;
+ BOX3D *box3d;
+ BOX2DFLOAT4 *box;
- loc = serialized_form+1;
+ loc = serialized_form+1;
- if (lwgeom_hasBBOX(type))
- {
- //woot - this is easy
+//elog(NOTICE,"getbox2d: type is %d", type);
+
+ if (lwgeom_hasBBOX(type))
+ {
+ //woot - this is easy
//elog(NOTICE,"getbox2d has box");
- memcpy(&result,loc, sizeof(BOX2DFLOAT4));
- return result;
- }
+ memcpy(&result,loc, sizeof(BOX2DFLOAT4));
+ return result;
+ }
- //we have to actually compute it!
+ //we have to actually compute it!
//elog(NOTICE,"getbox2d -- computing bbox");
- box3d = lw_geom_getBB_simple(serialized_form);
-//elog(NOTICE,"getbox2d got bbox3d(%.15g %.15g,%.15g %.15g)",box3d->xmin,box3d->ymin,box3d->xmax,box3d->ymax);
- box = box3d_to_box2df(box3d);
-//elog(NOTICE,"getbox2d made box2d(%.15g %.15g,%.15g %.15g)",box->xmin,box->ymin,box->xmax,box->ymax);
- memcpy(&result,box, sizeof(BOX2DFLOAT4));
- pfree(box3d);
- pfree(box);
- return result;
+ box3d = lw_geom_getBB_simple(serialized_form);
+//elog(NOTICE,"lw_geom_getBB_simple got bbox3d(%.15g %.15g,%.15g %.15g)",box3d->xmin,box3d->ymin,box3d->xmax,box3d->ymax);
+ box = box3d_to_box2df(box3d);
+//elog(NOTICE,"box3d made box2d(%.15g %.15g,%.15g %.15g)",box->xmin,box->ymin,box->xmax,box->ymax);
+ memcpy(&result,box, sizeof(BOX2DFLOAT4));
+ pfree(box3d);
+ pfree(box);
+ return result;
}
{
int size;
- if ( (n<0) || (n>=pa->npoints))
- {
- return ; //error
- }
+ if ( (n<0) || (n>=pa->npoints))
+ {
+ elog(NOTICE, "%d out of numpoint range (%d)", n, pa->npoints);
+ return ; //error
+ }
size = pointArray_ptsize(pa);
- // this does x,y
- memcpy(point, &pa->serialized_pointlist[size*n],sizeof(double)*2 );
- if (pa->ndims >2)
- memcpy(point+16, &pa->serialized_pointlist[size*n + sizeof(double)*2],sizeof(double) );
- else
- {
- double bad=NO_Z_VALUE;
+ // this does x,y
+ memcpy(point,
+ &pa->serialized_pointlist[size*n],
+ sizeof(double)*2 );
+
+ if (pa->ndims >2)
+ memcpy(point+16, &pa->serialized_pointlist[size*n + sizeof(double)*2],sizeof(double) );
+ else
+ {
+ double bad=NO_Z_VALUE;
memcpy(point+16, &bad,sizeof(double) );
//point->z = NO_Z_VALUE;
- }
+ }
}
result = (BOX3D*) palloc(sizeof(BOX3D));
- if (pa->npoints ==0)
+ if (pa->npoints == 0)
return result;
getPoint3d_p(pa,0,(char*)&pt);
getPoint3d_p(pa,t,(char*)&pt);
if (pt.x < result->xmin)
result->xmin = pt.x;
- if (pt.y < result->ymin)
+ if (pt.y <
+ result->ymin)
result->ymin = pt.y;
if (pt.x > result->xmax)
result->xmax = pt.x;
//dont forget to pfree() result
BOX3D *lw_geom_getBB_simple(char *serialized_form)
{
- char type = lwgeom_getType((unsigned char) serialized_form[0]);
- int t;
- char *loc;
- uint32 ngeoms;
- BOX3D *result;
- BOX3D *b1,*b2;
- int sub_size;
+ char type = lwgeom_getType((unsigned char) serialized_form[0]);
+ int t;
+ char *loc;
+ uint32 ngeoms;
+ BOX3D *result;
+ BOX3D *b1,*b2;
+ int sub_size;
+ if (type == POINTTYPE)
+ {
+ LWPOINT *pt = lwpoint_deserialize(serialized_form);
+ result = lwpoint_findbbox(pt);
+ pfree_point(pt);
+ return result;
+ /*
+ result = palloc(sizeof(BOX3D));
+ memcpy(result, serialized_form+1, sizeof(BOX2DFLOAT4));
+ memcpy(( (char *)result)+24, serialized_form+1, sizeof(BOX2DFLOAT4));
+ return result;
+ */
+ }
- if (type == POINTTYPE)
- {
+ else if (type == LINETYPE)
+ {
+ LWLINE *line = lwline_deserialize(serialized_form);
+ result = lwline_findbbox(line);
+ pfree_line(line);
+ return result;
- LWPOINT *pt = lwpoint_deserialize(serialized_form);
- result = lwpoint_findbbox(pt);
- pfree_point(pt);
- return result;
- /*
- result = palloc(sizeof(BOX3D));
- memcpy(result, serialized_form+1, sizeof(BOX2DFLOAT4));
- memcpy(( (char *)result)+24, serialized_form+1, sizeof(BOX2DFLOAT4));
- return result;
- */
- }
+ }
+ else if (type == POLYGONTYPE)
+ {
+ LWPOLY *poly = lwpoly_deserialize(serialized_form);
+ result = lwpoly_findbbox(poly);
+ pfree_polygon(poly);
+ return result;
+ }
- else if (type == LINETYPE)
- {
- LWLINE *line = lwline_deserialize(serialized_form);
- result = lwline_findbbox(line);
- pfree_line(line);
- return result;
+ loc = serialized_form+1;
- }
- else if (type == POLYGONTYPE)
+ if (lwgeom_hasBBOX((unsigned char) serialized_form[0]))
{
- LWPOLY *poly = lwpoly_deserialize(serialized_form);
- result = lwpoly_findbbox(poly);
- pfree_polygon(poly);
- return result;
+ loc += sizeof(BOX2DFLOAT4);
}
- loc = serialized_form+1;
+ if (lwgeom_hasSRID((unsigned char) serialized_form[0]) )
+ {
+ loc +=4;
+ }
- if (lwgeom_hasBBOX((unsigned char) serialized_form[0]))
- {
- loc += sizeof(BOX2DFLOAT4);
- }
+ ngeoms = get_uint32(loc);
+ loc +=4;
- if (lwgeom_hasSRID((unsigned char) serialized_form[0]) )
+ result = NULL;
+ // each sub-type
+ for (t=0;t<ngeoms;t++)
+ {
+ b1 = lw_geom_getBB_simple(loc);
+ sub_size = lwgeom_seralizedformlength_simple(loc);
+ loc += sub_size;
+ if (result != NULL)
{
- loc +=4;
+ b2= result;
+ result = combine_boxes(b2, b1);
+ pfree(b1);
+ pfree(b2);
}
-
- ngeoms = get_uint32(loc);
- loc +=4;
-
- result = NULL;
- // each sub-type
- for (t=0;t<ngeoms;t++)
+ else
{
- b1 = lw_geom_getBB_simple(loc);
- sub_size = lwgeom_seralizedformlength_simple(loc);
- loc += sub_size;
- if (result != NULL)
- {
- b2= result;
- result = combine_boxes(b2, b1);
- pfree(b1);
- pfree(b2);
- }
- else
- {
- result = b1;
- }
+ result = b1;
}
+ }
return result;
PG_FUNCTION_INFO_V1(LWGEOM_to_BOX2DFLOAT4);
Datum LWGEOM_to_BOX2DFLOAT4(PG_FUNCTION_ARGS)
{
- char *lwgeom = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- BOX2DFLOAT4 box, *result;
+ char *lwgeom = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ BOX2DFLOAT4 box, *result;
- box = getbox2d(lwgeom+4);
- result = palloc(sizeof(BOX2DFLOAT4));
- memcpy(result,&box, sizeof(BOX2DFLOAT4));
- PG_RETURN_POINTER(result);
+ box = getbox2d(lwgeom+4);
+ result = palloc(sizeof(BOX2DFLOAT4));
+ memcpy(result,&box, sizeof(BOX2DFLOAT4));
+ PG_RETURN_POINTER(result);
}
/*----------------------------------------------------------
PG_FUNCTION_INFO_V1(box2d_union);
Datum box2d_union(PG_FUNCTION_ARGS)
{
- BOX2DFLOAT4 *a = (BOX2DFLOAT4*) PG_GETARG_POINTER(0);
- BOX2DFLOAT4 *b = (BOX2DFLOAT4*) PG_GETARG_POINTER(1);
- BOX2DFLOAT4 *n;
-
- n = (BOX2DFLOAT4 *) palloc(sizeof(BOX2DFLOAT4));
+ BOX2DFLOAT4 *a = (BOX2DFLOAT4*) PG_GETARG_POINTER(0);
+ BOX2DFLOAT4 *b = (BOX2DFLOAT4*) PG_GETARG_POINTER(1);
+ BOX2DFLOAT4 *n;
+ n = (BOX2DFLOAT4 *) palloc(sizeof(BOX2DFLOAT4));
n->xmax = LWGEOM_Maxf(a->xmax, b->xmax);
- n->ymax = LWGEOM_Maxf(a->ymax, b->ymax);
- n->xmin = LWGEOM_Minf(a->xmin, b->xmin);
- n->ymin = LWGEOM_Minf(a->ymin, b->ymin);
+ n->ymax = LWGEOM_Maxf(a->ymax, b->ymax);
+ n->xmin = LWGEOM_Minf(a->xmin, b->xmin);
+ n->ymin = LWGEOM_Minf(a->ymin, b->ymin);
- PG_RETURN_POINTER(n);
+ PG_RETURN_POINTER(n);
}