In QGIS I have the layer tree with the following layers:

  1. Roads
  2. Transition
  3. Differences
  4. Study_area

I would like to iterate over the layers and export two different images where Roads and Differences always remain "On". At the end I would like to get these two Images:

  1. Roads and Differences + Transition
  2. Roads and Differences + Study_area

I have tried to use the script published in the post How to iterate over layers and export them as PNG images with PyQGIS in a standalone script to become the respective Images, the Code that I am using is:

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

qgisApp = QgsApplication([], True)
qgisApp.prefix="C:\\Program Files\\QGIS 2.18\\apps\\qgis"
qgisApp.setPrefixPath(qgis_prefix, True)

# Required variables with your shapefile paths and names
pngsPath = 'D:/QGIS_environment/'
RoadsLayer = QgsVectorLayer('D:/QGIS_environment/test_datei/Roads.shp', 'Roads', 'ogr')
DifferencesLayer = QgsVectorLayer('D:/QGIS_environment/test_datei/Differences.shp', 'Differences', 'ogr')
otherLayers = {'Transicion': QgsVectorLayer('D:/QGIS_environment/test_datei/Transicion.shp', 'Transicion', 'ogr'), 
    'Study_area':QgsVectorLayer('D:/QGIS_environment/test_datei/Study_area', 'Study_area', 'ogr')}
count = 0    

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

# Add layers to map canvas taking the order into account
QgsMapLayerRegistry.instance().addMapLayer( RoadsLayer)
QgsMapLayerRegistry.instance().addMapLayers( otherLayers.values() )
QgsMapLayerRegistry.instance().addMapLayer( DifferencesLayer )
layerSet = [QgsMapCanvasLayer(DifferencesLayer)]
layerSet.extend([QgsMapCanvasLayer(l) for l in otherLayers.values() ])
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
        root.findLayer( lyr.id()).setVisible(0) # Unchecked
    root.findLayer( otherLayers.values()[count].id()).setVisible(2) # Checked
    QTimer.singleShot(1000, exportMap) # Wait a second and export the map

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(1000, prepareMap) # Wait a second and prepare next map
    else: # Time to close everything
    count += 1

prepareMap() # Let's start the fun

But I keep getting the message:

root.findLayer( lyr.id()).setVisible(0) # Unchecked AttributeError: 'NoneType' object has no attribute 'setVisible'

What could be the problem?

  • 4
    please show us the exact code you are using – Ian Turton May 28 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.