r-spatial / leafsync

Small Multiples for Leaflet Webmaps
Other
36 stars 2 forks source link

leafsync strange behaviour - duplicate maps when syncing two different maps #7

Open manomuthus opened 9 months ago

manomuthus commented 9 months ago

I have a bizarre thing going on with leafsync::sync. I have two maps generated using mapview. The underlying data is in sf format

map1 image

map2 image

As you can see they are two different maps with different legends. But when I try to put them together using leafsync::sync the first map is duplicated instead of the second map (the legends are different though).

leafsync::sync(map1,map2) produces the following map image

and leafsync::sync(map2,map1) produces the following map image

This has never happened before and I am creating some other simple maps using leafsync::sync and they are absolutely fine. I have tried reinstalling leaflet and leafsync and it didn't help. This issue is driving me crazy. Any idea why this is happening?

tim-salabim commented 9 months ago

Can you show the code? Might have to do with layer names?

manomuthus commented 9 months ago
#function to generate a mapview map
generate_map <- function(haz_list) {

 #for dummy map layer for legend
  dummy_map <- haz_list %>% last() %>% dplyr::select(haz, cell) %>% st_rasterize() %>% rast() %>% raster()
  dummy_map$haz <- -0.01

 #range for legend
  haz_range <- haz_list %>%
    sapply(function (x) range(x %>% pull(haz), na.rm = T)) %>%
    c() %>%
    range

  mapview_pal <- RColorBrewer::brewer.pal(9,"Blues")
  hazard_map <- mapview(haz_list, zcol = "haz",col.regions = mapview_pal, na.color = "transparent", layer.name = paste(c(10, 20, 50, 100, 200, 500), "yr"), legend = F, homebutton = FALSE, alpha = 0, alpha.regions = 1, label = rep("haz", 6))+
    mapview(dummy_map, col.regions = mapview_pal, at = seq(haz_range[1], haz_range[2], length.out = 11), na.color = "transparent", layer.name = "haz", legend = T, homebutton = FALSE)
  return(hazard_map)
}

#generate maps for two different data sets
map1 <- generate_map(haz_list_1)
map2 <- generate_map(haz_list_2)
map1 #produces map1 shown above
map2 #produces map2 shown above

leafsync::sync(map1, map2) #produces duplicate maps

haz_list_1and haz_list_2 can be downloaded from here

manomuthus commented 9 months ago

Okay. As you mentioned it was due to layer names. I was using the same layer names inside map1 and map2 and it caused all the issues. Changing the layer names has fixed the issue. It would be better to mention this in the documentation.