qgis – Making Processing Algorithms Use Default Parameter Values in QGIS 2.14

coordinate systemgdalwarppyqgisqgisqgis-processing

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.

Best Answer

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.

Related Question