15

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')
  • 2
    I think you have to use import processing. – Joseph Jan 8 '15 at 10:58
  • @Joseph Sorry for not being specific: But I want to access it from a standalone script. – ustroetz Jan 8 '15 at 11:01
  • Ahh no worries :). In that case, apologies but I do not know as I always run it within QGIS. Hopefully others will advise. – Joseph Jan 8 '15 at 11:03
  • Hi @ustroetz, did you manage to run this from your standalone script? I ask because I am also trying to run external scripts. – Joseph Jan 12 '15 at 11:18
  • @Joseph No, I haven't – ustroetz Jan 12 '15 at 11:59
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.

  • 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 gis.stackexchange.com/questions/188074/… – Mr Purple Apr 8 '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: gis.stackexchange.com/questions/119961/… – Mr Purple Apr 8 '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
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.