Take a list of LWGEOMs and a number of clusters and return an integer array indicating which cluster each geometry is in.
323 uint32_t num_non_empty = 0;
327 assert(max_radius >= 0);
333 "%s: number of geometries is less than the number of clusters requested, not all clusters will get data",
342 uint8_t *geom_valid =
lwalloc(
sizeof(uint8_t) * n);
343 memset(geom_valid, 0,
sizeof(uint8_t) * n);
346 int *clusters =
lwalloc(
sizeof(
int) * n);
347 for (uint32_t i = 0; i < n; i++)
352 memset(centers, 0,
sizeof(
POINT4D) * n);
355 double *radii =
lwalloc(
sizeof(
double) * n);
356 memset(radii, 0,
sizeof(
double) * n);
359 for (uint32_t i = 0; i < n; i++)
361 const LWGEOM *geom = geoms[i];
380 lwerror(
"%s has an input point geometry with weight in M less or equal to 0",
410 objs_dense[num_non_empty++] = out;
413 if (num_non_empty < k)
416 "%s: number of non-empty geometries (%d) is less than the number of clusters (%d) requested, not all clusters will get data",
425 if (num_non_empty > 0)
427 uint32_t *clusters_dense =
lwalloc(
sizeof(uint32_t) * num_non_empty);
428 memset(clusters_dense, 0,
sizeof(uint32_t) * num_non_empty);
429 uint32_t output_cluster_count =
kmeans(objs_dense, clusters_dense, num_non_empty, centers, radii, k, max_radius);
432 for (uint32_t i = 0; i < n; i++)
434 clusters[i] = (int)clusters_dense[d++];
436 converged = output_cluster_count > 0;
int gbox_is_valid(const GBOX *gbox)
Return false if any of the dimensions is NaN or infinite.
LWGEOM * lwgeom_centroid(const LWGEOM *geom)
double lwpoint_get_m(const LWPOINT *point)
void lwgeom_free(LWGEOM *geom)
double lwpoint_get_x(const LWPOINT *point)
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
const GBOX * lwgeom_get_bbox(const LWGEOM *lwgeom)
Get a non-empty geometry bounding box, computing and caching it if not already there.
void * lwalloc(size_t size)
double lwpoint_get_z(const LWPOINT *point)
#define LW_TRUE
Return types for functions with status returns.
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
double lwpoint_get_y(const LWPOINT *point)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
static uint32_t kmeans(POINT4D *objs, uint32_t *clusters, uint32_t n, POINT4D *centers, double *radii, uint32_t min_k, double max_radius)
#define KMEANS_NULL_CLUSTER
Datum centroid(PG_FUNCTION_ARGS)