问题
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