Skip to content
Prev 7040 / 29559 Next

changing the data type of a gdal dataset

Thanks Robert for your advice.  Due to the details of my situation I 
decided it was easier to tough it out in the rgdal package, but fully 
intend to use the raster package for future projects.  In case anybody 
is interested, I eventually blundered across the answer to my original 
question.  Rather than changing the data type of a copy of a file, I am 
creating a new transient dataset of the desired data type and 
dimensions, then adding spatial reference info gleaned from an existing 
file.  Alan

# get spatial reference info from existing image file
gi <- GDALinfo(infile.name)
dims <- as.vector(gi)[1:2]
ps <- as.vector(gi)[6:7]
ll <- as.vector(gi)[4:5]
pref<-attr(gi,"projection")

# calculate position of upper left corner and get geotransform ala 
http://www.gdal.org/gdal_datamodel.html
ul <- c(ll[1]-ps[1]/2,ll[2]+(dims[1]+.5)*ps[2])
gt<-c(ul[1],ps[1],0,ul[2],0,ps[2])

# create transient dataset to hold output maps, and add spatial 
reference info
tds <- 
new("GDALTransientDataset",new('GDALDriver','GTiff'),rows=dims[1],cols=dims[2],type="Float32")
.Call("RGDAL_SetProject", tds, pref, PACKAGE = "rgdal")
.Call("RGDAL_SetGeoTransform", tds, gt, PACKAGE = "rgdal")
 
# use putRasterData()   in a loop to build the output file

# save the dataset
saveDataset(tds,outfile.name) 
GDAL.close(tds)
Robert J. Hijmans wrote: