Skip to content
Prev 4483 / 29559 Next

Using RGDAL to "copy" header information...

On Wed, 5 Nov 2008, Jonathan Greenberg wrote:

            
It is the internals of writeGDAL() and create2GDAL() without the bands:

data(meuse.grid)
gridded(meuse.grid) <- ~x+y

d.drv <- new("GDALDriver", "EHdr")

gp <- gridparameters(meuse.grid)
cellsize <- gp$cellsize
offset <- gp$cellcentre.offset
dims <- gp$cells.dim

nbands <- 1
tds.out <- new("GDALTransientDataset", driver = d.drv, rows = dims[2],
   cols = dims[1], bands = nbands, type = "Float32")

gt <- c(offset[1] - 0.5 * cellsize[1], cellsize[1], 0.0,
   offset[2] + (dims[2] -0.5) * cellsize[2], 0.0, -cellsize[2])
.Call("RGDAL_SetGeoTransform", tds.out, gt, PACKAGE = "rgdal")

list.files(tempdir())

fn <- tempfile()

saveDataset(tds.out, fn)

list.files(tempdir())

GDAL.close(tds.out)

list.files(tempdir())

However, the driver does commit the data file too, so you'd need to run 
the above code to produce the header so as not to overwrite your output 
data. The key thing is to get the input grid parameters right, but you've 
got them anyway. If you want the projection information out too, look in 
create2GDAL for the .Call() you need. Note that passing unchecked 
variables through .Call may crash your session - going through 
GridTopology should make sure that they are stored correctly.

Hope this helps,

Roger