[R] readGDAL or raster for reading bit map files

Michael Sumner mdsumner at gmail.com
Thu Nov 24 23:33:38 CET 2011


It is by magic, otherwise known as R classes and methods. See

sp:::as.matrix.SpatialGridDataFrame

There's a lot of background here so I suggest you start with the
general R documentation, and the sp package documentation, including

vignette("sp")

and these

citation("sp")

Cheers, Mike.

On Fri, Nov 25, 2011 at 1:49 AM, Alaios <alaios at yahoo.com> wrote:
> Thanks a lot!
> That worked. I have two R related questions
> when I use the
>   store<-readGDAL(fname='lena256.bmp')
> this returns the struct below
>
>> str(store)
> Formal class 'SpatialGridDataFrame' [package "sp"] with 6 slots
>   ..@ data       :'data.frame': 65536 obs. of  1 variable:
>   .. ..$ band1: int [1:65536] 255 255 255 255 255 255 255 255 255 255 ...
>   ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
>   .. .. ..@ cellcentre.offset: Named num [1:2] 0 -722925
>   .. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
>   .. .. ..@ cellsize         : num [1:2] 2835 2835
>   .. .. ..@ cells.dim        : int [1:2] 256 256
>   ..@ grid.index : int(0)
>   ..@ coords     : num [1:2, 1:2] 0 722925 -722925 0
>   .. ..- attr(*, "dimnames")=List of 2
>   .. .. ..$ : NULL
>   .. .. ..$ : chr [1:2] "x" "y"
>   ..@ bbox       : num [1:2, 1:2] -1418 -724342 724342 1418
>   .. ..- attr(*, "dimnames")=List of 2
>   .. .. ..$ : chr [1:2] "x" "y"
>   .. .. ..$ : chr [1:2] "min" "max"
>   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>   .. .. ..@ projargs: chr NA
>
> How the "R" when I run image(store) plots the lena correct. Where it store
> how the file was originally?
> Also when I do
> str(as.matrix(store))
> this takes only the data and transforms them to the original matri
> str(as.matrix(store))
>  int [1:256, 1:256]
> How it does that?
> Looks as "magic" to me....
> B.R
> Alex
> ________________________________
> From: Michael Sumner <mdsumner at gmail.com>
> To: Alaios <alaios at yahoo.com>
> Cc: "R-help at r-project.org" <R-help at r-project.org>
> Sent: Thursday, November 24, 2011 2:25 PM
> Subject: Re: [R] readGDAL or raster for reading bit map files
>
> Just do
>
> m <- as.matrix(store)
>
> or
>
> m <- as.image.SpatialGridDataFrame(store)$z
>
> image(m)
>
> They give the same result with different orientations.  Note that both
> assume a single-band raster, e.g. you only have a "greyscale" bitmap
> (for example).
>
> The details behind all this is given in the documentation for the sp
> package. See ?image for the basic image plot for a matrix.
>
> Cheers, Mike.
>
>
> On Thu, Nov 24, 2011 at 11:50 PM, Alaios <alaios at yahoo.com> wrote:
>> Dear all,
>> I have asked yesterday of how I can read a simple bitmap file in R cran.
>>
>> I was suggest to use either readGDAL or raster for loading my bitmap
>>
>>
>> a. I have done it with readGDAL like
>>
>>     store<-readGDAL(fname='lena256.bmp')
>>     and it works,... but it converts my matrix-like notion of a bitmap to
>> a large vector
>>
>> b. Raster also returns a class that I can not understand.
>>
>> So 1, I want to load it and keep the image as a matrix (so every cell in
>> the matrix will correspond to a pixel)
>> 2. I want to be able to plot the image based on that matrix that I have
>> loaded too.
>>
>> Could you please explain me how I can do those?
>>
>> B.R
>> Alex
>>
>>        [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
>
> --
> Michael Sumner
> Institute for Marine and Antarctic Studies, University of Tasmania
> Hobart, Australia
> e-mail: mdsumner at gmail.com
>
>
>



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-help mailing list