Tuesday, 4 December 2018

Using the processing framework in a PyQGIS standalone application


I asked a question several days ago about creating an in-memory layer using the processing.runalg() command in a standalone PyQGIS application (Performing in-memory difference operation between vector layers in PyQGIS). The solution seems to work perfectly well in the QGIS Python console, but not when I try it in a standalone application.


The relevant snippet of code I am running is:


diffp = processing.runalg("qgis:fixeddistancebuffer", layer, 0.000001, 10, False, None)
diff = processing.getObject(diffp['OUTPUT'])

(layer is a vector layer defining line segments)


My desired output is this, and this is what I get when I run the commands from the Python console in QGIS:


Desired buffer around roads



But what I get from the vector layer I create in the standalone application is shown in green in the following image (I've zoomed in so that it's visible):


Results from the standalone application


What I get from the standalone application appears to be only one feature from my desired output. I'm running the same two lines of code for both the console and standalone applications, and there are no exceptions thrown by the code. How can I produce the entire desired result from a standalone application, rather than just a single feature?


EDIT:


I've tried using the following line of code to directly create a .shp file, in case the in-memory layer was somehow causing the problem:


diffp = general.runalg("qgis:fixeddistancebuffer", layer, 0.000001, 10, False, "shape_file.shp")

This produces the same results as when I use the in-memory layer, so the problem I'm experiencing is definitely coming from runalg().



Answer



I think I've managed to figure it out now.



The processing framework I've been using in PyQGIS is located in OSGeo4W64\apps\qgis\python\plugins, but this isn't the same location as what the QGIS Python console uses (it uses OSGeo4W64\apps\qgis-ltr\python\plugins).


Changing the PYTHONPATH variable in my IDE to include the "qgis-ltr" directory rather than the "qgis" directory resolved my issue, and I'm getting exactly the results I was wanting now.


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...