r-spatial / leafem

leaflet extensions for mapview
https://r-spatial.github.io/leafem/
Other
108 stars 30 forks source link

Could addRasterRGB support SpatRaster class object #68

Closed tytony closed 5 months ago

tytony commented 1 year ago

Hi everyone,

The raster package announced it has been superseded by the "terra" package. https://cran.r-project.org/web/packages/raster/index.html

Could addRasterRGB function support object in terra package in future? Such as SpatRaster created by terra::rast() .

Thank you.

trafficonese commented 6 months ago

I thought this would be quite easy to implement, but apparently terra reprojects the data differently and changes the resolution and dimension of the data set (raster doesnt change those).

Using the plainview::poppendorf dataset, the original Raster looks like this (SpatRaster has the same dimensions and resolution):

class      : RasterBrick 
dimensions : 167, 166, 27722, 5  (nrow, ncol, ncell, nlayers)
resolution : 30, 30  (x, y)
extent     : 655905, 660885, 5508915, 5513925  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : memory
names      : B001n, B002n, B003n, B004n, B005n 
min values :  8921,  7983,  6894,  6154,  5954 
max values : 14123, 14060, 14390, 15080, 24972 

but after leaflet is reprojecting a SpatRaster, the data looks like this

class       : SpatRaster 
dimensions  : 172, 171, 1  (nrow, ncol, nlyr)
resolution  : 46.39897, 46.39897  (x, y)
extent      : 1242617, 1250552, 6396416, 6404396  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / Pseudo-Mercator 
source(s)   : memory
name        :     B004n 
min value   :  6166.153 

and we get the following error which fails here as the length of raster values and colors doesnt match anymore. :

Error in dim(tileData) <- c(4, ncol(x), nrow(x)) : dims [product 117648] do not match the length of object [110888]

Solutions