ST_ClusterKMeans — 窗口函数,使用K-Means算法返回每个输入几何图形的群集ID。
integer
ST_ClusterKMeans
(
geometry winset
geom
, integer
number_of_clusters
, float
max_radius
)
;
退货 K-均值 每个输入几何体的簇号。用于聚集的距离是二维几何图形的质心之间的距离,以及三维几何图形的边界框中心之间的距离。对于点输入,M坐标将被视为输入的权重,并且必须大于0。
max_radius
如果设置,将导致ST_ClusterKMeans生成比
k
确保输出中没有任何群集的半径大于
max_radius
。这在可达性分析中很有用。
增强:3.2.0支持
max_radius
增强:3.1.0对3D几何图形和权重的支持
可用性:2.3.0
生成虚拟地块集,例如:
CREATE TABLE parcels AS
SELECT lpad((row_number() over())::text,3,'0') As parcel_id, geom,
('{residential, commercial}'::text[])[1 + mod(row_number()OVER(),2)] As type
FROM
ST_Subdivide(ST_Buffer('SRID=3857;LINESTRING(40 100, 98 100, 100 150, 60 90)'::geometry,
40, 'endcap=square'),12) As geom;
按聚类编号(CID)进行颜色编码的地块
SELECT ST_ClusterKMeans(geom, 3) OVER() AS cid, parcel_id, geom
FROM parcels;
cid | parcel_id | geom
-----+-----------+---------------
0 | 001 | 0103000000...
0 | 002 | 0103000000...
1 | 003 | 0103000000...
0 | 004 | 0103000000...
1 | 005 | 0103000000...
2 | 006 | 0103000000...
2 | 007 | 0103000000...
按类型划分地块簇:
SELECT ST_ClusterKMeans(geom, 3) over (PARTITION BY type) AS cid, parcel_id, type
FROM parcels;
cid | parcel_id | type
-----+-----------+-------------
1 | 005 | commercial
1 | 003 | commercial
2 | 007 | commercial
0 | 001 | commercial
1 | 004 | residential
0 | 002 | residential
2 | 006 | residential
例如:使用3D聚类和加权对预聚聚的行星规模的数据人口数据集进行聚类。根据以下条件确定至少20个地区 孔图尔人口数据 距离其中心不超过3000公里的区域:
create table kontur_population_3000km_clusters as
select
geom,
ST_ClusterKMeans(
ST_Force4D(
ST_Transform(ST_Force3D(geom), 4978), -- cluster in 3D XYZ CRS
mvalue := population -- set clustering to be weighed by population
),
20, -- aim to generate at least 20 clusters
max_radius := 3000000 -- but generate more to make each under 3000 km radius
) over () as cid
from
kontur_population;
聚集到以上规格的世界人口产生了46个集群。集群集中在人口稠密的地区(纽约、莫斯科)。格陵兰岛是一个星系团。有一些岛群横跨安蒂默里迪亚山脉。簇边遵循地球的曲率。
ST_ClusterDBSCAN , ST_ClusterIntersecting , ST_ClusterWithin , ST_Subdivide , ST_Force3D , ST_Force4D ,