4

I created some geoalgorithms with QGIS and I need to use them in a Python script. By saving the layer obtained in a file having a specific path, I do not have problems.

For example:

processing.runalg("modeler:esplodilinee", "buildings", "C:\prova\sides.shp") 
lines = iface.addVectorLayer("C:\prova\sides.shp", "poligon sides", "ogr") 

But I would not use a file, but rather a temporary variable, in order to re-use it within the specific script. The description of the algorithm suggests to me that:

ALGORITHM: esplodilinee 
buildings <ParameterVector> 
OUTPUT_ALGQGISEXPLODELINES_1 <OutputVector> 

So, I thought to do something like this:

OUTPUT_ALGQGISEXPLODELINES_1 = processing.runalg("modeler:esplodilinee"
"buildings", None)
lines = processing.getObject(OUTPUT_ALGQGISEXPLODELINES_1)

But I get a mistake and not the desired results. Someone can help me?

Taras
  • 32,823
  • 4
  • 66
  • 137
Sara Gaudio
  • 123
  • 4

1 Answers1

2

It's correct to set the last parameter as None, so you create a .shp inside a temporary folder.

To get it: as you can see here the output of processing.runalg is a python dictionary.

So in your last code example you have to write OUTPUT_ALGQGISEXPLODELINES_1['OUTPUT'] instead of OUTPUT_ALGQGISEXPLODELINES_1 to get the path at the temporary layer.

The documentation is here and quite well written.

Oscar Campo
  • 556
  • 2
  • 17
  • Thank you a lot. Unfortunately I didn't solve the problem.

    If I correct my code as follow:

    OUTPUT_ALGQGISEXPLODELINES_1= processing.runalg ("modeler: esplodilinee", "buildings", None) lines = processing.getObjectFromUri(OUTPUT_ALGQGISEXPLODELINES_1['OUTPUT'])

    I get an error: "TypeError: 'NoneType' object has no attribute 'getitem' ". Do you suggest me why? Thanks again.

    – Sara Gaudio Apr 24 '18 at 09:57
  • The processing.runalg() doesn't work and return a None object...
    1. buildings is a QgsVectorLayer object?

    2. Your model is working well outside the script? The input parameter is only one?

    – Oscar Campo Apr 24 '18 at 10:03
  • 1)yes, it is a polygon layer. 2) the model works well outside the script and also when I fix a specific filepath (as in my first example) – Sara Gaudio Apr 24 '18 at 10:05
  • I edited my previous comment – Oscar Campo Apr 24 '18 at 10:06
  • 1
    Me too :) I would notice you that the error is linked to code line "lines = processing.getObjectFromUri(OUTPUT_ALGQGISEXPLODELINES_1['OUTPUT'])" and not to the model. – Sara Gaudio Apr 24 '18 at 10:10
  • Nope, doesn't work because OUTPUT_ALGQGISEXPLODELINES_1 isn't a dictionary but a None Type object. This because the runalg isn't working.... Try to run in the QGIS python console only the processing.runalg(...) untill it works :) – Oscar Campo Apr 24 '18 at 10:48
  • I found the error! :) I wrote a space between ":" and "esplodilinee". Now it works!

    prova=processing.runalg ("modeler:esplodilinee", buildings, None) lines = processing.getObject(prova['OUTPUT_ALGQGISEXPLODELINES_1'])

    Thank you so much!

    – Sara Gaudio Apr 24 '18 at 11:03
  • Well done :). If you find useful any of my aswer or comments please upvote them. I really appreciate it – Oscar Campo Apr 24 '18 at 11:20