5

I would like to know how to get processing algorithms in general, and warp (reproject) specifically, to run with default values. I am running a script in the QGIS Python console. In the QGIS 2.14 documentation it is stated that:

"Input parameters such as strings, booleans, or numerical values have default values. To use them, specify None in the corresponding parameter entry."

However, when I run the following code, I receive an error.

processing.runalg("gdalogr:warpreproject",
 path+"land_use.tif", #input
 "ESPG:3035", #source crs
 "ESPG:4326", #destination crs
 "0", #no data value
 0, #target resolution: 0=unchanged
 0, # method: 0=near
 5, # output raster type: 5=Float32
 0, #compression: 0=none
 None, #jpeg compression: default
 None, #zlevel: default
 None, #predictor: default
 None, #tiled: default
 2, #bigtiff: 2=no
 None, #TFW: default
 None, #extra: default
 path+"land_use_reprojected.tif")

The error is simply

Error: Wrong parameter value: None

I feel like QGIS is throwing an error instead of referring to default values, have I misunderstood the documentation?

I also find that the error message is uninformative.

asked Sep 3, 2016 at 11:20

2 Answers 2

7

It turns out you can actually use default parameter values calling Processing algorithms from PyQGIS using a different syntax. I didn't find it in the docs, but that's what we have GIS.SE for :D.

Just call the algorithm providing parameters as a Python dictionary with keys being parameter names. A minimal example:

import processing
processing.runalg( "gdalogr:warpreproject", {"INPUT":"/docs/geodata/raster.tif", "DEST_SRS":"EPSG:32618", "METHOD":0} )

You can get parameter names calling:

processing.alghelp("gdalogr:warpreproject")

So, in your case, the call could be:

processing.runalg("gdalogr:warpreproject",
 {"INPUT":path+"land_use.tif",
 "SOURCE_SRS":"ESPG:3035",
 "DEST_SRS":"ESPG:4326",
 "NO_DATA":"0", 
 "METHOD":0, 
 "RTYPE":5,
 "COMPRESS":0,
 "BIGTIFF":2,
 "OUTPUT":path+"land_use_reprojected.tif"})

You only need to include mandatory parameters and those for which you want to set your own values. The rest will take default values. If you miss any mandatory parameter, QGIS-Processing will let you know about that with an error message.

answered Sep 5, 2016 at 18:03
4
  • thanks for this! Interesting, so those parameters that should use default values can simply be left out of the call? Commented Sep 6, 2016 at 6:38
  • I'm marking this as the accepted answer because it more thoroughly addresses the key issue: accessing default parameters. Commented Sep 6, 2016 at 7:01
  • @JonR - I agree! I also did not know it was possible using dictionary keys, great solution :) Commented Sep 6, 2016 at 9:16
  • Right @JonR, you include mandatory parameters and those for which you want to set your own values. The rest will take default values. If you miss any mandatory parameter, QGIS-Processing will let you know about that with an error message. I've included this in the answer. Commented Sep 6, 2016 at 12:23
2

I would suggest to not use None as an input parameter value (except for when you want to create a temporary output) as a number of algorithms will report the

Error: Wrong parameter value: None.

Personally, I use "" which should essentially mean the same thing. Also note that the following parameters seem to require a minimum value of 1:

  • JPEGCOMPRESSION
  • ZLEVEL
  • PREDICTOR

So you could try using something like:

processing.runalg("gdalogr:warpreproject",
 path+"land_use.tif", #input
 "ESPG:3035", #source crs
 "ESPG:4326", #destination crs
 "0", #no data value
 0, #target resolution: 0=unchanged
 0, # method: 0=near
 5, # output raster type: 5=Float32
 0, #compression: 0=none
 1, #jpeg compression: default
 1, #zlevel: default
 1, #predictor: default
 "", #tiled: default
 2, #bigtiff: 2=no
 "", #TFW: default
 "", #extra: default
 path+"land_use_reprojected.tif")
answered Sep 5, 2016 at 10:36
3
  • 1
    This answer is working, thanks. Can you recommend a good resource for learning more of the details around what values are suitable for processing algorithms, such as the minimum value requirements on zlevel, etc that you mention? Commented Sep 5, 2016 at 15:06
  • @JonR - Most welcome, glad it helped! You ask a very good question, personally I think best resource for this is linked in your question where you can call the parameters of an algorithm from the Python Console. I also gave an answer fairly recently describing briefly how to use it here. Commented Sep 5, 2016 at 15:11
  • And the minimum requirements were just trial and error, I don't think there is documents for that. Note that the Processing plugin is continually being updated so changes are being made to various algorithms in each version. Which is why is probably easier to call the help parameters from the plugin itself instead of searching for online documentation :) Commented Sep 5, 2016 at 15:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.