[R-sig-Geo] raster: extract with different methods (simple, bilinear) among layers in brick?

Howard, Tim G (DEC) tim.howard at dec.ny.gov
Wed Jul 20 20:04:40 CEST 2016


Hi all,
I have a raster brick for which I'd like to extract data from each layer to a point dataset, but I'd like to use method="simple" for some of the layers and method="bilinear" for others. 

It looks like the call to extract won't take a vector for method like this:

x <- extract(myBrick, mySpatialPoints, method=c("simple","bilinear","bilinear"))

my ugly hack is to loop it like this:
    
listOfMethods <- c("simple","bilinear","bilinear")

for(k in 1:nrow(listOfMethods)){
      x <- extract(myBrick [[k]], mySpatialPoints, method= listOfMethods [[k]], sp=TRUE)
      if(k == 1){
        y <- x
      } else {
        layerName <- names(x at data)[ncol(x at data)]
        y at data <- cbind(y at data, x at data[,layerName])
        names(y at data)[length(names(y at data))] <- layerName
      }
    }


Any suggestions for a better approach?  

Aside: Note the myBrick[[k]] syntax (which is pretty fast!). I spent a while trying to use layer=1, nl=1 in the call like this:

x <- extract(myBrick, mySpatialPoints, layer=k, nl=1, method= listOfMethods [[k]], sp=TRUE)

but that consistently resulted in this error:

Error in cvv[cells, ] : incorrect number of dimensions

Thanks in advance, 
Tim



More information about the R-sig-Geo mailing list