1

I am working with a standalone PyQGIS script below (that was given as an answer to How to iterate over layers and export them as PNG images with PyQGIS in a standalone script). The script exports the "boundary, climits, and Div1_Irrig_1956_0" layers as one png. The script then exports the "boundary, climits, and Div1_Irrig_1976_1" layers as another png and so on. I am trying to add to the code a way to include a text element in each export process that reflects the Div1_Irrig... layer name.

enter image description here

from qgis.core import QgsApplication, QgsMapLayerRegistry, QgsVectorLayer, QgsProject
from qgis.gui import QgsMapCanvas, QgsMapCanvasLayer, QgsLayerTreeMapCanvasBridge
from PyQt4.QtCore import QTimer, QSize
import sys, os
import time

qgisApp = QgsApplication([], True)
qgis_prefix = "C:\\OSGeo4W\\apps\\qgis"
qgisApp.setPrefixPath(qgis_prefix, True)
qgisApp.initQgis()

# Required variables with your shapefile paths and names
pngsPath = 'E:/IrrigatedLands/FC_qgis/'
boundaryLayer = QgsVectorLayer("E:\\IrrigatedLands\\FC_qgis\\boundary.shp", 'boundary', 'ogr')
climitsLayer = QgsVectorLayer("E:\\IrrigatedLands\\FC_test\\citylimits\\climits.shp", 'climits', 'ogr')
otherLayers = {'Div1_Irrig_1956_0': QgsVectorLayer("E:\\IrrigatedLands\\FC_qgis\\Div1_Irrig_1956_0.shp", 'Div1_Irrig_1956_0', 'ogr'), 
    'Div1_Irrig_1976_1':QgsVectorLayer("E:\\IrrigatedLands\\FC_qgis\\Div1_Irrig_1976_1.shp", 'Div1_Irrig_1976_1', 'ogr'), 
    'Div1_Irrig_1987_2': QgsVectorLayer("E:\\IrrigatedLands\\FC_qgis\\Div1_Irrig_1987_2.shp", 'Div1_Irrig_1987_2', 'ogr')}
count = 0    

canvas = QgsMapCanvas()
canvas.resize(QSize(500, 500)) # You can adjust this values to alter image dimensions
#canvas.show()

# Add layers to map canvas taking the order into account
QgsMapLayerRegistry.instance().addMapLayer(boundaryLayer)
QgsMapLayerRegistry.instance().addMapLayers(otherLayers.values() )
QgsMapLayerRegistry.instance().addMapLayer(climitsLayer)
layerSet = [QgsMapCanvasLayer(climitsLayer)]
layerSet.extend([QgsMapCanvasLayer(l) for l in otherLayers.values() ])
layerSet.append(QgsMapCanvasLayer(boundaryLayer))
canvas.setLayerSet( layerSet )


# Link Layer Tree Root and Canvas
root = QgsProject.instance().layerTreeRoot()
bridge = QgsLayerTreeMapCanvasBridge(root, canvas)

def prepareMap(): # Arrange layers
    for lyr in otherLayers.values(): # make all layers invisible
        base = os.path.basename(lyr)
        name = os.path.splitext(base)[0]
        layerOn = "" # When required layer is turned on this will be the layer name to output
        root.findLayer( lyr.id() ).setVisible(0) # Unchecked

        for lyr in otherLayers:
            lyr.visible = False
            if lyr.name == "climits":
                lyr.visible = True
            if lyr.name == "boundary":
                lyr.visible = True
            if lyr.name == name:
                lyr.visible = True
                layerOn = lyr.name

            if layerOn:
                print layerOn

    root.findLayer( otherLayers.values()[count].id() ).setVisible(2) # Checked
    canvas.zoomToFullExtent()
    QTimer.singleShot(8000, exportMap) # Wait a second and export the map
    return

def exportMap(): # Save the map as a PNG
    global count # We need this because we'll modify its value
    canvas.saveAsImage( pngsPath + otherLayers.keys()[count] + ".png" )
    print "Map with layer",otherLayers.keys()[count],"exported!"
    if count < len(otherLayers)-1:
        QTimer.singleShot(8000, prepareMap) # Wait a second and prepare next map
    else: # Time to close everything
        qgisApp.exitQgis()
        qgisApp.exit() 
    count += 1
    return

prepareMap() # Let's start the fun
qgisApp.exec_()
brgionta
  • 507
  • 5
  • 14
  • 1
    Where do you exactly want the text to appear? What have you tried? You must do your best when posting questions (not the first time I tell you this) to not make people who want to help you waste their time. – Germán Carrillo May 02 '16 at 13:18
  • I've edited the code above to try to include the Div1_Irrig... layer name as a text element in the export process. Not sure if I am going down the right path. Thank you! – brgionta May 03 '16 at 08:10

0 Answers0