使用{terra}栅格包中的’project’函数,将NA视为0。

huangapple go评论135阅读模式
英文:

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(&quot;terra&quot;)

high_res &lt;- rast(ncol = 2, nrow = 2, vals = c(1, NA, NA, NA))

high_res[is.na(high_res)] &lt;- 0

low_res &lt;- rast(nrow = 1, ncol = 1)

new_rast &lt;- project(high_res, low_res, method = &quot;average&quot;)

答案2

得分: 0

以下是您要翻译的代码部分:

根据 Gerald T 的建议,您可以用零替换 `NA`。这里我展示了两种更好的方法(内存安全),比使用 `[&lt;-` 替换函数更好。

library(&quot;terra&quot;)
high_res &lt;- rast(ncol = 4, nrow = 2, vals = c(1, NA, NA, NA, NA, NA, NA, NA))
low_res &lt;- rast(nrow = 1, ncol = 2)

high_zero &lt;- subst(high_res, NA, 0)
# 或
# high_zero &lt;- ifel(is.na(high_res), 0, high_res)

new_rast &lt;- project(high_zero, low_res, method = &quot;average&quot;)

现在,要删除应该为 NA 的单元格中的零,您需要执行以下操作:

isna &lt;- project(high_res, low_res, method = &quot;average&quot;)
x &lt;- mask(new_rast, isna)

除非 high_res 中的所有值都是正数,以至于如果总和为零,则知道所有贡献的单元格都是 NA。在这种情况下,您可以执行以下操作:

x &lt;- subst(new_rast, 0, NA)

(对于这些示例数据,使用聚合将是一个更明显的方法。)

new_rast &lt;- aggregate(high_zero, 2, mean)
英文:

As suggested by Gerald T, you can replace the NAs with zero. Here I show two approaches for that that are better (memory safe) than using the [&lt;- replacement function.

library(&quot;terra&quot;)
high_res &lt;- rast(ncol = 4, nrow = 2, vals = c(1, NA, NA, NA, NA, NA, NA, NA))
low_res &lt;- rast(nrow = 1, ncol = 2)

high_zero &lt;- subst(high_res, NA, 0)
# or 
# high_zero &lt;- ifel(is.na(high_res), 0, high_res)

new_rast &lt;- project(high_zero, low_res, method = &quot;average&quot;)

Now to remove the zeros from cells that should be NA you would need to do

isna &lt;- project(high_res, low_res, method = &quot;average&quot;)
x &lt;- 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 &lt;- subst(new_rast, 0, NA)

(And with these example data, aggregate would be a more obvious method to use.)

new_rast &lt;- aggregate(high_zero, 2, mean)

huangapple
  • 本文由 发表于 2023年3月7日 00:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定