TransWikia.com

PyQGIS Temporal Controller current moment

Geographic Information Systems Asked on June 13, 2021

I have a QGIS Project with Temporal Controller, in Python Console. How could I capture the current moment of the simulation?

enter image description here


I’m trying, but doesn’t work

canvas = iface.mapCanvas()
temporal= canvas.temporalController().currentFrameNumber ()

2 Answers

You can use the following considering you have QGIS 3.14+

from qgis.PyQt import sip

canvas = iface.mapCanvas()
temporalController = canvas.temporalController()
# Cast the QgsTemporalController to QgsTemporalNavigationObject or stucked 
temporalNavigationObject = sip.cast(temporalController, QgsTemporalNavigationObject)


currentFrameNumber = temporalNavigationObject.currentFrameNumber()
print('Frame number', currentFrameNumber)
currentDateTimeRange = temporalNavigationObject.dateTimeRangeForFrameNumber(currentFrameNumber)
print('Current start time range', print(currentDateTimeRange.begin()))
print('Current end time range', print(currentDateTimeRange.end()))

# Experiment section as a reminder (because seems a bug in the signal connect later)
# def animation_state_changed(state):
#     print("DEBUG")

# Signal does not work here when above function + below statement uncommented
# temporalNavigationObject.stateChanged.connect(animation_state_changed)

Correct answer by ThomasG77 on June 13, 2021

I'm happy to report that this works as desired for me without a sip cast (currently testing on 3.18).

I think the desired signal is the updateTemporalRange, not the stateChanged signal as mentioned in @ThomasG77's answer (which still fails to connect also in my case).

This prints the current frame number in the QGIS Python console:

canvas = iface.mapCanvas()
controller = canvas.temporalController()

def f():
    print(controller.currentFrameNumber())
    
controller.updateTemporalRange.connect(f)

Via the frame number, the .dateTimeRangeForFrameNumber can be used to get the datetimes as well.

Answered by Huite Bootsma on June 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP