Skip to content
Prev 778 / 29559 Next

Writing a tif using rgdal

On Tue, 7 Feb 2006, Andy Bunn wrote:

            
Taking a deep breath and remembering to do save.image() frequently 
[overwriting GDAL handles can give seg.faults]:

library(rgdal)
logo <- system.file("pictures/Rlogo.jpg", package="rgdal")[1]
x <- new("GDALReadOnlyDataset", logo)
displayDataset(x)
y <- getRasterData(x)
z <- rowMeans(y, dims=2)
dim(z)
GDAL.close(x)

tif_driver <- new("GDALDriver", "GTiff")
getDriverLongName(tif_driver)
tif2 <- new("GDALTransientDataset", tif_driver, 77, 101, 1, 'Float64')
bnd1 <- putRasterData(tif2, z)
displayDataset(tif2)
tif_file <- tempfile()
saveDataset(tif2, tif_file)
GDAL.close(tif2)
GDAL.close(tif_driver)
tif3 <- GDAL.open(tif_file)
displayDataset(tif3)
GDAL.close(tif3)

$ gdalinfo zlogo.tif
Driver: GTiff/GeoTIFF
Size is 101, 77
Coordinate System is `'
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,   77.0)
Upper Right (  101.0,    0.0)
Lower Right (  101.0,   77.0)
Center      (   50.5,   38.5)
Band 1 Block=101x10 Type=Float64, ColorInterp=Gray

(I was playing with filename zlogo.tif, it reads into GRASS OK, but 
that's using the same GDAL drivers.)

Well, it works, though it's a bit too exciting dodging the seg.faults 
coming from over-writing live object handles while experimenting. For 
determined users, deleteDataset() works too (oh yes!). I'll try to wrap it 
up to be a bit smoother. No world file yet, but I guess that could just be 
copied? Suggestions welcome.

Roger