PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ lwcollection_extract()

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 386 of file lwcollection.c.

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

References LWGEOM::bbox, LWCOLLECTION::bbox, LWCOLLECTION::flags, FLAGS_GET_M, FLAGS_GET_Z, gbox_copy(), LWCOLLECTION::geoms, LINETYPE, lwalloc(), lwcollection_construct(), lwcollection_construct_empty(), lwcollection_extract(), 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, LWGEOM::type, and ovdump::type.

Referenced by _lwt_AddLineEdge(), lwcollection_extract(), lwgeom_to_basic_type(), ST_CollectionExtract(), and test_lwcollection_extract().

Here is the call graph for this function:
Here is the caller graph for this function: