titeuf
titeuf

Reputation: 163

How to set boundaries with leaflet and export to pdf with mapshot?

I have been trying for the whole day now to create a map an export it to a pdf. The problem is that I don't really understand how to either set "width" and "height" or use "fitBounds". Also I would like to crop this "Leaflet" logo at the bottom right. I tried to do a random reproducable example where I would like to only crop the blue rectangle. Any ideas?



remove(list = ls())
library(leaflet)
library(sf)
library(mapview)


### create random sf polygon
pol = st_sfc(st_polygon(list(cbind(c(0,3,3,0,0),c(0,0,3,3,0)))))
h = st_sf(r = 5, pol)


### create "map"
df <- leaflet(width = 1000, height = 1000) %>%  
  #fitBounds(100, 100, 100, 100) %>% 
  addPolygons(data = h,
              color = "blue",
              weight = 1,
              opacity = 1,
              fillOpacity = 1) %>% 
  setMapWidgetStyle(style = list(background = "transparent"))

### show map and export
df
mapshot(df, file = "leaflet.pdf")


enter image description here

Upvotes: 1

Views: 881

Answers (1)

Benson_YoureFired
Benson_YoureFired

Reputation: 434

I ran into the same problem. I fixed it by exporting the map to png instead of pdf. Then you can use the setView(lat = , lon = , zoom = ) to set the desired viewport with and then export the map.

leaflet_map %>% 
        setView(lat = 46.60, lng = 3.13, zoom = 6.1) %>%
        mapshot(
          file = "leaflet_map.png",
          useragent = 'Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0'
        )

Upvotes: 0

Related Questions