【发布时间】:2014-12-24 14:12:10
【问题描述】:
我有两个想要合并为一个的栅格图层。我们称它们为mask(值1 和NA)和vrs。
library(raster)
mask <- raster(ncol=10, nrow=10)
mask[] <- c(rep(0, 50), rep(1, 50))
mask[mask < 0.5] <- NA
vrs <-raster(ncol=10, nrow=10)
vrs[] <- rpois(100, 2)
vrs[vrs >= 4] <- NA
我希望合并两个大的层,但是为了便于理解,这些小例子是可以的。对于mask 层为1 和vrs 层为NA 的像素,我希望将输出层的像素值设置为零。所有其他像素应保持原始vrs 的值。
这是我唯一的想法:
zero.for.NA <- function(x, y, filename){
out <- raster(y)
if(canProcessInMemory(out, n = 4)) { #wild guess..
val <- getValues(y) #values
NA.pos <- which(is.na(val)) #positiones for all NA-values in values-layer
NA.t.noll.pos<-which(x[NA.pos]==1) #Positions where mask is 1 within the
#vector of positions of NA values in vrs
val[NA.pos[NA.t.noll.pos]] <- 0 #set values layer to 0 where condition met
out <- setValues(out, val)
return(out)
} else { #for large rasters the same thing by chunks
bs <- blockSize(out)
out <- writeStart(out, filename, overwrite=TRUE)
for (i in 1:bs$n) {
v <- getValues(y, row=bs$row[i], nrows=bs$nrows[i])
xv <- getValues(x, row=bs$row[i], nrows=bs$nrows[i])
NA.pos <- which(is.na(v))
NA.t.noll.pos <- which(xv[NA.pos]==1)
v[NA.pos[NA.t.noll.pos]] <- 0
out <- writeValues(out, v, bs$row[i])
}
out <- writeStop(out)
return(out)
}
}
这个功能确实适用于小例子,似乎也适用于更大的例子。有没有更快/更好的方法来做到这一点?某种方式对较大的文件更好?我将不得不在多组图层上使用它,我将不胜感激任何有助于使该过程更安全或更快的帮助!
【问题讨论】: