Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom size for addEasyprint with leaflet.extras2

I'm creating some html files for colleagues where they can explore a leaflet map interactively, then save a copy for presentations.

Unfortunately the downloads from addEasyprint in leaflet.extras2 are fairly low resolution. I've been unable to figure out how to configure a custom size. The documentation hints at custom sizes (say, width = 3000 pixels by height = 1800 pixels), but I can't quite get there - myself or trying with AI! What could I add below to define a custom size?

library(dplyr)
library(sf)
library(leaflet)
library(leaflet.extras2)

check <- st_as_sf(tibble(latitude = c(48, 48.00001, 49), 
                         longitude = c(-89, -89.0001, -90)), 
                  coords = c("longitude", "latitude"),
                  crs = "+proj=longlat +datum=WGS84 +no_defs")

check_bounds <- c(-87, 47, -91, 50)
leaflet() %>%
  fitBounds(check_bounds[1], check_bounds[2], 
            check_bounds[3], check_bounds[4]) %>%
  addTiles() %>%
  addMarkers(data = check) %>% 
  addProviderTiles("Esri.WorldTopoMap",group = "Topographic") %>%
  addProviderTiles("Esri.WorldImagery", group = "Imagery") %>%
  addLayersControl(
    baseGroups = c("Topographic", "Imagery"),
    options = layersControlOptions(collapsed = FALSE)
  ) |> 
  addEasyprint(options = easyprintOptions(
    title = "Save map to PNG",
    position = "topleft",
    filename = "check",
    hideControlContainer = FALSE, 
    sizeModes = c("CurrentSize"),
    exportOnly = TRUE
  ))
like image 364
Nova Avatar asked Dec 14 '25 21:12

Nova


1 Answers

Not an expert but I found this github issue, where the author of the package has created a new branch allowing "dpi" to be specified. If you install this updated version of the package using the remotes package, does it solve your problem?

E.g.

library(dplyr)
library(sf)
library(leaflet)
options(timeout = 600)
remotes::install_github("https://github.com/trafficonese/leaflet.extras2/tree/print_dpi")
library(leaflet.extras2)

check <- st_as_sf(tibble(latitude = c(48, 48.00001, 49), 
                         longitude = c(-89, -89.0001, -90)), 
                  coords = c("longitude", "latitude"),
                  crs = "+proj=longlat +datum=WGS84 +no_defs")

check_bounds <- c(-87, 47, -91, 50)
leaflet() %>%
  fitBounds(check_bounds[1], check_bounds[2], 
            check_bounds[3], check_bounds[4]) %>%
  addTiles() %>%
  addMarkers(data = check) %>% 
  addProviderTiles("Esri.WorldTopoMap",group = "Topographic") %>%
  addProviderTiles("Esri.WorldImagery", group = "Imagery") %>%
  addLayersControl(
    baseGroups = c("Topographic", "Imagery"),
    options = layersControlOptions(collapsed = FALSE)
  ) |> 
  addEasyprint(options = easyprintOptions(
    title = "Save map to PNG",
    position = "topleft",
    filename = "check",
    hideControlContainer = FALSE, 
    sizeModes = list("Custom Landscape"=list(
      width= 3000,
      height= 1600,
      name = "A custom landscape size tooltip",
      className= 'customCssClass')),
    exportOnly = TRUE,
    dpi = 600
  ))

The resulting png is larger, so perhaps the resolution is better? Maybe you can tweak the 'custom size' and dpi to get the output you want?

like image 181
jared_mamrot Avatar answered Dec 17 '25 16:12

jared_mamrot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!