A window function that assigns a cluster ID to each geometry object using the 2D density-based spatial clustering of applications with noise (DBSCAN) algorithm.
Syntax
Syntax 1
integer ST_ClusterDBSCAN(geometry winset geom, float8 eps, integer minpoints);Syntax 2
integer ST_ClusterDBSCANSpheroid(geometry winset geom, float8 eps, integer minpoints);
Parameters
| Parameter | Description |
|---|---|
geom | The geometry object to cluster. |
eps | The minimum distance that you want to specify. |
minpoints | The minimum number of geometry objects that must be located within the minimum distance specified by the eps parameter. This parameter is used to identify whether a geometry object is the core geometry object of a cluster. |
Description
Unlike ST_ClusterKMeans, ST_ClusterDBSCAN does not require you to specify the number of clusters. Instead, it derives clusters from the distance and density you define.
A geometry object is added to a cluster if it meets one of the following conditions:
At least
minpointsobjects fall withinepsdistance of the geometry object. In this case, the object is a core geometry object of the cluster.A core geometry object falls within
epsdistance of the geometry object. In this case, the object is a boundary geometry object of the cluster.
A boundary geometry object can be within eps of core geometry objects from multiple clusters. When this happens, the boundary geometry object is assigned to one of those clusters at random, and the resulting cluster may contain fewer objects than minpoints.
If a geometry object does not meet either condition, it is assigned a cluster ID of NULL.
Distance calculation:
Syntax 1 (
ST_ClusterDBSCAN): uses Euclidean distance. Theepsvalue is interpreted as the Euclidean distance between coordinates.Syntax 2 (
ST_ClusterDBSCANSpheroid): uses the length of a geometry object on an ellipsoid. When the geometry has a spatial reference identifier (SRID) expressed in longitude and latitude, clustering is performed in the SRID coordinate system with distances measured in meters.
Examples
SELECT ST_ClusterDBSCAN(geom, 2, 1) OVER (), st_AsText(geom)
FROM (
SELECT unnest(ARRAY[
'POINT (0 0)'::geometry,
'POINT(1 1)'::geometry,
'POINT (-1 -1)'::geometry,
'POINT (-3 -3)'::geometry
]) AS geom
) AS test;Output:
st_clusterdbscan | st_astext
------------------+--------------
0 | POINT(0 0)
0 | POINT(1 1)
0 | POINT(-1 -1)
1 | POINT(-3 -3)
(4 rows)