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?
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
ormapview
for example to create an image file, like png. Webshot and mapview let you save files as PNG,PDF or JPEG.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.