Saturday 23 March 2019

pyqgis - Run QGIS layer Action on all selected features


QGIS is such an amazing tool with lots of useful features, so that it's easy to overlook some of them.


Just to be sure it's not already implemented in QGIS 2.18:


Is there any Attribute Table option to run a layer Action on all currently selected features?


I'm using a Processing script as workaround, but it would be nice to have this feature directly implemented in the Attribute Table.


##MyTools=group
##VectorLayer=vector
##action_name=string

from qgis.core import *
import qgis.utils
from qgis.gui import *

layer = processing.getObject(VectorLayer)
actionManager = layer.actions()
actions = actionManager.listActions()
if len(actions) > 0:
cont = False
i = -1

for a in actions:
i = i+1
if a.name() == action_name:
cont = True
break

if cont:
selFeatures = layer.selectedFeatures()
if len(selFeatures) > 0:
for feature in selFeatures:

actionManager.doActionFeature(0,feature)
else:
qgis.utils.iface.messageBar().pushMessage("Processing Error"," No features selected.",level= QgsMessageBar.WARNING,duration=5)
else:
qgis.utils.iface.messageBar().pushMessage("Processing Error"," Wrong layer action defined.",level= QgsMessageBar.WARNING,duration=5)
else:
qgis.utils.iface.messageBar().pushMessage("Processing Error"," No layer action found.",level= QgsMessageBar.WARNING,duration=5)


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