PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum ST_CollectionExtract ( PG_FUNCTION_ARGS  )

Definition at line 2595 of file lwgeom_functions_basic.c.

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

2596 {
2597  GSERIALIZED *input = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2598  GSERIALIZED *output;
2599  LWGEOM *lwgeom = lwgeom_from_gserialized(input);
2600  LWGEOM *lwcol = NULL;
2601  int type = PG_GETARG_INT32(1);
2602  int lwgeom_type = lwgeom->type;
2603 
2604  /* Ensure the right type was input */
2605  if ( ! ( type == POINTTYPE || type == LINETYPE || type == POLYGONTYPE ) )
2606  {
2607  lwgeom_free(lwgeom);
2608  elog(ERROR, "ST_CollectionExtract: only point, linestring and polygon may be extracted");
2609  PG_RETURN_NULL();
2610  }
2611 
2612  /* Mirror non-collections right back */
2613  if ( ! lwgeom_is_collection(lwgeom) )
2614  {
2615  /* Non-collections of the matching type go back */
2616  if(lwgeom_type == type)
2617  {
2618  lwgeom_free(lwgeom);
2619  PG_RETURN_POINTER(input);
2620  }
2621  /* Others go back as EMPTY */
2622  else
2623  {
2624  lwcol = lwgeom_construct_empty(type, lwgeom->srid, FLAGS_GET_Z(lwgeom->flags), FLAGS_GET_M(lwgeom->flags));
2625  }
2626  }
2627  else
2628  {
2629  lwcol = lwcollection_as_lwgeom(lwcollection_extract((LWCOLLECTION*)lwgeom, type));
2630  }
2631 
2632 #if 0
2634  {
2635  lwgeom_free(lwgeom);
2636  PG_RETURN_NULL();
2637  }
2638 #endif
2639  output = geometry_serialize((LWGEOM*)lwcol);
2640  lwgeom_free(lwgeom);
2641  lwgeom_free(lwcol);
2642 
2643  PG_RETURN_POINTER(output);
2644 }
#define LINETYPE
Definition: liblwgeom.h:61
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:947
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:343
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:62
uint8_t flags
Definition: liblwgeom.h:353
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
static int input(void)
int32_t srid
Definition: liblwgeom.h:355
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:106
LWGEOM * lwgeom_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwgeom.c:1662
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:60
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:107
uint8_t type
Definition: liblwgeom.h:352
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:1229
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:219

Here is the call graph for this function: