[R-sig-Geo] Best raster of raster stack

Jonathan Greenberg jgrn at illinois.edu
Sat Mar 8 21:33:25 CET 2014


Just to throw yet another solution into the pot, here's how to do this
with rasterEngine (which will allow for parallel processing of your
input to potentially speed your process up):

# Individual rasters as inputs approach:
library(spatial.tools)
raster1 <- raster(system.file("external/tahoe_lidar_bareearth.tif",
package="spatial.tools"))
raster2 <- raster(system.file("external/tahoe_lidar_highesthit.tif",
package="spatial.tools"))

sfQuickInit() # Spawns a number of parallel workers = half your CPUs
(you can mod this with the cpus= parameter)
max_of_rasters <- function(raster1,raster2)
{
output <- pmax(raster1,raster2)
dim(output) <- c(dim(raster1)[1:2],1)
return(output)
}

max_of_rasters_output <-
rasterEngine(raster1=raster1,raster2=raster2,fun=max_of_rasters,debugmode=FALSE)

# Combining all the rasters into a single stack approach (probably
easier for your application):
max_of_stack <- function(stack1)
{
output <- apply(stack1,1:2,max)
dim(output) <- c(dim(stack1)[1:2],1)
return(output)
}

stack1 <- stack(raster1,raster2)
max_of_stack_output <-
rasterEngine(stack1=stack1,fun=max_of_stack,debugmode=FALSE)

sfQuickStop() # Turn off the parallel processing engine.

On Sat, Mar 8, 2014 at 10:15 AM, Maurizio Marchi
<mauriziomarchi85 at gmail.com> wrote:
> Tanks a lot for your suggestions. maybe there will be a lot of different
> ways to do that but merging your scripts I found exactly what I wanted to
> do:
>
> # beginning
> library(raster)
> r1 <- raster(matrix(c(1,1,1,2,2,2,4,4,4), ncol = 3))
> r2 <- raster(matrix(c(2,2,2,3,3,3,1,1,1), ncol = 3))
> r3 <- raster(matrix(c(5,5,5,1,1,1,2,2,2), ncol = 3))
> r <- stack(r1,r2,r3)
> plot(r)
> #?calc
> res1<-calc(r,fun=max) # single raster map with maximum values of all 3
> rasters
> plot(res1)
> # end
>
>  Now let's see if my i5 processor is able to process 22 raster maps
> with 77,760,000
> cells each.... :)
>
> thank You very much again!!
>
> --
> Maurizio Marchi, PhD student
> CRA-SEL (Arezzo, Italy)
> www.selvicoltura.eu
> ID skype: maurizioxyz
> *Ubuntu 12.04 LTS*
> *"Il bello dell'open-source è che le domande possono essere poste alla
> fonte"*
> *utente linux 552.742*
>
>         [[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
>



-- 
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
259 Computing Applications Building, MC-150
605 East Springfield Avenue
Champaign, IL  61820-6371
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007



More information about the R-sig-Geo mailing list