PostGIS  2.2.7dev-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 353 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 _lwt_AddLineEdge(), ST_CollectionExtract(), and test_lwcollection_extract().

354 {
355  int i = 0;
356  LWGEOM **geomlist;
357  LWCOLLECTION *outcol;
358  int geomlistsize = 16;
359  int geomlistlen = 0;
360  uint8_t outtype;
361 
362  if ( ! col ) return NULL;
363 
364  switch (type)
365  {
366  case POINTTYPE:
367  outtype = MULTIPOINTTYPE;
368  break;
369  case LINETYPE:
370  outtype = MULTILINETYPE;
371  break;
372  case POLYGONTYPE:
373  outtype = MULTIPOLYGONTYPE;
374  break;
375  default:
376  lwerror("Only POLYGON, LINESTRING and POINT are supported by lwcollection_extract. %s requested.", lwtype_name(type));
377  return NULL;
378  }
379 
380  geomlist = lwalloc(sizeof(LWGEOM*) * geomlistsize);
381 
382  /* Process each sub-geometry */
383  for ( i = 0; i < col->ngeoms; i++ )
384  {
385  int subtype = col->geoms[i]->type;
386  /* Don't bother adding empty sub-geometries */
387  if ( lwgeom_is_empty(col->geoms[i]) )
388  {
389  continue;
390  }
391  /* Copy our sub-types into the output list */
392  if ( subtype == type )
393  {
394  /* We've over-run our buffer, double the memory segment */
395  if ( geomlistlen == geomlistsize )
396  {
397  geomlistsize *= 2;
398  geomlist = lwrealloc(geomlist, sizeof(LWGEOM*) * geomlistsize);
399  }
400  geomlist[geomlistlen] = lwgeom_clone(col->geoms[i]);
401  geomlistlen++;
402  }
403  /* Recurse into sub-collections */
404  if ( lwtype_is_collection( subtype ) )
405  {
406  int j = 0;
407  LWCOLLECTION *tmpcol = lwcollection_extract((LWCOLLECTION*)col->geoms[i], type);
408  for ( j = 0; j < tmpcol->ngeoms; j++ )
409  {
410  /* We've over-run our buffer, double the memory segment */
411  if ( geomlistlen == geomlistsize )
412  {
413  geomlistsize *= 2;
414  geomlist = lwrealloc(geomlist, sizeof(LWGEOM*) * geomlistsize);
415  }
416  geomlist[geomlistlen] = tmpcol->geoms[j];
417  geomlistlen++;
418  }
419  lwfree(tmpcol);
420  }
421  }
422 
423  if ( geomlistlen > 0 )
424  {
425  GBOX gbox;
426  outcol = lwcollection_construct(outtype, col->srid, NULL, geomlistlen, geomlist);
427  lwgeom_calculate_gbox((LWGEOM *) outcol, &gbox);
428  outcol->bbox = gbox_copy(&gbox);
429  }
430  else
431  {
432  lwfree(geomlist);
433  outcol = lwcollection_construct_empty(outtype, col->srid, FLAGS_GET_Z(col->flags), FLAGS_GET_M(col->flags));
434  }
435 
436  return outcol;
437 }
#define LINETYPE
Definition: liblwgeom.h:71
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:403
void lwfree(void *mem)
Definition: lwutil.c:214
#define POLYGONTYPE
Definition: liblwgeom.h:72
#define MULTIPOINTTYPE
Definition: liblwgeom.h:73
GBOX * bbox
Definition: liblwgeom.h:489
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:81
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:652
uint8_t flags
Definition: liblwgeom.h:488
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:188
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:353
LWGEOM ** geoms
Definition: liblwgeom.h:493
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:999
int32_t srid
Definition: liblwgeom.h:490
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:124
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:395
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:207
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:125
uint8_t type
Definition: liblwgeom.h:380
void * lwalloc(size_t size)
Definition: lwutil.c:199
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:1297
#define MULTILINETYPE
Definition: liblwgeom.h:74
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74

Here is the call graph for this function:

Here is the caller graph for this function: