[R-sig-Geo] Prototyping some new features in rasterEngine...
Jonathan Greenberg
jgrn at illinois.edu
Tue Jan 7 16:53:18 CET 2014
Folks:
I released a new beta of spatial.tools today:
install.packages("spatial.tools", repos="http://R-Forge.R-project.org")
There are a few major new features I was hoping to get some eyes on to
work out any bugs prior to a CRAN push:
1) rasterEngine can now process and return > 1 file at a time. The
function should return a list of arrays corresponding to each file
(each array should be the same number of rows and columns) and, if you
want the outputs to be permanent, set the filename parameter to be a
vector of output file names.
2) I'm trying out a (very) experimental feature that
byte-code-compiles your function prior to executing it (call
rasterEngine with compileFunction=TRUE). I'm hoping for longer, more
intensive functions this may shave some time off the execution.
3) I implemented a setMinMax function that hopefully will make those
annoying warnings go away at the end.
4) I implemented Robert Hijmans hdr() function within rasterEngine
(modify with the "additional_header" parameter), and defaulted it to
ENVI. Any output file(s) will now automatically be openable in most
GIS systems. Note the normal "raster" header will also be created.
5) The focal function now has a "chunk" version, where the input to
your function, rather than being a single focal window at a time, is
an "unspooled" array of dimensions:
column,focal_window_length,band
Where the second dimension is the focal window "unravelled" for a
given focal position into a vector starting from the top left, moving
left-to-right, top-to-bottom. Conceptually this is more difficult to
understand, but will allow for more rapid, vectorized processing.
Compare these two (switch the return statements with the commented-out
one to see the multiple-file-output in action):
###
install.packages("spatial.tools", repos="http://R-Forge.R-project.org")
library(spatial.tools)
# sfQuickInit() # Uncomment out to run in parallel
tahoe_highrez <- brick(system.file("external/tahoe_highrez.tif",
package="spatial.tools"))
# Single focal window function:
local_smoother <- function(x,...)
{
# Assumes a 3-d array representing
# a single local window, and return
# a single value or a vector of values.
smoothed <- apply(x,3,mean)
# Uncomment for multiple output files:
# return(list(smoothed,smoothed*100))
return(smoothed)
}
# Apply the function to a 3x3 window:
system.time(
tahoe_3x3_smoothed <-
focal_hpc(x=tahoe_highrez,fun=local_smoother,window_dims=c(3,3))
)
# "Unravelled" focal window function:
local_smoother_fast <- function(x,...)
{
x_dims <- dim(x)
x_dims_out <- c(x_dims[1],1,3)
meanx <- array(colMeans(aperm(x,c(2,1,3))),dim=x_dims_out)
# Uncomment for multiple output files:
# return(list(meanx,meanx*100))
return(meanx)
}
system.time(
tahoe_3x3_smoothed_fast <-
focal_hpc(x=tahoe_highrez,fun=local_smoother_fast,window_dims=c(3,3),processing_unit="chunk")
)
# sfQuickStop() # Uncomment out to run in parallel
###
--
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