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

setFilter method for GeoJSONSource

Open jaericson opened this issue 4 years ago • 9 comments

Motivation

You can cluster a GeoJSONSource, but it's difficult to filter out source data because the layer filter expression applies to the clustered features. The GeoJSONSource has a filter property, but it can only be specified on source initialization.

To paint an illustrative example, let's say we have 50K features with a date property and as a user interacts with our app, we allow them to filter the clustered map to just show features in a certain date range. If the 50K features weren't clustered, we could just apply a filter to each layer we wanted to filter. But with the 50K features clustered, we either need to filter the source data ourselves and use setData or we need to remove the source and add it back with a filter applied to it. The former requires source data regeneration on our end, and the latter results in a flash as the map removes all features from the old source and adds all features from the new source.

Thanks for your consideration, and also open to alternative ideas! ❤️

Design Alternatives

Would it be possible to add a setFilter method to a GeoJSONSource to allow for this workflow?

Design

Currently, it looks like the setData workflow in a geojson_source will kick off a loadData, which checks to see if there is a filter, and if so, does a data.features.filter. Since this setData workflow looks at the filter, it seems like setFilter could potentially be a subset of this workflow.

Mock-Up

Concepts

Implementation

jaericson avatar May 27 '21 17:05 jaericson

Thanks for the issue! It totally makes sense to add a setFilter method. I didn't add it in #9864 initially, thinking that removing/adding the source would be a good workaround, but layer flashing is indeed an issue.

mourner avatar May 27 '21 18:05 mourner

Thanks so much, @mourner!!

jaericson avatar May 27 '21 18:05 jaericson

:wave: @mourner, just wondering if you had a general timeline for when this might be implemented. Thank you! 🙏

jaericson avatar Aug 10 '21 14:08 jaericson

@mourner as another year went by without support for filtering clustered layers, is this still considered as an idea worth implementing? Filtering functionality in Mapbox feels very incomplete due to this.

yeegor avatar Jun 27 '22 09:06 yeegor

This feature would be helpful

dustinfarris avatar Mar 13 '23 20:03 dustinfarris

This would be super helpful!

ananmaysharan avatar May 30 '23 20:05 ananmaysharan

@jaericson I worked a bit with Mapbox and I made this pull request adding a new method setFilter to allow filter updates on GeoJSON source, without invoking setData function to pass pre-filtered geojson data.

@mourner I don't know if this is sufficient and well done, but it does the job.

Ruschio avatar Dec 26 '23 10:12 Ruschio