12

Working with QGIS 2.4.0 Chugiak I have a question regarding the clipping of raster images. I have an aerial picture that I want to clip using the extents of a shapefile. So far so good, I used the raster/extraction/clipper tool to do this. The problem is that the file size of my output raster is nearly three times larger than the original file (i.e. 725.283 and 249.693 kb respectively), whereas it covers a smaller surface.

Why is my file larger? And is there any way to 'set' the output file size?

The help function of clipper refers to the following page: http://www.gdal.org/gdal_translate.html Though I cannot make much out of it.

I am relatively new to QGIS.

5
  • Usually it means that the original is compressed but the clipped tiff is uncompressed. You must read about the GDAL GeoTIFF driver and compression options.
    – user30184
    Commented Sep 26, 2015 at 11:59
  • If you want to use compression on your output dataset you have to edit the command, as shown at the bottom of the clipper interface, and incorporate the creation options with the -co flag. For a GeoTiff you could for example include -co compress=lzw to employ lossless LZW compression. We would need to know more about your dataset though to be able to help you (for instance via gdlainfo).
    – Kersten
    Commented Sep 26, 2015 at 12:18
  • Uncompressed TIFF images are usually faster.
    – Zoltan
    Commented Sep 26, 2015 at 14:35
  • Thanks to all for the replies. This may indeed be a compression problem, I had not thought of that. Yes Kersten I did notice the console in the clipper interface, though I have not figured out how to use this. I am trying to run gdalinfo from the Python console in QGIS but I have no experience with this. Does anyone know of a proper tutorial to familiarize myself? Cheers
    – Mathijs
    Commented Sep 26, 2015 at 15:39
  • You can go to Raster > Miscellaneous > Information to have gdalinfo run on your raster file. You can't run it directly form the Python console because it isn't a Python script.
    – bosth
    Commented Sep 26, 2015 at 18:33

2 Answers 2

7

When you run the raster clip tool in QGIS, it shows you the exact GDAL command that will be run in the bottom section of the dialog box.

Raster clip tool

By clicking the pen button to the right of the command, you can edit it directly and add in a compression configuration option. In my example, this is the starting command:

gdalwarp -q -cutline /tmp/mask.shp -crop_to_cutline -of GTiff /tmp/453A.tif /tmp/output.tif

You can change this to be:

gdalwarp -q -cutline /tmp/mask.shp -crop_to_cutline -of GTiff -co "COMPRESS=LZW" /tmp/453A.tif /tmp/output.tif

The extra -co "COMPRESS=LZW" is just one of many possible compression options you can use with GeoTIFF files. The GDAL documentation has more information on all the options.

7
  • Many thanks, that was exactly what I needed. The file is still 409.019 kb, so nearly double as big as the original, but rendering is much faster. That website I had seen but I could not understand much of it. Gdalinfo showed that the compression was indeed 'ticked off' for the first clip, so this was indeed the problem.
    – Mathijs
    Commented Sep 28, 2015 at 8:24
  • When you run gdalinfo, what does it report for compression? We should be able to replicate it in the second file once we know. My guess is that it might be lossy-JPEG if it is considerably smaller than LZW.
    – bosth
    Commented Sep 29, 2015 at 1:15
  • This is what it says for the original image: 'Pixel Size = (0.000738433000001,-0.000738432999949) Metadata: AREA_OR_POINT=Area Image Structure Metadata: COMPRESSION=LZW INTERLEAVE=PIXEL' and this is what it says for the clipped image: 'Pixel Size = (0.000738423955088,-0.000738437979722) Metadata: AREA_OR_POINT=Area Image Structure Metadata: COMPRESSION=LZW INTERLEAVE=PIXEL'
    – Mathijs
    Commented Sep 29, 2015 at 19:16
  • Is it possible you accidentally got the metadata for the same image twice since both are the same?
    – bosth
    Commented Sep 29, 2015 at 19:25
  • They are not, look at the pixel size ;)
    – Mathijs
    Commented Sep 29, 2015 at 19:30
3

Update for QGIS versions 3 and up: the compression option is now integrated in the Raster -> Extraction -> Clip raster by mask layer plugin, and in most other plugins which produce raster files.

Under Advanced parameters -> Profile, selecting either Low compression or High compression will yield a smaller file.

These compression options are lossless, but you can choose also lossy options:

  • Low compression -> -co PACKBITS (lossless)
  • High compression -> -co DEFLATE (lossless)
  • JPEG compression -> -co JPEG_QUALITY=75 (lossy: beware!)

Clip raster by mask layer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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