PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ gserialized2_from_lwgeom()

GSERIALIZED* gserialized2_from_lwgeom ( LWGEOM geom,
size_t *  size 
)

Allocate a new GSERIALIZED from an LWGEOM.

For all non-point types, a bounding box will be calculated and embedded in the serialization. The geodetic flag is used to control the box calculation (cartesian or geocentric). If set, the size pointer will contain the size of the final output, which is useful for setting the PgSQL VARSIZE information.

Definition at line 1165 of file gserialized2.c.

1166 {
1167  size_t expected_size = 0;
1168  size_t return_size = 0;
1169  uint8_t *ptr = NULL;
1170  GSERIALIZED *g = NULL;
1171  assert(geom);
1172 
1173  /*
1174  ** See if we need a bounding box, add one if we don't have one.
1175  */
1176  if ((!geom->bbox) && lwgeom_needs_bbox(geom) && (!lwgeom_is_empty(geom)))
1177  {
1178  lwgeom_add_bbox(geom);
1179  }
1180 
1181  /*
1182  ** Harmonize the flags to the state of the lwgeom
1183  */
1184  FLAGS_SET_BBOX(geom->flags, (geom->bbox ? 1 : 0));
1185 
1186  /* Set up the uint8_t buffer into which we are going to write the serialized geometry. */
1187  expected_size = gserialized2_from_lwgeom_size(geom);
1188  ptr = lwalloc(expected_size);
1189  g = (GSERIALIZED*)(ptr);
1190 
1191  /* Set the SRID! */
1192  gserialized2_set_srid(g, geom->srid);
1193  /*
1194  ** We are aping PgSQL code here, PostGIS code should use
1195  ** VARSIZE to set this for real.
1196  */
1197  SIZE_SET(g->size, expected_size);
1198  g->gflags = lwflags_get_g2flags(geom->flags);
1199 
1200  /* Move write head past size, srid and flags. */
1201  ptr += 8;
1202 
1203  /* Write in the extended flags if necessary */
1204  ptr += gserialized2_from_extended_flags(geom->flags, ptr);
1205 
1206  /* Write in the serialized form of the gbox, if necessary. */
1207  if (geom->bbox)
1208  ptr += gserialized2_from_gbox(geom->bbox, ptr);
1209 
1210  /* Write in the serialized form of the geometry. */
1211  ptr += gserialized2_from_lwgeom_any(geom, ptr);
1212 
1213  /* Calculate size as returned by data processing functions. */
1214  return_size = ptr - (uint8_t*)g;
1215 
1216  if (expected_size != return_size) /* Uh oh! */
1217  {
1218  lwerror("Return size (%lu) not equal to expected size (%lu)!", return_size, expected_size);
1219  return NULL;
1220  }
1221 
1222  if (size) /* Return the output size to the caller if necessary. */
1223  *size = return_size;
1224 
1225  return g;
1226 }
void gserialized2_set_srid(GSERIALIZED *g, int32_t srid)
Write the SRID into the serialized form (it is packed into three bytes so this is a handy function).
Definition: gserialized2.c:207
static size_t gserialized2_from_gbox(const GBOX *gbox, uint8_t *buf)
uint8_t lwflags_get_g2flags(lwflags_t lwflags)
Definition: gserialized2.c:116
static size_t gserialized2_from_lwgeom_any(const LWGEOM *geom, uint8_t *buf)
size_t gserialized2_from_lwgeom_size(const LWGEOM *geom)
Return the memory size a GSERIALIZED will occupy for a given LWGEOM.
Definition: gserialized2.c:774
static size_t gserialized2_from_extended_flags(lwflags_t lwflags, uint8_t *buf)
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:188
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
Definition: lwgeom.c:1191
void * lwalloc(size_t size)
Definition: lwutil.c:227
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:677
#define SIZE_SET(varsize, len)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:193
uint32_t size
Definition: liblwgeom.h:430
uint8_t gflags
Definition: liblwgeom.h:432
GBOX * bbox
Definition: liblwgeom.h:444
int32_t srid
Definition: liblwgeom.h:446
lwflags_t flags
Definition: liblwgeom.h:447

References LWGEOM::bbox, LWGEOM::flags, FLAGS_SET_BBOX, GSERIALIZED::gflags, gserialized2_from_extended_flags(), gserialized2_from_gbox(), gserialized2_from_lwgeom_any(), gserialized2_from_lwgeom_size(), gserialized2_set_srid(), lwalloc(), lwerror(), lwflags_get_g2flags(), lwgeom_add_bbox(), lwgeom_is_empty(), lwgeom_needs_bbox(), GSERIALIZED::size, SIZE_SET, and LWGEOM::srid.

Referenced by gserialized_from_lwgeom(), peek2_point_helper(), test_gserialized2_extended_flags(), test_gserialized2_is_empty(), test_gserialized2_peek_gbox_p_gets_correct_box(), test_gserialized2_peek_gbox_p_no_box_when_empty(), test_lwgeom_from_gserialized2(), and test_on_gser2_lwgeom_count_vertices().

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