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

add/remove filter

Open willymaps opened this issue 1 year ago • 0 comments

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":

image

Implementation

Probably missing a lot of aspects to this, but I assume:

  • If there is a filter expression without all, then wrap with all and insert the expression stated in addFilter.
  • addFilter could potentially insert/build expressions using logic from here

willymaps avatar Jul 25 '22 16:07 willymaps