This topic describes the ST_ClusterKMeans function. This function returns a cluster number for each specified geometry object by using the 2D-based K-means algorithm.

Syntax

integer  ST_ClusterKMeans(geometry winset  geom , integer  numberOfClusters);

Parameters

Parameter Description
geom The geometry objects that you want to specify.
numberOfClusters The number of clusters that you want to specify.

Description

  • The distance that is used for clustering is the distance between the centroids of the geometry objects that you specify.
  • The ST_ClusterKMeans function is a window function.

Examples

SELECT ST_ClusterKMeans(geom,2) over() ,st_AsText(geom)
    from (select unnest(ARRAY['POINT (0 0)'::geometry,
                            'POINT(1 1)'::geometry,
                            'POINT (-1 -1)'::geometry,
                            'POINT (-2 -2)'::geometry]) as geom) as test;
 st_clusterkmeans |  st_astext
------------------+--------------
                0 | POINT(0 0)
                0 | POINT(1 1)
                1 | POINT(-1 -1)
                1 | POINT(-2 -2)
(4 rows)