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

Howard, Tim G (DEC) tim.howard at dec.ny.gov
Thu Jul 21 17:34:09 CEST 2016


Robert,
Very slick. Thank you!  I had thought about splitting by method and running only twice, but hadn't come up with an approach yet. 

Perfect. 
Thanks again,
Tim

> -----Original Message-----
> From: Robert J. Hijmans [mailto:r.hijmans at gmail.com]
> Sent: Thursday, July 21, 2016 12:28 AM
> To: Howard, Tim G (DEC) <tim.howard at dec.ny.gov>
> Cc: r-sig-geo at r-project.org
> Subject: Re: [R-sig-Geo] raster: extract with different methods (simple,
> bilinear) among layers in brick?
> 
> ATTENTION: This email came from an external source. Do not open
> attachments or click on links from unknown senders or unexpected emails.
> 
> 
> 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