crazycapivara / mapboxer

An R Interface to Mapbox GL JS
https://crazycapivara.github.io/mapboxer/
Other
54 stars 7 forks source link

How to fill color for vector tiles based on a property #98

Open Jeffrey-Truong opened 2 years ago

Jeffrey-Truong commented 2 years ago

Hi I've been looking through the documentation and examples for filling Mapbox vector tiles.

I've noted in the example fill-layer-melbourne.R uses the "get" function to fill in the boundaries:

paint = list( "fill-color" = c("get", "fillColor"), "fill-opacity" = 0.7 )

This works well for imported shapefiles but I'm not able to find the correct syntax when I want to use my custom vector tiles in Mapbox. I've tested your example vector-layer-filled-polygons.R for one color with my custom vector tiles in Mapbox and works great but would like to extend that to have different colors for each polygon. This is my understanding of what the code would look like in Mapbox GL JS:

'paint': { 'fill-color': ['match', ['get', 'LHD_Name'], 'Sydney', 'red',
'South Western Sydney', 'blue', 'white'] }

What is the correct syntax for mapboxer to implement this type of visualisation?

Cheers

Jeffrey-Truong commented 2 years ago

An update to say I found one way of doing it after looking at the documentation further regarding expressions. This works for me:

"paint" = list( "fill-color" = list( "case", list("==", c("get", "LHN_Name"), "Sydney"), "red", # 'red' if 'LHD_Name == "Sydney"' list("==", c("get", "LHN_Name"), "Western Sydney"), "blue", # 'blue' if 'LHD_Name == "Western Sydney"' "yellow" # Defaults to 'yellow' )

I haven't worked out how to use the match function/expression yet but this will suffice for now.