ST_ClusterWithinWin — 窗口函数,使用分离距离进行聚类,为每个输入几何对象返回一个聚类 ID。
integer ST_ClusterWithinWin(geometry winset geom, float8 distance);
一个窗口函数,为每个输入几何对象返回一个聚类编号。聚类将几何对象划分为集合,其中每个几何对象与同一聚类中的至少一个其他几何对象的距离在指定的 distance 之内。距离是 SRID 单位中的笛卡尔距离。
ST_ClusterWithinWin 等效于运行 ST_ClusterDBSCAN,且 minpoints => 0。
可用性:3.4.0
此方法支持圆弧字符串和曲线。
WITH testdata AS (
SELECT id, geom::geometry FROM (
VALUES (1, 'LINESTRING (0 0, 1 1)'),
(2, 'LINESTRING (5 5, 4 4)'),
(3, 'LINESTRING (6 6, 7 7)'),
(4, 'LINESTRING (0 0, -1 -1)'),
(5, 'POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))')) AS t(id, geom)
)
SELECT id,
ST_AsText(geom),
ST_ClusterWithinWin(geom, 1.4) OVER () AS cluster
FROM testdata;
id | st_astext | cluster
----+--------------------------------+---------
1 | LINESTRING(0 0,1 1) | 0
2 | LINESTRING(5 5,4 4) | 0
3 | LINESTRING(6 6,7 7) | 1
4 | LINESTRING(0 0,-1 -1) | 0
5 | POLYGON((0 0,4 0,4 4,0 4,0 0)) | 0