1

How is it possible to perform multiple processing steps for one layer without creating additional ones?

I wanted to add several fields to attribute table using qgis:fieldcalculator but every time it works new layer is created. Is it possible to redirect output from first .runalg to second and so on until getting final result without additional layers?

first = processing.runalg('qgis:fieldcalculator', 'S1_PROJ_1992[PTS]', 'X_2000', 0, 10, 1, True, '@row_number', None)
second = processing.runalg('qgis:fieldcalculator', first['OUTPUT'], 'Y_2000', 0, 10, 1, True, '@row_number', None)
processing.runandload('qgis:fieldcalculator', second['OUTPUT'], 'Y_2000', 0, 10, 1, True, '@row_number*(-1)', ) 
3
  • Welcome to GIS:SE @MarkZ! Ideally when asking a question about coding, you should include what you have :). Saying that, replace runandload with just runalg and when calling the qgis:fieldcalculator function, use None as the output at each step excluding the final step where you can specify the path of the final output and use runandload to load it immediately.
    – Joseph
    Feb 15, 2019 at 11:28
  • Thank You for Your answer, i updated OP. Actually that brings up second question, how to adress to layer from .runalg since it is not (?) saved anywhere?
    – MarkZ
    Feb 15, 2019 at 11:44
  • use first['OUTPUT'] and second ['OUTPUT'] in your second runalg and runandload method
    – Fran Raga
    Feb 15, 2019 at 11:56

1 Answer 1

2

You could use something like the following where you can call each step of the field calculator as an input to the next step. As each step saves the output temporarily in a dictionary, you can call it using ['OUTPUT_LAYER'] :

layerOut = 'layer.shp'
first = processing.runalg('qgis:fieldcalculator', 'S1_PROJ_1992[PTS]', 'X_2000', 0, 10, 1, True, '@row_number', None)
second = processing.runalg('qgis:fieldcalculator', first['OUTPUT_LAYER'], 'Y_2000', 0, 10, 1, True, '@row_number', None)
processing.runandload('qgis:fieldcalculator', second['OUTPUT_LAYER'], 'Y_2000', 0, 10, 1, True, '@row_number*(-1)', layerOut)
1
  • @MarkZ - Most welcome, glad it helped :)
    – Joseph
    Feb 15, 2019 at 12:13

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.