Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: leaflet: use pane with addRasterImage

Tags:

r

leaflet

leaflet for R has now the option to use addMapPaneto stack leaflet objects in a determined order. This works fine for vector data but using it with addRasterImage returns an error message. Is there a special options to use a pane with rasters?

library(leaflet)
library(raster)

r <- raster(ncol=1000, nrow=1000)
r[] <- runif(ncell(r),0,1)
extent(r) <- matrix(c(172, -37, 175, -38), nrow=2)
crs(r)<-sp::CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
pal <- colorNumeric(c("#0C2C84", "#41B6C4", "#FFFFCC"), values(r),
                    na.color = "transparent")


m <- leaflet(m) %>% addTiles() %>% 
  addMapPane("baseMap", zIndex = 410) %>% 
  addMapPane("baseSat", zIndex = 420) %>% 
  addMapPane("data", zIndex = 425) %>% 
  addMapPane("r", zIndex = 430) %>% 
  addProviderTiles(providers$Esri.WorldImagery, group = "Satellite imagery",
                   options = pathOptions(pane = "baseSat")) %>% 
  addProviderTiles(providers$Esri.WorldTerrain,
                   options = pathOptions(pane = "baseMap")) %>% 
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R", 
             options = pathOptions(pane = "data")) %>% 
  addRasterImage(r, colors = pal, opacity = 0.8, project = FALSE, 
                 options = pathOptions(pane = "r"))

Error in addRasterImage(., r, colors = pal, opacity = 0.8, project = FALSE,  : 
  unused argument (options = pathOptions(pane = "r"))
like image 340
YGS Avatar asked Nov 16 '22 15:11

YGS


1 Answers

Very late to answer the question but I had the same problem and found a solution.

There is a not-yet-merged pull request on GitHub for leaflet which allows for the options argument in addRasterImage(). Currently, you can install this version of leaflet from here. To ensure that I could access this later or make changes, I downloaded this entire repo into a new folder, then you can open it in RStudio and run: devtools::install().

EDIT: The faster way to get a quick install now (without downloading a local copy of the package itself) is to run:

remotes::install_github("rstudio/leaflet", ref="joe/feature/raster-options")

Once this is done, the code that you already have should work fine. I have been using addRasterImage(..., options=leafletOptions(pane="r")) and I get the desired result.

like image 106
Rex Parsons Avatar answered Dec 24 '22 10:12

Rex Parsons