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.9k22 gold badges237 silver badges419 bronze badges
asked Jun 30, 2015 at 14:31
2
  • 1
    Which QGIS version are you using and can you use SAGA algorithms from the Processing Toolbox in the QGIS interface? Commented Jun 30, 2015 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. Commented Jul 1, 2015 at 7:09

1 Answer 1

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.

answered Jul 1, 2015 at 10:47
2
  • 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! Commented Jun 14, 2017 at 14:56
  • @balajeerc is it also possible to just update a specific algorithm? Updating the whole list takes quite long. Commented Dec 22, 2017 at 14:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.