名前

ST_ClusterKMeans — 入力ジオメトリごとにk平均法アルゴリズムを使ってクラスタ番号を返すウィンドウ関数です。

概要

integer ST_ClusterKMeans(geometry winset geom, integer number_of_clusters);

説明

入力ジオメトリごとの、2次元距離に基づくk平均法クラスタ番号を返します。クラスタリングに使用する距離はジオメトリの重心間の距離です。

Availability: 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('LINESTRING(40 100, 98 100, 100 150, 60 90)'::geometry,
    40, 'endcap=square'),12) As geom;

元の区画

クラスタ番号 (cid)による色付けを施した区画

SELECT ST_ClusterKMeans(geom, 5) 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...
(7 rows)

-- 種別による区画クラスタの分割
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
(7 rows)

関連情報

ST_ClusterDBSCAN, ST_ClusterIntersecting, ST_ClusterWithin, ST_Subdivide