Assigns a cluster ID to each input geometry using the 2D K-means algorithm.
Syntax
integer ST_ClusterKMeans(geometry winset geom, integer numberOfClusters);Parameters
| Parameter | Description |
|---|---|
geom | The geometry to cluster. |
numberOfClusters | The number of clusters to generate. |
Usage notes
Clustering distance is calculated between the centroids of the input geometries.
ST_ClusterKMeansis a window function. Use it with anOVERclause.
Example
The following query assigns cluster IDs to four points.
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;Output:
st_clusterkmeans | st_astext
------------------+--------------
0 | POINT(0 0)
0 | POINT(1 1)
1 | POINT(-1 -1)
1 | POINT(-2 -2)
(4 rows)该文章对您有帮助吗?