2

I wrote the following code to start using QGIS APIs. in a very short script seen below, the "Error: Algorithm not found" is thrown and "print processing.alglist()" is showing none. Any idea why this is happening?

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import nltk
import sys, os
import shapely 
import math
from collections import defaultdict
from shapely.geometry import LineString
import json
import datetime
import time
import itertools
import glob
import sys


app = QApplication(sys.argv)
qgis_prefix = "C:/Program Files/QGIS Valmiera/apps/qgis"
QgsApplication.setPrefixPath(qgis_prefix, True)
QgsApplication.initQgis()
layer1 = QgsVectorLayer("D:/Ehsan/Test/InputData/SHP/OSM_roads_no_psv_latin1_id.shp",     "test" , "ogr")

if not layer1.isValid():
    print "Layer failed to load! :("
print layer1.isValid()

import processing
processing.alglist()
processing.runalg("qgis:clip","D:/Ehsan/Test/InputData/SHP/OSM_roads_no_psv_latin1_id.shp","D:/Ehsan/Test/InputData/temporary/tmpotile.shp","D:/Ehsan/Test/LM_Feat_test.shp")

Notice: the imports list is to show that I have almost everything imported and added to the path and pythonpath...

Please help me to figure out how to resolve it. So far, the QGIS has seemed to be so buggy and problematic outside its python console. @Lukas Graf

2
  • 1
    Are you sure about the double colon? Compare processing.alghelp("qgis:clip") vs processing.alghelp("qgis::clip") for example.
    – Mr Purple
    Commented Jun 5, 2014 at 4:13
  • I think its a typo here. right now I am trying to use Arcpy. if it is more difficult then I will use qgis APIs again. we will see.
    – msc87
    Commented Jun 10, 2014 at 10:28

1 Answer 1

3

For using processing standalone, it must be initiated with an interface. Thus, you can create a dummy iface before calling processing (this worked for me for QGIS 2.4):

import qgis
app = qgis.core.QgsApplication([], True)
import processing

class DummyInterface(object):
    def __init__(self):
        self.destCrs = None
    def __getattr__(self, *args, **kwargs):
        def dummy(*args, **kwargs):
            return DummyInterface()
        return dummy
    def __iter__(self):
        return self
    def next(self):
        raise StopIteration
    def layers(self):
        # simulate iface.legendInterface().layers()
        return qgis.core.QgsMapLayerRegistry.instance().mapLayers().values()
iface = DummyInterface()
plugin = processing.classFactory(iface)

print processing.alglist()

See here for some additional comments on this example.

As indicated by @gcarrillo, the processing.alglist() returns None for QGIS 2.6.1. I don't know the reason for this, but I found that an answer on lists.osgeo works for my Windows install of 2.6.1:

import sys, os
import inspect
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# init QApplication for processing and set the customSettingFolder
#a = QApplication( sys.argv )
QgsApplication( sys.argv, False, r"C:\folder\to\this\script" )

# supply path to where is your qgis installed
QgsApplication.setPrefixPath(r"C:\OSGeo4W\bin", True)

# load providers
QgsApplication.initQgis()
p = QgsProject.instance()
p.read( QFileInfo( r"C:\path\to\a\qgis\project\myproject.qgs" ) )
print p.title()

mlr = QgsMapLayerRegistry.instance()
qa = QApplication( sys.argv )
from processing.core.Processing import Processing


cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
if cmd_folder not in sys.path:
 sys.path.insert(0, cmd_folder)
Processing.initialize()

from processing.tools import general
general.alglist()
1
  • Does this code really works for you? It stops with errors at the 3rd line. If I solve those errors, I'm still unable to get a list of algorithms by calling print processing.alglist(). It returns None. I use GNU/Linux, QGIS 2.6.1, and Processing 2.2.0-2 Commented Jan 13, 2015 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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