Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writeRaster output file size

I have a function that reads a multi-band image in as a raster brick object, iterates through the bands doing various calculations, and then writes the raster out as a new .tif. All of this works fine, but the file size of the new image file is roughly four times greater (I assume because the original image has 4 bands). I'm wondering if there's a parameter in the writeRaster() function that I'm unaware of, or if there's some other way I can ensure that the output image is basically the same file size as the input.

Original file size is 134 MB; output ranges from 471 to 530 MB or so, depending on format.

Simplified code:

library(rgdal)
library(raster)

path = "/Volumes/ENVI Standard Files/"
img = "qb_tile.img"

imageCorrection = function(path, img){
  raster = brick(paste0(path, img))   
  raster = reclassify(raster, cbind(0, NA))  

  for(i in 1:nlayers(raster)){   
    raster[[i]] = raster[[i]] - minValue(raster[[i]]) 
  }
  writeRaster(raster, paste0(path,img,"_process.tif"), format = "GTiff", overwrite=TRUE)
}
like image 867
Danple Avatar asked Feb 04 '17 14:02

Danple


1 Answers

You can set the default datatype for writing rasters with the rasterOptions() as follows:

rasterOptions(datatype="INT2U")

Or directly in the writeRaster call:

writeRaster(yourRas, "path/to/raster/", dataType="INT2U", options="COMPRESS=LZW")

Also notice the options argument where you can specify compression.

Usually when I export integer rasters from R, I make sure that I really have integers and not floats, since this can result in an empty raster. Try the following before exporting:

ras <- as.integer(ras)

Please note: Also check for negative values in your raster. Try INT2S if you have values below zero.

like image 78
maRtin Avatar answered Sep 28 '22 06:09

maRtin