4

I wanted to use temporary results in my pyqgis script just like

import processing
from qgis.core import *

carte_init=processing.getObject("carte_postes")

extent = "%s,%s,%s,%s" % ( carte_init.extent().xMinimum(), 
carte_init.extent().xMaximum(), carte_init.extent().yMinimum(), 
carte_init.extent().yMaximum() )

# Exrtacting test posts centroids
postes= processing.runalg('qgis:saveselectedfeatures', carte_init, None)
centroides_postes=processing.runalg('qgis:polygoncentroids', 
postes['OUTPUT'], None)
voronoi_max_extent=processing.runalg('grass7:v.voronoi', 
centroides_postes['OUTPUT'], False, False, extent,  -1, 0, 0, None) }

But I get the following when I run it in the python console...

Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 'OUTPUT'

As it works perfectly well in this script

import processing
from qgis.core import *

layer=processing.getObject("carte")
layer.invertSelection()

tampon_plus = processing.runalg('qgis:fixeddistancebuffer', layer, 
"20","5",False, None)
dissolve = processing.runalg('qgis:dissolve', tampon_plus['OUTPUT'], True, 
None, None)
carte_finale = processing.runalg('qgis:fixeddistancebuffer', 
dissolve['OUTPUT'], "-20","5",False, 
"c:/Users/LENOVO/Desktop/zones/carte_finale.shp")}

I wonder where is my mistake. I tried to use algorithm ['Result'] just as in Saving layer as temporary file usig PyQGIS? but it's not working any better.

1 Answer 1

4

You need to use ['OUTPUT_LAYER'] instead of ['OUTPUT']. You can see this if you type in the Python Console:

>>>import processing
>>>processing.alghelp('qgis:saveselectedfeatures')
ALGORITHM: Save selected features
    INPUT_LAYER <ParameterVector>
    OUTPUT_LAYER <OutputVector>

Both qgis:saveselectedfeatures and qgis:polygoncentroids algorithms require this.


So try running the following:

import processing
from qgis.core import *

carte_init=processing.getObject("carte_postes")

extent = "%s,%s,%s,%s" % ( carte_init.extent().xMinimum(), 
carte_init.extent().xMaximum(), carte_init.extent().yMinimum(), 
carte_init.extent().yMaximum() )

# Exrtacting test posts centroids
postes= processing.runalg('qgis:saveselectedfeatures', carte_init, None)
centroides_postes=processing.runalg('qgis:polygoncentroids', 
postes['OUTPUT_LAYER'], None)
voronoi_max_extent=processing.runalg('grass7:v.voronoi', 
centroides_postes['OUTPUT_LAYER'], False, False, extent,  -1, 0, 0, None)
2
  • 1
    Well saving my life once agin, I thought ['OUTPUT'] was the only way to get the output... Thans !
    – Clement
    Commented Jul 11, 2017 at 11:14
  • @Clement - Most welcome! There are three parameters of which I am aware: ['SAVENAME'], ['OUTPUT'] and ['OUTPUT_LAYER']. Just depends on the algorithm used. This is how processing does this. You can do this manually which was described in your link by using processing.getObject(postes['Results']) :)
    – Joseph
    Commented Jul 11, 2017 at 11:21

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.