Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addGeotiff unable to make NA values transparent #52

Closed
piersond opened this issue Feb 4, 2022 · 2 comments
Closed

addGeotiff unable to make NA values transparent #52

piersond opened this issue Feb 4, 2022 · 2 comments

Comments

@piersond
Copy link

piersond commented Feb 4, 2022

After much trial and error using code from issue #25, I can't get NA values in my raster to be transparent.

Here's example code. Option A uses addGeotiff and option B uses addRasterImage.
Raster file available at: https://drive.google.com/file/d/1QbpOUPy_thsonsefdSZrEl2K1VDY0slw/view?usp=sharing

addGeotiff greatly improves raster load time for my application. I appreciate the work that's gone into development.

library(leaflet)
library(leafem)
library(raster)

raster_path <- "C:/Users/Derek/Google Drive/RCrk/BSU_ScholorWorks_Archive/Pierson_RCrk_Soil-C-modeling_data/Spatial Data/Spatial forcing rasters/RCrk_MAST_estimate.tif"

# Create leaflet map and set map location + zoom
map <- leaflet() %>%       
  addProviderTiles("Esri.WorldImagery", 
                   group = "Esri.WorldImagery",
                   options = providerTileOptions(opacity = 1)) %>%
  setView(lng = -116.75, lat = 43.16, zoom = 11)

# OPTION A
### Adding raster using addGeotiff
map %>% addGeotiff(raster_path,
           colorOptions = colorOptions(
            palette = colorRampPalette(c("#05475e", "#faff78", "#ff9a00", "#ff3500"))(100) ,
             breaks = c(5:15),
             na.color = "transparent"),
           opacity = 1,
           group = "MAST")

# OPTION B
### Adding raster using addRasterImage
# Load raster and make a color palette
raster_img <- raster(raster_path)
raster_vals <- values(raster_img)
MAST_pal <- colorBin(palette = c("#05475e", "#faff78", "#ff9a00", "#ff3500"),
                     domain = raster_vals,
                     bins = c(5:15), 
                     pretty = TRUE,
                     na.color = "transparent")
# Add raster to map
map %>% addRasterImage(raster_img, colors = MAST_pal, opacity = 1, group = "MAST")

@piersond piersond changed the title addGeotiff unable to to make NA values transparent addGeotiff unable to make NA values transparent Feb 4, 2022
@tim-salabim
Copy link
Member

Thanks for spotting this. Should now work with the latest version from here.

@piersond
Copy link
Author

piersond commented Feb 8, 2022

Thank you for the quick fix! Works great now.

@piersond piersond closed this as completed Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants