{"version":"1.0","type":"rich","title":"kmeans function and examples","description":"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.","provider_name":"VizHub AI Studio","provider_url":"https://next.vizhub.com","html":"<iframe src=\"https://next.vizhub.com/viz/gist-4179609?mode=embed\" width=\"960\" height=\"500\" scrolling=\"no\" frameborder=\"1\"></iframe>","width":960,"height":500,"cache_age":3600}