Clusters geometry objects using the 2D density-based spatial clustering of applications with noise (DBSCAN) algorithm and returns a cluster ID for each object.
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. |
eps | The minimum distance threshold. |
minpoints | The minimum number of geometry objects that must fall within the distance specified by eps for a geometry object to be classified as a core object in a cluster. |
Description
Unlike ST_ClusterKMeans,
ST_ClusterDBSCANdoes not require a predefined number of clusters. It builds clusters based on the distance and density you specify.A geometry object is added to a cluster if it meets either of the following conditions:
At least
minpointsgeometry objects fall within the distanceepsfrom the object. In this case, the object is the core object of the cluster.A core object falls within the distance
epsfrom the object. In this case, the object is a boundary object of the cluster.A boundary object may fall within the distance
epsfrom core objects in multiple clusters. If this happens, the boundary object is assigned to one of those clusters at random, and the resulting cluster may contain fewer objects than the value ofminpoints.
If a geometry object does not meet either condition in any cluster,
ST_ClusterDBSCANassigns it a cluster number of NULL.ST_ClusterDBSCANis a window function.ST_ClusterDBSCAN (Syntax 1) uses Euclidean distance. The value of
epsis measured in coordinate units based on Euclidean distance.ST_ClusterDBSCANSpheroid (Syntax 2) uses the length of the geometry object on an ellipsoid. For example, if the geometry has a spatial reference identifier (SRID) expressed in longitude and latitude, clustering is performed in the SRID's coordinate system 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;
st_clusterdbscan | st_astext
------------------+--------------
0 | POINT(0 0)
0 | POINT(1 1)
0 | POINT(-1 -1)
1 | POINT(-3 -3)
(4 rows)