[R-sig-Geo] How to quickly add many raster layers to a thick RasterStack?

Ariel Ortiz-Bobea aortizbobea at arec.umd.edu
Wed Nov 2 00:46:19 CET 2011


Hello everyone,

I have about 90,000 individual grid files in GRIB format I'm importing and
converting to raster format in R.

Some of the calculations I need to do (clipping with polygons... thanks
Robert J. Hijmans for valuable advice on this!) are best performed on many
raster layers at once, e.g. over a RasterStack.

I have tried "stack()" and "addLayer()" commands. "stack" seems to read all
objects in the command line slowing the process down more than
proportionately to object size (see code below) which I suppose explains how
"addLayer" came about. However, the time it takes "addLayer" to add an
additional layer is roughly proportional to the size of the RasterStack it
is being added to... so when I have very "thick" stacks/bricks adding an
extra layer takes some time.

Would there be an alternative way to combine many multi-band raster layers
more quickly (in R or outside of R) from many individual files and still
manage to keep things orderly, i.e. being able to identify specific layers
afterwards?

Any thoughts would be greatly appreciated.

Ariel

#---------------
# setup
	library(raster) 
	r <- raster(ncol=139, nrow=97) 
	r[] <- 1:ncell(r)
		
	# stack command
	s1<-r
	t1<- c()
	while (nlayers(s1)<=200) {
		print(nlayers(s1))
		timer1 <- system.time( {	
			s1 <- stack(s1,r) 
			})
			t1 <- rbind(t1,timer1[3] )
		}
	
	# addLayer command
	s2<-r
	t2<- c()
	while (nlayers(s2)<=200) {
		print(nlayers(s2))
		timer2 <- system.time( {	
			s2 <- addLayer(s2,r) 
			})
			t2 <- rbind(t2,timer2[3] )
		}
		
	# visualize
	plot(1:200,t1, type="l", xlab="# of layers in object", ylab="time for
adding extra layer (s)")
	lines(1:200,t2,col="blue")
	
	plot(1:200,t2, type="l", col="blue", xlab="# of layers in object",
ylab="time for adding extra layer (s)")
	


-----
Ariel Ortiz-Bobea
PhD Candidate in Agricultural & Resource Economics
University of Maryland - College Park
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/How-to-quickly-add-many-raster-layers-to-a-thick-RasterStack-tp6953662p6953662.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list