2

I'm using QGIS 2.14 and GDAL 1.11.3 and I'm running into a weird error with the clipped outputs. I created a shapefile in QGIS and used it to clip my image. Here's a simplified version of the command the Clipper plugin generates:

gdalwarp -q 
         -cutline ${TESTDIR}/shapefile.shp 
         -crop_to_cutline 
         -tr 5.0 5.0 
         -of GTiff 
         ${TESTDIR}/${INFILE}.TIF 
         ${TESTDIR}/${INFILE}_sub.tif

My results are below. The shapefile is displayed in the dotted red line, with the clip overlaid on the original. Notice the grid offset produced by gdalwarp. I've also run this on lower resolultion Landsat8 data with similar results (the ratio of the offset error is the same, i.e. ~20% in x, ~5% in y). This issue QGIS clipper problem when mask used is similar except I am already using the -tr option. Anyone know what's going on? Is this a GDAL bug or a QGIS?

Misalignment in clipped raster image

1

Unless your cutline lines up perfectly with cell edges, the output raster will always be shifted/resampled to align with the polygon when that option is selected. If you leave the -crop_to_cutline option out, cells outside the masking layer just get converted to no-data in the output instead of being removed. I'm sure there's a way to get rid of those no-data cells in a subsequent step, but I tend to just do my raster work in GRASS instead, and avoid the problem entirely.

  • Accepted this answer over @xunilk because this can be implemented with the Clipper plugin in QGIS. – tenfishsticks Jul 7 '16 at 17:49
2

It is preferable to use gdal_translate command. The syntax is:

gdal_translate -projwin xmin ymax xmax ymin input_raster output_raster

where -projwin parameter guarantees complete alignment.

To test my approach I used the raster and the clipper polygon of next image; where it can be observed that there is a complete misalignment between both layers. From into red rectangle area of Layer Properties Window, for clipper polygon, I got xmin ymax xmax ymin parameters.

enter image description here

In my case, the complete command was:

gdal_translate -projwin 355144.88 4473174.81 355826.88 4472712.24 /home/zeito/pyqgis_data/aleatorio.tif /home/zeito/pyqgis_data/my_output.tif

After running it in my bash console (Windows users can use OSGeo Console), I got:

enter image description here

where it can be observed a perfect alignment.

  • can you please tell us which version of GDAL you used to get this effect? I've had zero luck with 2.0.1 in a windows environment, the projwin tag always shifts the output cells a little. – obrl_soil Jul 26 '16 at 12:22
  • It is gdal 1.10.1 in an stable Debian system. – xunilk Jul 26 '16 at 13:51
  • thanks. I tried the same command in 1.11 and it worked perfectly, so the newer version's been broken. Infuriating! – obrl_soil Jul 26 '16 at 22:30
  • The change in the latest versions of GDAL is intended. See this discussion: osgeo-org.1560.x6.nabble.com/… It might be nice if QGIS could implement a 'snap to pixels' checkbox for example, to allow the old behavior again. – Rutger Kassies Jul 28 '16 at 8:01

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.