leafem icon indicating copy to clipboard operation
leafem copied to clipboard

Add `changeColors()` for changing the color of a map layer on the fly

Open dfriend21 opened this issue 1 year ago • 0 comments

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:

  1. 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)
  2. 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:

Screen Shot 2023-05-20 at 7 39 57 PM

And here I've changed the colors using the changeColors() function:

Screen Shot 2023-05-20 at 7 40 27 PM

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.

dfriend21 avatar May 21 '23 01:05 dfriend21