Take a list of LWGEOMs and a number of clusters and return an integer array indicating which cluster each geometry is in.
247 uint32_t num_centroids = 0;
248 uint32_t num_non_empty = 0;
270 lwerror(
"%s: number of geometries is less than the number of clusters requested, not all clusters will get data", __func__);
276 memset(centroids, 0,
sizeof(
LWGEOM*) * n);
281 memset(centers_raw, 0,
sizeof(
POINT2D) * k);
285 clusters =
lwalloc(
sizeof(
int) * n);
289 memset(objs, 0,
sizeof(
POINT2D*) * n);
290 memset(clusters, 0,
sizeof(
int) * n);
291 memset(centers, 0,
sizeof(
POINT2D*) * k);
294 for (i = 0; i < n; i++)
296 const LWGEOM* geom = geoms[i];
313 centroids[num_centroids++] =
centroid;
325 if (num_non_empty < k)
327 lwnotice(
"%s: number of non-empty geometries is less than the number of clusters requested, not all clusters will get data", __func__);
334 result =
kmeans(objs, clusters, n, centers, k);
340 for (i = 0; i < n; i++)
342 if (k == 0 || !objs[i])
357 if (result)
return clusters;
LWGEOM * lwgeom_centroid(const LWGEOM *geom)
void lwgeom_free(LWGEOM *geom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
void * lwalloc(size_t size)
#define LW_TRUE
Return types for functions with status returns.
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 const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
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 void kmeans_init(POINT2D **objs, 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)