I want to use clipper function over a raster layer with the help of mask layer (.shp file) which will produce a clipped raster layer. How this can be done with Python script?

I used this script:

import processing
inputlayer = qgis.utils.iface.activeLayer()
#define mask layer
overlaylayer = "E:\abc.shp"

but it shows this error:

Unable to execute algorithm
Wrong parameter value
  • Quite similar gis.stackexchange.com/questions/56832/… – DIV Nov 23 '16 at 10:32
  • 1
    What have you tried so far? You need at first get a running stand-alone script what is not out of the box working. Several questions on this site deal with that matter. The syntax of the function can be found in the documentation or when you run the tool and look into the protocol window. – Matte Nov 23 '16 at 10:33
  • I tried [link]( gis.stackexchange.com/questions/56832/…) but it is not working – User18 Nov 23 '16 at 10:35
  • 1
    @User18 post your code, It will be much easier to help – DIV Nov 23 '16 at 10:37
  • 1
    @DIVAD Please post your comment as an answer, so that User18 can mark it as the accepted answer and all other users can know this question has been properly solved. – Germán Carrillo Nov 23 '16 at 13:25

There is a couple of methods that can help you to clip raster by polygon/mask.

Best way is to type processing.alglist("clip") in console and test some of methods that popped out, e. g. "saga:clipgridwithpolygon".

| improve this answer | |

I want to add an updated solution for QGIS 3 as some things have changed. I referenced various other StackOverflow threads and the QGIS Documentation page to make it work.

First, I'd like to mention this command that was hugely helpful to me. From the console you can run:


This prints out step-by-step instructions for the parameters, what values are accepted, etc. I used this to populate my parameter values.

Here is a code example:

import processing
from qgis.core import *

raster_layer = QgsRasterLayer('path to your input raster', 'raster')
mask_layer = QgsVectorLayer('path to your vector mask layer', 'mask', 'ogr')

parameters = {'INPUT': raster_layer,
        'MASK': mask_layer,
        'NODATA': -9999,
        'ALPHA_BAND': False,
        'CROP_TO_CUTLINE': True,
        'KEEP_RESOLUTION': True,
        'OPTIONS': None,
        'DATA_TYPE': 0,
        'OUTPUT': 'path to where you want to save'}

processing.runAndLoadResults('gdal:cliprasterbymasklayer', parameters)

A nice way to build your parameters list is to make a dictionary with them before feeding them into the GDAL tool.

Also, notice the processing.runandload method from QGIS 2 has now become processing.runAndLoadResults in QGIS 3.

| improve this answer | |

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.