02 Apr

Extract raster values in parallel

Dear fellow Remote Senseis,

Here is a code chunk that will help you speed up the processing time when you want to extract raster values at specific locations.
It uses the a package called snowfall which allows parallel processing.
As usual, please find here under a dummy example. As there is only 3 bands it the stack, you may not see the positive effect of parallel computing…try with a 100-layer stack and you’ll see.

Cheers

install.packages("snowfall")
 
library(snowfall)
library(raster)
 
ncpus <- 3
 
r <- (stack(system.file("external/rlogo.grd", package="raster")))
inSlist <- unstack(r) # convert to list of single raster layers
 
rId <- subset(r,1)
idPix <- which(rId[]>200)
 
sfInit(parallel=TRUE,cpus=ncpus)
sfLibrary(raster)
sfLibrary(rgdal)
a <- sfSapply(inSlist,extract,y=idPix)
sfStop()

2 thoughts on “Extract raster values in parallel

Leave a Reply

Your email address will not be published. Required fields are marked *