[R-sig-Geo] apply "raster::aggregate" to multi-layer raster stack, using function that processes values from multiple layers to create a single layer?

Robert J. Hijmans r.hijmans at gmail.com
Mon Sep 1 19:56:26 CEST 2014


Aggregate in the development version of raster now has this
functionality (aggregation over space and layers ("time") in one step)
. I would appreciate it if you or others can test it.
install.packages("raster", repos="http://R-Forge.R-project.org")
Robert

On Sat, Jun 21, 2014 at 9:26 AM, Carlos Carroll
<klamathconservation at gmail.com> wrote:
> While mean of calc(y, mean) would allow me to operate on the original stack,
> and then subsequently aggregate the results, what I need to do is operate on
> the entire set of values within the aggregation window (x * y * nlayers) in
> one go, because I am deriving a multivariate distance metric (below).
>
> multidist <- function(x,...)
> {
> footprintsize<-225
> numvars<-3
> meandist <- mean(dist(matrix(x,footprintsize,numvars), method =
> "euclidean"),na.rm=TRUE)
> return(meandist)
> }
>
>
>
> On Sat, Jun 21, 2014 at 4:35 AM, Robert J. Hijmans <r.hijmans at gmail.com>
> wrote:
>>
>> Carlos,
>>
>> How about:
>>
>> x <- mean(y)
>>
>> or
>>
>> x <- calc(y, mean)
>>
>> Robert
>>
>>
>>
>> On Fri, Jun 20, 2014 at 2:50 PM, Carlos Carroll
>> <klamathconservation at gmail.com> wrote:
>> > When I use "aggregate" (package raster) on a multi-layer stack, with a
>> > function such as "mean", it returns a second multi-layer stack at the
>> > aggregated resolution. The function is applied separately to values from
>> > each layer in turn.
>> > What I want to do is send the vector/matrix of values extracted across
>> > all
>> > layers to the user-defined function, which returns a single value that
>> > would then go to form the output, a single layer raster or stack.
>> > I can do this for a focal function using package spatial.tools, e.g.:
>> >
>> > v<-rasterEngine(w,fun=mean,window_dims = c(15,15,3))  ## for a 3 band
>> > stack
>> > with a window size of 15 by 15.
>> >
>> > But I just need the value at the aggregate resolution, so running a
>> > focal
>> > function takes ~200 times longer than necessary in this case.
>> > Is it possible to use raster::aggregate or another function to do a
>> > similar
>> > multi-layer operation?
>> >
>> >         [[alternative HTML version deleted]]
>> >
>> > _______________________________________________
>> > 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