ST_ClusterWithinWin — 入力ジオメトリごとに分離距離を使ったクラスタリングを行い、クラスタIDを返すウィンドウ関数です。
integer ST_ClusterWithinWin(geometry winset geom, float8 distance);
入力ジオメトリ毎のクラスタ番号を返すウィンドウ関数です。クラスタリングはジオメトリを、それぞれのジオメトリが、同じクラスタ内の他のジオメトリのうち少なくとも一つとの距離がdistance以内となる集合に分割します。距離はSRIDの単位を取るデカルト距離です。
ST_ClusterWithinWinはST_ClusterDBSCANをminpoints := 0で実行するのと同じです。
Availability: 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