You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One interesting observation is that arithmetic operations (e.g. computing NDVI) are faster for vector/matrices than for raster objects (stars or SpatRaster) in R. At this moment, this is especially true for the terra package, where it is probably better to convert the raster to a matrix, perform calculations, and then assign the results back to the raster object.
library(stars)
library(terra)
ndvi=function(red, nir) {(nir-red) / (nir+red)}
rasters= list.files("data/LC08_L1TP_190024_20200418_20200822_02_T1/",
pattern="\\.TIF$", full.names=TRUE)
### calculate NDVI using 'stars' objectsras= read_stars(rasters, along=3, proxy=FALSE)
red= adrop(ras[,,,6])
nir= adrop(ras[,,,7])
system.time(ndvi(red, nir))
#> user system elapsed#> 0.756 0.711 1.467### calculate NDVI using 'SpatRaster' objectsras= rast(rasters)
ras=ras*1red=ras[[6]]
nir=ras[[7]]
system.time(ndvi(red, nir))
#> user system elapsed#> 4.141 3.320 7.462### calculate NDVI using vectorsvred= values(red, mat=FALSE)
vnir= values(nir, mat=FALSE)
system.time(ndvi(vred, vnir))
#> user system elapsed#> 0.558 0.209 0.767### calculate NDVI using vectors### and assign values to new 'SpatRaster'new= rast(red)
system.time(new<- setValues(new, ndvi(vred, vnir)))
#> user system elapsed#> 0.989 0.768 1.758
The text was updated successfully, but these errors were encountered:
One interesting observation is that arithmetic operations (e.g. computing NDVI) are faster for vector/matrices than for raster objects (
stars
orSpatRaster
) in R. At this moment, this is especially true for theterra
package, where it is probably better to convert the raster to a matrix, perform calculations, and then assign the results back to the raster object.The text was updated successfully, but these errors were encountered: