Wednesday, 13 December 2017

grass - Getting Output names of Processing algorithms via PyQGIS


Calling the results of Processing algorithms in order to plot them to the screen differs depending on the provider used.


An example with QGIS provider:


tmpggis = processing.runalg("qgis:reprojectlayer",vektor,"EPSG:4326",None)
out = tmpggis["OUTPUT"]
qgisLyr = QgsRasterLayer(out, "Orfeo")
QgsMapLayerRegistry.instance().addMapLayer(qgisLyr)

So, for QGIS it is: ["OUTPUT"] - capital letters



For GRASS it is: ["output"] - lower case letters


What is the Output name for other providers such as Orfeo Toolbox, SAGA, etc.?



Answer



It may also depend on the algorithm. For example, the Ordinary Kriging from SAGA returns two outputs (VARIANCE and PREDICTION), but other SAGA algorithm outputs are called OUTPUT. So, you cannot just use a single output name for all algorithms in a provider.


However, there is a way to get output parameter names from any Processing algorithm. We can define the following function (for instance in the QGIS Python console):


import processing

def getAlgOutputNames( algName ):
for output in processing.Processing.getAlgorithm(algName).outputs:
print output.name


You can call the function in this way, giving it algorithm names you're interested in:


>>> getAlgOutputNames("grass7:v.surf.idw")
output
>>> getAlgOutputNames("qgis:creategrid")
OUTPUT
>>> getAlgOutputNames("saga:ordinarykriging")
PREDICTION
VARIANCE
>>> getAlgOutputNames("gdalogr:clipvectorsbypolygon")

OUTPUT_LAYER
>>> getAlgOutputNames("lidartools:lasmerge")
OUTPUT_LASLAZ
>>> getAlgOutputNames("taudem:dinfinityavalancherunout")
-rz
-dfs
>>>getAlgOutputNames("otb:radiometricindices")
-out

Note: Remember that you can get algorithm names calling processing.alglist() from the QGIS Python console.



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