[R-sig-Geo] Writing a tif using rgdal

Roger Bivand Roger.Bivand at nhh.no
Tue Feb 7 20:19:40 CET 2006


On Tue, 7 Feb 2006, Andy Bunn wrote:

> Howdy List:
> 
> I'm reading in a large number of satellite images and analyzing them in R. I
> know how to read a tif into R using rgdal and manipulate it. Is there a good
> way to write a new tif out using the projection data from the original?
> 
> So in the example below how can I write out z as a jpg using the metadata
> and such from x?
> 
> -Andy
> 
> 
> 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)

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

> 
> > version
>          _
> platform i386-pc-mingw32
> arch     i386
> os       mingw32
> system   i386, mingw32
> status
> major    2
> minor    2.0
> year     2005
> month    10
> day      06
> svn rev  35749
> language R
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list