Alwayslearning
Alwayslearning

Reputation: 35

Using mapshot() to save mapview(), data layer doesn't show when saving PNG

This has to be an easy fix but this is my first time using mapview and mapshot and I'm stuck. I've investigated other postings and the documentation but I can't see a source.

I've created an object with mapview. I can view the map ok and I can export and save successfully from the viewer. However, I'm trying to save from code using mapshot.

This works great with an html output but not for the png. No error messages show and the file saves in the right place absolutely fine. It's just that the saved PNG file shows a map and no data.

library(openair)
library(dplyr)
library(mapview)

sites <- importMeta(source = "saqn", all = FALSE)

edinburgh_sites <- sites %>% filter(substr(site, 1, 9) == "Edinburgh")
glimpse(edinburgh_sites)

Rows: 14 Columns: 5 $ site "Edinburgh Centre", "Edinburgh Currie", "Edinbur… $ code "ED", "ED11", "ED10", "ED5", "ED6", "ED2", "EDNS… $ latitude 55.95197, 55.89691, 55.93903, 55.93771, 55.94583… $ longitude -3.195775, -3.319660, -3.392727, -3.232267, -3.2… $ site_type "Urban Background", "Suburban Background", "Urba…

edinburgh_sites_map = mapview(edinburgh_sites, xcol = "longitude", ycol = "latitude", crs = 4326, grid = FALSE)
edinburgh_sites_map
mapshot(edinburgh_sites_map, 
        file = paste0(getwd(), "/Figures/edin_sites_map.png"))

In the viewer (and when exported from viewer): enter image description here

When saved using mapshot() as a png: enter image description here

Upvotes: 2

Views: 1921

Answers (1)

dimfalk
dimfalk

Reputation: 1423

Since I was facing the same issue myself and found this post, I'd like to draw a little conclusion (as of today) from the options suggested in the comments.

Basically, mapview::mapshot() as well as mapview::mapshot2() did not produce the desired result in my case, but mapviewOptions(fgb = FALSE) did the trick before calling mapview():

library(openair)
library(dplyr)
library(mapview)

sites <- importMeta(source = "saqn", all = FALSE)

edinburgh_sites <- sites |> filter(substr(site, 1, 9) == "Edinburgh")

glimpse(edinburgh_sites)
#> Rows: 14
#> Columns: 6
#> $ source    <chr> "saqn", "saqn", "saqn", "saqn", "saqn", "saqn", "saqn", "saq…
#> $ site      <chr> "Edinburgh Centre", "Edinburgh Currie", "Edinburgh Glasgow R…
#> $ code      <chr> "ED", "ED11", "ED10", "ED5", "ED6", "ED2", "EDNS", "ED7", "E…
#> $ latitude  <dbl> 55.95197, 55.89691, 55.93903, 55.93771, 55.94583, 55.94427, …
#> $ longitude <dbl> -3.195775, -3.319660, -3.392727, -3.232267, -3.220556, -3.19…
#> $ site_type <chr> "Urban Background", "Suburban Background", "Urban Traffic", …
# make rendered html self-contained
mapviewOptions(fgb = FALSE)

edinburgh_sites_map <- mapview(edinburgh_sites, 
                               xcol = "longitude", 
                               ycol = "latitude", 
                               crs = 4326,
                               grid = FALSE)

edinburgh_sites_map

mapshot(edinburgh_sites_map, 
        file = "edin_sites_map.png")

Mapshot Output

Upvotes: 0

Related Questions