Apply QgsMapSettings then render via Python?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Apply QgsMapSettings then render via Python?

Larry_S
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

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Apply QgsMapSettings then render via Python?

Larry_S
Hi Martin,

Any suggestions on the following?

Larry

On Tue, Mar 4, 2014 at 2:39 PM, Larry Shaffer <[hidden email]> wrote:
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


_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Apply QgsMapSettings then render via Python?

Martin Dobias
In reply to this post by Larry_S
Hi Larry

(sorry for coming back to you so late, I have been offline / busy last week)

On Wed, Mar 5, 2014 at 4:39 AM, Larry Shaffer <[hidden email]> wrote:

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

the QgsMapSettings object inside QgsMapCanvas is meant to be read-only
(it is a const reference in c++, however there is no such thing in
Python). The configuration of the map in canvas should be set by
calling the setter methods directly on QgsMapCanvas instance, which
will make sure that appropriate signals are emitted (so that other
components can react to it). Some of the QgsMapSettings properties are
meant to be automatically handled by QgsMapCanvas (e.g. output image
size or DPI), but it makes sense to have a method like
"applyMapSettings(ms)" that would apply all the allowed settings from
an external QgsMapSettings.

Regards
Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer