Friday 21 August 2015

pyqgis - Piping output of one processing algorithm as input into another algorithm in QGIS Processing script?


I created a processing script in QGIS 2.18 that connects two processing algorithms (first clip and than dissolve). The output vector from the clip is supposed to be an input into the dissolve.


The code of my processing script is provided here:



##input_layer=vector
##clip_layer=vector
##clipped_layer=output vector
##processed_layer=output vector

clip_area = processing.runalg("qgis:clip", input_layer, clip_layer,
clipped_layer)

fin_area = processing.runalg("qgis:dissolve", clip_area, "true", None,
processed_layer)


The screeshot of processing script UI when I try to run it is below:


UI of processing script


Both of the algorithms work correctly standalone, but when I try to pipe them it does not work as intended. I only get clipped_layer as output and the following error notification in Python Console:


enter image description here


There are several questions adressing related issues like Pipe custom processing algorithm output into another in QGIS 3.0 but none really answers my question.


Does anybody know how to correctly pipe algorithms in the example processing script provided above?


I know it could be solved by using Processing Modeler but since I plan to expand the script further, please, don´t suggest this as solution.



Answer



The processing.runalg() return a python dictionary with one record: 'OUTPUT' is the key and the value is the absolute path at result layer.



So in the next algorithm instead of the parameter clip_area you have to use clip_area['OUTPUT']


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