I am trying to switch from Robert Hijmans' raster package to the newer and faster terra package for raster calculations in r. In doing so I have come up with a ridiculously simple issue I cannot solve. No matter what I do, I cannot get the new writeRaster function to write a geotiff to file with compression.
Here is a reproduceable example: Let's make 2 rasters from scratch:
A large raster with sequential values
library(terra)
r <- rast(ncol=1000, nrow=1000, xmin=0, xmax=10, ymin=0, ymax=10)
values(r) <- 1:ncell(r)
A large raster with a single value
r2 <- rast(ncol=1000, nrow=1000, xmin=0, xmax=10, ymin=0, ymax=10)
values(r2) <- 1
No matter whether I specify a compression option in the new writeRaster function, the output files result in exactly the same size for the two rasters. Indicating there is no compression being applied.
writeRaster(r2, "D:/simple_raster_test_no_comp_lzw.tif", overwrite=T) #complex raster no compression
writeRaster(r2, "D:/simple_raster_test_comp_lzw.tif", overwrite=T, gdal=c("COMPRESS=LZW"))
writeRaster(r, "D:/test_comp_lzw.tif", overwrite=T) #simple raster no compression
writeRaster(r, "D:/test_comp_lzw2.tif", overwrite=T, gdal=c("COMPRESS=LZW"))
file.size("D:/simple_raster_test_no_comp_lzw.tif")
file.size("D:/simple_raster_test_comp_lzw.tif")
file.size("D:/test_comp_lzw.tif")
file.size("D:/test_comp_lzw2.tif")
I realized the the gdal compression arguments are just not being considered, as I can replace the LZW string for any value and still get a raster output without compression, and without errors.
writeRaster(r, "D:/test_comp_lzw3.tif", overwrite=T, gdal=c("COMPRESS=IAmSuperman"))
file.size("D:/test_comp_lzw3.tif")
Lastly, trying to save using the older raster package syntax yields the same uncompressed results
writeRaster(r, "D:/test_comp_lzw4.tif", overwrite=T, compress="lzw")
file.size("D:/test_comp_lzw4.tif")
Any idea how to overcome this? I work with quite large geotifs and without compression I will stuff my hard drive like a sausage in no time...