mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

GeoJSON source with dynamic: true and cluster: true renders no features

Open annamenitskaya opened this issue 2 months ago • 1 comments

mapbox-gl-js version

3.31.0

Browser and version

Chrome 151, macOS

Expected behavior

A GeoJSON source with both dynamic: true and cluster: true renders clusters and unclustered points, the same as the source without dynamic, and updateData re-clusters the updated features.

Actual behavior

The source renders nothing. querySourceFeatures returns 0 features, whether the data is passed inline to addSource, set with setData, or added with updateData. The same data renders with cluster: true alone, and with dynamic: true alone.

In GeoJSONWorkerSource.loadData, when params.dynamic && params.cluster, the features passed to Supercluster come from this._dynamicIndex.getFeatures(). GeoJSONRT.load stores features after convertFeature, so Supercluster receives features in the internal geojson-vt format instead of GeoJSON Features with geometry.coordinates, and produces an empty index.

Link to the demonstration

Inline repro below.

Steps to trigger the unexpected behavior

const features = Array.from({ length: 1000 }, (_, i) => ({
  type: 'Feature',
  id: i,
  geometry: { type: 'Point', coordinates: [-122.4 + Math.random() * 0.2, 47.55 + Math.random() * 0.2] },
  properties: {},
}));

map.on('load', async () => {
  const results = {};
  for (const [name, options] of Object.entries({
    cluster: { cluster: true },
    dynamic: { dynamic: true },
    dynamicCluster: { dynamic: true, cluster: true },
  })) {
    map.addSource(name, { type: 'geojson', data: { type: 'FeatureCollection', features }, ...options });
    map.addLayer({ id: name, type: 'circle', source: name });
    await new Promise((resolve) => map.once('idle', resolve));
    results[name] = map.querySourceFeatures(name).length;
  }
  console.log(results);
});
  1. Run the snippet on a map centered at zoom 10.
  2. Observe cluster and dynamic report features; dynamicCluster reports 0 and nothing is drawn.

Relevant log output

{ cluster: 42, dynamic: 1061, dynamicCluster: 0 }

No errors are logged.

annamenitskaya avatar Sep 28 '26 21:09 annamenitskaya

Yes, dynamic is fundamentally incompatible with cluster — we should throw a descriptive error about it. If you need clustering, dynamic should be false.

mourner avatar Sep 29 '26 06:09 mourner