[R-sig-Geo] Writing a byte image

Ned Horning horning at amnh.org
Thu Feb 26 15:15:29 CET 2009

What is the recommended way to output a large byte GeoTiff image one 
line at a time? I am creating large double and integer images using 
writeRaster to create a GeoTiff file. I've been converting these to a 
byte data type outside of R but it would be nice to R to output a byte 
GeoTiff directly. I see there is a "type" flag for writeGDAL but I 
haven't been able to tell if I can use that for line-by-line writing.

This is what I'm using now (thanks to feedback from Robert Hijmans and 
possible unnecessary complications by me):
# Set output image properties
predrast <- setRaster(spot)
filename(predrast) <- outImage
nameVec <- colnames(trainvals)
cname <- nameVec[2]
for (n in 2:numbands) {
  cname <- c(cname, nameVec[n+1])
# Use the randomForest object to predict land cover type based on the input
# image one line at a time
for (r in 1:nrow(spot)) {
  spot <- readRow(spot, r)
  rowvals <- values(spot, names=TRUE)
  colnames(rowvals) <- cname
  pred <- predict(randfor, rowvals)
  pred <- as.numeric(pred)
  predrast <- setValues(predrast, pred, r)
  predrast <- writeRaster(predrast, format='GTiff', overwrite=TRUE)


More information about the R-sig-Geo mailing list