Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapbox gl setFilter filter by indexOf, contains, or substring

I want to use setFilter expression to filter by substring for a given feature property inside a tileset. Note: I do NOT want to have to load an array of features external to the tileset, I want it to use setFilter only, not getFeatures functions and no looping. If user begins typing, "smith .." it would filter out features as they typed using setFilter only.

I only see "==" or "match" but neither do case insensitive substring filtering, such as indexOf, contains, Like, etc. Something like ['contains', feature.prop, 'smi'] then ['contains', feature.prop, 'smith'], for example.

I see the example on mapbox examples, for filtering as you type, but I want to only use setFilter. It doesn't look like it supports what I want to do, but I thought I'd ask anyway. It is a waste of client-side resources to have to populate any local array of features from the tileset. It defeats the purpose of putting the data inside a tileset to begin with.

Any standard expression for parsing a feature property by a partial string, not an exact match?

like image 250
Donnie Kerr Avatar asked Jan 17 '26 09:01

Donnie Kerr


2 Answers

Now mapbox support to filter for tyle layer for sub string from string

expression code is

['in', {filter-substring-value}, ['string', ['get', {mapbox-property-field-string}]]]

like image 190
Ankit Suthar Avatar answered Jan 20 '26 22:01

Ankit Suthar


As you have noticed, Mapbox-GL expressions don't support substrings or regexes. So I think the only workaround is along the lines you mentioned: getting a list of attribute values and using that as an autocomplete.

There are two ways to get that list of attribute values that don't require making separate, arguably redundant, queries.

  1. Use the values within the TileJSON. Depending how the TileJSON is generated, it often contains a list of the most common values for each attribute (up to 1000 or so, I think).
  2. Use querySourceFeatures() to fetch all features within the current viewport, then filter down to find the values of the attribute you care about. This won't help if the user wants to filter towards something which is currently outside the viewport.
like image 35
Steve Bennett Avatar answered Jan 20 '26 21:01

Steve Bennett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!