How do I export specific layers as a png
from a QGIS .qgs map?
From the table of contents the script would first export the "boundary, climits, and Div1_Irrig_1956_0" layers as one png
. The standalone script would then iterate to export "boundary, climits, and Div1_Irrig_1976_1" layers as the next png
and so on. I am working with the script below to begin with but am only getting one png
with all of the layers exported.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
import sys, os
import glob
qgs = QgsApplication(sys.argv, True)
qgis_prefix = "C:\\OSGeo4W\\apps\\qgis"
QgsApplication.setPrefixPath(qgis_prefix, True)
qgs.initQgis()
#layers = glob.glob((configData['destination_folder'])+"\\*.shp")
layers = glob.glob(r"E:\IrrigatedLands\FC_qgis\*.shp")
for layer in layers:
print layer
irrig = QgsVectorLayer(layer, "testlayer_shp", "ogr")
print irrig.isValid()
layerset = []
QgsMapLayerRegistry.instance().addMapLayer(irrig)
layerset.append(irrig.id())
# create image
imageType = "png"
pathToFile = "C:\\Users\\James\\Desktop\\"
name = "render"
img = QImage(QSize(800, 600), QImage.Format_ARGB32_Premultiplied)
# set image's background color
color = QColor(255, 255, 255)
img.fill(color.rgb())
# create painter
p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)
render = QgsMapRenderer()
# set layer set
layer_set = [irrig.id()] # add ID of every layer
print layer_set
render.setLayerSet(layer_set)
# set extent
rect = QgsRectangle(render.fullExtent())
rect.scale(1.1)
render.setExtent(rect)
# set output size
render.setOutputSize(img.size(), img.logicalDpiX())
# do the rendering
render.render(p)
p.end()
img.save(pathToFile + name + "." + imageType ,imageType)
Best Answer
In order to solve this question, we need to use timers or something that delays the execution of the script, so the canvas can reflect the layer arrangement at the time the map is exported. In other words, if you don't use timers you'll end up with 3 PNG images with the same content because everything will happen too fast.
In the QGIS map, set the map extent you want to export before running the following script in the QGIS Python Console (adjust the path ):
After the execution of the script, you'll end up with 3 (different) PNG images in
/tmp/
.If you need to iterate over more layers, you just need to add their names to the
otherLayers
list, the script will do the rest for you.----------------------------------------------------------------
EDIT: How to run this as a standalone script (outside of QGIS)?
The following script can be run outside of QGIS. Just make sure you adjust the file paths to your own directory structure and that you use a QGIS prefix that works for your own environment (see this answer for details):
Again, if you need to iterate over more layers, just add them to the
otherLayers
dictionary, the script will do the rest.The resulting PNG image file names will correspond to your layers.