PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ gserialized1_from_lwgeom()

GSERIALIZED* gserialized1_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 1089 of file gserialized1.c.

1090 {
1091  size_t expected_size = 0;
1092  size_t return_size = 0;
1093  uint8_t *serialized = NULL;
1094  uint8_t *ptr = NULL;
1095  GSERIALIZED *g = NULL;
1096  assert(geom);
1097 
1098  /*
1099  ** See if we need a bounding box, add one if we don't have one.
1100  */
1101  if ( (! geom->bbox) && lwgeom_needs_bbox(geom) && (!lwgeom_is_empty(geom)) )
1102  {
1103  lwgeom_add_bbox(geom);
1104  }
1105 
1106  /*
1107  ** Harmonize the flags to the state of the lwgeom
1108  */
1109  if ( geom->bbox )
1110  FLAGS_SET_BBOX(geom->flags, 1);
1111  else
1112  FLAGS_SET_BBOX(geom->flags, 0);
1113 
1114  /* Set up the uint8_t buffer into which we are going to write the serialized geometry. */
1115  expected_size = gserialized1_from_lwgeom_size(geom);
1116  serialized = lwalloc(expected_size);
1117  ptr = serialized;
1118 
1119  /* Move past size, srid and flags. */
1120  ptr += 8;
1121 
1122  /* Write in the serialized form of the gbox, if necessary. */
1123  if ( geom->bbox )
1124  ptr += gserialized1_from_gbox(geom->bbox, ptr);
1125 
1126  /* Write in the serialized form of the geometry. */
1127  ptr += gserialized1_from_lwgeom_any(geom, ptr);
1128 
1129  /* Calculate size as returned by data processing functions. */
1130  return_size = ptr - serialized;
1131 
1132  if ( expected_size != return_size ) /* Uh oh! */
1133  {
1134  lwerror("Return size (%d) not equal to expected size (%d)!", return_size, expected_size);
1135  return NULL;
1136  }
1137 
1138  if ( size ) /* Return the output size to the caller if necessary. */
1139  *size = return_size;
1140 
1141  g = (GSERIALIZED*)serialized;
1142 
1143  /*
1144  ** We are aping PgSQL code here, PostGIS code should use
1145  ** VARSIZE to set this for real.
1146  */
1147  g->size = return_size << 2;
1148 
1149  /* Set the SRID! */
1150  gserialized1_set_srid(g, geom->srid);
1151 
1152  g->gflags = lwflags_get_g1flags(geom->flags);
1153 
1154  return g;
1155 }
static size_t gserialized1_from_lwgeom_any(const LWGEOM *geom, uint8_t *buf)
Definition: gserialized1.c:981
static size_t gserialized1_from_gbox(const GBOX *gbox, uint8_t *buf)
size_t gserialized1_from_lwgeom_size(const LWGEOM *geom)
Return the memory size a GSERIALIZED will occupy for a given LWGEOM.
Definition: gserialized1.c:721
uint8_t lwflags_get_g1flags(lwflags_t lwflags)
Definition: gserialized1.c:51
void gserialized1_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: gserialized1.c:159
#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
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, gserialized1_from_gbox(), gserialized1_from_lwgeom_any(), gserialized1_from_lwgeom_size(), gserialized1_set_srid(), lwalloc(), lwerror(), lwflags_get_g1flags(), lwgeom_add_bbox(), lwgeom_is_empty(), lwgeom_needs_bbox(), GSERIALIZED::size, and LWGEOM::srid.

Referenced by peek1_point_helper(), test_gbox_same_2d(), test_gserialized1_is_empty(), test_gserialized1_peek_gbox_p_gets_correct_box(), test_gserialized1_peek_gbox_p_no_box_when_empty(), test_lwgeom_from_gserialized(), and test_on_gser_lwgeom_count_vertices().

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