The following python commands both appear to function when called in qgis 2.6 to generate Voronoi diagrams:

processing.runalg("qgis:voronoipolygons", pointLayer, 1, None)
processing.runalg("grass:v.voronoi",pointLayer,False,False, bbox,-1,0.0001,0, None)

but neither of them seems to work well. The first (qgis) is extremely slow, taking 38 seconds with 180 points vs 2 seconds for the grass method. The qgis method also seems to ignore the output variable when it is specified ("C:/tmp/output.shp" in place of None), but the output layer does appear in the Layers pane so I can iterate through the layers and find it.

The second algorithm seems to ignore the output parameter (again, "C:/tmp/output.shp" in place of None), and no result seems to appear in the layer pane. Trying to use memory:name generated an error message. However, the following results in a temp file path being returned:

output = processing.runalg("grass:v.voronoi",PointLayer,False,False, bbox,-1,0.0001,0, None)
print output.output

My datasets will have thousands of points, so the qgis library is much too slow and the grass library requires a workaround to get the result out.

Is there a way to make qgis:voronoipolygons run faster? Is there a syntax I am missing to get the result layer from the algorithm? It will be an intermediate result used for future calculations so memory storage is preferred.

  • 1
    You can try using processing.runandload instead of processing.runalg. This should load the result layers from memory. – Joseph Jan 27 '15 at 12:29
  • Thank you for that. I hadn't heard of that method. Is there a way to get a reference to the output layer in python without assuming that it is the topmost layer? – user15741 Jan 27 '15 at 16:20
  • If I understand you correctly: for your first command, you can use something like output_1=processing.runalg(... . Then when you call your second command, you can reference the output layer from the first command by using output_1 as the input layer. – Joseph Jan 27 '15 at 16:27
  • I guess I was hoping for more of a 'best practice'. The output.output string is in the form of "c:/user/AppData/Local/Temp/processing/c3e39c28ab044c3596d02a24c66f3e79/..." I would prefer to set my own temp location, or specify the file name, or receive the result in memory as an already-instantiated QgsVectorLayer or similar. – user15741 Jan 27 '15 at 16:50
  • And trying to use that output temp location, the results are deleted immediately after being created, so opening the layer from that path fails... – user15741 Jan 27 '15 at 17:09

Fairly old post but will post a possible answer which would allow you to run algorithms from a script outside QGIS. Algorithms should run faster as more memory would be available without having QGIS loaded. Note that this assumes you have installed QGIS via the OSGeo4W installer.

Follow the first couple of steps as described by @gcarrillo here: Problem with import qgis.core when writing a stand-alone PyQGIS script. I use the .bat file to make life easier.

Once the PATHS are set, type python into the command line and then copy/paste the following code (edit paths/parameters etc. to your requirements:

import os, sys, glob
from qgis.core import *
from qgis.gui import *
from PyQt4.QtGui import *

from os.path import expanduser
home = expanduser("~")

QgsApplication( [], False, home + "/AppData/Local/Temp" )

QgsApplication.setPrefixPath("C://OSGeo4W64//apps//qgis", True)
app = QApplication([])

sys.path.append(home + '/.qgis2/python/plugins')
from processing.core.Processing import Processing
from processing.tools import *

point_directory = home + "/Desktop/Test//"     # Change to path containing point shapefiles
output = "C:/tmp//"                            # Path to save outputs

for pointLayer in glob.glob("*.shp"):
        output_0=general.runalg("qgis:voronoipolygons", pointLayer, 1, output + pointLayer)
        # For follow-on algorithms, use output_0 as input
        # Can also replace this with grass algorithms

I have tested using follow-on algorithms and replaced certain outputs with None (if I don't want the output of a certain algorithm) and it works for me. Of course, this would differ from what you require but I hope it helps somewhat :)

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.