Dynamic filtering logic with clustering activated can lead to unexpected behaviors
mapbox-gl-js version: 3.0.1
Question
I am using Mapbox and, more especially, the clustering logic we can use in the addSource function. But there are some little things I don't understand or that have not been deeply explained.
- Filtering language logic
I am using the filtering language logic that works very easily at the layer level when the clustering is set to false. But we can't use it when the clustering is set to true because the filter is done at the source level. So in the clustering case, you need to use the setData logic and filter yourself. Could you explain the underlying explanation? Are those two processes completely different? or is it better developed when the clustering is false?
- Filters at the source level can cause issues with the setData logic.
In the following code, I had to remove the filter logic with the clustering mode activated to make the setData function work. Why? How are the two related?
// Add a new source from our GeoJSON data and
// set the 'cluster' option to true. GL-JS will
// add the point_count property to your source data.
const sourceName = 'source-' + vl.id
// Warning: we can not set a filter at this level since we use the setdata property
// to filter in an other function. If you use filter here, some logic is broken
this.map.addSource(sourceName, {
type: 'geojson',
data: vl.featureCollection,
cluster: true,
// filter: ["==", ["get", "value"], 1],
clusterMaxZoom: 14, // Max zoom to cluster points on
clusterRadius: 50 // Radius of each cluster when clustering points (defaults to 50)
})
//Later in the code
this.map.getSource(sourceName).setData(filteredData)
Links to related documentation
Those are two different filters. filter on a layer filters features within a layer. filter on a GeoJSON source filters features within that source.
So in the clustering case, you need to use the setData logic and filter yourself.
Why? I'm not sure I'm following.
In the following code, I had to remove the filter logic with the clustering mode activated to make the setData function work.
Can you please provide a minimal reproducible test case?
Why? I'm not sure I'm following.
Why don't we access the filtering logic at the layer level when clustering is activated on the source data? That is weird. I guess the reason is that the data structure behind loses the individual properties of the points. Hence, it is then impossible to filter on something that does not exist anymore.
Can you please provide a minimal reproducible test case?
I'm sorry. It was a mistake that I made, and mapboxgl did not raise it. In fact, the filter I added directly to the source (the one commented on the code) was messing with my logic. Removing it resolved the issue.
Here is an example: live code