For the complete documentation index, see llms.txt. This page is also available as Markdown.

clustering

This module contains functions that perform clustering on geographies.

CREATE_CLUSTERDBSCAN

CREATE_CLUSTERDBSCAN(input, output_table, geom_column, epsilon, min_points [, partition_column])

Description

Takes a set of points as input and groups them into clusters using the DBSCAN algorithm, writing the result to a new table.

DBSCAN groups together points lying in dense neighborhoods and labels the rest as noise. Unlike k-means it does not require the number of clusters up front, it finds clusters of arbitrary shape, and it does not force every point into a cluster.

Input parameters

  • input: VARCHAR name of the table or literal SQL query to be clustered.

  • output_table: VARCHAR(MAX) qualified name of the output table, e.g. <my-schema>.<my-output-table>. It is replaced if it already exists.

  • geom_column: VARCHAR name of the POINT column to be clustered, in SRID 4326 or 0.

  • epsilon: FLOAT8 the search radius in meters.

  • min_points: INT the minimum number of points, counting the point itself, that form a dense neighborhood.

  • partition_column (optional): VARCHAR name of a column to cluster within, NULL values forming their own group. If omitted the whole input is one set.

Output

The output table contains all the columns of input plus cluster_id and pt_type.

cluster_id is a zero-based cluster index, restarting at zero in each partition. It is NULL for any point that is not in a cluster.

pt_type is the role of the point. A core point has at least min_points points within epsilon of it, counting itself, and core points within epsilon of each other belong to the same cluster. A border point is not a core point but lies within epsilon of one, so it joins that cluster without connecting it to any other. A noise point is neither. A skipped point was never clustered because its geometry was NULL, which is absent input rather than a result.

info

Cluster assignments match sklearn.cluster.DBSCAN with metric='haversine' for the same epsilon and min_points. Where DBSCAN is inherently ambiguous — a border point reachable from two clusters — this implementation always picks the cluster with the lowest canonical label, so results are deterministic and reproducible across runs.

Examples

CREATE_CLUSTERKMEANS

Description

Takes a set of points as input and partitions them into clusters using the k-means algorithm. Creates a new table with the same columns as input plus a cluster_id column with the cluster index for each of the input features.

Input parameters

  • input: VARCHAR name of the table or literal SQL query to be clustered.

  • output_table: VARCHAR(MAX) qualified name of the output table, e.g. <my-schema>.<my-output-table>. The process will fail if the table already exists.

  • geom_column: VARCHAR name of the column to be clusterd.

  • number_of_clusters: INT number of clusters that will be generated.

Examples

ST_CLUSTERKMEANS

Description

Takes a set of points as input and partitions them into clusters using the k-means algorithm. Returns an array of tuples with the cluster index for each of the input features and the input geometry.

Input parameters

  • geog: GEOMETRY points to be clustered.

  • numberOfClusters (optional): INT number of clusters that will be generated. It defaults to the square root of half the number of points (sqrt(<NUMBER OF POINTS>/2)). The output number of cluster cannot be greater to the number of distinct points of the geog.

Return type

SUPER: containing objects with cluster as the cluster id and geom as the geometry in GeoJSON format.

Examples

Last updated

Was this helpful?