rstudio / leaflet

R Interface to Leaflet Maps
http://rstudio.github.io/leaflet/
Other
805 stars 509 forks source link

addRasterImage for maps with other projections #610

Open mtennekes opened 5 years ago

mtennekes commented 5 years ago

To my surprise, it doesn't seem possible to add raster objects to interactive maps with custom map projections, even though the raster object is already in the correct projection. Is that true? If so, I would really like this feature.

To illustrate the application: I want to visualize a raster projected according to EPSG 3035 (the European standard), for which tile server exist.

library(leaflet)
library(sf

# load data
tmpfile <- tempfile(pattern = ".zip")
tmpdir <- tempdir()
download.file("https://github.com/rstudio/leaflet/files/2972282/data.zip", destfile = tmpfile)
unzip(tmpfile, exdir = tmpdir)

load(file.path(tmpdir, "ZL_muni.rda"))
load(file.path(tmpdir, "ZL_elevation.rda"))

With the normal EPSG 4326 projection:

leaflet() %>%
    addTiles() %>%
    addPolygons(data = st_transform(ZL_muni, 4326)) %>%
    addRasterImage(ZL_elevation, project = TRUE)

Screenshot from 2019-03-15 18-56-15

With the EPSG 3035 projection (and another tile server)

crs3035 <- leafletCRS(crsClass = "L.Proj.CRS",
           code='EPSG:3035',
           proj4def="+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs",
           resolutions = 2^(7:0))

leaflet(options = leafletOptions(crs = crs3035)) %>%
    addWMSTiles("https://image.discomap.eea.europa.eu/arcgis/services/GioLandPublic/DEM/MapServer/WmsServer", layers = "Image") %>% 
    addPolygons(data = st_transform(ZL_muni, 4326)) %>%
    addRasterImage(ZL_elevation, project = FALSE)

Screenshot from 2019-03-15 18-57-03

@tim-salabim I noticed mapview uses plainview for this, so I guess you haven't managed to do this with leaflet either.

tim-salabim commented 5 years ago

@mtennekes no I haven't and I don't think its possible at the moment. There is a related discussion in #371

orlandombaa commented 5 years ago

I am also trying to plot rasters with information of kernel distribution , I see some problems with the scale.

I got something like this but from mexico city:

https://rpubs.com/yamano357/90346

and when I want to addProviderTiles to plot another background doesnt work. Always the same background.