Spatial Clustering of GeoDataFrame into Polygons¶
This example demonstrates how to cluster spatial point data (e.g., buildings or services) into polygons using density-based algorithms:
DBSCAN or HDBSCAN methods
Parameters for minimum distance and minimum points per cluster
# Import necessary libraries
from objectnat import get_clusters_polygon
import geopandas as gpd
1. Load Point Dataset¶
Load a set of points (e.g., buildings) for spatial clustering.
# Load building data
buildings = gpd.read_parquet('examples_data/buildings.parquet')
2. Perform Clustering and Create Cluster Polygons¶
Use the get_clusters_polygon() function to cluster points into groups based on spatial proximity.
Parameters:
min_dist: maximum distance between neighboring points (e.g., 20 meters)min_point: minimum number of points to form a cluster (e.g., 10)method: clustering algorithm (‘DBSCAN’ or ‘HDBSCAN’)
# Apply clustering with DBSCAN
clusters, buildings_clustered = get_clusters_polygon(
points=buildings,
min_dist=70,
min_point=2,
method='DBSCAN'
)
# Show cluster polygons
m = clusters.explore()
# Optional: show clustered buildings colored by cluster ID
# buildings_clustered.explore(m=m, column='cluster', categorical=True)
m