Thursday 28 May 2015

pyqgis - How can I get information displayed at tooltips for each algorithm's name at Processing Toolbox for QGIS 3?


I use following code for searching adequate id name for each algorithm presents in Processing Toolbox (gdal, grass7, qgis, saga) for QGIS 3.


from os import listdir
from os.path import isfile, join

gdal_path_algs = processing.algs.gdal.__path__[0]

grass_path_algs = processing.algs.grass7.__path__[0] + '/description'
qgis_path_algs = processing.algs.qgis.__path__[0]
saga_path_algs = processing.algs.saga.__path__[0] + '/description'

gdal_algs = [ 'gdal:' + file[:-3].lower() for file in listdir(gdal_path_algs) if isfile(join(gdal_path_algs, file)) ]
grass_algs = [ 'grass7:' + file[:-4].lower() for file in listdir(grass_path_algs) if isfile(join(grass_path_algs, file)) ]
qgis_algs = [ 'qgis:' + file[:-3].lower() for file in listdir(qgis_path_algs) if isfile(join(qgis_path_algs, file)) ]
saga_algs = [ 'saga:' + file[:-4].lower() for file in listdir(saga_path_algs) if isfile(join(saga_path_algs, file)) ]

#print (gdal_algs)

#print (grass_algs)
#print (qgis_algs)
print (saga_algs)

However, when list algorithms is printed at Python Console, for instance saga, some of them has special characters ("-", "(", ")") or author's name of algorithm and id's name doesn't work for retrieving parameters list by using 'algorithmHelp' processing method.


Afterwards, I founded out that tooltips displayed when I put cursor over algorithm's name at Processing Toolbox (see following image) had correct information I need. How can I get information displayed at tooltips for each algorithm's name at Processing Toolbox for QGIS 3 as alternative approach of my above code?


enter image description here



Answer



Perhaps it might be better to obtain the algorithms' details directly from the QgsApplication::processingRegistry(), you could then store the display names and algorithm ID in a dictionary (or list). Then you can call the ID by entering the name displayed in the Processing Toolbox or from the tooltip:


gdal_algs = {}

grass_algs = {}
qgis_algs = {}
saga_algs = {}

for algs in QgsApplication.processingRegistry().algorithms():
if 'gdal' in algs.id():
gdal_algs[algs.displayName()] = 'Algorithm ID: ' + algs.id()
if 'grass7' in algs.id():
grass_algs[algs.displayName()] = 'Algorithm ID: ' + algs.id()
if 'qgis' in algs.id():

qgis_algs[algs.displayName()] = 'Algorithm ID: ' + algs.id()
if 'saga' in algs.id():
saga_algs[algs.displayName()] = 'Algorithm ID: ' + algs.id()

Then use the following to get the ID of your chosen algorithm:


saga_algs['Imcorr - feature tracking']

>>>'Algorithm ID: saga:imcorrfeaturetracking'

I'm not sure how to access the tooltip directly to get the same info.





EDIT:


You could also just bundle all algorithms into a single dictionary and call the algorithmHelp() function for the tool you're interested in:


algorithms = {}
for algs in QgsApplication.processingRegistry().algorithms():
algorithms[algs.displayName()] = algs.id()

processing.algorithmHelp(algorithms['Imcorr - feature tracking'])

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