I am writing a script which must work outside of QGIS GUI. I call some API functions from qgis.core but I would like to use the processing plugin.
I am able to import processing with sys.path.append()
but I cannot run any process. Moreover, all "native" algs are missing in QgsApplication.processingRegistry().algorithms()
Is it possible to run processing that way?
import os, sys
from qgis.core import *
QgsApplication.setPrefixPath('/usr', True)
qgs = QgsApplication([], False)
qgs.initQgis()
sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
import processing
layer1 = QgsVectorLayer('data/ROUTE_PRIMAIRE.SHP')
layer2 = QgsVectorLayer('data/ROUTE_SECONDAIRE.SHP')
processing.run('qgis:union', layer1, layer2, 'test.shp') # returns nothing
I am using QGIS 3.0.1 on Debian 9.
Best Answer
You can run a QGIS Processing algorithm in standalone (no GUI) mode in this way:
Note for QGIS versions LESS THAN 3.16.5
If you want to use a native algorithm (i.e., an algorithm from the native provider, whose algorithms are written in C++), you need to add the provider after initializing Processing:
Note: If you actually wrote your own Processing algorithm and would like to run it in a standalone PyQGIS script, see: Using custom Processing algorithm from standalone PyQGIS scripts (outside of GUI)