Saturday 22 July 2017

python - Error: Algorithm not found (QGIS)


I wrote the following code using QGIS'API.



import os

from qgis.core import *
import qgis.utils

prefix_path = os.environ['QGIS_PREFIX_PATH']
QgsApplication.setPrefixPath(prefix_path, True)
QgsApplication.initQgis()
app = QgsApplication([], True)


import processing
from processing import *

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)


uri = "file:///C:\Users\Tabrez\Desktop\importPoint.csv?crs=epsg:4326&delimiter=%s&xField=%s&yField=%s" % (",", "lon", "lat")
layer = QgsVectorLayer(uri, "importPoints", "delimitedtext")
layer1 = iface.addVectorLayer(uri, "importPoints", "delimitedtext")
extent = layer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()
outputExtent = str(xmin) + "," + str(xmax) + "," + str(ymin) + "," + str(ymax)
radius = 0.05

cellSize = 0.005
print cellSize
processing.runandload("saga:kerneldensityestimation", layer, "weightage", radius, 0, outputExtent, cellSize, "D:/test/raster.tif")

iter = layer.getFeatures()
pointCount = len(list(iter))
probabilityFailure = [[0.846], [0.846, 0.846], [0.28, 0.50, 0.72], [0.21, 0.39, 0.61, 0.79], [0.17, 0.32, 0.50, 0.68, 0.83], [0.14, 0.27, 0.42, 0.58, 0.73, 0.86], [0.12, 0.23, 0.36, 0.50, 0.64, 0.77, 0.88], [0.10, 0.20, 0.32, 0.44, 0.56, 0.68, 0.80, 0.90], [0.09, 0.18, 0.28, 0.39, 0.50, 0.61, 0.72, 0.82, 0.91], [0.08, 0.16, 0.26, 0.35, 0.45, 0.55, 0.65, 0.74, 0.84, 0.92], [0.07, 0.15, 0.23, 0.32, 0.41, 0.50, 0.59, 0.68, 0.77, 0.85, 0.93]]
for i in probabilityFailure:
print i
os.system("gdal_contour -a 0.21 -fl 0.01 " + " D:/test/raster.tif " + " " + "D:/test/contour")

QgsApplication.exitQgis()

This line is giving me error:


processing.runandload("saga:kerneldensityestimation", layer, "weightage", radius, 0, outputExtent, cellSize, "D:/test/raster.tif")

I am very new to python and QGIS.



Answer



Several things to check here:


Firstly, make sure that %QGIS_PATH%\apps\qgis\python\plugins\processing is added to the PYTHON_PATH environment variable.


Secondly, instead of:



import processing
from processing import *

Use:


import processing
from processing.core.Processing import Processing

Finally, before calling any algorithms using processing, do the following:


Processing.initialize()
Processing.updateAlgsList()


This should do what you want.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...