1

I am very new to Mapbox and was wondering if anyone knew how to filter a layer based on a formula defined for a field in that layer. For example, I have a 'depth' field in the data and I want to display all the lines that have depth <=500 and is completely divisible by 100 (with no remainder). So the result should look something like this: show only the lines that have depth 100, 200, 300, 400, and 500. There is something like this for styling the layer as a text symbol but for symbolizing the layer as a line, the only option I get is a filter where I need to choose a specific value and not use a formula.

1

I have done this in two parts

First, apply the filter property as:

"filter": 
[
  "<=",
  "depth",
  500
]

Second,then in paint property of line

"paint": { "line-color": ["case", ["==", [ "%", [ "to-number", [ "get", "depth" ] ], 100 ], 0], "red",//color you want to give for lines divisible by 100 "blue"//color similar to background or any color ], "line-opacity":["case", ["==", [ "%", [ "to-number", [ "get", "depth" ] ], 100 ], 0], "1",//opacity given for lines divisible by 100 "0"//opacity zero will not show other lines ] }

Example Fiddle:

https://jsfiddle.net/p5d2gvn3/1/

  • Thank you. It worked! – P G Jul 30 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.