6

This is a follow on from Importing QGIS Processing in stand-alone python script?

I do not understand yet how to call an algorithm from the processing toolbox in a stand-alone Python script.

According to the Qgis documentation, I should use processing.runalg() but this doesn't seem to exist in my installation. When I do dir(Processing) I get:

['__doc__', '__module__', 'actions', 'addAlgListListener', 'addProvider', 'algs', 'contextMenuActions', 'fireAlgsListHasChanged', 'getAlgorithm', 'getAlgorithmFromFullName', 'getObject', 'getProviderFromName', 'initialize', 'listeners', 'loadActions', 'loadAlgorithms', 'loadContextMenuActions', 'loadFromProviders', 'modeler', 'providers', 'removeAlgListListener', 'removeProvider', 'runAlgorithm', 'runandload', 'tr', 'updateAlgsList', 'updateProviders']

Is the Qgis doc outdated or do I have a wrong version of something on my machine?

try:
    Processing.runAlgorithm('gdalwarp -overwrite -s_srs EPSG:32642 -t_srs "+proj=tmerc +lat_0=0 +lon_0=74.51666666666667 +k=1 +x_0=3300000 +y_0=14743.5 +ellps=GRS80 +units=m +no_defs" /home/steph/Temp/python_tmp/input/dsm.tif /home/steph/Temp/python_tmp/output/20160813-1225-UAVanalyse/dsm_proj.tif')
except:
    print 'ERROR ' + str(sys.exc_info())

Gives me:

ERROR (<type 'exceptions.TypeError'>, TypeError('runAlgorithm() takes at least 2 arguments (1 given)',), <traceback object at 0x7fa47898dbd8>)

But I'm not sure what the second argument should be?

While

try:
    Processing.runalg('gdalwarp -overwrite -s_srs EPSG:32642 -t_srs "+proj=tmerc +lat_0=0 +lon_0=74.51666666666667 +k=1 +x_0=3300000 +y_0=14743.5 +ellps=GRS80 +units=m +no_defs" /home/steph/Temp/python_tmp/input/dsm.tif /home/steph/Temp/python_tmp/output/20160813-1225-UAVanalyse/dsm_proj.tif')
except:
    print 'ERROR ' + str(sys.exc_info())

Gives me:

ERROR (<type 'exceptions.AttributeError'>, AttributeError("class Processing has no attribute 'runalg'",), <traceback object at 0x7f183586abd8>)

Using Qgis 2.16.1 on Ubuntu Xenial 16.04 LTS

2 Answers 2

4

The arguments are:

runAlgorithm(algOrName, onFinish, *args, **kwargs)

Set the onFinish argument to None (i.e. no post-processing)

However, by the looks of it you are just wanting to run GDAL Warp. May I suggest you go for a simpler approach for any of the GDAL functions? QGIS is a wrapper around GDAL so for a stand-alone process why involve QGIS at all? Cut out the middle-man and use the GDAL bindings directly in Python. This is not only simpler, it is also more portable. Bundled in GDAL (and therefore QGIS) you will find a standalone exe version of GDAL Warp (and others) ready to go and that may be all you need.

In a standard install, you'll find the GDAL Python libraries and standalone exe files in the bin folder (e.g. C:/OSGeo4W64/bin on my machine). On a machine without QGIS you can install the GDAL bindings from here (I recommend the GISInternals site for Windows). You only need to do this is you move your code to a machine without QGIS of course. Numpy and SciPy are also similarly available and usable without the QGIS wrapper.

In the case of GDAL Warp (or any of the GDAL exe processes) you can call the exe as a subprocess from Python or use it directly on the commandline.

1
  • That sounds like a good idea and I am giving it a try. Will probably come back with questions though :-) However, I will have to use other tools (Qgis, Lastools,...) and I thus need to understand how the Qgis wrapper works. Any chance you can give me a full working example of the runAlgorithm() syntax? I am new to it and not sure, exactly how the args should look like. Or any link to a working script example? haven't found anything really clear (to me) so far... Thanks! Aug 16, 2016 at 4:47
3
+100

I'm not completely sure but when I run processing tools from a standalone script, I call general.runalg("qgis:...") but this probably depends how you set up your paths and imports. Also, since you're using processing, you may need to call gdalogr:warpreproject (when you run this inside QGIS, you can see the various parameters required) instead of gdalwarp.

So perhaps try running:

general.runalg("gdalogr:warpreproject", "/home/steph/Temp/python_tmp/input/dsm.tif", "EPSG:32642", "+proj=tmerc +lat_0=0 +lon_0=74.51666666666667 +k=1 +x_0=3300000 +y_0=14743.5 +ellps=GRS80 +units=m +no_defs", -9999, 0, 0, 0, 0, 1, 1, 3, False, 0, False, "", "/home/steph/Temp/python_tmp/output/20160813-1225-UAVanalyse/dsm_proj.tif")
4
  • 2
    I will try so, thanks! But where does the "general" come from? Is it just an alias for "processing" in your script? Aug 18, 2016 at 3:21
  • 2
    @StéphaneHenriod - I believe you call the general.py script when running algorithms outside QGIS.
    – Joseph
    Aug 18, 2016 at 10:44
  • 1
    Ok, I managed to get it working by playing around with the import, thanks! But where can I find the doc of runalg, to know what parameters are expected for each tool? I found this: docs.qgis.org/2.6/en/docs/user_manual/processing_algs/gdalogr/… but it's for 2.6 and apparently the number and order of the arguments doesn't match with your (perfectly working) example. Aug 19, 2016 at 19:17
  • @StéphaneHenriod - Most welcome, glad you got it working! The docs for 2.14 and for testing aren't much help. I don't use the docs to check the paramters, instead I use the processing method alghelp to show what parameters are required as described here: Using processing algorithms from the console.
    – Joseph
    Aug 25, 2016 at 11:49

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.