walking_the_cow
walking_the_cow

Reputation: 491

Mapbox GL/Maplibre GL 'to-number' fallback if null and not 0

As the documentation states, in the style specification expressions, the to-number expression converts null values to 0 as intended behavior. For my use case, null and 0 mean different things, as 0 is a valid value.

Here is and example of how I'd hope to style my data:

'paint': {
      'circle-color': [
        'case',
          ['all', [">=", ["to-number", ["get", "value"]], 0], ["<", ["to-number", ["get", "value"]], 100]], '#fff',
          ['all', [">=", ["to-number", ["get", "value"]], 100], ["<", ["to-number", ["get", "value"]], 500]], '#000',
          "#2f2f2f"
      ]
    },

0-100 are white, 100-500 are black, and anything else (null included) are #2f2f2f.

How could I make a case statement catch null values? I've tried using the == operator for null, as well as using to-string == ''. Neither worked for me, or their behavior impacted the other valid values.

I'm using a pg_tileserv instance to serve my data if that makes a difference.

Upvotes: 1

Views: 690

Answers (1)

Steve Bennett
Steve Bennett

Reputation: 126527

I have not tested this, but this make non-number values red:

["case", ["typeof", ["get", "value"]], "number", [ /* insert handling of number here */ ], "red"]

If your non-null values are string not number, change to "string".

Upvotes: 0

Related Questions