PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ gserialized2_set_gbox()

GSERIALIZED * gserialized2_set_gbox ( GSERIALIZED g,
GBOX gbox 
)

Update the bounding box of a GSERIALIZED, allocating a fresh one if there is not enough space to just write the new box in.

Copy a new bounding box into an existing gserialized.

WARNING if a new object needs to be created, the input pointer will have to be freed by the caller! Check to see if input == output. Returns null if there's a problem like mismatched dimensions.

Definition at line 1613 of file gserialized2.c.

1614{
1615
1616 int g_ndims = G2FLAGS_NDIMS_BOX(g->gflags);
1617 int box_ndims = FLAGS_NDIMS_BOX(gbox->flags);
1618 GSERIALIZED *g_out = NULL;
1619 size_t box_size = 2 * g_ndims * sizeof(float);
1620 float *fbox;
1621 int fbox_pos = 0;
1622
1623 /* The dimensionality of the inputs has to match or we are SOL. */
1624 if (g_ndims != box_ndims)
1625 {
1626 return NULL;
1627 }
1628
1629 /* Serialized already has room for a box. */
1630 if (G2FLAGS_GET_BBOX(g->gflags))
1631 {
1632 g_out = g;
1633 }
1634 /* Serialized has no box. We need to allocate enough space for the old
1635 data plus the box, and leave a gap in the memory segment to write
1636 the new values into.
1637 */
1638 else
1639 {
1640 size_t varsize_in = SIZE_GET(g->size);
1641 size_t varsize_out = varsize_in + box_size;
1642 uint8_t *ptr_out, *ptr_in, *ptr;
1643 g_out = lwalloc(varsize_out);
1644 ptr_out = (uint8_t*)g_out;
1645 ptr = ptr_in = (uint8_t*)g;
1646 /* Copy the head of g into place */
1647 memcpy(ptr_out, ptr_in, 8); ptr_out += 8; ptr_in += 8;
1648 /* Optionally copy extended bit into place */
1650 {
1651 memcpy(ptr_out, ptr_in, 8); ptr_out += 8; ptr_in += 8;
1652 }
1653 /* Copy the body of g into place after leaving space for the box */
1654 ptr_out += box_size;
1655 memcpy(ptr_out, ptr_in, varsize_in - (ptr_in - ptr));
1656 G2FLAGS_SET_BBOX(g_out->gflags, 1);
1657 SIZE_SET(g_out->size, varsize_out);
1658 }
1659
1660 /* Move bounds to nearest float values */
1661 gbox_float_round(gbox);
1662 /* Now write the float box values into the memory segement */
1663 fbox = (float*)(g_out->data);
1664 /* Copy in X/Y */
1665 fbox[fbox_pos++] = gbox->xmin;
1666 fbox[fbox_pos++] = gbox->xmax;
1667 fbox[fbox_pos++] = gbox->ymin;
1668 fbox[fbox_pos++] = gbox->ymax;
1669 /* Optionally copy in higher dims */
1671 {
1672 fbox[fbox_pos++] = gbox->zmin;
1673 fbox[fbox_pos++] = gbox->zmax;
1674 }
1676 {
1677 fbox[fbox_pos++] = gbox->mmin;
1678 fbox[fbox_pos++] = gbox->mmax;
1679 }
1680
1681 return g_out;
1682}
void gbox_float_round(GBOX *gbox)
Round given GBOX to float boundaries.
Definition gbox.c:774
int gserialized2_is_geodetic(const GSERIALIZED *g)
Check if a GSERIALIZED is a geography.
int gserialized2_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
int gserialized2_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
#define G2FLAGS_NDIMS_BOX(gflags)
#define G2FLAGS_GET_BBOX(gflags)
#define G2FLAGS_GET_EXTENDED(gflags)
#define G2FLAGS_SET_BBOX(gflags, value)
#define FLAGS_NDIMS_BOX(flags)
Definition liblwgeom.h:195
void * lwalloc(size_t size)
Definition lwutil.c:227
#define SIZE_GET(varsize)
Macro for reading the size from the GSERIALIZED size attribute.
#define SIZE_SET(varsize, len)
double ymax
Definition liblwgeom.h:343
double zmax
Definition liblwgeom.h:345
double xmax
Definition liblwgeom.h:341
double zmin
Definition liblwgeom.h:344
double mmax
Definition liblwgeom.h:347
double ymin
Definition liblwgeom.h:342
double xmin
Definition liblwgeom.h:340
double mmin
Definition liblwgeom.h:346
lwflags_t flags
Definition liblwgeom.h:339
uint32_t size
Definition liblwgeom.h:430
uint8_t data[1]
Definition liblwgeom.h:433
uint8_t gflags
Definition liblwgeom.h:432

References GSERIALIZED::data, GBOX::flags, FLAGS_NDIMS_BOX, G2FLAGS_GET_BBOX, G2FLAGS_GET_EXTENDED, G2FLAGS_NDIMS_BOX, G2FLAGS_SET_BBOX, gbox_float_round(), GSERIALIZED::gflags, gserialized2_has_m(), gserialized2_has_z(), gserialized2_is_geodetic(), lwalloc(), GBOX::mmax, GBOX::mmin, GSERIALIZED::size, SIZE_GET, SIZE_SET, GBOX::xmax, GBOX::xmin, GBOX::ymax, GBOX::ymin, GBOX::zmax, and GBOX::zmin.

Referenced by gserialized_set_gbox().

Here is the call graph for this function:
Here is the caller graph for this function: