I have a QGIS Project with Temporal Controller, in Python Console. How could I capture the current moment of the simulation?
I'm trying, but doesn't work
canvas = iface.mapCanvas() temporal= canvas.temporalController().currentFrameNumber ()
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
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)
I'm happy to report that this works as desired for me without a sip cast (currently testing on 3.18).
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.