PostGIS  2.5.0dev-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.

WARNING: the output will contain references to geometries in the input, so the result must be carefully released, not freed.

Definition at line 372 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(), lwgeom_to_basic_type(), ST_CollectionExtract(), and test_lwcollection_extract().

373 {
374  uint32_t i = 0;
375  LWGEOM** geomlist;
376  LWCOLLECTION* outcol;
377  int geomlistsize = 16;
378  int geomlistlen = 0;
379  uint8_t outtype;
380 
381  if (!col) return NULL;
382 
383  switch (type)
384  {
385  case POINTTYPE:
386  outtype = MULTIPOINTTYPE;
387  break;
388  case LINETYPE:
389  outtype = MULTILINETYPE;
390  break;
391  case POLYGONTYPE:
392  outtype = MULTIPOLYGONTYPE;
393  break;
394  default:
395  lwerror(
396  "Only POLYGON, LINESTRING and POINT are supported by "
397  "lwcollection_extract. %s requested.",
398  lwtype_name(type));
399  return NULL;
400  }
401 
402  geomlist = lwalloc(sizeof(LWGEOM*) * geomlistsize);
403 
404  /* Process each sub-geometry */
405  for (i = 0; i < col->ngeoms; i++)
406  {
407  int subtype = col->geoms[i]->type;
408  /* Don't bother adding empty sub-geometries */
409  if (lwgeom_is_empty(col->geoms[i])) continue;
410  /* Copy our sub-types into the output list */
411  if (subtype == type)
412  {
413  /* We've over-run our buffer, double the memory segment
414  */
415  if (geomlistlen == geomlistsize)
416  {
417  geomlistsize *= 2;
418  geomlist = lwrealloc(
419  geomlist, sizeof(LWGEOM*) * geomlistsize);
420  }
421  geomlist[geomlistlen] = lwgeom_clone(col->geoms[i]);
422  geomlistlen++;
423  }
424  /* Recurse into sub-collections */
425  if (lwtype_is_collection(subtype))
426  {
427  uint32_t j = 0;
429  (LWCOLLECTION*)col->geoms[i], type);
430  for (j = 0; j < tmpcol->ngeoms; j++)
431  {
432  /* We've over-run our buffer, double the memory
433  * segment */
434  if (geomlistlen == geomlistsize)
435  {
436  geomlistsize *= 2;
437  geomlist = lwrealloc(geomlist,
438  sizeof(LWGEOM*) *
439  geomlistsize);
440  }
441  geomlist[geomlistlen] = tmpcol->geoms[j];
442  geomlistlen++;
443  }
444  if (tmpcol->ngeoms) lwfree(tmpcol->geoms);
445  if (tmpcol->bbox) lwfree(tmpcol->bbox);
446  lwfree(tmpcol);
447  }
448  }
449 
450  if (geomlistlen > 0)
451  {
452  GBOX gbox;
453  outcol = lwcollection_construct(
454  outtype, col->srid, NULL, geomlistlen, geomlist);
455  lwgeom_calculate_gbox((LWGEOM*)outcol, &gbox);
456  outcol->bbox = gbox_copy(&gbox);
457  }
458  else
459  {
460  lwfree(geomlist);
461  outcol = lwcollection_construct_empty(outtype,
462  col->srid,
463  FLAGS_GET_Z(col->flags),
464  FLAGS_GET_M(col->flags));
465  }
466 
467  return outcol;
468 }
#define LINETYPE
Definition: liblwgeom.h:85
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:433
void lwfree(void *mem)
Definition: lwutil.c:244
#define POLYGONTYPE
Definition: liblwgeom.h:86
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
GBOX * bbox
Definition: liblwgeom.h:504
uint32_t ngeoms
Definition: liblwgeom.h:506
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
unsigned int uint32_t
Definition: uthash.h:78
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:739
uint8_t flags
Definition: liblwgeom.h:503
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:372
LWGEOM ** geoms
Definition: liblwgeom.h:508
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:1086
int32_t srid
Definition: liblwgeom.h:505
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:475
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:237
#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:395
void * lwalloc(size_t size)
Definition: lwutil.c:229
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:1386
#define MULTILINETYPE
Definition: liblwgeom.h:88
unsigned char uint8_t
Definition: uthash.h:79
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190

Here is the call graph for this function:

Here is the caller graph for this function: