The question
How can I get the current animation time of a Temporal Controller animation using expressions with QGIS 3.24? I guess I'm almost there (see below), but have trouble dividing a time formatted as interval
by a number.
The background (what I need it for)
I finally want to get a value that shows what percentage of the whole animation has elapsed at a certain frame. I want to use this value to style a line layer with geometry generator, using line_substring() function to draw the same percentage of the line's total length as the percentage of time elapsed: so at the beginning, 0% of the line, after 30% of the whole animation the first 30% of the line's lenght etc. Like this, I could animate the line without creating interpolated points with timestamps (what would be a workaround) by activating Dynamic Temporal Contral in the layer properties with Configuration Redraw Layer Only
.
The expression to create this would look like: line_substring( $geometry, 0, $length * [percentage] )
- where [percentage]
is the value I'm trying to create.
What I tried
My idea was to the get the total length of the whole anmimation with @animation_end_time - @animation_start_time
. This return a time interval (like <interval: 10.375 minutes>
). Unfortunately, there is no variable like @animation_current_time
that returns the time of the current frame.
There is, however, the variable @frame_number
that returns the current frame number during animation playback. The variables @frame_timestep
and @frame_duration
return the temporal duration of each animation frame.
Example
Let's say I have the following values. A total time of 24 frames * 8 seconds = 192 seconds have elapsed, that is 30.8% of the total animation time of 10.375 minutes:
- Total length of animation:
<interval: 10.375 minutes>
( =@animation_end_time - @animation_start_time
) - Current frame number:
24
( =@frame_number
) - Temporal duration of each animation frame:
8
(using@frame_timestep
) or<interval: 8 seconds>
(with@frame_duration
) - What I want to get (pseudocode):
<current-time> / <total-duration> -> 0.308 (30.8%)
So how to divide <interval: 10.375 minutes>
by the time elapsed : 192 (@frame_number * @frame_timestep
)?