5

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)?

1 Answer 1

4

You can get the percentage by using an expression such as:

second(datetime_from_epoch(epoch(to_datetime(to_string("mytime")+'Z')) - epoch(@animation_start_time)) - make_datetime(1970,1,1,0,0,0)) / second(@animation_interval)

Be aware of the timezones! Seems like QGIS temporal-controller-variables always work with UTC, while your datetime may be in another timezone. You can correct this by adding or subtracting the hour difference in seconds. Or a little more elegant change the 0-time of make_datetime(1970,1,1,0,0,0) to e.g. make_datetime(1970,1,1,1,0,0) In my example above I had to manually set UTC as timezone, thats why I use to_string("mytime")+'Z'. Remove that part if you dont need it!

Example result:

enter image description here

1
  • 1
    Converting the time to seconds was what solved my problem. As I have no date, only time, I don't have to care about time zones. So the expression that worked for me looks like: line_substring( $geometry, 0, $length * @frame_timestep * @frame_number / second (@animation_end_time - @animation_start_time) )
    – Babel
    Mar 27, 2022 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.