PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum LWGEOM_force_collection ( PG_FUNCTION_ARGS  )

Definition at line 426 of file lwgeom_functions_basic.c.

References LWGEOM::bbox, COLLECTIONTYPE, geometry_serialize(), gserialized_get_type(), gserialized_has_bbox(), lwcollection_construct(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_collection(), result, LWGEOM::srid, SRID_UNKNOWN, and LWGEOM::type.

427 {
428  GSERIALIZED *geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
430  LWGEOM **lwgeoms;
431  LWGEOM *lwgeom;
432  int srid;
433  GBOX *bbox;
434 
435  POSTGIS_DEBUG(2, "LWGEOM_force_collection called");
436 
437  /*
438  * This funx is a no-op only if a bbox cache is already present
439  * in input. If bbox cache is not there we'll need to handle
440  * automatic bbox addition FOR_COMPLEX_GEOMS.
441  */
442  if ( gserialized_get_type(geom) == COLLECTIONTYPE &&
443  gserialized_has_bbox(geom) )
444  {
445  PG_RETURN_POINTER(geom);
446  }
447 
448  /* deserialize into lwgeoms[0] */
449  lwgeom = lwgeom_from_gserialized(geom);
450 
451  /* alread a multi*, just make it a collection */
452  if ( lwgeom_is_collection(lwgeom) )
453  {
454  lwgeom->type = COLLECTIONTYPE;
455  }
456 
457  /* single geom, make it a collection */
458  else
459  {
460  srid = lwgeom->srid;
461  /* We transfer bbox ownership from input to output */
462  bbox = lwgeom->bbox;
463  lwgeom->srid = SRID_UNKNOWN;
464  lwgeom->bbox = NULL;
465  lwgeoms = palloc(sizeof(LWGEOM*));
466  lwgeoms[0] = lwgeom;
468  srid, bbox, 1,
469  lwgeoms);
470  }
471 
472  result = geometry_serialize(lwgeom);
473  lwgeom_free(lwgeom);
474 
475  PG_FREE_IF_COPY(geom, 0);
476  PG_RETURN_POINTER(result);
477 }
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:56
GBOX * bbox
Definition: liblwgeom.h:354
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:947
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:30
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
int gserialized_has_bbox(const GSERIALIZED *gser)
Check if a GSERIALIZED has a bounding box without deserializing first.
Definition: g_serialized.c:20
char ** result
Definition: liblwgeom.h:218
int32_t srid
Definition: liblwgeom.h:355
LWGEOM * geom
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:154
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition: liblwgeom.h:352
#define COLLECTIONTYPE
Definition: liblwgeom.h:66

Here is the call graph for this function: