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

◆ ST_CollectionExtract()

Datum ST_CollectionExtract ( PG_FUNCTION_ARGS  )

Definition at line 2943 of file lwgeom_functions_basic.c.

2944{
2945 GSERIALIZED *gser_in, *gser_out;
2946 LWGEOM *lwg_in = NULL;
2947 LWGEOM *lwg_out = NULL;
2948 int extype = 0;
2949
2950 if (PG_NARGS() > 1)
2951 extype = PG_GETARG_INT32(1);
2952
2953 /* Ensure the right type was input */
2954 if (!(extype == 0 || extype == POINTTYPE || extype == LINETYPE || extype == POLYGONTYPE))
2955 {
2956 elog(ERROR, "ST_CollectionExtract: only point, linestring and polygon may be extracted");
2957 PG_RETURN_NULL();
2958 }
2959
2960 gser_in = PG_GETARG_GSERIALIZED_P(0);
2961 lwg_in = lwgeom_from_gserialized(gser_in);
2962
2963 /* Mirror non-collections right back */
2964 if (!lwgeom_is_collection(lwg_in))
2965 {
2966 /* Non-collections of the matching type go back */
2967 if (lwg_in->type == extype || !extype)
2968 {
2969 lwgeom_free(lwg_in);
2970 PG_RETURN_POINTER(gser_in);
2971 }
2972 /* Others go back as EMPTY */
2973 else
2974 {
2975 lwg_out = lwgeom_construct_empty(extype, lwg_in->srid, lwgeom_has_z(lwg_in), lwgeom_has_m(lwg_in));
2976 PG_RETURN_POINTER(geometry_serialize(lwg_out));
2977 }
2978 }
2979
2980 lwg_out = (LWGEOM*)lwcollection_extract((LWCOLLECTION*)lwg_in, extype);
2981
2982 gser_out = geometry_serialize(lwg_out);
2983 lwgeom_free(lwg_in);
2984 lwgeom_free(lwg_out);
2985 PG_RETURN_POINTER(gser_out);
2986}
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1246
#define LINETYPE
Definition liblwgeom.h:103
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition lwgeom.c:962
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition liblwgeom.h:102
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM contains sub-geometries or not This basically just checks that the struct ...
Definition lwgeom.c:1125
#define POLYGONTYPE
Definition liblwgeom.h:104
LWGEOM * lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition lwgeom.c:2219
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition lwgeom.c:969
LWCOLLECTION * lwcollection_extract(const LWCOLLECTION *col, uint32_t type)
uint8_t type
Definition liblwgeom.h:462
int32_t srid
Definition liblwgeom.h:460

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: