# [R-sig-Geo] Converting array into raster brick

Christian Kamenik christian.kamenik at giub.unibe.ch
Thu Dec 23 08:20:56 CET 2010

```Works like a charm - awesome!

Many thanks, Christian

> Christian,
>
> You should also be able to do this:
>
> out<- setValues(x, xx)
>
> (where x is a RasterBrick and xx a matching 3 dimensional array)
>
> Robert
>
> On Wed, Dec 22, 2010 at 3:06 AM, Christian Kamenik
> <christian.kamenik at giub.unibe.ch>  wrote:
>> Dear all,
>>
>> I replaced my layer-by-layer approach
>>
>> for (l in 1:nlayers(x)) x
>> <-setValues(x,values=as.vector(t(xx[,,l])),layer=l)
>>
>> with the new functionality of brick
>>
>> out<-
>> brick(xx,xmn=slot(extent(x),'xmin'),xmx=slot(extent(x),'xmax'),ymn=slot(extent(x),'ymin'),ymx=slot(extent(x),'ymax'),crs=projection(x))
>>
>> where x is the original RasterBrick, xx is a three-dimensional array and out
>> is the new RasterBrick.
>>
>> It worked perfectly fine!
>>
>> Many thanks, Christian
>>
>>> 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
>>>>>>
>>
>> --
>> -----------------------------------------------
>> Oeschger Centre for Climate Change Research, Institute of Geography&
>>   Institute of Plant Sciences
>> University of Bern
>>
>> http://www.stomatocysts.unibe.ch/kamenik
>> mailto: christian.kamenik at giub.unibe.ch
>>
>> Dr. Christian Kamenik
>> Institute of Geography
>> Erlachstrasse 9a, Trakt 3
>> 3012 Bern, Switzerland
>>
>> Tel. +41 (0)31 631 5091
>> Fax +41 (0)31 631 43 38
>>
>>

--
-----------------------------------------------
Oeschger Centre for Climate Change Research, Institute of Geography&  Institute of Plant Sciences
University of Bern

http://www.stomatocysts.unibe.ch/kamenik
mailto: christian.kamenik at giub.unibe.ch