PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ gserialized_from_lwgeom()

GSERIALIZED* gserialized_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 1178 of file g_serialized.c.

References LWGEOM::bbox, GSERIALIZED::flags, LWGEOM::flags, FLAGS_SET_BBOX, genraster::g_size, gserialized_from_gbox(), gserialized_from_lwgeom_any(), gserialized_from_lwgeom_size(), gserialized_set_srid(), lwalloc(), lwerror(), lwgeom_add_bbox(), lwgeom_from_gserialized_buffer(), lwgeom_is_empty(), lwgeom_needs_bbox(), GSERIALIZED::size, and LWGEOM::srid.

Referenced by cluster_within_distance_garray(), geography_centroid(), geography_point_outside(), LWGEOM_dumppoints(), lwgeom_over_gserialized(), polyhedralsurface_parse(), RASTER_convex_hull(), RASTER_dumpAsPolygons(), RASTER_envelope(), RASTER_getPixelPolygons(), ST_GeneratePoints(), ST_OffsetCurve(), test_gbox_same_2d(), test_gserialized_from_lwgeom(), test_gserialized_is_empty(), test_gserialized_peek_gbox_p_gets_correct_box(), test_gserialized_peek_gbox_p_no_box_when_empty(), test_lwgeom_from_gserialized(), test_on_gser_lwgeom_count_vertices(), tin_parse(), and triangle_parse().

1179 {
1180  size_t expected_size = 0;
1181  size_t return_size = 0;
1182  uint8_t *serialized = NULL;
1183  uint8_t *ptr = NULL;
1184  GSERIALIZED *g = NULL;
1185  assert(geom);
1186 
1187  /*
1188  ** See if we need a bounding box, add one if we don't have one.
1189  */
1190  if ( (! geom->bbox) && lwgeom_needs_bbox(geom) && (!lwgeom_is_empty(geom)) )
1191  {
1192  lwgeom_add_bbox(geom);
1193  }
1194 
1195  /*
1196  ** Harmonize the flags to the state of the lwgeom
1197  */
1198  if ( geom->bbox )
1199  FLAGS_SET_BBOX(geom->flags, 1);
1200  else
1201  FLAGS_SET_BBOX(geom->flags, 0);
1202 
1203  /* Set up the uint8_t buffer into which we are going to write the serialized geometry. */
1204  expected_size = gserialized_from_lwgeom_size(geom);
1205  serialized = lwalloc(expected_size);
1206  ptr = serialized;
1207 
1208  /* Move past size, srid and flags. */
1209  ptr += 8;
1210 
1211  /* Write in the serialized form of the gbox, if necessary. */
1212  if ( geom->bbox )
1213  ptr += gserialized_from_gbox(geom->bbox, ptr);
1214 
1215  /* Write in the serialized form of the geometry. */
1216  ptr += gserialized_from_lwgeom_any(geom, ptr);
1217 
1218  /* Calculate size as returned by data processing functions. */
1219  return_size = ptr - serialized;
1220 
1221  if ( expected_size != return_size ) /* Uh oh! */
1222  {
1223  lwerror("Return size (%d) not equal to expected size (%d)!", return_size, expected_size);
1224  return NULL;
1225  }
1226 
1227  if ( size ) /* Return the output size to the caller if necessary. */
1228  *size = return_size;
1229 
1230  g = (GSERIALIZED*)serialized;
1231 
1232  /*
1233  ** We are aping PgSQL code here, PostGIS code should use
1234  ** VARSIZE to set this for real.
1235  */
1236  g->size = return_size << 2;
1237 
1238  /* Set the SRID! */
1239  gserialized_set_srid(g, geom->srid);
1240 
1241  g->flags = geom->flags;
1242 
1243  return g;
1244 }
GBOX * bbox
Definition: liblwgeom.h:398
uint8_t flags
Definition: liblwgeom.h:397
static size_t gserialized_from_gbox(const GBOX *gbox, uint8_t *buf)
void gserialized_set_srid(GSERIALIZED *s, int32_t srid)
Write the SRID into the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:117
int32_t srid
Definition: liblwgeom.h:399
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:148
size_t gserialized_from_lwgeom_size(const LWGEOM *geom)
Calculate required memory segment to contain a serialized form of the LWGEOM.
Definition: g_serialized.c:810
uint32_t size
Definition: liblwgeom.h:381
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:648
static size_t gserialized_from_lwgeom_any(const LWGEOM *geom, uint8_t *buf)
void * lwalloc(size_t size)
Definition: lwutil.c:229
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1346
unsigned char uint8_t
Definition: uthash.h:79
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
Definition: lwgeom.c:1152
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
uint8_t flags
Definition: liblwgeom.h:383
Here is the call graph for this function:
Here is the caller graph for this function: