如何用 R 中另一个栅格的值替换大型栅格中的 NA?

How to replace NA in a large raster with values from another raster in R?

我有两张大光栅图像 (30,000 x 30,000),一张用于 2000 年,另一张用于 2005 年。我想用 2005 年光栅中各自的非 NA 值替换 2000 年光栅中具有 NA 的像素。这是一个例子:

r1 <- raster(ncols=36, nrows=18)
r1[] <- 1:ncell(r1)
plot(r1)
r2 <- raster(ncols=36, nrows=18)
r2[] <- 1:ncell(r2)
r2[r2<300] <- NA
plot(r2)

在这个例子中,我想用 "r1" 中的相应值替换 "r2" 中的 NA。我在 R 中尝试了命令 "cover()" 但那个命令没有完成 "r2," 而是它 returns "r2" 中缺少但存在于 "r1" 中的信息:

r2.fix <- cover(r2, r1)
plot(r2.fix)

对如何完成这项工作有什么想法吗?提前谢谢你。

更新:此解决方案的问题:

r2[is.na(r2)] <- r1[is.na(r2)]

是它不适用于大栅格。 R returns 出现以下错误:

Error: cannot allocate vector of size 2.6 Gb
In addition: Warning messages:
1: In order(cells[, 2]) :
  Reached total allocation of 16080Mb: see help(memory.size)

我只能用大光栅重现你的问题。
我有 错误:无法分配大小为 6.7 Gb 的矢量 和 16GB RAM。
您需要在 Session options 中更改 chunksize

From Introduction to the ’raster’ package

The options chunksize and maxmemory determine the maximum size (in number of cells) of a single chunk of values that is read/written in chunk-by-chunk processing of very large files.