I am using QGIS tool "Clip raster by mask layer" in Processing Toolbox for clipping satellite(georeferenced) image raster using shape polygon with "clip to mask layer's extent" setting switched on.

I observed that the output clipped raster's pixels have an offset of around 0.5 pixel size with respect to source image.

Apparently there is a mistake in the algorithm of this tool especially while setting the Geotransform (which decides the positioning) of the output clipped raster.

How can I access the algorithm of this tool?


Found that the pycode ClipByMask.py is apparently (correct me if needed) not the code I am looking for, since it returns argument that calls other GDAL code. Below is the line that shows it:

return ['gdalwarp', GdalUtils.escapeAndJoin(arguments)]

Also observed that the pycode ClipByMask.py has following imports:

from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm

from processing.algs.gdal.GdalUtils import GdalUtils

I was looking for the lines which contain geotransform[0] and geotransform[3] which define the positioning of the image top-left corner, but only found:


above lines are related to only pixel width and height.

I am in search of the GDAL code that has code snippets that involve positioning the raster and has functions to do the raster clipping operation.

1 Answer 1

  • If you installed QGIS using the OSGeo4W Network Installer:


  • If you installed QGIS using the Standalone Installer (e.g. QGIS 2.18):

    C:/Program Files/QGIS 2.18/apps/qgis/python/plugins/processing/algs/gdal/ClipByMask.py
  • 1
    Thank you. After reading the pyscript of "ClipByMask.py", I realize that it deals only with creating the argument which is in the format required to be given as input to some other GDAL algorithm. I couldn't trace the source GDAL algorithm that actually contains the code snippets to clip the raster. I am editing the question accordingly.. Commented Dec 6, 2016 at 12:12
  • @SaiKrishna - Apologies, hopefully others can advise on where that hidden code is =)
    – Joseph
    Commented Dec 7, 2016 at 9:26
  • Looking forward! Commented Dec 7, 2016 at 13:27
  • Unfortunately the problem is still present. To correct it, I use the script here => gis.stackexchange.com/questions/80774/… given by maria Commented Jun 21, 2022 at 9:10
  • I have the same problem with this tool, but only when the Keep resolution of input raster option is selected. I've submitted a bug report at github.com/qgis/QGIS/issues/50801
    – Stu Smith
    Commented Nov 5, 2022 at 18:31

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.