PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ST_ClusterWithinWin()

Datum ST_ClusterWithinWin ( PG_FUNCTION_ARGS  )

Definition at line 212 of file lwgeom_window.c.

213 {
214  WindowObject win_obj = PG_WINDOW_OBJECT();
215  uint32_t row = WinGetCurrentPosition(win_obj);
216  uint32_t ngeoms = WinGetPartitionRowCount(win_obj);
217  cluster_context* context = fetch_cluster_context(win_obj, ngeoms);
218 
219  if (row == 0) /* beginning of the partition; do all of the work now */
220  {
221  uint32_t i;
222  uint32_t* result_ids;
223  LWGEOM** geoms;
224  UNIONFIND* uf;
225  bool tolerance_is_null;
226  double tolerance = DatumGetFloat8(WinGetFuncArgCurrent(win_obj, 1, &tolerance_is_null));
227 
228  /* Validate input parameters */
229  if (tolerance_is_null || tolerance < 0)
230  {
231  lwpgerror("Tolerance must be a positive number", tolerance);
232  PG_RETURN_NULL();
233  }
234 
235  context->is_error = LW_TRUE; /* until proven otherwise */
236 
237  geoms = lwalloc(ngeoms * sizeof(LWGEOM*));
238  uf = UF_create(ngeoms);
239  for (i = 0; i < ngeoms; i++)
240  {
241  bool geom_is_null;
242  geoms[i] = read_lwgeom_from_partition(win_obj, i, &geom_is_null);
243  context->clusters[i].is_null = geom_is_null;
244 
245  if (!geoms[i])
246  {
247  lwpgerror("Error reading geometry.");
248  PG_RETURN_NULL();
249  }
250  }
251 
252  initGEOS(lwpgnotice, lwgeom_geos_error);
253 
254  if (union_dbscan(geoms, ngeoms, uf, tolerance, 1, NULL) == LW_SUCCESS)
255  context->is_error = LW_FALSE;
256 
257  for (i = 0; i < ngeoms; i++)
258  {
259  lwgeom_free(geoms[i]);
260  }
261  lwfree(geoms);
262 
263  if (context->is_error)
264  {
265  UF_destroy(uf);
266  lwpgerror("Error during clustering");
267  PG_RETURN_NULL();
268  }
269 
270  result_ids = UF_get_collapsed_cluster_ids(uf, NULL);
271  for (i = 0; i < ngeoms; i++)
272  {
273  context->clusters[i].cluster_id = result_ids[i];
274  }
275 
276  lwfree(result_ids);
277  UF_destroy(uf);
278  }
279 
280  if (context->clusters[row].is_null)
281  PG_RETURN_NULL();
282 
283  PG_RETURN_INT32(context->clusters[row].cluster_id);
284 }
void lwgeom_geos_error(const char *fmt,...)
int union_dbscan(LWGEOM **geoms, uint32_t num_geoms, UNIONFIND *uf, double eps, uint32_t min_points, char **is_in_cluster_ret)
#define LW_FALSE
Definition: liblwgeom.h:94
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define LW_SUCCESS
Definition: liblwgeom.h:97
void lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
static cluster_context * fetch_cluster_context(WindowObject win_obj, uint32_t ngeoms)
Definition: lwgeom_window.c:63
static LWGEOM * read_lwgeom_from_partition(WindowObject win_obj, uint32_t i, bool *is_null)
Definition: lwgeom_window.c:71
uint32_t * UF_get_collapsed_cluster_ids(UNIONFIND *uf, const char *is_in_cluster)
Definition: lwunionfind.c:146
void UF_destroy(UNIONFIND *uf)
Definition: lwunionfind.c:54
UNIONFIND * UF_create(uint32_t N)
Definition: lwunionfind.c:35
cluster_entry clusters[1]
Definition: lwgeom_window.c:59
uint32_t cluster_id
Definition: lwgeom_window.c:50
char is_null
Definition: lwgeom_window.c:51

References cluster_entry::cluster_id, cluster_context::clusters, fetch_cluster_context(), cluster_context::is_error, cluster_entry::is_null, LW_FALSE, LW_SUCCESS, LW_TRUE, lwalloc(), lwfree(), lwgeom_free(), lwgeom_geos_error(), read_lwgeom_from_partition(), UF_create(), UF_destroy(), UF_get_collapsed_cluster_ids(), and union_dbscan().

Here is the call graph for this function: