[prev in list] [next in list] [prev in thread] [next in thread] 

List:       qgis-user
Subject:    Re: [Qgis-user] Accessing temporal controller stuff via the python console
From:       Nyall Dawson <nyall.dawson () gmail ! com>
Date:       2022-01-25 21:55:43
Message-ID: CAB28AsjkJ+oxoGQ82N8ebiNCRUUnz-cQTJ8dme9rn8gkX-W7WA () mail ! gmail ! com
[Download RAW message or body]

On Wed, 26 Jan 2022 at 07:30, Cory Albrecht <maps@hanfastolfe.com> wrote:
> 
> Thanks.
> 
> While I no longer got a whole bunch of similar images, instead I got a series of \
> partially rendered canvases in triplets. First the static, non-temporal-data layers \
> and a few features of one of the temporal layers. Second is all the layers but no \
> labels. Third is with the labels (including the date of the frame). Each triplet \
> appears to be from the same time frame selected with setCurrentFrameNumber(), but \
> the third image of the triplet shows a date that is is three items in the list \
> after the previous one.

As you've found, saving the canvas is a fragile approach. Calling
canvas.saveAsImage() will save **whatever** is currently shown in the
canvas to an image, even if it's only a half-rendered map!

The better approach is using the QgsMapRenderer classes directly,
which allow you to render maps as images without going through the
canvas at all. This has many benefits:

- The extent, scale, and image size can all be precisely controlled,
and you aren't bound to the canvas size itself. (Of course, you could
take those extent/scale/sizes direct from the canvas if this is
behaviour you do wany)
- The map rendering can be done in a "blocking" way, so that you can
be sure that the render is completely finished before saving the image
out.
- You aren't messing with the canvas's temporal range, so after
exporting images the previous time range will still be visible and the
user's QGIS state won't be magically changed after running your
script.

Something like this should work (will need some adapting!)

   # now create an set of images so you can create an animated gif or so
   def render_time_range(map_settings, time_range):
     img = QImage(map_settings.outputSize(), map_settings.outputImageFormat())
     img.fill(map_settings.backgroundColor().rgb())

     p = QPainter()
     p.begin(img)
     map_settings.setTemporalRange(time_range)
     render = QgsMapRendererCustomPainterJob(map_settings, p)
     render.start()
     render.waitForFinished()
     p.end()
     return img

   map_settings = QgsMapSettings()
   # setup all your default map settings stuff here, e.g. scale,
extent, image size, etc
   map_settings.setLayers(iface.mapCanvas().layers())
   map_settings.setOutputSize(QSize(300, 150)) # width, height
   rect = QgsRectangle(iface.mapCanvas().fullExtent())
   map_settings.setExtent(rect)
   map_settings.setIsTemporal(True)

  for time_range in ....:
      img = render_time_range(map_settings, time_range)
      img.save( ... )



> Longer waits in processEvents() or time.sleep() after setting the frame didn't seem \
> to matter, the key was sleeping and processing events multiple times and 5 seems to \
> be the minimum. I also had to do the set frame to the first date before the loop, \
> as for some reason inside the loop it required more than 5 times when jumping \
> backwards from the final date to the beginning and would thus result in a bunch of \
> incompletely drawn images and a few skipped dates. 
> Next I need to find if there is a way to change the size of the output image to \
> different than the map canvas, but I suspect that will require playing with the \
> pyqgis stuff that controlls the Print Layout stuff, and that is probably beyond my \
> nonexistent python skills. 
> On Mon, Jan 24, 2022 at 6:27 PM Nyall Dawson <nyall.dawson@gmail.com> wrote:
> > 
> > On Tue, 25 Jan 2022 at 04:39, Cory Albrecht <maps@hanfastolfe.com> wrote:
> > > 
> > > But the `navigator.setCurrentFrameNumber(f)` doesn't change the canvas' \
> > > temporal frame. 
> > > If I do `f=54003` manually in the console and then \
> > > `navigator.setCurrentFrameNumber(f)` manually as well, the frame changes. I've \
> > > used time.sleep() with values up to 5 seconds to see if setting the frame was \
> > > just a background task that returns early before actually completed, but that \
> > > didn;t make any difference. 
> > 
> > Try QCoreApplication.processEvents() instead of time.sleep
> > 
> > > Why does it not change the frame in the middle of the for loop?
> > 
> > it's a bit technical, but you need to let the Qt "event loop" process
> > in order for any GUI widgets to update. time.sleep doesn't do this --
> > it just blocks the caller for the requested time, and doesn't let Qt
> > do it's thing and update the screen.
> > 
> > But a huge warning is needed here: processEvents() is very dangerous
> > to call in certain circumstances. Here it's ok to do, but definitely
> > use with caution!! (You'll know you're using it wrong if you get
> > crashes...)
> > 
> > Nyall
> > 
> > 
> > 
> > > 
> > > On Tue, Jan 18, 2022 at 2:32 PM Cory Albrecht <maps@hanfastolfe.com> wrote:
> > > > 
> > > > Thanks, I'll check it out.
> > > > 
> > > > On Sun, Jan 16, 2022 at 8:04 PM Nyall Dawson <nyall.dawson@gmail.com> wrote:
> > > > > 
> > > > > On Mon, 17 Jan 2022 at 10:46, Cory Albrecht <maps@hanfastolfe.com> wrote:
> > > > > > 
> > > > > > Thanks for the pointer, but unfortunately it is incomplete. It tells me \
> > > > > > this: 
> > > > > > # get the current  responsible for the mapCanvas behaviour and Temporal \
> > > > > > Controller gui navigator = iface.mapCanvas().temporalController()
> > > > > > 
> > > > > > And a few other methods for that object, but it's all about mimicking the \
> > > > > > "Save Animation" button with no explanation how to choose a specific \
> > > > > > frame or even anything as simple as going forward or backward by a single \
> > > > > > frame. Is there no list anywhere of all that temporal controller object's \
> > > > > > methods?
> > > > > 
> > > > > Yes, in the usual place (the PyQGIS API reference):
> > > > > 
> > > > > https://qgis.org/pyqgis/master/core/QgsTemporalNavigationObject.html
> > > > > 
> > > > > Nyall
> > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > On Sun, Jan 16, 2022 at 3:40 PM Delaz J <delazj@gmail.com> wrote:
> > > > > > > 
> > > > > > > Hi Cory,
> > > > > > > 
> > > > > > > I don't know if it's still compatible with API and of any help, but \
> > > > > > > there's that stale pull request in the docs by Richard \
> > > > > > > (https://github.com/qgis/QGIS-Documentation/pull/5521) you might want \
> > > > > > > to look at. 
> > > > > > > Regards,
> > > > > > > 
> > > > > > > Harrissou
> > > > > > > 
> > > > > > > Le 16/01/2022 à 18:34, Cory Albrecht a écrit :
> > > > > > > 
> > > > > > > Hello,
> > > > > > > 
> > > > > > > Is there a way I can access and change/set stuff for the dynamic \
> > > > > > > temporal controller programmatically in python? I'm looking to make a \
> > > > > > > simple script that I can just cut and paste into the python console \
> > > > > > > that will change the current frame of the DTC to a given timestamp, \
> > > > > > > export the canvas to a PNG, and repeat for all the timestamps in an \
> > > > > > > array. 
> > > > > > > _______________________________________________
> > > > > > > Qgis-user mailing list
> > > > > > > Qgis-user@lists.osgeo.org
> > > > > > > List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> > > > > > > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> > > > > > 
> > > > > > _______________________________________________
> > > > > > Qgis-user mailing list
> > > > > > Qgis-user@lists.osgeo.org
> > > > > > List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> > > > > > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> > > 
> > > _______________________________________________
> > > Qgis-user mailing list
> > > Qgis-user@lists.osgeo.org
> > > List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> > > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
_______________________________________________
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic