How to interpolate between rasters?

ε祈祈猫儿з 提交于 2019-12-12 07:16:56

问题


If have three rasters(as matrix):

r1 <- raster(nrows=10, ncols=10); r1 <- setValues(r1, 1:ncell(r1))
r16 <- raster(nrows=10, ncols=10);r16 <- setValues(r16, 1:ncell(r16))
r30 <- raster(nrows=10, ncols=10);r30 <- setValues(r30, 1:ncell(r30))

I would like to linearly interpolate r1,c16,c30 to find the values in between i.e. r2,r3,r4,......r15 then r17,r18,r19,..........r29.

Is this possible using R?


回答1:


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).



来源:https://stackoverflow.com/questions/33127122/how-to-interpolate-between-rasters

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