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
mgri
  • 16,159
  • 6
  • 47
  • 80
User18
  • 509
  • 4
  • 15
  • Quite similar http://gis.stackexchange.com/questions/56832/calling-clip-function-in-pyqgis-from-python-console – 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 but it is not working – User18 Nov 23 '16 at 10:35
  • Script i used is added in Question – User18 Nov 23 '16 at 10:36
  • 1
    @User18 post your code, It will be much easier to help – DIV Nov 23 '16 at 10:37
  • @DIVAD check now. code is added – User18 Nov 23 '16 at 10:40
  • For qgis you need to add some code to be able to run it stand alone. Lots of posts on this site about it. Probably the best answer here: http://gis.stackexchange.com/questions/129915/cannot-run-standalone-qgis-script?noredirect=1&lq=1 Also you need to take care of the input of the overlay layer as you use an escape character in your path. Use r"c:" or "c:/" or "c:\" – Matte Nov 23 '16 at 10:48
  • above written script is given error for input layer. i cheked the crs for both but still same error through python script occurs. is any way to resolve this error ? – User18 Nov 23 '16 at 11:00
  • maybe try "saga:clipgridwithpolygon" instead of "qgis:clip" – DIV Nov 23 '16 at 11:15
  • please check http://gis.stackexchange.com/questions/218835/raster-calculation-in-qgis-using-python-script Question too. might be u can help in this – User18 Nov 23 '16 at 11:24
  • great :) Also, if you type processing.alglist("clip") in console, you will see more methods. Take a look at that. – DIV Nov 23 '16 at 11:26
  • yeah i checked it .@DIVAD – User18 Nov 23 '16 at 11:27
  • 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

2 Answers2

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

DIV
  • 886
  • 1
  • 6
  • 18
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.

Erich Purpur
  • 1,861
  • 3
  • 18
  • 35