Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for new spatial data classes #728

Closed
rhijmans opened this issue Mar 9, 2021 · 11 comments
Closed

Support for new spatial data classes #728

rhijmans opened this issue Mar 9, 2021 · 11 comments

Comments

@rhijmans
Copy link
Contributor

rhijmans commented Mar 9, 2021

It would be great if addRasterImage could support for SpatRaster objects from the terra package (a replacement for the raster package). It should perform better as terra::project is much faster than raster::projectRaster. It also should be easy to do, the interface of terra is very similar to that of raster. It would also be great if the leaflet method could support vector data from sf and SpatVector from terra.

@jcheng5
Copy link
Member

jcheng5 commented Mar 9, 2021

Hi @rhijmans! That would be really great indeed.

Can you clarify what you mean by "vector data from sf"? I thought we already had pretty good sf support, though I haven't been paying much attention.

@rhijmans
Copy link
Contributor Author

rhijmans commented Mar 9, 2021

My bad, I missed that as I was reading the docs --- but it is clearly there.

@rhijmans
Copy link
Contributor Author

rhijmans commented Mar 9, 2021

And, by the way, I think you set me on the path towards terra (C++ with some R around it) with your contributions to make raster faster for leaflet.

@gilbertocamara
Copy link

I second the request from @rhijmans: supporting "terra" objects in leaflet would help all those that work with geospatial data.

@AMBarbosa
Copy link

Dear colleagues, any news on implementing "SpatRaster" input for addRasterImage, and "SpatVector" input for addPolygons, addPolylines, etc.? This is becoming more and more urgent, given the evolution in spatial data users. The "SpatVector" implementation could be as simple as coercing to the already-implemented 'sf' format at the beginning of the functions:

if (inherits(data, "SpatVector"))  data <- sf::st_as_sf(data)

I guess the analogous could provisionally be done for "SpatRaster" too (if (inherits(x, "SpatRaster")) x <- raster::raster(x)), though given the differences in performance, it is (speaking from experience) totally worth the trouble to do the opposite, i.e. make the minimal changes in code to accomodate "SpatRaster" instead, and eventually coerce "Raster" inputs to it (if (inherits(x, "RasterLayer")) x <- terra::rast(x)). Thanks and kudos for the great package!

@rhijmans
Copy link
Contributor Author

@jcheng5: I would happily create a PR if you are interested.

@jcheng5
Copy link
Member

jcheng5 commented Jun 29, 2022

@rhijmans That would be fantastic, thank you

@rhijmans
Copy link
Contributor Author

rhijmans commented Jun 29, 2022

With my fork, that you can install with:

remotes::install_github("https://github.com/rhijmans/leaflet")

I can now do

library(terra)
library(leaflet)

# SpatRaster
r <- rast(xmin=-2.8, xmax=-2.79, ymin=54.04, ymax = 54.05, nrow=30, ncol=30, crs="epsg:4326", vals=1:900)
leaflet() |> addTiles() |> addRasterImage(r, colors = "Spectral", opacity = 0.8)

# SpatVector
v <- geodata::gadm("Uganda", level = 1, path =".") |> simplifyGeom(.01) |> makeValid()
leaflet() |> addTiles() |> addPolygons(data = v)
#or
leaflet(v) |> addTiles() |> addPolygons()

I would appreciate it if any of you could test this a bit more, and let me know.

@AMBarbosa
Copy link

I've tested it with a few different 'leaflet' scripts, combining a variety of layers of different classes (including "Spatial*", "SpatVector", "RasterLayer", "SpatRaster"), and it seems to work seamlessly! Also, 'terra' layers plot visibly faster than their "Spatial*" versions (cf. Uganda in the above example). Thank you so much, Robert!

@AMBarbosa
Copy link

Forgot to mention I'm on Linux Mint 19.3 Cinnamon. I also deployed a Shiny app with 'leaflet' maps at https://ambiogeo.shinyapps.io/atlasmampor/, using @rhijmans's fork and a few 'SpatVector' layers, with no problems so far.

@rhijmans
Copy link
Contributor Author

rhijmans commented Jul 3, 2022

This has now been merged into the main branch. A follow-up PR (currently pending, #808) adds support for color-tables and RGB rasters. See my fork at with these examples

library(terra)
library(leaflet)

# RGB
s <- rast(system.file("ex/logo.tif", package="terra"))  
crs(s) <- "+proj=longlat" 
leaflet() |> addTiles() |> addRasterImage(s)

# color table
r <- rast("/vsicurl/https://geodata.ucdavis.edu/test/pr_nlcd.tif")
r[550:750, ] <- NA
leaflet() |> addTiles() |> addRasterImage(r, opacity = 0.75)

# difference with a continuous raster
x <- r * 1
leaflet() |> addTiles() |> addRasterImage(x, opacity = 0.75)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants