1

I'm trying to render a layer with the given .qml style into .png image using a standalone PyQGIS application. The issue is that not all the layers of the symbology are rendered, e.g. a polygon with the colour fill, pattern fill and the svg-marker outline will be rendered with only colour fill, but pattern fill and svg-marker outline will be missing. And during execution of the script I get this (seems to be Qt-error) message: QPainter::begin: Paint device returned engine == 0, type: 3. Tried to google this error but found nothing helpful. QGIS 2.14.

The code:

from qgis.core import *
from PyQt4.QtCore import QSize
from PyQt4.QtGui import QPainter, QImage, QColor

qgisApp = QgsApplication([], False)
QgsApplication.setPrefixPath('/usr', True)
QgsApplication.initQgis()
print QgsApplication.showSettings()


lyr = QgsVectorLayer('bbox.shp', 'bbox', 'ogr')
lyr_id = lyr.id()
QgsMapLayerRegistry.instance().addMapLayer(lyr)
lyr.loadNamedStyle( 'bbox.qml')


# create image
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)

layers = QgsMapLayerRegistry.instance().mapLayers()
lst = layers.keys()
print 'lst', lst
renderer = QgsMapRenderer()
renderer.setLayerSet(lst)

extent = renderer.fullExtent()
rect = QgsRectangle(extent)
# print rect.toString()
rect.scale(1.1)
renderer.setExtent(rect)

# set output size
renderer.setOutputSize(img.size(), img.logicalDpiX())

# do the rendering
renderer.render(p)
p.end()

# save image
img.save('test.png', 'png')

QgsApplication.exitQgis()

A test layer and style can be downloaded here.

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.