[QGIS-Developer] outdated cookbook

Martin Dobias wonder.sk at gmail.com
Tue Jul 10 04:16:06 PDT 2018


Hi Raymond!

Try the following snippet in the console - it will render the
currently selected layer and save it as a PNG file:

layer = iface.activeLayer()

settings = QgsMapSettings()
settings.setOutputSize(QSize(512,512))
settings.setExtent(layer.extent())
settings.setLayers([layer])

job = QgsMapRendererSequentialJob(settings)
job.start()
job.waitForFinished()
img = job.renderedImage()
img.save("/tmp/rendered.png")

When you call job.start(), it will start rendering in background and
immediately return. That's why you need to call job.waitForFinished()
to block your code until the final image is ready. This approach has a
downside that it blocks the main thread and therefore the GUI will be
blocked until rendering has finished. If you would like to avoid this
blocking, after job.start() you could have a slot that would listen to
finished() signal coming from "job" - and save the image there. This
approach makes the code a bit more complex, but the GUI does not get
blocked and you can even start multiple jobs at a time in parallel.

Cheers
Martin



On Mon, Jul 9, 2018 at 11:21 AM, Raymond Nijssen <r.nijssen at terglobo.nl> wrote:
> Thanks Jurgen!
>
> That kind of works for my current problem (though it seems hard to loop this
> function and export different maps after updating the mapcanvas).
>
> Still I would like to fix this in the cookbook using the
> QgsMapRenderer(Sequential)Job in the right way. So if anyone could post a
> working example??
>
> Thanks!
> Raymond
>
>
>
> On 05-07-18 18:17, Jürgen E. Fischer wrote:
>>
>> Hi Raymond,
>>
>> On Thu, 05. Jul 2018 at 16:53:21 +0200, Raymond Nijssen wrote:
>>>
>>> Can anyone provide a working example for exporting maps to png files?
>>
>>
>> Probably not a nice example for the cookbook, but this does the job and
>> even
>> works alike on 2 and 3 (essentially what QgisApp::saveAsImage does):
>>
>> c = iface.mapCanvas()
>> pm = QPixmap(c.width(), c.height())
>> c.saveAsImage("/tmp/foo.png", pm)
>>
>>
>> Jürgen
>>
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list