Search code examples
javascriptmapboxmapbox-gldeck.glkepler.gl

how to mapbox-gl expressions unique ID style settings


I have a question about what is wrong with the Mapbox-gl vector tile style configuration.

I want to express 5 colors uniquely for the ID column.

example Data structure

id : 0 => 2 data
id : 1 => 7 data
id : 2 => 10 data
...

The value of this ID is not known. Can I give a unique color by setting a column name?

The results I want to say above are red for ID 0, yellow for ID 1, and blue for ID 3. For example, I don't know if the value of the ID column is an integer or a string. I want to give unique color automatically.


Solution

  • Suppose you have set ID in the data-side as feature's property so the style can get its ID as template string like {ID}.

    You can use style expression as below:

    somePaintProperty = [
        "case", // similar with switch ~ case
        ["==", ["get", "ID"], 0], // when the value of ID property equals to 0,
        "red", // then, colors red
        ["==", ["get", "ID"], 1],
        "yellow",
        ...
        "black", // else(default), black
    ]