What is the most efficient way to store a binary terra raster, in terms of file size?

73 views Asked by At

I have a binary raster with only two numeric values. What are the best options for the writeRaster function in order to obtain the smallest possible geotiff file size? I do not need to keep the two values, they can be converted to a TRUE/FALSE boolean, if useful.

Would it be useful to convert one of the two values to NA first? Or to convert the raster to boolean (using the as.bool function) ? Is there a "logical" datatype= option that would optimize file size? I haven't found it in the documentation. Is there a suitable compression option (COMPRESS=) for such data?

Currently, my best solution is as follows:

# build a numeric binary 1000*1000 raster filled with 0 or 10: 
 r <- rast(nrows=1000, ncols=1000,
           vals=ifelse(runif(1000*1000)>0.5,0,10))
 
 writeRaster(r, filename="r_CCITTFAX4.tif", gdal="NBITS=1",
             gdal = "COMPRESS=CCITTFAX4", datatype="INT1U")
 
 # filesize on a Windows 10 system:
 file.info("r_CCITTFAX4.tif")$size
 # [1] 265242

 # it's interesting to note that the initial raster values (0 and 10) are retained, and not replaced by any boolean values.
 rast("r_CCITTFAX4.tif")
 # class       : SpatRaster 
 # dimensions  : 1000, 1000, 1  (nrow, ncol, nlyr)
 # resolution  : 0.36, 0.18  (x, y)
 # extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
 # coord. ref. : lon/lat WGS 84 (EPSG:4326) 
 # source      : test_CCITTFAX4_INT1Ud.tif 
 # color table : 1 
 # name        : lyr.1 
 # min value   :     0 
 # max value   :    10

But can we reduce this file size even further? Or get the same size with simpler options? Thanks for any hints.

Jean-Luc

0

There are 0 answers