Home > database >  How to successfully save leaflet map in a .png image?
How to successfully save leaflet map in a .png image?

Time:06-11

Out of geocoordinates, I want to draw simple plain points on a city map and save the output as a .png. I'm on Linux Ubuntu 22.04LTS. For the first part I found a pretty brief and straightforward leaflet solution.

library(leaflet); library(htmlwidgets); library(webshot)

m <- leaflet() %>% 
  addTiles("https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png") %>%
  setView(-.12, 51.50, zoom=13)
m$x$options <- append(m$x$options, list("zoomControl" = FALSE))
m <- m %>%
  addCircleMarkers(c(-.11, -.12, -.13), c(51.48, 51.52, 51.50),
                   radius=1, color="red")
m

enter image description here

However, the second part, saving the .png fails. I tried this solution,

saveWidget(m, "temp.html", selfcontained=TRUE)
webshot("temp.html", file="Rplot.png", cliprect="viewport")

but - using phantomjs 2.1.1 - it gives me these errors:

Auto configuration failed
139740646049728:error:25066067:DSO support routines:DLFCN_LOAD:could not load the shared library:dso_dlfcn.c:185:filename(libproviders.so): libproviders.so: cannot open shared object file: No such file or directory
139740646049728:error:25070067:DSO support routines:DSO_load:could not load the shared library:dso_lib.c:244:
139740646049728:error:0E07506E:configuration file routines:MODULE_LOAD_DSO:error loading dso:conf_mod.c:285:module=providers, path=providers
139740646049728:error:0E076071:configuration file routines:MODULE_RUN:unknown module name:conf_mod.c:222:module=providers
Error in webshot("temp.html", file = "Rplot.png", cliprect = "viewport") : 
  webshot.js returned failure value: 1
In addition: Warning message:
In is.null(x) || is.na(x) : 'length(x) = 4 > 1' in coercion to 'logical(1)'

Using slightly older version phantomjs 1.9.8 gives no error, but the image is completely blank. I used these sources to install phantomjs on Ubuntu: 1.9.8, 2.1.1. I looked into this answer, according to which you have to install the dev version, but I didn't find it useful, because I don't know of a dev version of phantomjs.

I don't necessarily need a interactive leaflet, but I thought the dark theme looks cool and corresponds to the research topic of nocturnal crimes. Thus, I'm openish to a static solution if it looks similar (if it won't use Google).

CodePudding user response:

Use the new package webshot2, which uses the Chrome or Chromium headless browser instead of phantomjs.

  • Related