All Products
Search
Document Center

PolarDB:ST_ClusterDBSCAN

Last Updated:Mar 28, 2026

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

ParameterDescription
geomThe geometry object.
epsThe minimum distance threshold.
minpointsThe 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_ClusterDBSCAN does 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 minpoints geometry objects fall within the distance eps from the object. In this case, the object is the core object of the cluster.

    • A core object falls within the distance eps from the object. In this case, the object is a boundary object of the cluster.

      A boundary object may fall within the distance eps from 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 of minpoints.

  • If a geometry object does not meet either condition in any cluster, ST_ClusterDBSCAN assigns it a cluster number of NULL.

  • ST_ClusterDBSCAN is a window function.

  • ST_ClusterDBSCAN (Syntax 1) uses Euclidean distance. The value of eps is 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)