Leaflet: addRasterImage not working in conjunction with addLayersControl

Created on 27 Jul 2017  路  3Comments  路  Source: rstudio/leaflet

When I use addLayersControl() to allow users to choose between different base maps, rasters added with addRasterImage() aren't shown. The following code reproduces this issue for me. When I remove the addLayersControl() call, the raster displays on the map as desired, but with it the raster doesn't show up.

library(leaflet)
library(raster)

r <- raster(xmn=-2.8, xmx=-2.79, ymn=54.04, ymx=54.05, nrows=30, ncols=30)
values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
crs(r) <- CRS("+init=epsg:4326")

leaflet() %>% 
  addTiles(group = "Map") %>%
  addProviderTiles("Esri.WorldImagery", group = "Satellite") %>% 
  addLayersControl(
    baseGroups = c("Map", "Satellite"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  addRasterImage(r)

Most helpful comment

@mstrimas it works if you change the order of the call so that addLayersControl gets added last.

library(leaflet)
library(raster)

r <- raster(xmn=-2.8, xmx=-2.79, ymn=54.04, ymx=54.05, nrows=30, ncols=30)
values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
crs(r) <- CRS("+init=epsg:4326")

leaflet() %>% 
  addTiles(group = "Map") %>%
  addProviderTiles("Esri.WorldImagery", group = "Satellite") %>%
  addRasterImage(r, group = "Raster") %>% 
  addLayersControl(
    baseGroups = c("Map", "Satellite"),
    overlayGroups = c("Raster"),
    options = layersControlOptions(collapsed = FALSE)
  ) 

All 3 comments

Give a group name to the raster layer too and specify it as an overlay group in addLayersControl

Thanks, @bhaskarvk, I did try that, but still having the same problem:

library(leaflet)
library(raster)

r <- raster(xmn=-2.8, xmx=-2.79, ymn=54.04, ymx=54.05, nrows=30, ncols=30)
values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
crs(r) <- CRS("+init=epsg:4326")

leaflet() %>% 
  addTiles(group = "Map") %>%
  addProviderTiles("Esri.WorldImagery", group = "Satellite") %>% 
  addLayersControl(
    baseGroups = c("Map", "Satellite"),
    overlayGroups = c("Raster"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  addRasterImage(r, group = "Raster")

@mstrimas it works if you change the order of the call so that addLayersControl gets added last.

library(leaflet)
library(raster)

r <- raster(xmn=-2.8, xmx=-2.79, ymn=54.04, ymx=54.05, nrows=30, ncols=30)
values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
crs(r) <- CRS("+init=epsg:4326")

leaflet() %>% 
  addTiles(group = "Map") %>%
  addProviderTiles("Esri.WorldImagery", group = "Satellite") %>%
  addRasterImage(r, group = "Raster") %>% 
  addLayersControl(
    baseGroups = c("Map", "Satellite"),
    overlayGroups = c("Raster"),
    options = layersControlOptions(collapsed = FALSE)
  ) 
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Make42 picture Make42  路  7Comments

simon-tarr picture simon-tarr  路  4Comments

kent37 picture kent37  路  3Comments

okjed picture okjed  路  6Comments

tim-salabim picture tim-salabim  路  4Comments