Until I get to work the generic way of doing it, I will tell you a workaround.
I use the Processing plugin version 2.2.0-2 (I suggest you to use this version), which is installed in /home/germap/.qgis2/python/plugins/
on my computer. You need to know this folder location, because you import the processing module from there.
Since you know the provider (qgis
) and the algorithm (explodelines
), you can look at /home/germap/.qgis2/python/plugins/processing/algs/qgis/
to take the explode lines script name: Explode.py
This information allows you to import the algorithm directly to your Python standalone script.
So, open a Python console and copy the following script (I use GNU/Linux, so the environment variables are set by default, allowing me to import qgis and PyQt4 libraries in a breeze):
# Prepare the environment
import sys
from PyQt4.QtGui import *
from qgis.core import *
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
# Tell Python where you will get processing from
sys.path.append('/home/germap/.qgis2/python/plugins')
# Reference the algorithm you want to run
from processing.algs.qgis.Explode import *
alg = Explode()
# Set input and output
inLayer = QgsVectorLayer('/home/user/data/in.shp', 'input', 'ogr')
outLayer = '/home/user/data/out.shp'
alg.setParameterValue('INPUT', inLayer)
alg.setOutputValue('OUTPUT', outLayer)
# Run the algorithm
from processing.core.SilentProgress import SilentProgress
progress = SilentProgress()
alg.processAlgorithm(progress)
If you don't get error messages, that's it. The output layer has been saved in the output path you specified (/home/user/data/out.shp
)
Note: Regarding a generic way (i.e., calling algorithms by name), I've found some troubles that I need to solve before posting it. As soon as I get it to work, I'll post it.
import processing
.