mapbox-gl-js
mapbox-gl-js copied to clipboard
add/remove filter
Motivation
It would be nice to make it easier to add/remove expressions from filters. Currently, to create a mask around a single country and filter place labels appropriately, a user would need to parse through and update the all
expression, which is quite complex in Mapbox core styles.
Current filter in settlement-major-label
:
[
"all",
[
"<=",
["get", "filterrank"],
3
],
[
"match",
["get", "class"],
"settlement",
[
"match",
["get", "worldview"],
["all", "US"],
true,
false
],
"disputed_settlement",
[
"all",
[
"==",
["get", "disputed"],
"true"
],
[
"match",
["get", "worldview"],
["all", "US"],
true,
false
]
],
false
],
[
"step",
["zoom"],
false,
2,
[
"<=",
["get", "symbolrank"],
6
],
4,
[
"<",
["get", "symbolrank"],
7
],
6,
[
"<",
["get", "symbolrank"],
8
],
7,
[
"<",
["get", "symbolrank"],
10
],
10,
[
"<",
["get", "symbolrank"],
11
],
11,
[
"<",
["get", "symbolrank"],
13
],
12,
[
"<",
["get", "symbolrank"],
15
],
13,
[
">=",
["get", "symbolrank"],
11
],
14,
[
">=",
["get", "symbolrank"],
15
]
]
]
Design Alternatives
Without having done it, I assume a combination of getFilter
and setFilter
could be used to parse through these long expressions and add a new expression to the all
array.
Design
map.addFilter('my-layer', ['==', ['get', 'name'], 'USA'])
map.removeFilter('my-layer', ['==', ['get', 'name'], 'USA'])
addFilter
might conflict with setFilter
so maybe addToFilter
or addFilterExpression
would be less confusing
Concepts
This concept is borrowed from how Mapbox Studio handles data filter "conditions":
Implementation
Probably missing a lot of aspects to this, but I assume:
- If there is a filter expression without
all
, then wrap withall
and insert the expression stated inaddFilter
. -
addFilter
could potentially insert/build expressions using logic from here