6

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?

EDIT:

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:

      arguments.append(unicode(geoTransform[1]))
      arguments.append(unicode(geoTransform[5]))

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

5
  • If you installed QGIS using the OSGeo4W Network Installer:

    C:/OSGeo4W64/apps/qgis/python/plugins/processing/algs/gdal/ClipByMask.py
    

  • 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
    
5
  • 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.. Dec 6, 2016 at 12:12
  • @SaiKrishna - Apologies, hopefully others can advise on where that hidden code is =)
    – Joseph
    Dec 7, 2016 at 9:26
  • Looking forward! 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 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
    Nov 5, 2022 at 18:31

Your Answer

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

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