PostGIS  3.0.6dev-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 387 of file lwcollection.c.

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

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: