0

Generating leaflet html's easily results in a 50-75 MB file (depending on the number of layers). However, they can be zipped to reduce the file size by factors. So i guess there is lots of repetitive information in the HTML. The data is the same for most of the layers.

Simplifying the geometries would help, but I don't want to alter them. I further red, that GeoJson plots all the boundaries of polygons, even if they are bordering each other, but I didn't succeed down that line.

Any idea on how to do that? I included the mapview-tag as it seems to me that mapview is more data efficient.

The following code creates a simple map with a number of identical layers. Adding two layers, results in 3.7 MB (zip to 1.4), adding 20 layers (with the same information), results in 32.6 MB (zip to 12.9).

library(leaflet)
library(htmlwidgets)
nrep <- 2
colors <- colorFactor(palette="viridis",
                      domain=gadmCHE@data$NAME_1, na.color="transparent")
mapCH <- gadmCHE %>% leaflet() %>% addTiles() 

for (i in 1:nrep){
  print(i)
  mapCH <- mapCH %>% 
    addPolygons(fillColor = ~colors(NAME_1), fillOpacity = 1,group = as.character(i))
}
mapCH <- mapCH %>% 
    addLayersControl(position = "topleft", 
                     baseGroups = c("OpenStreetMap"),
                     options = layersControlOptions(collapsed=F),
                     overlayGroups=paste(1:nrep)) %>%
   clearControls()%>%
   hideGroup(paste(1:nrep))

saveWidget(mapCH, file = paste0("Test_",nrep,".html"))
Beni
  • 191
  • 9
  • I have two comments: 1) mapview is built on top of leaflet so it is not either / or - but yes, mapview has some optimizations, such as using geobuf 2) the logic of {leaflet} R package does not allow re-using geometry definition between layers. In theory this should be possible in `leaflet.js` directly, but would require you to work in javascript – Jindra Lacko Aug 18 '22 at 17:20
  • thanks! So is mapview re-using the geometry when coding something like `mapview(leaflet::gadmCHE, burst=T)`? I then would need to figure out how to apply different color scales to each layer... or eventually learn javascript. – Beni Aug 19 '22 at 08:42
  • to be honest I don't really know. The optimizations that mapview applies are over & beyond my (very limited) javascript knowledge – Jindra Lacko Aug 19 '22 at 13:20
  • Many thanks, this is already very helpful! I could imagine user:2555983 (TimSalabim) might know more about the internals of mapview ;-) – Beni Aug 19 '22 at 13:43
  • I do, but everything really depends on the concrete use-case. The optimisations mentioned are mostly about the data model used to pass data between R and JS. I have no idea how to re-use geometries. The `add*` functions provided by leaflet do exactly what they say, they add a layer to the map, even if it is already present. I haven't investigated how to avoid data duplication TBH. – TimSalabim Aug 22 '22 at 08:51

0 Answers0