PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ ST_CollectionExtract()

Datum ST_CollectionExtract ( PG_FUNCTION_ARGS  )

Definition at line 2715 of file lwgeom_functions_basic.c.

2716{
2717 GSERIALIZED *input = PG_GETARG_GSERIALIZED_P(0);
2718 GSERIALIZED *output;
2719 LWGEOM *lwgeom = lwgeom_from_gserialized(input);
2720 LWGEOM *lwcol = NULL;
2721 int type = PG_GETARG_INT32(1);
2722 int lwgeom_type = lwgeom->type;
2723
2724 /* Ensure the right type was input */
2725 if (!(type == POINTTYPE || type == LINETYPE || type == POLYGONTYPE))
2726 {
2727 lwgeom_free(lwgeom);
2728 elog(ERROR, "ST_CollectionExtract: only point, linestring and polygon may be extracted");
2729 PG_RETURN_NULL();
2730 }
2731
2732 /* Mirror non-collections right back */
2733 if (!lwgeom_is_collection(lwgeom))
2734 {
2735 /* Non-collections of the matching type go back */
2736 if (lwgeom_type == type)
2737 {
2738 lwgeom_free(lwgeom);
2739 PG_RETURN_POINTER(input);
2740 }
2741 /* Others go back as EMPTY */
2742 else
2743 {
2744 lwcol = lwgeom_construct_empty(
2745 type, lwgeom->srid, lwgeom_has_z(lwgeom), lwgeom_has_m(lwgeom));
2746 }
2747 }
2748 else
2749 {
2751 }
2752
2753 output = geometry_serialize((LWGEOM *)lwcol);
2754 lwgeom_free(lwgeom);
2755 lwgeom_free(lwcol);
2756
2757 PG_RETURN_POINTER(output);
2758}
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
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:916
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition liblwgeom.h:116
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition lwgeom.c:1079
#define POLYGONTYPE
Definition liblwgeom.h:118
LWGEOM * lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition lwgeom.c:2083
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition lwgeom.c:923
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition lwgeom.c:291
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition liblwgeom.h:448
int32_t srid
Definition liblwgeom.h:446

References geometry_serialize(), LINETYPE, lwcollection_as_lwgeom(), 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: