PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ ST_CollectionExtract()

Datum ST_CollectionExtract ( PG_FUNCTION_ARGS  )

Definition at line 2554 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(), lwgeom_is_empty(), PG_FUNCTION_INFO_V1(), POINTTYPE, POLYGONTYPE, LWGEOM::srid, ST_CollectionHomogenize(), ovdump::type, and LWGEOM::type.

Referenced by ST_GeoHash().

2555 {
2556  GSERIALIZED *input = PG_GETARG_GSERIALIZED_P(0);
2557  GSERIALIZED *output;
2558  LWGEOM *lwgeom = lwgeom_from_gserialized(input);
2559  LWGEOM *lwcol = NULL;
2560  int type = PG_GETARG_INT32(1);
2561  int lwgeom_type = lwgeom->type;
2562 
2563  /* Ensure the right type was input */
2564  if ( ! ( type == POINTTYPE || type == LINETYPE || type == POLYGONTYPE ) )
2565  {
2566  lwgeom_free(lwgeom);
2567  elog(ERROR, "ST_CollectionExtract: only point, linestring and polygon may be extracted");
2568  PG_RETURN_NULL();
2569  }
2570 
2571  /* Mirror non-collections right back */
2572  if ( ! lwgeom_is_collection(lwgeom) )
2573  {
2574  /* Non-collections of the matching type go back */
2575  if(lwgeom_type == type)
2576  {
2577  lwgeom_free(lwgeom);
2578  PG_RETURN_POINTER(input);
2579  }
2580  /* Others go back as EMPTY */
2581  else
2582  {
2583  lwcol = lwgeom_construct_empty(type, lwgeom->srid, FLAGS_GET_Z(lwgeom->flags), FLAGS_GET_M(lwgeom->flags));
2584  }
2585  }
2586  else
2587  {
2588  lwcol = lwcollection_as_lwgeom(lwcollection_extract((LWCOLLECTION*)lwgeom, type));
2589  }
2590 
2591 #if 0
2593  {
2594  lwgeom_free(lwgeom);
2595  PG_RETURN_NULL();
2596  }
2597 #endif
2598  output = geometry_serialize((LWGEOM*)lwcol);
2599  lwgeom_free(lwgeom);
2600  lwgeom_free(lwcol);
2601 
2602  PG_RETURN_POINTER(output);
2603 }
#define LINETYPE
Definition: liblwgeom.h:86
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1040
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:369
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:87
uint8_t flags
Definition: liblwgeom.h:397
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
int32_t srid
Definition: liblwgeom.h:399
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:140
LWGEOM * lwgeom_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwgeom.c:1851
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:141
uint8_t type
Definition: liblwgeom.h:396
type
Definition: ovdump.py:41
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1346
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:268
Here is the call graph for this function:
Here is the caller graph for this function: