7

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.

underdark
  • 84,148
  • 21
  • 231
  • 413
Tabrez Khan
  • 191
  • 1
  • 10
  • 1
    Which QGIS version are you using and can you use SAGA algorithms from the Processing Toolbox in the QGIS interface? – Joseph Jun 30 '15 at 14:56
  • I'm using QGIS 2.8.2 and yes i can use this algorithm from Toolbox and from python console of QGIS. – Tabrez Khan Jul 01 '15 at 07:09

1 Answers1

12

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.

balajeerc
  • 374
  • 4
  • 15
  • I installed the plugin "LeCoS" (for landscape ecology) in QGIS and it is available through the processing plugin (inside QGIS GUI) as "lecos:patchstatistics". However, when I go to my standalone Python and list the algorithms in processing, the algorithm is no there, not event executing "updateAlgsList()". So the output is the long list of by-default plugins for QGIS and finally, a "None". Any idea? Thanks! – Irene Jun 14 '17 at 14:56
  • @balajeerc is it also possible to just update a specific algorithm? Updating the whole list takes quite long. – Wonka Dec 22 '17 at 14:43