Monday 27 February 2017

qgis - QgsMapRenderer to export current extent using PyQGIS


I am trying to accomplish PyQGIS simple map rendering via the QGIS 2.0 Python console, but end up with only a blank image. I've followed instructions of this link. My objective is to export a simple image of the current extent of layers of interest to a png. Where can I be going wrong?


#identify crs

crs = QgsCoordinateReferenceSystem(3297, QgsCoordinateReferenceSystem.EpsgCrsId)

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

render = QgsMapRenderer()

#set crs
render.setDestinationCrs(crs)

# set layer set

layers = qgis.utils.iface.legendInterface.layers()
lst = []
for layer in layers:
lst.append(layer.())

render.setLayerSet(lst)

# set to CURRENT extent**map of layer's full extent is successfully rendered when QgsRectangle(render.fullExtent() is used)
rect = QgsRectangle(render.extent())
rect.scale(1.1)

render.setExtent(rect)

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

# do the rendering
render.render(p)

p.end()


# save image
img.save("render.png","png")


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...