20

I want to access the explode lines function in Python outside of QGIS from a standalone script.

What module do I have to load in order to use it?

How can I access processing?

from qgis.core import *

layerInput = QgsVectorLayer('test.shp', 'test', 'ogr')

processing.runalg('qgis:explodelines', layerInput, 'temp.shp')

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
ustroetz
  • 7,994
  • 10
  • 72
  • 118

2 Answers2

25

UPDATE 24.04.2018: See how to do this in QGIS v3.x here.


For QGIS v2.x

Finally found the proper way of running processing algorithms via standalone PyQGIS scripts.

Using Processing plugin version 2.2.0-2, you can try the following script:

# Prepare the environment
import sys
from qgis.core import *
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework 
sys.path.append('/home/user/.qgis2/python/plugins') # Folder where Processing is located
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

# Run the algorithm
layerInput = QgsVectorLayer('test.shp', 'test', 'ogr')
general.runalg('qgis:explodelines', layerInput, 'temp.shp')

# Exit applications
QgsApplication.exitQgis()
QApplication.exit()

Newer Processing versions could be located at /usr/share/qgis/python/plugins, so you might need to use sys.path.append('/usr/share/qgis/python/plugins') accordingly.

I found the working example in Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion.

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
  • 1
    The processing plugin may not be at '/home/user/.qgis2/python/plugins'. You can find the location by opening QGIS and clicking plugins/Manage and install plugins scroll down to processing and you will see the installation location in the description. – Mr Purple Mar 25 '16 at 23:48
  • 2
    I received lots of warnings about cannot-create-a-qpixmap with this stand alone function but I see you solved that too in this question http://gis.stackexchange.com/questions/188074/pyqgis-error-cannot-create-a-qpixmap-or-qwidget-when-no-gui-is-being-used/188089?noredirect=1#comment284492_188089 – Mr Purple Apr 08 '16 at 22:20
  • 1
    You can make the standalone script even more generic by dropping layerInput and changing the runalg line to: general.runalg(*sys.argv[1:]) not to mention that this script can be the basis for parallel GIS processing as per this question: http://gis.stackexchange.com/questions/119961/how-can-i-parallelise-embarrassingly-parallel-gis-operations-in-qgis-python – Mr Purple Apr 08 '16 at 22:28
  • Is it possible to do the same framework but to run PLUGINS from a standalone Python (not inside QGIS)? I executed alglist() function and I cannot see my plugins installed in my QGIS in there... – Irene Jun 14 '17 at 15:27
4

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.

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178