3

I'm trying to learn a little bit of Python in QGIS with the following tutorial: QGIS Maprendering.

Now the tutorial works, with minor adjustments. In QGIS I have four layers, and one them is showing labels (country names), but when I export to PNG or PDF the labels of the layer won't show in the output file.

enter image description here

My code:

from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import *
import processing
from qgis.PyQt.QtCore import *

img = QImage(QSize(1200, 1000), QImage.Format_ARGB32_Premultiplied)
color = QColor(250, 250, 250)
img.fill(color.rgb())
p = QPainter()
p.begin(img)

p.setRenderHint(QPainter.Antialiasing)
render = QgsMapRenderer()

layers = iface.mapCanvas().layers()
print layers

layers_ids = []
for layer in layers:
    print layer.id()
    layers_ids.append(layer.id())   

render.setLayerSet(layers_ids)
rect = QgsRectangle((iface.mapCanvas().extent().xMinimum()),(iface.mapCanvas().extent().yMinimum()),(iface.mapCanvas().extent().xMaximum()),(iface.mapCanvas().extent().yMaximum()))
rect.scale(1.1)
render.setExtent(rect)

render.setOutputSize(img.size(), img.logicalDpiX())
render.render(p)

p.end()

img.save("map2.png","png")

Is there anyone who can help me with this problem?

1
  • Not sure about that code snippet, another way to create an image from canvas extent is: iface.mapCanvas().saveAsImage( 'C:/Temp/image.png')
    – artwork21
    Commented Dec 23, 2016 at 21:45

0

Your Answer

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

Browse other questions tagged or ask your own question.