4

Using Python for QGIS 2.14 Essen i try to clip a raster by a Polygon, as indicated here: http://docs.qgis.org/2.6/de/docs/user_manual/processing_algs/gdalogr/gdal_extraction/cliprasterbymasklayer.html

with this code:

processing.runalg('gdalogr:cliprasterbymasklayer', input, mask, no_data,     alpha_band, keep_resolution, extra, output)

My code read as following:

processing.runalg('gdalogr:cliprasterbymasklayer', inputlayer, maskshape, "none", False, False, "", outputraster)

Exactly as derived from tutorials. However, this is the error message returned and I can't figure why: Error: Wrong number of parameters

I´ve adapted the code to the 16 parameters of the new version, although I´m not sure if correct

processing.runalg('gdalogr:cliprasterbymasklayer', inputlayer, maskshape, "", False, False, False, 0, 0, 0, 0, False, 0, False, "",  "outputraster")

The error message says still the same.

8
  • Have you tried to replace "none" with ""?
    – ArMoraer
    Commented Apr 28, 2016 at 13:43
  • yes, same response Commented Apr 28, 2016 at 13:43
  • 2
    Using Processing version 2.12.2, the algorithm is said to have 16 parameters. You can check this from the Python Console by typing: processing.alghelp("gdalogr:cliprasterbymasklayer"). Before this version it was 7 parameters, I guess it has been updated to include the advanced parameters shown in the GUI version.
    – Joseph
    Commented Apr 28, 2016 at 13:45
  • ok thanks, i´ll check for 2.14 then. Python is very new to me. Commented Apr 28, 2016 at 13:48
  • @PhilippDahlem - Don't confuse QGIS version with the Processing plugin version (I did!). The Processing plugin contains the algorithm you mentioned along with many others. You have QGIS 2.14 but the Processing plugin itself may be the latest version (2.12.2).
    – Joseph
    Commented Apr 28, 2016 at 13:57

2 Answers 2

1

I think the following parameters requires a minimum value of 1:

  • JPEGCOMPRESSION
  • ZLEVEL
  • PREDICTOR

The others can be set to 0 so you could try running the following which works for me:

processing.runalg('gdalogr:cliprasterbymasklayer', inputlayer, maskshape, "", False, False, False, 0, 0, 1, 1, 1, False, 0, False, "", "outputraster")
2
  • 1
    Thanks,this is how i actually made it yesterday, after lots of Try and Error. However i also had to set the frist three boolean operators to True, for whatever reason, to get an correctly clipped raster with non-zero values. Now the raster ends up with a fourth band, but whatever..it works and I am happy. Thank you! Commented Apr 29, 2016 at 17:07
  • @PhilippDahlem - Most welcome buddy, glad it worked eventually =)
    – Joseph
    Commented May 3, 2016 at 9:24
0

The solution above didn't work me with QGIS 2.18.20. Still not enough parameters although the documentation says something different.

My solution: Do it with the graphical modeler (Processing -> Graphical modeler) and than export it as python script.

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.