[R-sig-Geo] reading .grd file in R?

Rainer Hurling rhurlin at gwdg.de
Tue Oct 12 22:00:49 CEST 2010


On 12.10.2010 20:36 (UTC+1), Barry Rowlingson wrote:
> Anybody still reading this?

Yes, even though I am not working with grd data, the problem is 
interesting. Instead I am working with .sgrd (SAGA GRID binaries), which 
can be read with rgdal and/or RSAGA.

> I'm sometimes so stuck with sp-objects and rgdal that I forget that
> the easiest way to read raw binary data is with readBin. Wrap it into
> a function, make it a matrix, now I can easily read time slices:
>
> readTime<-
>    function(f,tm,x=69,y=65,size=4,type="double",NAvalue=-999){
>      c = file(f,open="rb")
>      seek(c,x*y*(tm-1)*size)
>      m=matrix(readBin(c,type,x*y,size),x,y)
>      close(c)
>      m[abs(m-NAvalue)<.Machine$double.eps] = NA
>      m
>    }
>
>   par(mfrow=c(2,2))
>   for(p in 1:4){
>   image(readTime("rf0.5_1975.grd",p))
>   }
>
>   I didn't realise it was India because my gdal virtual raster was sideways.

Thanks for showing this way for working with binary data,
Rainer Hurling

> Barry



More information about the R-sig-Geo mailing list