I have many vector layers which I want to display and print, one at a time. The following script works well for producing an image from the map canvas, but I want to produce an image from a print composer and that part does not work - the images from the print composer show all the layers visible in the map canvas at the time the script is run.
from PyQt4.QtCore import * from PyQt4.QtGui import * outputFolder = "C:/Users/fsc/Documents/Temp/" layers = iface.legendInterface().layers() settings = iface.mapCanvas().mapSettings() for lyr in layers: # display layer and wait for completion settings.setLayers([lyr.id()]) job = QgsMapRendererParallelJob(settings) job.start() job.waitForFinished() # save image from map canvas image = job.renderedImage() image.save(outputFolder + lyr.name() + ".png") # generate image from print composer c = iface.activeComposers().composition() c.refreshItems() dpi = c.printResolution() dpmm = dpi / 25.4 width = int(dpmm * c.paperWidth()) height = int(dpmm * c.paperHeight()) # create output image and initialize it image = QImage(QSize(width, height), QImage.Format_ARGB32) image.setDotsPerMeterX(dpmm * 1000) image.setDotsPerMeterY(dpmm * 1000) image.fill(0) # render the composition imagePainter = QPainter(image) sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight()) targetArea = QRectF(0, 0, width, height) c.render(imagePainter, targetArea, sourceArea) imagePainter.end() # save image from print composer image.save(outputFolder + lyr.name() + "-comp.png", "png")
I have tried turning the visibility of all layers off and then turning each on inside the loop before producing the output, but in that case the print composer just produces blank maps. (I think that would have worked before the introduction of multi-threaded rendering - MTR - but not now.) Any ideas anyone?