Robbert
Robbert

Reputation: 139

Mapbox Style - why does my simple case expression not work?

I try to style my Mapbox layer using a case expression. My expression is quite simple and should for different integer ranges set different colors. Although doing the same as provided in examples, it must contain an error, and all lines are only visualized in black (although this is not the default color I set). After searching for hours, I still cannot find out why it won't work.

"line-color": [
      "case",
      [">=", ["to-number", ["get", "temperature"]],  30],
      "green",
      [">=", ["to-number", ["get", "temperature"]],  50],
      "red",
      "blue"
   

I included a default value, I tried it with and without the to-number operation (although it is a number value), I tried to put the values 30 and 50 into quotes or brackets.

I also tried setting it as interpolation expression.

"line-color": [
  "interpolate",
  ["linear"],
  ["to-number", ["get", "temperature"]],
  10, "green",
  50, "red",
  150, "blue"
]

Any ideas what I am doing wrong?

Edit: I try to visualize the results in QGIS, if that matters.

UPDATE: I found the Mapbox GL Style Validator and with this I do not get any errors. Unfortunately, the case still does not work, but I can do it with filters by splitting up my temperature layer and setting a filter for each range. Of course this is not a good solution as it enlarges my styling code significantly.

Upvotes: 0

Views: 401

Answers (1)

Steve Bennett
Steve Bennett

Reputation: 126527

Hard to tell with the information provided, but this result would be consistent with data that contains values that can't be converted to numbers.

You can try this:

"line-color": [
      "case",
      [">=", ["to-number", ["get", "temperature"], 0],  30],
      "green",
      [">=", ["to-number", ["get", "temperature"], 0],  50],
      "red",
      "blue"

Upvotes: 1

Related Questions