[R] importing .bil files

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon Mar 22 19:39:55 CET 2010


On Mon, Mar 22, 2010 at 12:09 PM, Sebastian Leuzinger
<sebastian.leuzinger at env.ethz.ch> wrote:
> Dear list
>
> Has anyone got a recipie at hand to import .bil files into R? From what I
> understand the .bil files I got contain layered matricies which I would lke
> to make available in R as an array or list.
>
> GIS people seem to be familiar with the .bil format but I am not using any
> GIS software and would prefer to deal with the data in R.
>
> I use the latest version of R on Mac OSX  10.5.8.

 There's another complication that might make things more complicated,
or, pervesely, make it simpler...

 .bil files should come with a .hdr file. The .bil is just the NxMxZ
raw data. No definition of the structure or coordinates at all - it
could be NxMxZ or ZxNxM or even any numbers with the right product.
All that info is held in the accompanying .hdr file. If you cant get
the .hdr file and don't know the structure and it isn't a product of
three primes times 4, 8, or 16, then you are possibly in trouble...

 So without a .hdr and with dimension known you can try using R's
binary connection functions to read in the raw bytes and whack up an
array of the right dimension. See ?readBin.

 Or you can create a .hdr file yourself. They are plain text and quite
descriptive - here's one I made earlier:

BYTEORDER      I
LAYOUT         BIL
NROWS          22
NCOLS          20
NBANDS         1
NBITS          32
BANDROWBYTES   80
TOTALROWBYTES  80
PIXELTYPE      FLOAT
ULXMAP         22.7946212255725
ULYMAP         5.45149245118748
XDIM           0.333594138544999
YDIM           0.333594138544999

this is for a single layer 22x20 grid of floating point numbers. You
can use writeGDAL with the EHdr driver to create these things (and the
.bil files) to see what it should be, or read a spec somewhere...

How's that?

Barry



More information about the R-help mailing list