4

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

2

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.

  • Finally a solution! =) – mgri Dec 17 '16 at 10:16
  • I still kind of find it strange that the processing module in "qgis" is different from the processing module in "qgis-ltr"... Thank you again for all your help with resolving this. – Benjamin Dec 17 '16 at 13:10
1

Try importing:

from qgis.core import *
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

and using general.runalg() instead of processing.runalg(). Does it change something?

  • For some reason, my IDE is saying that "general" is an unresolved reference. – Benjamin Dec 14 '16 at 21:23
  • Hmm, I'm unfamiliar with standalone scripts... What happens if you add from processing.tools import general ? – mgri Dec 14 '16 at 21:26
  • The reference is resolved now, but there are some exceptions being thrown. It seems that general.getObject() doesn't exist. Is there a different syntax for general.runalg() than for processing.runalg()? – Benjamin Dec 14 '16 at 21:29
  • I think that general must be used only when you run an algorithm... The remaing part of the code should be remain the same – mgri Dec 14 '16 at 21:34
  • Okay, it's running properly now. But I'm still only getting a single feature in my output layer... – Benjamin Dec 14 '16 at 21:36

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.