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"), selfcontainted = F)

It seems all the vertex from all the polygons are put within the html as json. So if one could gather all the duplicated coordinates and replace them by a link to a json, either placed in a separate folder or within the html, that could solve the problem.

0

There are 0 best solutions below