2

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 OUTPUT_ALGQGISEXPLODELINES_1

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?

0

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.

  • 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 9: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

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.