[R-sig-Geo] overlay with 2 bricks?

Agustin Lobo alobolistas at gmail.com
Tue Jul 26 17:27:16 CEST 2011


I'm trying to combine 2 operations into one to speed up a process.
It's simple calculating a median raster across layers of a brick object, but
the values that must be considered NA are defined by a second brick.
Normally, we first set the NA in the brick and then calculate the median:

fun1 <- function(x,y) { x[y!=248 & y!=232] <- NA; return(x)}
decadaN_v2 <- overlay(decadaN, decadaS, fun=fun1, overwrite=TRUE)
a <- overlay(decadaN_v2, fun=median, na.rm=T, overwrite=TRUE)

But this is too slow (we actually try to do this for 36 bricks within
a loop). So I tried:

mimedian <- function(x,y) {
 x[y!=248 & y!=232] <- NA
 median(x,na.rm=T)
}

And then try to use overlay only once:

a <- overlay(decadaN, decadaS, fun=mimedian, na.rm=T, overwrite=TRUE)

but raster complains that the function is not vectorized.
Is there any way around? I suspect the problem is that overlay works
across layers if one brick is provided,
but not if 2 bricks are provided. In other words, we want x in
mimedian to be a vector of 1 pixel across layers but
actually is a brick object.

Agus



More information about the R-sig-Geo mailing list