Skip to content
Prev 10451 / 29559 Next

Converting array into raster brick

Agus,

I have added an new method for 'brick', included below

library(raster)
# new method, included in raster 1.7-17
setMethod('brick', signature(x='array'),
	function(x, xmn=0, xmx=1, ymn=0, ymx=1, crs=NA) {
		dm <- dim(x)
		if (length(dm) != 3) {
			stop('array has wrong dimensions')
		}
		b <- brick(xmn=xmn, xmx=xmx, ymn=ymn, ymx=ymx, crs=crs)
		dim(b) <- dm
		setValues(b, matrix(sapply(x, as.vector), ncol=dm[3]))
	}
)

r = raster(nrow=5, ncol=5)
r[] = 1:25
s = stack(r,r*2,r*3)

a = as.array(s)
b = brick(a)
# values of b are the same as those of a
sum(getValues(s) != getValues(b))

Best, Robert
On Sun, Dec 19, 2010 at 3:20 AM, Agustin Lobo <alobolistas at gmail.com> wrote: