[QGIS-Developer] outdated cookbook

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[QGIS-Developer] outdated cookbook

Raymond Nijssen
Hi all,

I'm working on a qgis3 plugin, and want it to export my map as a png
image. According to the cookbook it should go like this:

https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/composer.html#simple-rendering

But QgsMapRenderer has been removed in v3, so I will need other code for
this. I have been trying all kinds of stuff, using QgsMapRendererJob and
QgsMapRendererSequentialJob but I cannot get it to work.
Even worse, qgis crashes all the time while I'm trial-and-erroring this.

Can anyone provide a working example for exporting maps to png files?

I'd be happy to work on updating the cookbook, for example during the
Foss4g Europe code sprint in 2 weeks. But I will need some input from
the people knowing the new object model.

Kind regards,
Raymond
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: outdated cookbook

Jürgen E. Fischer
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

--
Jürgen E. Fischer           norBIT GmbH             Tel. +49-4931-918175-31
Dipl.-Inf. (FH)             Rheinstraße 13          Fax. +49-4931-918175-50
Software Engineer           D-26506 Norden             http://www.norbit.de

_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

signature.asc (844 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: outdated cookbook

Raymond Nijssen
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
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: outdated cookbook

Martin Dobias
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 <[hidden email]> 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
> [hidden email]
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer