PostGIS  2.5.2dev-r@@SVN_REVISION@@

◆ ST_CollectionExtract()

Datum ST_CollectionExtract ( PG_FUNCTION_ARGS  )

Definition at line 2689 of file lwgeom_functions_basic.c.

References LWGEOM::flags, FLAGS_GET_M, FLAGS_GET_Z, geometry_serialize(), LINETYPE, lwcollection_as_lwgeom(), lwcollection_extract(), lwgeom_construct_empty(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_collection(), PG_FUNCTION_INFO_V1(), POINTTYPE, POLYGONTYPE, LWGEOM::srid, ST_CollectionHomogenize(), ovdump::type, and LWGEOM::type.

Referenced by ST_GeoHash().

2690 {
2691  GSERIALIZED *input = PG_GETARG_GSERIALIZED_P(0);
2692  GSERIALIZED *output;
2693  LWGEOM *lwgeom = lwgeom_from_gserialized(input);
2694  LWGEOM *lwcol = NULL;
2695  int type = PG_GETARG_INT32(1);
2696  int lwgeom_type = lwgeom->type;
2697 
2698  /* Ensure the right type was input */
2699  if ( ! ( type == POINTTYPE || type == LINETYPE || type == POLYGONTYPE ) )
2700  {
2701  lwgeom_free(lwgeom);
2702  elog(ERROR, "ST_CollectionExtract: only point, linestring and polygon may be extracted");
2703  PG_RETURN_NULL();
2704  }
2705 
2706  /* Mirror non-collections right back */
2707  if ( ! lwgeom_is_collection(lwgeom) )
2708  {
2709  /* Non-collections of the matching type go back */
2710  if(lwgeom_type == type)
2711  {
2712  lwgeom_free(lwgeom);
2713  PG_RETURN_POINTER(input);
2714  }
2715  /* Others go back as EMPTY */
2716  else
2717  {
2718  lwcol = lwgeom_construct_empty(type, lwgeom->srid, FLAGS_GET_Z(lwgeom->flags), FLAGS_GET_M(lwgeom->flags));
2719  }
2720  }
2721  else
2722  {
2723  lwcol = lwcollection_as_lwgeom(lwcollection_extract((LWCOLLECTION*)lwgeom, type));
2724  }
2725 
2726  output = geometry_serialize((LWGEOM*)lwcol);
2727  lwgeom_free(lwgeom);
2728  lwgeom_free(lwcol);
2729 
2730  PG_RETURN_POINTER(output);
2731 }
#define LINETYPE
Definition: liblwgeom.h:85
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1085
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:386
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:86
uint8_t flags
Definition: liblwgeom.h:399
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
int32_t srid
Definition: liblwgeom.h:401
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
LWGEOM * lwgeom_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwgeom.c:2085
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
uint8_t type
Definition: liblwgeom.h:398
type
Definition: ovdump.py:41
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:300
Here is the call graph for this function:
Here is the caller graph for this function: