Skip to content
Prev 4125 / 29559 Next

Export Matrix to ESRI Grid or tif file

Hi,

here an alternative approach using RSAGA to write ASCII grids:

library(RSAGA)
x <- matrix(rep(seq(11,13,0.5), 5), byrow=T, nrow=5)
y <- matrix(rep(seq(3,5,0.5), 5), byrow=F, nrow=5)
z = 0.0052 + 0.001*x - 0.00023*y + 0.0011*x*y
hdr = list(
    ncols = ncol(z), nrows = nrow(z),
    xllcenter = min(x), yllcenter = min(y),
    cellsize = 0.5, nodata_value = -9999)
write.ascii.grid(x, "x.asc", header = hdr, digits = 1, georef = "center")
write.ascii.grid(y, "y.asc", header = hdr, digits = 1, georef = "center")
write.ascii.grid(z, "z.asc", header = hdr, digits = 6, georef = "center")


-- 
Alexander Brenning
Department of Geography and Environmental Management
University of Waterloo
200 University Ave. W - Waterloo, ON - Canada N2L 3G1
http://www.environment.uwaterloo.ca/geography/faculty/brenning/





----- Urspr?ngliche Mail ----
Von: Roger Bivand <Roger.Bivand at nhh.no>
An: Jean-Paul Kibambe Lubamba <jean-paul.kibambe at uclouvain.be>
CC: r-sig-geo at stat.math.ethz.ch
Gesendet: Montag, den 1. September 2008, 13:50:41 Uhr
Betreff: Re: [R-sig-Geo] Export Matrix to ESRI Grid or tif file
On Mon, 1 Sep 2008, Jean-Paul Kibambe Lubamba wrote:

            
library(sp)
grd <- GridTopology(cellcentre.offset=c(11,3), cellsize=c(0.5, 0.5),
  cells.dim=c(5, 5))
crds <- coordinates(grd)
z <- 0.0052 + 0.001*crds[,1] - 0.0023*crds[,2] + 0.0011*crds[,1]*crds[,2]
SGDF <- SpatialGridDataFrame(grd, data=data.frame(z=z))
spplot(SGDF)
library(rgdal)
writeGDAL(SGDF, "SGDF.tif")

or other drivers for writeGDAL() if you prefer.

Hope this helps,

Roger