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

Definition at line 366 of file lwcollection.c.

References LWGEOM::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().

367 {
368  int i = 0;
369  LWGEOM **geomlist;
370  LWCOLLECTION *outcol;
371  int geomlistsize = 16;
372  int geomlistlen = 0;
373  uint8_t outtype;
374 
375  if ( ! col ) return NULL;
376 
377  switch (type)
378  {
379  case POINTTYPE:
380  outtype = MULTIPOINTTYPE;
381  break;
382  case LINETYPE:
383  outtype = MULTILINETYPE;
384  break;
385  case POLYGONTYPE:
386  outtype = MULTIPOLYGONTYPE;
387  break;
388  default:
389  lwerror("Only POLYGON, LINESTRING and POINT are supported by lwcollection_extract. %s requested.", lwtype_name(type));
390  return NULL;
391  }
392 
393  geomlist = lwalloc(sizeof(LWGEOM*) * geomlistsize);
394 
395  /* Process each sub-geometry */
396  for ( i = 0; i < col->ngeoms; i++ )
397  {
398  int subtype = col->geoms[i]->type;
399  /* Don't bother adding empty sub-geometries */
400  if ( lwgeom_is_empty(col->geoms[i]) )
401  {
402  continue;
403  }
404  /* Copy our sub-types into the output list */
405  if ( subtype == type )
406  {
407  /* We've over-run our buffer, double the memory segment */
408  if ( geomlistlen == geomlistsize )
409  {
410  geomlistsize *= 2;
411  geomlist = lwrealloc(geomlist, sizeof(LWGEOM*) * geomlistsize);
412  }
413  geomlist[geomlistlen] = lwgeom_clone(col->geoms[i]);
414  geomlistlen++;
415  }
416  /* Recurse into sub-collections */
417  if ( lwtype_is_collection( subtype ) )
418  {
419  int j = 0;
421  for ( j = 0; j < tmpcol->ngeoms; j++ )
422  {
423  /* We've over-run our buffer, double the memory segment */
424  if ( geomlistlen == geomlistsize )
425  {
426  geomlistsize *= 2;
427  geomlist = lwrealloc(geomlist, sizeof(LWGEOM*) * geomlistsize);
428  }
429  geomlist[geomlistlen] = tmpcol->geoms[j];
430  geomlistlen++;
431  }
432  lwfree(tmpcol);
433  }
434  }
435 
436  if ( geomlistlen > 0 )
437  {
438  GBOX gbox;
439  outcol = lwcollection_construct(outtype, col->srid, NULL, geomlistlen, geomlist);
440  lwgeom_calculate_gbox((LWGEOM *) outcol, &gbox);
441  outcol->bbox = gbox_copy(&gbox);
442  }
443  else
444  {
445  lwfree(geomlist);
446  outcol = lwcollection_construct_empty(outtype, col->srid, FLAGS_GET_Z(col->flags), FLAGS_GET_M(col->flags));
447  }
448 
449  return outcol;
450 }
#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:438
void lwfree(void *mem)
Definition: lwutil.c:242
#define POLYGONTYPE
Definition: liblwgeom.h:86
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
GBOX * bbox
Definition: liblwgeom.h:504
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
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:665
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:216
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:366
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:1012
int32_t srid
Definition: liblwgeom.h:505
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:139
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:408
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:235
#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
type
Definition: ovdump.py:41
void * lwalloc(size_t size)
Definition: lwutil.c:227
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:1310
#define MULTILINETYPE
Definition: liblwgeom.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
Here is the call graph for this function:
Here is the caller graph for this function: