[R-sig-Geo] calc, overlay and raster stacks

Agustin Lobo alobolistas at gmail.com
Fri Nov 19 15:50:11 CET 2010


Hi!

I have to set as NA data in a 3D array (time series of images)
according to values in another 3D array of Quality flags

#Example
NDVI = round(array(rnorm(5*5*3,180,10),dim=c(5,5,3)))
SM = array(rep(248,5*5*3),dim=c(5,5,3))
SM[4,3:4,1:2] = 232
SM[2,3:4,1:2] = 243
SM[3,4,1:2] = 243

#The only valid data are the ones for which SM is either 232 or 248, so:
NDVI[SM!=248] <- NA
NDVI[SM!=232] <- NA
#which is the same as
NDVI[SM!=248 & SM!=232] <- NA

#The problem is that what I really have are images 3697 columns x
1905 rows and 360 Bands (and even larger) , so I try package raster

require(rgdal)
require(raster)

#First, I make fake data:
rNDVI = stack(raster(NDVI[,,1]),raster(NDVI[,,2]),raster(NDVI[,,2]))
rSM = stack(raster(SM[,,1]),raster(SM[,,2]),raster(SM[,,2]))

fun <- function(x,y) { x[y!=248 & y!=232] <- NA; return(x)}
> rc <- overlay(rNDVI, rSM, fun)
Error in function (classes, fdef, mtable)  :
  unable to find an inherited method for function "overlay", for
signature "RasterStack", "RasterStack"
Calls: overlay -> <Anonymous>

#It seems that calc() must be used for stacks, but seems to me that calc()
#only accepts one stack, not two. Maybe I should just stack both NDVI and SM?

Thanks!
Agus



More information about the R-sig-Geo mailing list