leafem
leafem copied to clipboard
Add `changeColors()` for changing the color of a map layer on the fly
This PR adds a function to re-map the colors of a leaflet layer on the fly. I had two closely related reasons for creating it:
- I had generated some map tiles and wanted the ability to easily change the color scheme without having to regenerate them all (as they took a long time to generate)
- I wanted the ability to be able to change the colors of tiles added via WMS
It uses the gradientmaps JavaScript library to change the colors. For an example, here's a WMS layer as-is:
And here I've changed the colors using the changeColors()
function:
Here's the code to recreate the previous example:
l <- leaflet() %>%
setView(-112.56, 40.66, 5) %>%
addTiles() %>%
addWMSTiles("https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Bare_Ground_Shrubland_Fractional_Component/ows?SERVICE=WMS&",
layers = "NLCD_2016_Bare_Ground_Shrubland_Fractional_Component",
options = WMSTileOptions(className = "bare_ground", transparent = TRUE, format = "image/png"))
l
l %>% changeColors("bare_ground", terrain.colors(20))
I thought it might be a good fit for the leafem
package, and it seems like something that could be useful for others as well. And from a more selfish point of view, it'd be nice for me if it was part of an already-existing package rather than its own tiny package, which is how I have it right now.
No worries if you'd rather not include it in the package. Also, I'm happy to make any changes to the code and/or documentation.