ST_ClusterWithin — 按间隔距离对几何图形进行聚合的聚合函数。
geometry[] ST_ClusterWithin(geometry set g, float8 distance);
返回 GeometryCollections 数组的聚合函数,其中每个集合都是包含一些输入几何图形的簇。 聚类将输入几何图形划分为多个集合,其中每个几何图形都在同一簇中至少一个其他几何图形的指定距离内。 距离是以 SRID 为单位的笛卡尔距离。
ST_ClusterWithin相当于运行ST_ClusterDBSCAN使用minpoints => 0。
可用性:2.2.0
此方法支持圆形字符串和曲线。
WITH testdata AS
(SELECT unnest(ARRAY['LINESTRING (0 0, 1 1)'::geometry,
'LINESTRING (5 5, 4 4)'::geometry,
'LINESTRING (6 6, 7 7)'::geometry,
'LINESTRING (0 0, -1 -1)'::geometry,
'POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))'::geometry]) AS geom)
SELECT ST_AsText(unnest(ST_ClusterWithin(geom, 1.4))) FROM testdata;
--result
st_astext
---------
GEOMETRYCOLLECTION(LINESTRING(0 0,1 1),LINESTRING(5 5,4 4),LINESTRING(0 0,-1 -1),POLYGON((0 0,4 0,4 4,0 4,0 0)))
GEOMETRYCOLLECTION(LINESTRING(6 6,7 7))