[Qgis-developer] Apply QgsMapSettings then render via Python?

Larry Shaffer larrys at dakotacarto.com
Tue Mar 4 13:39:35 PST 2014


Hi Martin, et al.,

Maybe I'm missing something elemental here, but I can't seem to find a
simple way to apply my built-up QgsMapSettings to QgsMapCanvas or
QgsMapRenderer, initiate a render from Python, then save into image on disk.

This is what I would like to do (for simple screen-res renders):

cnv = iface.mapCanvas()
ms = cnv.mapSettings()  <-- or create fresh: ms = QgsMapSettings()
(manipulate map settings)
cnv.applyMapSettings(ms)  <-- shouldn't this be possible?
cnv.refresh()  <-- since cnv.map().render() is stubbed now
cnv.saveAsImage(my_imgpath)

This is functioning (for creating unit test control images), but seems
excessive:

ms = my_map_settings
pal = QgsPalLabeling()
pal.init(ms)  <-- is this necessary?
r = QgsMapRenderer()
r.setLabelingEngine(pal)

# couldn't the next lines be covered by a r.applyMapSettings(ms) function?
r.setOutputSize(ms.outputSize(), ms.outputDpi())
r.setMapUnits(ms.mapUnits())
r.setExtent(ms.extent())
r.setProjectionsEnabled(ms.hasCrsTransformEnabled())
r.setDestinationCrs(ms.destinationCrs())
r.setLayerSet(ms.layers())

image = QImage(ms.outputSize(), QImage.Format_ARGB32)
image.fill(ms.backgroundColor().rgb())
image.setDotsPerMeterX(ms.outputDpi() / 25.4 * 1000)
image.setDotsPerMeterY(ms.outputDpi() / 25.4 * 1000)

p = QPainter(image)
r.render(p)
p.end()
image.save(my_imgpath, 'png'):


This is already convenient in QgsComposition, using QgsComposition( const
QgsMapSettings& mapSettings ), then just render the page.

Any ideas on the best approach here?

Regards,

Larry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20140304/cee85fe4/attachment.html>


More information about the Qgis-developer mailing list