PostGIS  3.3.9dev-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 1561 of file gserialized2.c.

1562 {
1563 
1564  int g_ndims = G2FLAGS_NDIMS_BOX(g->gflags);
1565  int box_ndims = FLAGS_NDIMS_BOX(gbox->flags);
1566  GSERIALIZED *g_out = NULL;
1567  size_t box_size = 2 * g_ndims * sizeof(float);
1568  float *fbox;
1569  int fbox_pos = 0;
1570 
1571  /* The dimensionality of the inputs has to match or we are SOL. */
1572  if (g_ndims != box_ndims)
1573  {
1574  return NULL;
1575  }
1576 
1577  /* Serialized already has room for a box. */
1578  if (G2FLAGS_GET_BBOX(g->gflags))
1579  {
1580  g_out = g;
1581  }
1582  /* Serialized has no box. We need to allocate enough space for the old
1583  data plus the box, and leave a gap in the memory segment to write
1584  the new values into.
1585  */
1586  else
1587  {
1588  size_t varsize_in = LWSIZE_GET(g->size);
1589  size_t varsize_out = varsize_in + box_size;
1590  uint8_t *ptr_out, *ptr_in, *ptr;
1591  g_out = lwalloc(varsize_out);
1592  ptr_out = (uint8_t*)g_out;
1593  ptr = ptr_in = (uint8_t*)g;
1594  /* Copy the head of g into place */
1595  memcpy(ptr_out, ptr_in, 8); ptr_out += 8; ptr_in += 8;
1596  /* Optionally copy extended bit into place */
1597  if (G2FLAGS_GET_EXTENDED(g->gflags))
1598  {
1599  memcpy(ptr_out, ptr_in, 8); ptr_out += 8; ptr_in += 8;
1600  }
1601  /* Copy the body of g into place after leaving space for the box */
1602  ptr_out += box_size;
1603  memcpy(ptr_out, ptr_in, varsize_in - (ptr_in - ptr));
1604  G2FLAGS_SET_BBOX(g_out->gflags, 1);
1605  LWSIZE_SET(g_out->size, varsize_out);
1606  }
1607 
1608  /* Move bounds to nearest float values */
1609  gbox_float_round(gbox);
1610  /* Now write the float box values into the memory segement */
1611  fbox = (float*)(g_out->data);
1612  /* Copy in X/Y */
1613  fbox[fbox_pos++] = gbox->xmin;
1614  fbox[fbox_pos++] = gbox->xmax;
1615  fbox[fbox_pos++] = gbox->ymin;
1616  fbox[fbox_pos++] = gbox->ymax;
1617  /* Optionally copy in higher dims */
1619  {
1620  fbox[fbox_pos++] = gbox->zmin;
1621  fbox[fbox_pos++] = gbox->zmax;
1622  }
1624  {
1625  fbox[fbox_pos++] = gbox->mmin;
1626  fbox[fbox_pos++] = gbox->mmax;
1627  }
1628 
1629  return g_out;
1630 }
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:173
int gserialized2_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized2.c:158
int gserialized2_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
Definition: gserialized2.c:163
#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:196
#define LWSIZE_GET(varsize)
Macro for reading the size from the GSERIALIZED size attribute.
Definition: liblwgeom.h:339
#define LWSIZE_SET(varsize, len)
Definition: liblwgeom.h:340
void * lwalloc(size_t size)
Definition: lwutil.c:227
double ymax
Definition: liblwgeom.h:372
double zmax
Definition: liblwgeom.h:374
double xmax
Definition: liblwgeom.h:370
double zmin
Definition: liblwgeom.h:373
double mmax
Definition: liblwgeom.h:376
double ymin
Definition: liblwgeom.h:371
double xmin
Definition: liblwgeom.h:369
double mmin
Definition: liblwgeom.h:375
lwflags_t flags
Definition: liblwgeom.h:368
uint32_t size
Definition: liblwgeom.h:459
uint8_t data[1]
Definition: liblwgeom.h:462
uint8_t gflags
Definition: liblwgeom.h:461

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(), LWSIZE_GET, LWSIZE_SET, GBOX::mmax, GBOX::mmin, GSERIALIZED::size, 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: