我正在尝试使用低分辨率栅格作为模板重新采样/投影高分辨率栅格
library("terra")
high_res <- rast(ncol = 2, nrow = 2, vals = c(1, NA, NA, NA))
low_res <- rast(nrow = 1, ncol = 1)
new_rast <- project(high_res, low_res, method = "average")
这很好用,但是新栅格的单元格中的值为“1”,因为它取了高分辨率栅格中所有非 NA 单元格的平均值。
相反,我希望它将 NA 值视为 0。结果应该是 0.25,而不是 1。
执行此操作的方法必须是内存安全的,因为我将在非常大的栅格上执行此操作(想想覆盖整个国家的 5m 分辨率)。
请问有什么巧妙的方法吗?
您可以简单地将
NA
值替换为 0s
library("terra")
high_res <- rast(ncol = 2, nrow = 2, vals = c(1, NA, NA, NA))
high_res[is.na(high_res)] <- 0
low_res <- rast(nrow = 1, ncol = 1)
new_rast <- project(high_res, low_res, method = "average")
正如 Gerald T 所建议的,您可以将
NA
替换为零。在这里,我展示了两种比使用 [<-
替换功能更好(内存安全)的方法。
此外,对于这些示例数据,聚合将是一种更明显的使用方法。
library("terra")
high_res <- rast(ncol = 2, nrow = 2, vals = c(1, NA, NA, NA))
high_zero <- subst(high_res, NA, 0)
# or
# high_zero <- ifel(is.na(high_res), 0, high_res)
new_rast <- aggregate(high_zero, 2, mean)