PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ combine_geometries()

static int combine_geometries ( UNIONFIND uf,
void **  geoms,
uint32_t  num_geoms,
void ***  clusterGeoms,
uint32_t *  num_clusters,
char  is_lwgeom 
)
static

Uses a UNIONFIND to identify the set with which each input geometry is associated, and groups the geometries into GeometryCollections.

Supplied geometry array may be of either LWGEOM* or GEOSGeometry*; is_lwgeom is used to identify which. Caller is responsible for freeing input geometry array but not the items contained within it.

Definition at line 337 of file lwgeom_geos_cluster.c.

References COLLECTIONTYPE, LW_FAILURE, LW_SUCCESS, lwalloc(), lwcollection_construct(), lwfree(), UNIONFIND::num_clusters, UF_find(), and UF_ordered_by_cluster().

Referenced by cluster_intersecting(), and cluster_within_distance().

338 {
339  size_t i, j, k;
340 
341  /* Combine components of each cluster into their own GeometryCollection */
342  *num_clusters = uf->num_clusters;
343  *clusterGeoms = lwalloc(*num_clusters * sizeof(void*));
344 
345  void** geoms_in_cluster = lwalloc(num_geoms * sizeof(void*));
346  uint32_t* ordered_components = UF_ordered_by_cluster(uf);
347  for (i = 0, j = 0, k = 0; i < num_geoms; i++)
348  {
349  geoms_in_cluster[j++] = geoms[ordered_components[i]];
350 
351  /* Is this the last geometry in the component? */
352  if ((i == num_geoms - 1) ||
353  (UF_find(uf, ordered_components[i]) != UF_find(uf, ordered_components[i+1])))
354  {
355  if (k >= uf->num_clusters) {
356  /* Should not get here - it means that we have more clusters than uf->num_clusters thinks we should. */
357  return LW_FAILURE;
358  }
359 
360  if (is_lwgeom)
361  {
362  LWGEOM** components = lwalloc(j * sizeof(LWGEOM*));
363  memcpy(components, geoms_in_cluster, j * sizeof(LWGEOM*));
364  (*clusterGeoms)[k++] = lwcollection_construct(COLLECTIONTYPE, components[0]->srid, NULL, j, (LWGEOM**) components);
365  }
366  else
367  {
368  int srid = GEOSGetSRID(((GEOSGeometry**) geoms_in_cluster)[0]);
369  GEOSGeometry* combined = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION, (GEOSGeometry**) geoms_in_cluster, j);
370  GEOSSetSRID(combined, srid);
371  (*clusterGeoms)[k++] = combined;
372  }
373  j = 0;
374  }
375  }
376 
377  lwfree(geoms_in_cluster);
378  lwfree(ordered_components);
379 
380  return LW_SUCCESS;
381 }
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:30
void lwfree(void *mem)
Definition: lwutil.c:214
#define LW_SUCCESS
Definition: liblwgeom.h:65
#define LW_FAILURE
Definition: liblwgeom.h:64
uint32_t UF_find(UNIONFIND *uf, uint32_t i)
Definition: lwunionfind.c:48
uint32_t * UF_ordered_by_cluster(UNIONFIND *uf)
Definition: lwunionfind.c:93
uint32_t num_clusters
Definition: lwunionfind.h:22
void * lwalloc(size_t size)
Definition: lwutil.c:199
#define COLLECTIONTYPE
Definition: liblwgeom.h:76
Here is the call graph for this function:
Here is the caller graph for this function: