[英]Add two layers of a raster stack, store result in third layer using terra
I am trying to use a custom formula on two layers of a raster stack, and update the values in a third layer with the result.我正在尝试在栅格堆栈的两层上使用自定义公式,并使用结果更新第三层中的值。 Here is my attempt (which does not work):这是我的尝试(不起作用):
a <- rast(ncol = 10, nrow = 10)
values(a) <- rep(5,100)
names(a) <- "layer_one"
b <- rast(ncol = 10, nrow = 10)
values(b) <- rep(10,100)
names(b) <- "layer_two"
c <- rast(ncol = 10, nrow = 10)
values(c) <- rep(NA,100)
names(c) <- "layer_three"
z <- c(a,b)
raster_fun <- function(i) {
i[[1]] * i[[2]] + 30/10
}
z[["layer_three"]] <- app(z, raster_fun)
z
class : SpatRaster
dimensions : 10, 10, 2 (nrow, ncol, nlyr)
resolution : 36, 18 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84
sources : memory
memory
names : layer_one, layer_two
min values : 5, 10
max values : 5, 10
That is a bug that has now been fixed in the development version of "terra" (thanks to the report by JimShady).这是一个现已在“terra”开发版本中修复的错误(感谢 JimShady 的报告)。 Here is a work-around这是一个解决方法
z$layer_three <- app(z, raster_fun)
z
#class : SpatRaster
#dimensions : 10, 10, 3 (nrow, ncol, nlyr)
#resolution : 36, 18 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84
#sources : memory
# memory
# memory
#names : layer_one, layer_two, layer_three
#min values : 5, 10, 53
#max values : 5, 10, 53
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.