Skip to content

Instantly share code, notes, and snippets.

@gacarrillor
Created October 4, 2016 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gacarrillor/a37615d0fee1d0c11628cbf5ee12f242 to your computer and use it in GitHub Desktop.
Save gacarrillor/a37615d0fee1d0c11628cbf5ee12f242 to your computer and use it in GitHub Desktop.
"Run" method of a QGIS test plugin to export a composer iterating layers inside a group and making them visible one at a time.
def run(self): # Run method of a QGIS plugin
ruta="/tmp/"
# The plugin Maps Printer is required!!!
mp = qgis.utils.plugins['MapsPrinter']
composer = self.iface.activeComposers()[0] # We'll only use the first composer
extension = '.png'
# Get the group to start iterating layers (making only 1 layer visible at a time)
group = QgsProject.instance().layerTreeRoot().findGroup( 'DEPTOS' )
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
group.setVisible(0) # Make all the group (and its children) not visible
self.iface.legendInterface().setLayerVisible( layer, True )
title = ruta + layer.name()
for item in composer.composition().items():
if isinstance(item, QgsComposerLegend):
item.updateLegend() # Needed to always get an updated legend
break
mp.exportCompo( composer, ruta, title, extension )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment