2

I have a bunch of georeferenced TIFF images created in QGIS. The person creating them saved them as uncompressed (around 1 GB each). I would like to compress them as LZW to save space.

How can I do that in a programmatic way (e.g. batch)?

I am open to QGIS or R approches. Please consider I do not know how Georeferencing information are attached to an image (e.g. I have seen somewhere references to a world file, but I do not know what it is).

  • 2
    TIFF files can be georeferenced in one of two ways: (1) Georeferencing information is embedded in the TIFF file as part of the metadata, so you only have one file, with the extension .tif or .tiff. (2) Georeferencing information is written in a second, ("sidecar") file with the extension .tfw, alongside the TIFF file. If you want more information about how georeferencing info is attached to a tiff file, read this Wikipedia article. – csk Apr 15 at 16:01
4

You can do this using the command line tool gdal_translate. This is available for Windows, Linux, and Mac OSs (you don't state your OS).

Running:

gdalinfo none.tif

will show the info on the file, including the compression type and the locations:

Driver: GTiff/GeoTIFF
Files: none.tif
Size is 204, 228
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (96.546435160745574,17.535114346013060)
Pixel Size = (0.004491576420598,-0.004491576420598)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (  96.5464352,  17.5351143) ( 96d32'47.17"E, 17d32' 6.41"N)
Lower Left  (  96.5464352,  16.5110349) ( 96d32'47.17"E, 16d30'39.73"N)
Upper Right (  97.4627168,  17.5351143) ( 97d27'45.78"E, 17d32' 6.41"N)
Lower Right (  97.4627168,  16.5110349) ( 97d27'45.78"E, 16d30'39.73"N)
Center      (  97.0045760,  17.0230746) ( 97d 0'16.47"E, 17d 1'23.07"N)
[etc]

it doesn't mention a compression type, because it doesn't have one. If your GeoTIFFs aren't compressed they should also not say anything. Note the spatial information is stored in geoTIFF chunks and is output as a bounding box corner set. Great.

Let's compress it. Uncompressed file is 4.7Mb:

$ ls -hs none.tif 
4.7M none.tif

Run this:

$ gdal_translate none.tif lzw.tif -co COMPRESS=LZW
Input file size is 204, 228
0...10...20...30...40...50...60...70...80...90...100 - done.

and get

$ ls -hs lzw.tif 
1.6M lzw.tif

1.6Mbytes in LZW compressed form. Again gdalinfo shows:

$ gdalinfo lzw.tif 
Driver: GTiff/GeoTIFF
Files: lzw.tif
Size is 204, 228
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (96.546435160745574,17.535114346013060)
Pixel Size = (0.004491576420598,-0.004491576420598)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  COMPRESSION=LZW
  INTERLEAVE=PIXEL
Corner Coordinates:
[etc]

Note the COMPRESSION=LZW message.

Loop over your files using your command line interpreter loop functions.

You might also be able to do this via the gdalUtils package in R which will run these command line commands via a shell.

Indeed for my test file:

library(gdalUtils)
gdal_translate(
  src_dataset="none.tif",
  dst_dataset="lzwR.tif",
  co="COMPRESS=LZW")

results in a byte-for-byte identical output file.

  • 1
    some other -co to consider and offers additional compression (lossless) are -mo "INTERLEAVE=PIXEL" -co "INTERLEAVE=PIXEL" -co "TILED=YES" -co "COMPRESS=LZW" -co "PREDICTOR=2" – SaultDon Apr 15 at 18:34
3

In addition to @Spacedman's answer, you can set up a loop in R to compress tiffs using LZW with the writeRaster function in the raster package, which still uses GDAL. The options argument allows you to apply LZW compression.

In this example, the file is not overwritten but rather has an "_LZW" appended to the original name. To just overwrite the original file you can omit the paste0 function in writeRaster and just use the file iterator (ie., rfiles[i]). You could also paste a directory path into the file name to aim the compressed files to a different directory. If the tif files are multi-band then you would use stack rather than raster to read the data.

library(raster)
setwd("C:/...")
rfiles <- list.files(getwd(), "tif$")
  for(i in 1:length(rfiles)) {
    r <- raster::raster(rfiles[i])
    raster::writeRaster(r, paste0(gsub(pattern = "\\.tif$", "", 
                        rfiles[i]), "_LZW", ".tif"), overwrite=TRUE,  
                        options="COMPRESS=LZW")
}
  • Thanks Jeffrey Evans and @Spacedman. Just for clarity. I assume that an image georeferenced in qgis and saved as .tif is a geoTif. is this correct? if not... will the solutions you both proposed work anyway? – Filippo Apr 15 at 18:03
  • I'd be concerned that this would read in the entire +1Gb files. Might not be too efficient. The GDAL command line routines are pretty well optimised. – Spacedman Apr 15 at 18:25
  • A "geoTIFF" is a TIFF with special metadata blocks in it that give it a spatial reference. Rasters saved from QGIS should have this data and so be valid geoTIFFs - check with the gdalinfo command line or gdalUtils package. – Spacedman Apr 15 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.