2

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
processing.alglist("clip")
processing.alghelp("qgis:clip")
inputlayer = qgis.utils.iface.activeLayer()
#define mask layer
overlaylayer = "E:\abc.shp"
processing.runalg("qgis:clip",inputlayer,overlaylayer,"E:/output_file.tif")

but it shows this error:

Unable to execute algorithm
Wrong parameter value
13
  • Quite similar gis.stackexchange.com/questions/56832/…
    – DIV
    Commented Nov 23, 2016 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
    Commented Nov 23, 2016 at 10:33
  • I tried [link]( gis.stackexchange.com/questions/56832/…) but it is not working
    – User18
    Commented Nov 23, 2016 at 10:35
  • 1
    @User18 post your code, It will be much easier to help
    – DIV
    Commented Nov 23, 2016 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. Commented Nov 23, 2016 at 13:25

2 Answers 2

4

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".

1

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:

processing.algorithmHelp('gdal:cliprasterbymasklayer')

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.

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.