PostGIS  2.4.9dev-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 369 of file lwcollection.c.

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

370 {
371  int i = 0;
372  LWGEOM **geomlist;
373  LWCOLLECTION *outcol;
374  int geomlistsize = 16;
375  int geomlistlen = 0;
376  uint8_t outtype;
377 
378  if ( ! col ) return NULL;
379 
380  switch (type)
381  {
382  case POINTTYPE:
383  outtype = MULTIPOINTTYPE;
384  break;
385  case LINETYPE:
386  outtype = MULTILINETYPE;
387  break;
388  case POLYGONTYPE:
389  outtype = MULTIPOLYGONTYPE;
390  break;
391  default:
392  lwerror("Only POLYGON, LINESTRING and POINT are supported by lwcollection_extract. %s requested.", lwtype_name(type));
393  return NULL;
394  }
395 
396  geomlist = lwalloc(sizeof(LWGEOM*) * geomlistsize);
397 
398  /* Process each sub-geometry */
399  for ( i = 0; i < col->ngeoms; i++ )
400  {
401  int subtype = col->geoms[i]->type;
402  /* Don't bother adding empty sub-geometries */
403  if ( lwgeom_is_empty(col->geoms[i]) )
404  {
405  continue;
406  }
407  /* Copy our sub-types into the output list */
408  if ( subtype == type )
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] = lwgeom_clone(col->geoms[i]);
417  geomlistlen++;
418  }
419  /* Recurse into sub-collections */
420  if ( lwtype_is_collection( subtype ) )
421  {
422  int j = 0;
424  for ( j = 0; j < tmpcol->ngeoms; j++ )
425  {
426  /* We've over-run our buffer, double the memory segment */
427  if ( geomlistlen == geomlistsize )
428  {
429  geomlistsize *= 2;
430  geomlist = lwrealloc(geomlist, sizeof(LWGEOM*) * geomlistsize);
431  }
432  geomlist[geomlistlen] = tmpcol->geoms[j];
433  geomlistlen++;
434  }
435  if (tmpcol->geoms)
436  lwfree(tmpcol->geoms);
437  if (tmpcol->bbox)
438  lwfree(tmpcol->bbox);
439  lwfree(tmpcol);
440  }
441  }
442 
443  if ( geomlistlen > 0 )
444  {
445  GBOX gbox;
446  outcol = lwcollection_construct(outtype, col->srid, NULL, geomlistlen, geomlist);
447  lwgeom_calculate_gbox((LWGEOM *) outcol, &gbox);
448  outcol->bbox = gbox_copy(&gbox);
449  }
450  else
451  {
452  lwfree(geomlist);
453  outcol = lwcollection_construct_empty(outtype, col->srid, FLAGS_GET_Z(col->flags), FLAGS_GET_M(col->flags));
454  }
455 
456  return outcol;
457 }
#define LINETYPE
Definition: liblwgeom.h:86
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:244
#define POLYGONTYPE
Definition: liblwgeom.h:87
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
GBOX * bbox
Definition: liblwgeom.h:505
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:701
uint8_t flags
Definition: liblwgeom.h:504
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:369
LWGEOM ** geoms
Definition: liblwgeom.h:509
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:1048
int32_t srid
Definition: liblwgeom.h:506
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:140
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:444
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:237
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:141
uint8_t type
Definition: liblwgeom.h:396
type
Definition: ovdump.py:41
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:1346
#define MULTILINETYPE
Definition: liblwgeom.h:89
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: