[R-sig-Geo] Landcover grd/gri Diva files in R

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Sun May 20 20:57:02 CEST 2012


On Sun, May 20, 2012 at 12:22 PM, Agustin Lobo <alobolistas at gmail.com> wrote:
> Hi!
>
> I read LC grd files (from the DIVA server) into R through raster(),
> but as far as I  can tell the
> landcover table (in the gri file) is not imported. I would like to use
> rsgdf < as(r,SpatialGridDataFrame)
>
> but rsgdf at data has just the column of LC numeric codes, not the LC definitions-
>
> Is there a function to read the gri file or should I make my own?

 Looking at the size of  the files, the .gri file contains the values
and the .grd contains the meta-data. The raster() function doesn't
care if you specify the .gri or the .grd, somehow it works out the
file format and gets the info from both of them. However, as you say,
its not reading in the labels...

 The .grd file is a text file in Windows .ini file format, and the
labels you want are in the [Legend] section:

[Legend]
Count=22
Color1=16384
Min1=1
Max1=1
Label1=Tree Cover, broadleaved, evergreen
Color2=32768
Min2=2
Max2=2
Label2=Tree Cover, broadleaved, deciduous, closed
Color3=32768
Min3=3
Max3=3

 etc...

Shouldn't be too hard to read that in with readLines, look for the
[Legend] section, and then get out the labels.

Someone had a quick hack at reading ini files here:

https://stat.ethz.ch/pipermail/r-help/2007-June/134055.html

but it fails on your example because of some spaces somewhere.

Don't readLines the .gri file! :)

Barryy



More information about the R-sig-Geo mailing list