PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ 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 1580 of file gserialized2.c.

1581 {
1582 
1583  int g_ndims = G2FLAGS_NDIMS_BOX(g->gflags);
1584  int box_ndims = FLAGS_NDIMS_BOX(gbox->flags);
1585  GSERIALIZED *g_out = NULL;
1586  size_t box_size = 2 * g_ndims * sizeof(float);
1587  float *fbox;
1588  int fbox_pos = 0;
1589 
1590  /* The dimensionality of the inputs has to match or we are SOL. */
1591  if (g_ndims != box_ndims)
1592  {
1593  return NULL;
1594  }
1595 
1596  /* Serialized already has room for a box. */
1597  if (G2FLAGS_GET_BBOX(g->gflags))
1598  {
1599  g_out = g;
1600  }
1601  /* Serialized has no box. We need to allocate enough space for the old
1602  data plus the box, and leave a gap in the memory segment to write
1603  the new values into.
1604  */
1605  else
1606  {
1607  size_t varsize_in = SIZE_GET(g->size);
1608  size_t varsize_out = varsize_in + box_size;
1609  uint8_t *ptr_out, *ptr_in, *ptr;
1610  g_out = lwalloc(varsize_out);
1611  ptr_out = (uint8_t*)g_out;
1612  ptr = ptr_in = (uint8_t*)g;
1613  /* Copy the head of g into place */
1614  memcpy(ptr_out, ptr_in, 8); ptr_out += 8; ptr_in += 8;
1615  /* Optionally copy extended bit into place */
1616  if (G2FLAGS_GET_EXTENDED(g->gflags))
1617  {
1618  memcpy(ptr_out, ptr_in, 8); ptr_out += 8; ptr_in += 8;
1619  }
1620  /* Copy the body of g into place after leaving space for the box */
1621  ptr_out += box_size;
1622  memcpy(ptr_out, ptr_in, varsize_in - (ptr_in - ptr));
1623  G2FLAGS_SET_BBOX(g_out->gflags, 1);
1624  SIZE_SET(g_out->size, varsize_out);
1625  }
1626 
1627  /* Move bounds to nearest float values */
1628  gbox_float_round(gbox);
1629  /* Now write the float box values into the memory segement */
1630  fbox = (float*)(g_out->data);
1631  /* Copy in X/Y */
1632  fbox[fbox_pos++] = gbox->xmin;
1633  fbox[fbox_pos++] = gbox->xmax;
1634  fbox[fbox_pos++] = gbox->ymin;
1635  fbox[fbox_pos++] = gbox->ymax;
1636  /* Optionally copy in higher dims */
1638  {
1639  fbox[fbox_pos++] = gbox->zmin;
1640  fbox[fbox_pos++] = gbox->zmax;
1641  }
1643  {
1644  fbox[fbox_pos++] = gbox->mmin;
1645  fbox[fbox_pos++] = gbox->mmax;
1646  }
1647 
1648  return g_out;
1649 }
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.
Definition: gserialized2.c:171
int gserialized2_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized2.c:156
int gserialized2_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
Definition: gserialized2.c:161
#define G2FLAGS_NDIMS_BOX(gflags)
Definition: gserialized2.h:38
#define G2FLAGS_GET_BBOX(gflags)
Definition: gserialized2.h:24
#define G2FLAGS_GET_EXTENDED(gflags)
Definition: gserialized2.h:26
#define G2FLAGS_SET_BBOX(gflags, value)
Definition: gserialized2.h:31
#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: