PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ ST_CollectionExtract()

Datum ST_CollectionExtract ( PG_FUNCTION_ARGS  )

Definition at line 2722 of file lwgeom_functions_basic.c.

2723 {
2724  GSERIALIZED *gser_in, *gser_out;
2725  LWGEOM *lwg_in = NULL;
2726  LWGEOM *lwg_out = NULL;
2727  int extype = 0;
2728 
2729  if (PG_NARGS() > 1)
2730  extype = PG_GETARG_INT32(1);
2731 
2732  /* Ensure the right type was input */
2733  if (!(extype == 0 || extype == POINTTYPE || extype == LINETYPE || extype == POLYGONTYPE))
2734  {
2735  elog(ERROR, "ST_CollectionExtract: only point, linestring and polygon may be extracted");
2736  PG_RETURN_NULL();
2737  }
2738 
2739  gser_in = PG_GETARG_GSERIALIZED_P(0);
2740  lwg_in = lwgeom_from_gserialized(gser_in);
2741 
2742  /* Mirror non-collections right back */
2743  if (!lwgeom_is_collection(lwg_in))
2744  {
2745  /* Non-collections of the matching type go back */
2746  if (lwg_in->type == extype || !extype)
2747  {
2748  lwgeom_free(lwg_in);
2749  PG_RETURN_POINTER(gser_in);
2750  }
2751  /* Others go back as EMPTY */
2752  else
2753  {
2754  lwg_out = lwgeom_construct_empty(extype, lwg_in->srid, lwgeom_has_z(lwg_in), lwgeom_has_m(lwg_in));
2755  PG_RETURN_POINTER(geometry_serialize(lwg_out));
2756  }
2757  }
2758 
2759  lwg_out = (LWGEOM*)lwcollection_extract((LWCOLLECTION*)lwg_in, extype);
2760 
2761  gser_out = geometry_serialize(lwg_out);
2762  lwgeom_free(lwg_in);
2763  lwgeom_free(lwg_out);
2764  PG_RETURN_POINTER(gser_out);
2765 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
#define LINETYPE
Definition: liblwgeom.h:117
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:917
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
LWCOLLECTION * lwcollection_extract(const LWCOLLECTION *col, uint32_t type)
Definition: lwcollection.c:432
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1080
#define POLYGONTYPE
Definition: liblwgeom.h:118
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:924
LWGEOM * lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwgeom.c:2082
uint8_t type
Definition: liblwgeom.h:476
int32_t srid
Definition: liblwgeom.h:474

References LINETYPE, lwcollection_extract(), lwgeom_construct_empty(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_has_m(), lwgeom_has_z(), lwgeom_is_collection(), POINTTYPE, POLYGONTYPE, LWGEOM::srid, and LWGEOM::type.

Here is the call graph for this function: