4

I'm using Processing algorithm with PyQgis, and I'm asking how can I directly get a layer from the OUTPUT <OutputVector> value returned by the qgis:fixeddistancebuffer algorithm.

print processing.alghelp("qgis:fixeddistancebuffer")
ALGORITHM: Fixed distance buffer
    INPUT <ParameterVector>
    DISTANCE <ParameterNumber>
    SEGMENTS <ParameterNumber>
    DISSOLVE <ParameterBoolean>
    OUTPUT <OutputVector>

I try to compute multiple radius on multiple feature stored in my vlayer shapefile.

from qgis.core import *

vlayer = QgsVectorLayer("my_shapes.shp", "blabla", "ogr")

poly_output_file="test_poly.shp"

radius = [0.2, 0.5]
for r in radius:
    processing.runalg("qgis:fixeddistancebuffer", vlayer , r, 99, False, poly_output_file + "_%s" % r )

Is there a more direct way to get all features generated into one layer, without creating multiple poly_output_file_r.shp ?

Error with 2.18 :

Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 196, in qgis_excepthook
    showException(type, value, tb, None, messagebar=True)
  File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 107, in showException
    open_stack_dialog(type, value, tb, msg)
  File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 142, in open_stack_dialog
    iface.messageBar().popWidget()
AttributeError: 'NoneType' object has no attribute 'messageBar'

Original exception was:
Traceback (most recent call last):
  File "/home/reyman/Projets/cours/exercice/pyQgis/generateBufferIsland.py", line 73, in <module>
    get_output= general.getObject(buff_res['OUTPUT']) # you can use any name intead of get_output
AttributeError: 'module' object has no attribute 'getObject'
1
  • I edit my question because i found answer.
    – reyman64
    Commented Dec 5, 2016 at 19:03

1 Answer 1

1

If you don't want to write the result directly to the disk, you can manage the output layer by adding a name to your algorithm and then by setting None as output.

For example, if you change this code block:

for r in radius:
    processing.runalg("qgis:fixeddistancebuffer", vlayer , r, 99, False, poly_output_file + "_%s" % r )

to this one (for example):

for r in radius:
    buff_res = processing.runalg("qgis:fixeddistancebuffer", vlayer , r, 99, False, None ) # you can use any name instead of buff_res

get_output= processing.getObject(buff_res['OUTPUT']) # you can use any name intead of get_output

you will able to use it for your needs.

For example, if you want to iterate over the resulting features, you will write:

for feat in get_output.getFeatures():
    # some stuff
10
  • Thanks it works :) One little mistake, its Processing.getObject(...)
    – reyman64
    Commented Dec 5, 2016 at 22:30
  • Thanks, you was right in that case but, since with the libraries provided before there was another error in QgsVectorLayer, I preferred changing the original code. Now it should be work.
    – mgri
    Commented Dec 5, 2016 at 22:55
  • Hum, i try this code with 2.18 version of Qgis and don't work again, it's weird :s
    – reyman64
    Commented Dec 6, 2016 at 11:06
  • I tested it on QGIS 2.18.1: what is the error you get?
    – mgri
    Commented Dec 6, 2016 at 11:14
  • Hum, i remove Processing by processing and i retry
    – reyman64
    Commented Dec 6, 2016 at 11:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.