最新版本的postgis2.3支持点的聚类操作:ST_ClusterDBSCAN和ST_ClusterKMeans,这篇文章介绍了dbscan的操作。
在此基础上做一些操作:
纪录每个点的聚类
CREATE TABLE mtl_crimes_cid (
id integer,
cid integer,
geom geometry(Point, 2950)
);
INSERT INTO mtl_crimes_cid
SELECT * FROM
(SELECT id, ST_ClusterDBSCAN(geom, eps := 300, minPoints := 50)
OVER () AS cluster_id, geom
FROM mtl_crimes) sq
WHERE cluster_id IS NOT NULL;
每个聚类选择一个代表点(质心)
CREATE TABLE mtl_crimes_cen (
cid integer,
geom geometry(Point, 2950)
);
INSERT INTO mtl_crimes_cen
SELECT * FROM
(SELECT cid, ST_Centroid(ST_Union(geom)) as geom
FROM mtl_crimes_cid
GROUP BY cid) sq;
效果图如下,深绿色为原始点,浅绿色为聚类后的点,红色为每个簇的代表点