Name

ST_ClusterDBSCAN — 使用 DBSCAN 算法返回每个输入几何图形的簇 id 的窗口函数。

Synopsis

integer ST_ClusterDBSCAN(geometry winset geom, float8 eps, integer minpoints);

描述

一个窗口函数,使用 2D 基于密度的噪声应用空间聚类 (DBSCAN)算法。 与 ST_ClusterKMeans 不同,它不需要指定簇的数量,而是使用所需的距离 (eps )和密度(minpoints)参数来确定每个簇。

如果输入几何体满足以下任一条件,则将其添加到簇中:

  • 在eps 距离内的“核心”几何图形,至少有minpoints个输入几何图形(包括它自己);或

  • “边界”几何图形,位于核心几何图形的 eps 距离 内。

Note that border geometries may be within eps distance of core geometries in more than one cluster. Either assignment would be correct, so the border geometry will be arbitrarily assigned to one of the available clusters. In this situation it is possible for a correct cluster to be generated with fewer than minpoints geometries. To ensure deterministic assignment of border geometries (so that repeated calls to ST_ClusterDBSCAN will produce identical results) use an ORDER BY clause in the window definition. Ambiguous cluster assignments may differ from other DBSCAN implementations.

[Note]

不满足加入任何簇的条件的几何图形将被分配簇号 NULL。

可用性:2.3.0

此方法支持圆形字符串和曲线。

示例

对彼此相距 50 米以内的多边形进行聚类,并且每个聚类至少需要 2 个多边形。

50 米范围内的聚类,每个聚类至少有两个面。 如果只有一个,则 cid 为 NULL

SELECT name, ST_ClusterDBSCAN(geom, eps =
> 50, minpoints =
> 2) over () AS cid
FROM boston_polys
WHERE name 
> '' AND building 
> ''
        AND ST_DWithin(geom,
        ST_Transform(
            ST_GeomFromText('POINT(-71.04054 42.35141)', 4326), 26986),
           500);

                name                 | bucket
-------------------------------------+--------
 Manulife Tower                      |      0
 Park Lane Seaport I                 |      0
 Park Lane Seaport II                |      0
 Renaissance Boston Waterfront Hotel |      0
 Seaport Boston Hotel                |      0
 Seaport Hotel & World Trade Center  |      0
 Waterside Place                     |      0
 World Trade Center East             |      0
 100 Northern Avenue                 |      1
 100 Pier 4                          |      1
 The Institute of Contemporary Art   |      1
 101 Seaport                         |      2
 District Hall                       |      2
 One Marina Park Drive               |      2
 Twenty Two Liberty                  |      2
 Vertex                              |      2
 Vertex                              |      2
 Watermark Seaport                   |      2
 Blue Hills Bank Pavilion            |   NULL
 World Trade Center West             |   NULL
(20 rows)

显示将具有相同簇编号的地块组合成几何集合的示例。

SELECT cid, ST_Collect(geom) AS cluster_geom, array_agg(parcel_id) AS ids_in_cluster FROM (
    SELECT parcel_id, ST_ClusterDBSCAN(geom, eps => 0.5, minpoints => 5) over () AS cid, geom
    FROM parcels) sq
GROUP BY cid;