PostGIS  2.1.10dev-r@@SVN_REVISION@@
LWCOLLECTION* lwcollection_extract ( LWCOLLECTION col,
int  type 
)

Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of the specified type.

Definition at line 343 of file lwcollection.c.

References LWCOLLECTION::bbox, LWCOLLECTION::flags, FLAGS_GET_M, FLAGS_GET_Z, gbox_copy(), LWCOLLECTION::geoms, LINETYPE, lwalloc(), lwcollection_construct(), lwcollection_construct_empty(), lwerror(), lwfree(), lwgeom_calculate_gbox(), lwgeom_clone(), lwgeom_is_empty(), lwrealloc(), lwtype_is_collection(), lwtype_name(), MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, LWCOLLECTION::ngeoms, POINTTYPE, POLYGONTYPE, LWCOLLECTION::srid, and LWGEOM::type.

Referenced by ST_CollectionExtract(), and test_lwcollection_extract().

344 {
345  int i = 0;
346  LWGEOM **geomlist;
347  LWCOLLECTION *outcol;
348  int geomlistsize = 16;
349  int geomlistlen = 0;
350  uint8_t outtype;
351 
352  if ( ! col ) return NULL;
353 
354  switch (type)
355  {
356  case POINTTYPE:
357  outtype = MULTIPOINTTYPE;
358  break;
359  case LINETYPE:
360  outtype = MULTILINETYPE;
361  break;
362  case POLYGONTYPE:
363  outtype = MULTIPOLYGONTYPE;
364  break;
365  default:
366  lwerror("Only POLYGON, LINESTRING and POINT are supported by lwcollection_extract. %s requested.", lwtype_name(type));
367  return NULL;
368  }
369 
370  geomlist = lwalloc(sizeof(LWGEOM*) * geomlistsize);
371 
372  /* Process each sub-geometry */
373  for ( i = 0; i < col->ngeoms; i++ )
374  {
375  int subtype = col->geoms[i]->type;
376  /* Don't bother adding empty sub-geometries */
377  if ( lwgeom_is_empty(col->geoms[i]) )
378  {
379  continue;
380  }
381  /* Copy our sub-types into the output list */
382  if ( subtype == type )
383  {
384  /* We've over-run our buffer, double the memory segment */
385  if ( geomlistlen == geomlistsize )
386  {
387  geomlistsize *= 2;
388  geomlist = lwrealloc(geomlist, sizeof(LWGEOM*) * geomlistsize);
389  }
390  geomlist[geomlistlen] = lwgeom_clone(col->geoms[i]);
391  geomlistlen++;
392  }
393  /* Recurse into sub-collections */
394  if ( lwtype_is_collection( subtype ) )
395  {
396  int j = 0;
397  LWCOLLECTION *tmpcol = lwcollection_extract((LWCOLLECTION*)col->geoms[i], type);
398  for ( j = 0; j < tmpcol->ngeoms; j++ )
399  {
400  /* We've over-run our buffer, double the memory segment */
401  if ( geomlistlen == geomlistsize )
402  {
403  geomlistsize *= 2;
404  geomlist = lwrealloc(geomlist, sizeof(LWGEOM*) * geomlistsize);
405  }
406  geomlist[geomlistlen] = tmpcol->geoms[j];
407  geomlistlen++;
408  }
409  lwfree(tmpcol);
410  }
411  }
412 
413  if ( geomlistlen > 0 )
414  {
415  GBOX gbox;
416  outcol = lwcollection_construct(outtype, col->srid, NULL, geomlistlen, geomlist);
417  lwgeom_calculate_gbox((LWGEOM *) outcol, &gbox);
418  outcol->bbox = gbox_copy(&gbox);
419  }
420  else
421  {
422  lwfree(geomlist);
423  outcol = lwcollection_construct_empty(outtype, col->srid, FLAGS_GET_Z(col->flags), FLAGS_GET_M(col->flags));
424  }
425 
426  return outcol;
427 }
#define LINETYPE
Definition: liblwgeom.h:61
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:362
void lwfree(void *mem)
Definition: lwutil.c:190
#define POLYGONTYPE
Definition: liblwgeom.h:62
#define MULTIPOINTTYPE
Definition: liblwgeom.h:63
GBOX * bbox
Definition: liblwgeom.h:461
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:81
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
Calculate bounding box of a geometry, automatically taking into account whether it is cartesian or ge...
Definition: lwgeom.c:608
uint8_t flags
Definition: liblwgeom.h:460
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:30
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:164
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 ** geoms
Definition: liblwgeom.h:465
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:955
int32_t srid
Definition: liblwgeom.h:462
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:106
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:351
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:65
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:183
#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
void * lwalloc(size_t size)
Definition: lwutil.c:175
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
#define MULTILINETYPE
Definition: liblwgeom.h:64

Here is the call graph for this function:

Here is the caller graph for this function: