Clustering and Large Point Datasets

Reduce visual overlap with GeoJSON clustering, style cluster counts and individual points, and zoom into clusters on click.

🎯 Introduction

By the end of this article, you will be able to cluster dense points at the current zoom level and smoothly zoom into a cluster when clicked.


🧱 Why use clustering?

Displaying many points individually causes overlap and increases rendering and interaction query costs. Clustering temporarily combines nearby points into one circle with a count, separating them progressively as users zoom in.

Clustering does not change the original coordinates. It is a display result generated by a GeoJSON source for a given zoom level.


⚙️ Enable GeoJSON clustering

map.addSource('places', {
    type: 'geojson',
    data: placeGeoJSON,
    cluster: true,
    clusterRadius: 50,
    clusterMaxZoom: 14,
})
  • clusterRadius is the clustering radius in pixels. Larger values make nearby points more likely to merge.
  • clusterMaxZoom is the highest zoom level at which clustering is performed; zooming farther in reveals individual points.

Adjust both values for data density, map dimensions, and how users need to read the data.


🎨 Render clusters and individual points separately

map.addLayer({
    id: 'clusters',
    type: 'circle',
    source: 'places',
    filter: ['has', 'point_count'],
    paint: {
        'circle-color': [
            'step', ['get', 'point_count'],
            '#60a5fa',
            20, '#2563eb',
            100, '#1e3a8a',
        ],
        'circle-radius': [
            'step', ['get', 'point_count'],
            18,
            20, 24,
            100, 30,
        ],
    },
})

map.addLayer({
    id: 'cluster-count',
    type: 'symbol',
    source: 'places',
    filter: ['has', 'point_count'],
    layout: {
        'text-field': ['get', 'point_count_abbreviated'],
        'text-size': 12,
    },
    paint: { 'text-color': '#ffffff' },
})

map.addLayer({
    id: 'unclustered-places',
    type: 'circle',
    source: 'places',
    filter: ['!', ['has', 'point_count']],
    paint: { 'circle-radius': 6, 'circle-color': '#16a34a' },
})

Only cluster features have point_count, so filters use it to route clusters and individual points to different layers.


🔍 Zoom into a cluster on click

map.on('click', 'clusters', (event) => {
    const feature = event.features?.[0]
    if (!feature || feature.geometry.type !== 'Point') return

    const clusterId = Number(feature.properties?.cluster_id)
    const coordinates = feature.geometry.coordinates as [number, number]
    const source = map.getSource('places') as mapboxgl.GeoJSONSource
    source.getClusterExpansionZoom(clusterId, (error, zoom) => {
        if (error || zoom === null || zoom === undefined) return

        map.easeTo({
            center: coordinates,
            zoom,
        })
    })
})

getClusterExpansionZoom returns the zoom level at which a cluster begins to split. This adapts to data density better than always adding two zoom levels.

The component below implements this logic. Click a blue cluster to see its point count and target zoom, then watch the map zoom in. Continue clicking clusters to see them split into smaller clusters or individual green points:

Click a blue cluster to zoom to the level where it starts to split

⚖️ When clustering is appropriate

Clustering suits many independent points and tasks that start with an overview before zooming into detail, such as stores, devices, and incidents. If users must compare every point simultaneously, points cannot be merged, or the data is not point geometry, consider zoom-based filtering, server-side tiling, or another representation.

Mapbox calculates clusters in the source and renders them with WebGL layers, which generally suits dense points better than many DOM markers. Data volume, device performance, and style complexity still matter, so test with real data.


🧮 Aggregate application properties in clusters

By default, cluster counts are available through point_count. If points also have fields such as value or online, use clusterProperties to calculate totals or category counts:

map.addSource('places', {
    type: 'geojson',
    data: placeGeoJSON,
    cluster: true,
    clusterRadius: 50,
    clusterProperties: {
        totalValue: ['+', ['get', 'value']],
        onlineCount: ['+', ['case', ['get', 'online'], 1, 0]],
    },
})

totalValue sums each point's value in a cluster. onlineCount counts points whose online field is true by assigning them 1 and summing. Use these generated fields in symbol text or color expressions, just like point_count.

clusterProperties aggregates only properties already in the point source. Calculate deduplication, cross-table statistics, or precise reports on the backend and let the map display the results.


🧾 Summary

  • Clustering combines nearby points into one displayed feature.
  • clusterRadius controls the pixel radius; clusterMaxZoom controls the clustering zoom limit.
  • This example uses three layers for cluster circles, count labels, and individual points.
  • Use step to change circle color and size by count.
  • Use getClusterExpansionZoom to choose an appropriate zoom level on click.
  • clusterProperties aggregates numeric values and category counts within clusters.
  • Clustering suits overall distributions rather than simultaneous comparison of every individual point.

❓ Questions and answers

Q1: Is point_count part of the original data?

No. The clustered source generates it automatically for cluster features.

Q2: Why are points still clustered after zooming in?

The zoom level may not yet exceed clusterMaxZoom, or points may still lie within clusterRadius.

Q3: Can clustering be used with Polygons?

No. Built-in GeoJSON source clustering is intended for Point features.


🧪 Exercises

  1. Adjust clusterRadius and compare cluster counts and distributions.
  2. Zoom in when a cluster is clicked and show a popup when an individual point is clicked.
// Write your code here

🎉 You now know how to display clustered point data!