[R-sig-Geo] Brick and Stack in package raster

Robert J. Hijmans r.hijmans at gmail.com
Fri Nov 26 20:22:19 CET 2010

On Fri, Nov 26, 2010 at 2:25 AM, Agustin Lobo <alobolistas at gmail.com> wrote:
> Robert,
> At some point you made this comment:
>>>> (as you are dealing with large
>>>> files, I think it is more efficient to first make a stack and then
>>>> write the results to a brick)
> But I have some doubts:
> bN = brick(N1,N2,N3,...,N72)
> where
> N1 = raster(paste(midir,"NDV_19990101__Extract.img",sep="")) etc
> takes a very long time. Is a file on disk being written somewhere?

That is why I am saying that it is better to do

sN = stack(N1,N2,N3,...,N72)

and then, perhaps,

bN <- brick(sN )

But that is probably not worth it. I may change a few things in the
code and manual to make this go smoother.
I think the approach is to always makes a stack if you have separate
files and a brick if it is a single file. Adding a layer to a brick
should perhaps result in a stack. Computations on a stack/brick always
result in a brick (single file if on disk)

> Is overlay() going to be any faster with brick than with stack objects? ie:
> bNv2 <- overlay(bN, bSM,
> fun=fun1,filename="bNv2.tif",overwrite=TRUE,NAflag=0) faster than
> sNv2 <- overlay(sN, sSM, fun=fun1,filename="sNv2.tif",overwrite=TRUE,NAflag=0) ?
> (bN, bSM, brick objects; sN, sSM, stack objects)

Yes, I would think that would be faster with a brick, because the data
need to come out of a single object/file (i.e. 72 times fewer files
need to be opened and closed). But I am not sure if it is worth it to
first make a brick from the stack, because that also takes time.

> When you write a brick to a file, can you control if the resulting
> file is going to be BSQ or BIL?

Yes, but only for the native format (not when writing via rgdal, e.g.
GeoTIFF). BIL is default and should work. BIP is rather inefficient
except for extracting point values. BSQ best for extracting single

> Are the files written by writeRaster() identical for stack and brick objects?
> writeRaster(bNv2,
> filename="bNv2b.tif",overwrite=TRUE,format="GTiff",datatype='INT2U',NAflag=0)
> writeRaster(sNv2,
> filename="sNv2b.tif",overwrite=TRUE,format="GTiff",datatype='INT2U',NAflag=0)

Yes, always writes a single file (brick object).

> Thanks
> Agus

Cheers, Robert

More information about the R-sig-Geo mailing list