kmeans function and examples
✓ Published0🌍 Public
Aandrewxhill
Last edited Nov 30, 2012
Created on Nov 30, 2012
The example shows how to cluster geographic points into groups using a custom k-means algorithm implemented in PostgreSQL. It defines a PL/pgSQL function that takes an array of geometries, iteratively refines cluster centroids, and returns the final centroid locations. The code uses PostGIS functions like `ST_Accum`, `ST_Centroid`, `ST_Collect`, and `ST_Distance` to perform spatial calculations, with queries that assign each point to its nearest cluster via a lateral join and the `<->` operator. The examples also demonstrate optional preprocessing with `ST_SnapToGrid` to simplify input geometries.
AI-generated description