Take a list of LWGEOMs and a number of clusters and return an integer array indicating which cluster each geometry is in.
266 lwerror(
"%s: number of geometries is less than the number of clusters requested, not all clusters will get data", __func__);
272 memset(centroids, 0,
sizeof(
LWGEOM*) * n);
277 memset(centers_raw, 0,
sizeof(
POINT2D) * k);
281 clusters =
lwalloc(
sizeof(
int) * n);
285 memset(objs, 0,
sizeof(
POINT2D*) * n);
286 memset(clusters, 0,
sizeof(
int) * n);
287 memset(centers, 0,
sizeof(
POINT2D*) * k);
290 for (i = 0; i < n; i++)
292 const LWGEOM* geom = geoms[i];
309 centroids[num_centroids++] =
centroid;
321 if (num_non_empty < k)
323 lwnotice(
"%s: number of non-empty geometries is less than the number of clusters requested, not all clusters will get data", __func__);
329 kmeans_init(objs, clusters, n, centers, centers_raw, k);
330 result =
kmeans(objs, clusters, n, centers, k);
336 for (i = 0; i < n; i++)
338 if (k == 0 || !objs[i])
353 if (result)
return clusters;
LWGEOM * lwgeom_centroid(const LWGEOM *geom)
void lwgeom_free(LWGEOM *geom)
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
void * lwalloc(size_t size)
#define LW_TRUE
Return types for functions with status returns.
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
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 void kmeans_init(POINT2D **objs, int *clusters, uint32_t n, POINT2D **centers, POINT2D *centers_raw, uint32_t k)
#define KMEANS_NULL_CLUSTER
static int kmeans(POINT2D **objs, int *clusters, uint32_t n, POINT2D **centers, uint32_t k)
Datum centroid(PG_FUNCTION_ARGS)