1

I am using QGIS 2.18.7 on Windows 7. I am attempting to write a pyqgis script that will create a map rendering and save it, using a raster layer, a polygon layer, a line layer and a point layer. The polygon layer and the raster layer are already in the project at the time the script is run. The point layer and line layer are created within the script

The problem I am running into is that in the rendering, only the raster and the line layer appear - the point layer and the polygon layer do not seem to appear in the rendering anywhere, and the line layer does not have the proper symbology used.

I am currently using QgsMapRenderer to do this operation.

from qgis.core import *
from qgis.utils import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os.path

##Map_Save_Folder=folder
##Map_Save_Name=string roadmap

# Create save file location
mapName = "%s.png" %Map_Save_Name
outfile = os.path.join(Map_Save_Folder,mapName)

# Create point and line layers for later
URIstrP = "Point?crs=EPSG:3035"
layerP = QgsVectorLayer(URIstrP,"pointsPath","memory")
provP = layerP.dataProvider()
URIstrL = "LineString?crs=EPSG:3035"
layerL = QgsVectorLayer(URIstrL,"linePath","memory")
provL = layerL.dataProvider()

# Add points to point layer
feat1 = QgsFeature()
feat2 = QgsFeature()
feat3 = QgsFeature()
feat1.setGeometry(QgsGeometry.fromPoint(QgsPoint(5200000,2600000)))
feat2.setGeometry(QgsGeometry.fromPoint(QgsPoint(5300000,2800000)))
provP.addFeatures([feat1, feat2])

# Add line to line layer
feat3.setGeometry(QgsGeometry.fromPolyline([feat1.geometry().asPoint(),feat2.geometry().asPoint()]))
provL.addFeatures([feat3])

# Set symbology for line layer
symReg = QgsSymbolLayerV2Registry.instance()
metaRegL = symReg.symbolLayerMetadata("SimpleLine")
symLayL = QgsSymbolV2.defaultSymbol(layerL.geometryType())
metaL = metaRegL.createSymbolLayer({'width':'1','color':'0,0,0'})
symLayL.deleteSymbolLayer(0)
symLayL.appendSymbolLayer(metaL)
symRendL = QgsSingleSymbolRendererV2(symLayL)
layerL.setRendererV2(symRendL)

# Set symbology for point layer
metaRegP = symReg.symbolLayerMetadata("SimpleMarker")
symLayP = QgsSymbolV2.defaultSymbol(layerP.geometryType())
metaP = metaRegP.createSymbolLayer({'size':'3','color':'0,0,0'})
symLayP.deleteSymbolLayer(0)
symLayP.appendSymbolLayer(metaP)
symRendP = QgsSingleSymbolRendererV2(symLayP)
layerP.setRendererV2(symRendP)

# Load the layers
QgsMapLayerRegistry.instance().addMapLayer(layerP)
QgsMapLayerRegistry.instance().addMapLayer(layerL)

# Start the map rendering
visImg = QImage(QSize(1600,1200),QImage.Format_ARGB32_Premultiplied)
# Background color
visColor = QColor(255,255,255)
visImg.fill(visColor.rgb())
# Create Painter
visPainter = QPainter()
visPainter.begin(visImg)
visPainter.setRenderHint(QPainter.Antialiasing)
visRender = QgsMapRenderer()
# set rendering window
layerList = []
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    layerList.append(layer.id())
visRender.setLayerSet(layerList)
# set viewing window
visExtent = QgsRectangle(layerP.extent())
visExtent.scale(1.25)
visRender.setExtent(visExtent)
visRender.setOutputSize(visImg.size(),visImg.logicalDpiX())
visRender.render(visPainter)
visPainter.end()
visImg.save(outfile,"png")

When I run this script, the QGIS browser window looks like - the polygon vector layer (the purple thing) is clearly visible:enter image description here

However, the image that is saved looks different. The polygon vector layer is nowhere to be seen, and the line and point layers do not match their symbology:Map in the QGIS Browser window

How do I get the vectors layer to show up properly in the saved image?

2
  • @EastWest - Does it work if you do it manually (e.g. Project > Save as Image...)? What happens if you add qgis.utils.iface.mapCanvas().refresh() before saving it as an image?
    – Joseph
    Jun 22, 2017 at 10:39
  • @Joseph I just tried adding iface.mapCanvas().refresh(), as well as a layer.triggerRepaint() for each layer, and got the same result as above. When I try saving as an image, it works successfully and shows the vector layer.
    – EastWest
    Jun 22, 2017 at 10:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.