0
votes

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:

  1. 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)

  2. 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...

1
Nevermind! updated package (terra version 1.1.4) now works!Lucas Fortini

1 Answers

1
votes

If you find a bug in a package, then the best place to report it would be on github or such (here for the terra package.

I only saw your comment after writing the answer; so for posterity, below I show that compression works; at least in the current version on CRAN and in the development version.

library(terra)
#terra version 1.1.4

r <- rast(ncol=1000, nrow=1000, xmin=0, xmax=10, ymin=0, ymax=10)
values(r) <- sqrt(1:ncell(r))

ff <- replicate(4, paste0(tempfile(), ".tif"))

writeRaster(r, ff[1], overwrite=T) #complex raster no compression
writeRaster(r, ff[2],  gdal=c("COMPRESS=LZW"))
writeRaster(r>100, ff[3]) #simple raster no compression
writeRaster(r>100, ff[4],   gdal=c("COMPRESS=LZW"))

sapply(ff, file.size, USE.NAMES=FALSE)
#[1] 4003716 2886677 4003712  146519