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
I have faced up with strange behaviour of the terra package. I was trying to scale DEM values to the maximum height. While the raster package gives the expected results, the terra package gives me an inverted one. The problem seems to be associated with a subtraction syntax. I solved it by multiplying the rast object by (-1). Please take a look at the example below. Is it a bug or a feature? :-)
library(raster) # CRAN v3.6-14
library(terra) # CRAN v1.6-53
# load datasets -----------------------------------------------------------
dem <-
system.file("ex/elev.tif", package="terra")
dem_terra <-
rast(dem)
dem_raster <-
raster(dem)
# helper functions --------------------------------------------------------
# function to compare two rast objects
compare_rasts <-
function(.rast1, .rast2, .rast3){
rbind(
.get_global_stats(.rast1),
.get_global_stats(.rast2),
.get_global_stats(.rast3)
)
}
.get_global_stats <-
function(.rast){
tibble::tibble(
rast = names(.rast),
res = res(.rast)[1],
mean = global(.rast, "mean", na.rm = T)[1,1],
med = global(.rast, function(x) median(x, na.rm = T))[1,1],
sd = global(.rast, function(x) sd(x, na.rm = T))[1,1],
max = global(.rast, function(x) max(x, na.rm = T))[1,1],
min = global(.rast, function(x) min(x, na.rm = T))[1,1],
na = global(.rast, function(x) sum(is.na(x)))[1,1]
)
}
# function to get maximum values
.max_terra <-
function(x){
global(x, function(i) max(i, na.rm = T))[1,1]
}
# scale process -----------------------------------------------------------
# raster approach
norm_raster <- 1 - (dem_raster / (maxValue(dem_raster)))
names(norm_raster) <- "raster"
norm_raster <- rast(norm_raster)
# terra straightforward approach
norm_terra_inverted <- 1 - (dem_terra / (.max_terra(dem_terra)))
names(norm_terra_inverted) <- "terra inverted"
# terra solution...
norm_terra <- 1 + (-1) * (dem_terra / (.max_terra(dem_terra)))
names(norm_terra) <- "terra"
# compare -----------------------------------------------------------------
compare_rasts(norm_terra_inverted, norm_raster, norm_terra)
# A tibble: 3 × 8
rast res mean med sd max min na
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
1 terra inverted 0.00833 -0.363 -0.391 0.147 0 -0.742 3942
2 raster 0.00833 0.363 0.391 0.147 0.742 0 3942
3 terra 0.00833 0.363 0.391 0.147 0.742 0 3942
The text was updated successfully, but these errors were encountered:
I have faced up with strange behaviour of the
terra
package. I was trying to scale DEM values to the maximum height. While theraster
package gives the expected results, theterra
package gives me an inverted one. The problem seems to be associated with a subtraction syntax. I solved it by multiplying therast
object by(-1)
. Please take a look at the example below. Is it a bug or a feature? :-)The text was updated successfully, but these errors were encountered: