PostGIS  3.7.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 1560 of file gserialized2.c.

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