QGIS – Utilizing QGIS Processing Tools in a Standalone Script

pyqgisqgisqgis-processing

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

Best Answer

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.

Related Question