How to interpolate between rasters?

房东的猫 提交于 2019-11-30 16:10:13
Robert Hijmans

Here is a way to do that

library(raster)
r <- raster(nrows=10, ncols=10); 
values(r) <- NA

x <- sapply(1:30, function(...) r)
x[[1]] <- setValues(r, runif(ncell(r)))
x[[16]] <- setValues(r, runif(ncell(r))) + 10
x[[30]] <- setValues(r, runif(ncell(r))) + 20

s <- stack(x)

z <- approxNA(s)

plot(z)
plot(1:30, z[1])

Here is another way to do it

library(raster)
r <- raster(nrows=10, ncols=10); 
x1 <- setValues(r, runif(ncell(r)))
x16 <- setValues(r, runif(ncell(r))) + 10
x30 <- setValues(r, runif(ncell(r))) + 20

s <- stack(x1, x16, x30)
x <- calc(s, fun=function(y) approx(c(1,16,30), y, 1:30)$y)

But this will fail if there are NA values in the three layers. You would need to adjust the function fun to deal with that (here is an example).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!