[Qgis-developer] Map Canvas Current Zoomed Extent

Martin Dobias wonder.sk at gmail.com
Mon Jan 13 08:47:29 PST 2014


On Mon, Jan 13, 2014 at 11:33 PM, bjorn.burr.nyberg at gmail.com
<bjorn.burr.nyberg at gmail.com> wrote:
> Related to the second portion of my question regarding getting an image of
> the rendered canvas extent as an output i've tried to implement the
> following as modified from the pyqgis handbook
>
> from qgis.core import *
> from qgis.gui import *
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> from PyQt4.QtXml import *
>
> output = r'C:\output.png'
>
> mc = iface.mapCanvas()
> rect = mc.extent()
> img = QImage(QSize(800,600), QImage.Format_ARGB32_Premultiplied)
> color = QColor(255,255,255)
> img.fill(color.rgb())
> p = QPainter()
> p.begin(img)
> p.setRenderHint(QPainter.Antialiasing)
> render = QgsMapRenderer()
> render.setExtent(rect)
> render.setOutputSize(img.size(), img.logicalDpiX())
> render.render(p)
> p.end()
> img.save(output,"png")
>
> however the resulting image that I receive is simply a blank white image

You also need to specify what layers should be drawn by the
QgsMapRenderer you create:
render.setLayerSet( ... )
The argument should be a list of layer IDs. You can get list of layers
from map canvas like this:
layers = mc.mapRenderer().layerSet()

> (from the im.fill step) in addition im not entirely sure how the size of the
> QImage can be related to the current extent rect?

Basically the extent says which part of the world to draw, the size of
the image says how big the resulting rendered image should be. So they
are independent.

Regards
Martin


More information about the Qgis-developer mailing list