Search code examples
rleafletrstudiotiffgeotiff

Create a GeoTiff from leaflet map


I'm trying to create a GeoTiff from a leaflet map. I've tried to find answers to this online but non of them seem to work for me. I'm quite new with using R. The geodata is a 2000*10 matrix where the title, long and latitude is found. Here is my code:

install.packages('leaflet')
install.packages('rgdal')
install.packages('raster')
install.packages('sp')


library(leaflet)
library(raster)
library(rgdal)
library(raster)
library(sp)


sites <- data.frame(Name=(geodata[,2]),Long=(geodata[,10]),Lati=(geodata[,9]))

ma <- leaflet()
ma <- addTiles(ma)
ma <- addMarkers(ma, lng=sites$Long, lat=sites$Lati, popup=sites$Name)
ma

rast <- writeRaster(ma, filename="Worldmap.tif", format="GTiff")

It is the last step "writeRaster" that is not working. The error message that I get looks like this :

Error in (function (classes, fdef, mtable)  : 
unable to find an inherited method for function 'writeRaster' 
for signature '"leaflet", "character"'

Any idea of where the error might be?


Solution

  • Leaflet is based on a JavaScript library and produces dynamic web-maps.

    There is no direct method to save a dynamic map as a static map, like geotiff or png. If you dont have to do it automatically, you can just export the image in RStudio, which gives you the options of saving as PNG, JPEG or BMP.

    Otherwise, there are some workarounds, where you save the leaflet map as an html-file and then scrape the information with the library webshot or mapview for example to create an image file, like png. Webshot and mapview let you save files as PNG,PDF or JPEG.

    library(leaflet)
    rand_lng = function(n = 10) rnorm(n, -93.65, .01)
    rand_lat = function(n = 10) rnorm(n, 42.0285, .01)
    ma <- addMarkers(ma, lng=rand_lng(50), lat=rand_lat(50), popup="a")
    
    ## With webshot and htmlwidgets:    
    library(webshot)
    saveWidget(ma, "temp.html", selfcontained = FALSE)
    webshot("temp.html", file = "Rplot.png", cliprect = "viewport")
    
    ## With mapview
    library(mapview)
    mapshot(ma, file = "Rplot.png", remove_url = TRUE)
    

    Take a look at this aswell.

    If you really need GeoTiff, you would have to load the image in R again, convert it to a raster and then save it as GeoTiff. Here's a short example. You will have to adapt your extent and add a reference system. And maybe control the values of the raster, since im not exactly sure how the image data is loaded into the matrix. It seems that a lot of information gets lost with this conversion.

    library(png)
    library(raster)
    img <- readPNG("pathto/staticImage.png")
    
    img[img==1]=NA
    ar2mat <- matrix(img, nrow = nrow(img), ncol = ncol(img))
    ## Define the extent
    rast = raster(ar2mat, xmn=0, xmx=1, ymn=0, ymx=1)
    ## Define the spatial reference system
    proj4string(rast) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
    
    plot(rast); extent(rast)
    writeRaster(rast, "pathto/png2tif.tif", format="GTiff", overwrite=TRUE)