2

I have a console script where I'm trying to iterate through saved bookmarks from the open project to have the map pan/zoom to the bookmark extent. I would like to eventually save each map canvas as pdf file. I can manually set the right parameters to cycle through the bookmarks and the canvas refreshes each time I set the extent.

However, if I cycle through the saved bookmarks and set the canvas extent in a for loop (as below), the canvas does not refresh with each extent change, it only refreshes once the script has run it's course. I've added sleep commands in the for loop to allow for redraw time, but that didn't work. Am I missing something simple?

bmarks = QgsApplication.bookmarkManager()

bmarks.bookmarks()

for b in bmarks.bookmarks(): canv = iface.mapCanvas() canv.setExtent(b.extent()) iface.mapCanvas().refresh()

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Bryan Taylor
  • 188
  • 9
  • Please review the answers in this post: https://gis.stackexchange.com/questions/231446/pyqgis-make-screenshot-of-mapcanvas-after-setextent-is-called – Kadir Şahbaz Nov 25 '20 at 10:39

2 Answers2

5

I see that your request is for the canvas, but it seems the ultimate goal is a file based output, so I'll answer that one instead.

You don't need to update the canvas itself. You can also very well do this in jobs that render to images.

bmarks = QgsApplication.bookmarkManager()
bmarks.bookmarks()

settings = iface.mapCanvas().mapSettings()

for i, b in enumerate(bmarks.bookmarks()): settings.setExtent(b.extent()) renderer = QgsMapRendererParallelJob(settings)

event_loop = QEventLoop()
renderer.finished.connect(event_loop.quit)
renderer.start()
event_loop.exec_()

img = renderer.renderedImage()
img.save('/tmp/img{}.png'.format(i))

The advantage of this approach is also that you are able to fine tune parameters in the settings, e.g. width and height of the rendered image (by default it depends on the window size) or that you can have a transparent background. See https://qgis.org/api/classQgsMapSettings.html for details.

You could even launch several background tasks in parallel for this, this is left as an exercise for the reader.

Matthias Kuhn
  • 27,780
  • 3
  • 88
  • 129
  • Excellent. Thanks for the help. Is there any trick to rendering to geopdf? I'll try it out and see if there is success on my end. Cheers...I do however like to see the render taking place while I'm working in the console because I can "see" it working rather than having to check directory outputs. – Bryan Taylor Nov 26 '20 at 06:48
  • I would possibly exclude python and explore layout atlas functionality / processing functionality
  • Add some print statements
  • – Matthias Kuhn Nov 26 '20 at 11:04
  • How difficult would it be to automate a process to auto-open a project and have an atlas output pdf map products? Would I use a standalone script entirely, or would it be a combination of a standalone script and a python script embedded within the project that runs when the project opens? Ideally I'd like cron to open the project on a scheduled basis to produce map outputs, but also be able to manually open the project and get the same results on demand. Anyhow, thanks for your direction, I'll dive into atlas and see what happens! – Bryan Taylor Nov 26 '20 at 21:16
  • That deserves a completely new question with the complete scope included. In the answer you will be redirected towards qgis_process, processing and layouts. – Matthias Kuhn Nov 26 '20 at 21:37