英文:
Use 'project' function in {terra} raster package, treat NA as 0s
问题
不翻译代码部分,只返回翻译好的内容:
"这个工作正常,但新栅格单元格中的值是 '1',因为它取自高分辨率栅格中所有非NA单元格的平均值。
相反,我希望它将NA值视为0。结果应该是0.25,而不是1。
请问是否有巧妙的方法可以做到这一点,而且必须是内存安全的,因为我将在非常大的栅格上执行此操作(考虑覆盖整个国家的5米分辨率)?"
英文:
I'm trying to resample/project a high res raster, using a low res raster as a template
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")
This works fine, but the value in the cell of the new rast is '1' as it has taken the average of all non-NA cells from the high_res raster.
Instead, I want it to treat NA values as 0. The result should be 0.25, not 1.
The method to do this must be memory-safe as I will be doing this on very large rasters (think 5m resolution covering a whole country).
Is there a clever way of doing this please?
答案1
得分: 1
你可以简单地用0
替换NA
值。
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")
英文:
You can simply replace NA
values with 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")
答案2
得分: 0
以下是您要翻译的代码部分:
根据 Gerald T 的建议,您可以用零替换 `NA`。这里我展示了两种更好的方法(内存安全),比使用 `[<-` 替换函数更好。
library("terra")
high_res <- rast(ncol = 4, nrow = 2, vals = c(1, NA, NA, NA, NA, NA, NA, NA))
low_res <- rast(nrow = 1, ncol = 2)
high_zero <- subst(high_res, NA, 0)
# 或
# high_zero <- ifel(is.na(high_res), 0, high_res)
new_rast <- project(high_zero, low_res, method = "average")
现在,要删除应该为 NA
的单元格中的零,您需要执行以下操作:
isna <- project(high_res, low_res, method = "average")
x <- mask(new_rast, isna)
除非 high_res
中的所有值都是正数,以至于如果总和为零,则知道所有贡献的单元格都是 NA
。在这种情况下,您可以执行以下操作:
x <- subst(new_rast, 0, NA)
(对于这些示例数据,使用聚合将是一个更明显的方法。)
new_rast <- aggregate(high_zero, 2, mean)
英文:
As suggested by Gerald T, you can replace the NA
s with zero. Here I show two approaches for that that are better (memory safe) than using the [<-
replacement function.
library("terra")
high_res <- rast(ncol = 4, nrow = 2, vals = c(1, NA, NA, NA, NA, NA, NA, NA))
low_res <- rast(nrow = 1, ncol = 2)
high_zero <- subst(high_res, NA, 0)
# or
# high_zero <- ifel(is.na(high_res), 0, high_res)
new_rast <- project(high_zero, low_res, method = "average")
Now to remove the zeros from cells that should be NA you would need to do
isna <- project(high_res, low_res, method = "average")
x <- mask(new_rast, isna)
Unless all values in high_res are positive such that if the sum is zero you know that all the contributing cells were NA. In that case you can do
x <- subst(new_rast, 0, NA)
(And with these example data, aggregate would be a more obvious method to use.)
new_rast <- aggregate(high_zero, 2, mean)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论