PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ 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 560 of file lwgeom_geos_cluster.c.

561{
562 size_t i, j, k;
563
564 /* Combine components of each cluster into their own GeometryCollection */
565 *num_clusters = uf->num_clusters;
566 *clusterGeoms = lwalloc(*num_clusters * sizeof(void*));
567
568 void** geoms_in_cluster = lwalloc(num_geoms * sizeof(void*));
569 uint32_t* ordered_components = UF_ordered_by_cluster(uf);
570 for (i = 0, j = 0, k = 0; i < num_geoms; i++)
571 {
572 geoms_in_cluster[j++] = geoms[ordered_components[i]];
573
574 /* Is this the last geometry in the component? */
575 if ((i == num_geoms - 1) ||
576 (UF_find(uf, ordered_components[i]) != UF_find(uf, ordered_components[i+1])))
577 {
578 if (k >= uf->num_clusters) {
579 /* Should not get here - it means that we have more clusters than uf->num_clusters thinks we should. */
580 return LW_FAILURE;
581 }
582
583 if (is_lwgeom)
584 {
585 LWGEOM** components = lwalloc(j * sizeof(LWGEOM*));
586 memcpy(components, geoms_in_cluster, j * sizeof(LWGEOM*));
587 (*clusterGeoms)[k++] = lwcollection_construct(COLLECTIONTYPE, components[0]->srid, NULL, j, (LWGEOM**) components);
588 }
589 else
590 {
591 int srid = GEOSGetSRID(((GEOSGeometry**) geoms_in_cluster)[0]);
592 GEOSGeometry* combined = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION, (GEOSGeometry**) geoms_in_cluster, j);
593 GEOSSetSRID(combined, srid);
594 (*clusterGeoms)[k++] = combined;
595 }
596 j = 0;
597 }
598 }
599
600 lwfree(geoms_in_cluster);
601 lwfree(ordered_components);
602
603 return LW_SUCCESS;
604}
LWCOLLECTION * lwcollection_construct(uint8_t type, int32_t srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
#define COLLECTIONTYPE
Definition liblwgeom.h:108
#define LW_FAILURE
Definition liblwgeom.h:96
#define LW_SUCCESS
Definition liblwgeom.h:97
void * lwalloc(size_t size)
Definition lwutil.c:227
void lwfree(void *mem)
Definition lwutil.c:248
uint32_t UF_find(UNIONFIND *uf, uint32_t i)
Definition lwunionfind.c:62
uint32_t * UF_ordered_by_cluster(UNIONFIND *uf)
uint32_t num_clusters
Definition lwunionfind.h:35

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().

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