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

Robert J. Hijmans r.hijmans at gmail.com
Thu Jul 21 06:27:41 CEST 2016


Tim, here is an an approach:

library(raster)
b <- brick(system.file("external/rlogo.grd", package="raster"))
p <- SpatialPoints(cbind(sample(77,10), sample(77,10)))

methods <- c("bilinear","simple","bilinear")
k <- methods == "simple"
x1 <- extract(b[[which(k), drop=F]], p)
x2 <- extract(b[[which(!k), drop=F]], p, method='bilinear')
x <- cbind(x1, x2)
x <- x[, names(b)]


Robert

On Wed, Jul 20, 2016 at 11:04 AM, Howard, Tim G (DEC)
<tim.howard at dec.ny.gov> wrote:
> 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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list