[R-sig-Geo] convert multiple dimension array to raster object/.png/.tif

Robert J. Hijmans r.hijmans at gmail.com
Tue Oct 14 00:35:39 CEST 2014


Chane,
You can use   b <- brick(array)

# for example
library(raster)
r <- raster(ncol=3, nrow=3)
r[] <- 1:ncell(r)
s <- stack(r,r*2,r*3)
a <- as.array(s)
brick(a)


Robert

On Mon, Oct 13, 2014 at 3:00 PM, Chane <zoeeeregister at gmail.com> wrote:
> Dear list,
>
> I have a multiple dimension array (9 x 9 x 7) and I would like to convert
> it to a raster object. I want the dim of the raster object to be the same
> as the dim of the array; the values in each pixel in the raster object to
> be the same value as they are in the array. It seems that as.raster()
> function could only convert array with dim of d x d x 3(or4) but can not
> deal with array with dim of d x d x n. Eventually, I would like to export
> this array/raster as a image file (.png or .tif) - with no geographic
> information attached. Any help would be greatly appreciated.
>
> Here is what my array looks like:
>
>> formated_array
> , , 1
>
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
>  [1,]  285  269  326  390  386  301  327  457  559
>  [2,]  714  891  927  890  884  894  809  703  666
>  [3,]  526  289  236  425  430  511  554  611  621
>  [4,]  436  345  530  718  757  729  709  673  703
>  [5,]  774  717  505  261  147  153  667  816  783
>  [6,]  816  778  882  794  439  356  401  358  372
>  [7,]  428  376  388  483  486  326  166  151  216
>  [8,]  886  950 1009  945  876  830  773  596  321
>  [9,]  225  266  270  242  252  276  284  344  304
>
> , , 2
>
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
>  [1,]  285  269  326  390  386  301  327  457  559
>  [2,]  714  891  927  890  884  894  809  703  666
>  [3,]  526  289  236  425  430  511  554  611  621
>  [4,]  436  345  530  718  757  729  709  673  703
>  [5,]  774  717  505  261  147  153  667  816  783
>  [6,]  816  778  882  794  439  356  401  358  372
>  [7,]  428  376  388  483  486  326  166  151  216
>  [8,]  886  950 1009  945  876  830  773  596  321
>  [9,]  225  266  270  242  252  276  284  344  304
>
> , , 3
>
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
>  [1,]  285  269  326  390  386  301  327  457  559
>  [2,]  714  891  927  890  884  894  809  703  666
>  [3,]  526  289  236  425  430  511  554  611  621
>  [4,]  436  345  530  718  757  729  709  673  703
>  [5,]  774  717  505  261  147  153  667  816  783
>  [6,]  816  778  882  794  439  356  401  358  372
>  [7,]  428  376  388  483  486  326  166  151  216
>  [8,]  886  950 1009  945  876  830  773  596  321
>  [9,]  225  266  270  242  252  276  284  344  304
>
> , , 4
>
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
>  [1,]  285  269  326  390  386  301  327  457  559
>  [2,]  714  891  927  890  884  894  809  703  666
>  [3,]  526  289  236  425  430  511  554  611  621
>  [4,]  436  345  530  718  757  729  709  673  703
>  [5,]  774  717  505  261  147  153  667  816  783
>  [6,]  816  778  882  794  439  356  401  358  372
>  [7,]  428  376  388  483  486  326  166  151  216
>  [8,]  886  950 1009  945  876  830  773  596  321
>  [9,]  225  266  270  242  252  276  284  344  304
>
> , , 5
>
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
>  [1,]  285  269  326  390  386  301  327  457  559
>  [2,]  714  891  927  890  884  894  809  703  666
>  [3,]  526  289  236  425  430  511  554  611  621
>  [4,]  436  345  530  718  757  729  709  673  703
>  [5,]  774  717  505  261  147  153  667  816  783
>  [6,]  816  778  882  794  439  356  401  358  372
>  [7,]  428  376  388  483  486  326  166  151  216
>  [8,]  886  950 1009  945  876  830  773  596  321
>  [9,]  225  266  270  242  252  276  284  344  304
>
> , , 6
>
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
>  [1,]  285  269  326  390  386  301  327  457  559
>  [2,]  714  891  927  890  884  894  809  703  666
>  [3,]  526  289  236  425  430  511  554  611  621
>  [4,]  436  345  530  718  757  729  709  673  703
>  [5,]  774  717  505  261  147  153  667  816  783
>  [6,]  816  778  882  794  439  356  401  358  372
>  [7,]  428  376  388  483  486  326  166  151  216
>  [8,]  886  950 1009  945  876  830  773  596  321
>  [9,]  225  266  270  242  252  276  284  344  304
>
> , , 7
>
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
>  [1,]  285  269  326  390  386  301  327  457  559
>  [2,]  714  891  927  890  884  894  809  703  666
>  [3,]  526  289  236  425  430  511  554  611  621
>  [4,]  436  345  530  718  757  729  709  673  703
>  [5,]  774  717  505  261  147  153  667  816  783
>  [6,]  816  778  882  794  439  356  401  358  372
>  [7,]  428  376  388  483  486  326  166  151  216
>  [8,]  886  950 1009  945  876  830  773  596  321
>  [9,]  225  266  270  242  252  276  284  344  304
>
>
> Thanks,
> Chane
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> 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