PostGIS  3.4.0dev-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 1559 of file gserialized2.c.

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

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: