[R-sig-Geo] Converting array into raster brick
Robert J. Hijmans
r.hijmans at gmail.com
Sun Dec 19 17:50:50 CET 2010
updated function, without redundant sapply. R
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(as.vector(x), ncol=dm[3]))
}
)
On Sun, Dec 19, 2010 at 8:06 AM, Robert J. Hijmans <r.hijmans at gmail.com> wrote:
> 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:
>> Robert,
>>
>> Converting from a regular 3D array to brick is probably going to be
>> quite a common operation,
>> perhaps you could consider a simpler syntax in the future, i,e:
>>
>> r = as.brick(x)
>>
>> where x would be a 3D array?
>>
>> Agus
>>
>> 2010/12/16 Robert J. Hijmans <r.hijmans at gmail.com>:
>>> Christian,
>>>
>>> Could be quicker if you make a matrix out of the array (or, if
>>> possible, avoid the array in the first place). See below
>>>
>>> library(raster)
>>>
>>> # create an array
>>> m <- matrix(1:100, ncol=10, byrow=TRUE)
>>> a <- array(list(m, m, m, m, m))
>>>
>>> # empty RasterBrick
>>> x <- brick(ncol=10, nrow=10)
>>>
>>> # make matrix from array
>>> v <- sapply(a, function(x) as.vector(t(x)))
>>>
>>> x <- setValues(x, v)
>>> plot(x)
>>>
>>>
>>> Best, Robert
>>>
>>> On Thu, Dec 16, 2010 at 12:26 PM, <christian.kamenik at giub.unibe.ch> wrote:
>>>> Dear all,
>>>>
>>>> I needed to convert an array into a RasterBrick object. The output did not
>>>> need to be written to memory, as assessed by canProcessInMemory(). The only
>>>> way to put the data into the RasterBrick that worked for me was layer by
>>>> layer:
>>>>
>>>> for (l in 1:nlayers(x)) x
>>>> <-setValues(x,values=as.vector(t(xx[,,l])),layer=l)
>>>>
>>>> This was quite complicated and slow. So I am wondering if there is a better
>>>> way of doing this.
>>>>
>>>> Many thanks, Christian
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at r-project.org
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>
More information about the R-sig-Geo
mailing list