PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ ST_ClusterWithinWin()

Datum ST_ClusterWithinWin ( PG_FUNCTION_ARGS  )

Definition at line 213 of file lwgeom_window.c.

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

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: