ST_ClusterKMeans — 使用 K 均值算法为每个输入几何返回一个聚类 ID 的窗口函数。
integer ST_ClusterKMeans(
geometry winset geom, integer number_of_clusters, float max_radius)
;
返回每个输入几何的K 均值聚类编号。用于聚类的距离是 2D 几何的质心之间的距离,以及 3D 几何的边界框中心之间的距离。对于 POINT 输入,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;
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 聚类和加权对预先聚合的行星级数据人口数据集进行聚类。根据Kontur 人口数据识别至少 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;