4

I have the project where, in one part of it, I need to add the timestamp to uniform generated points along the line (road, in my project). There is a road and uniform distributed points on it, which represent the trajectory of a car. Each point represents the car's location at certain time on the road.

How can I add dynamic timestamp that if, let's say, I need to decrease the speed of the car, the timestamp of the certain point will change as well?

3
  • 1
    You've tagged this with shapefile which only supports day resolution in date fields.
    – Vince
    Jan 6, 2022 at 22:04
  • You had a tag for PyQGIS but have not presented any code in your question body and so I have removed it.
    – PolyGeo
    Jan 6, 2022 at 23:12
  • 1
    Yo also have tags for shapefile and spatial-database but do not tell us which you are using. If it's a spatial database then which type?
    – PolyGeo
    Jan 6, 2022 at 23:14

1 Answer 1

7

Using the QGIS field calculator you can create an expression on a Time type field, with the speed as a variable. Changing the speed and re-calculating the expression will adjust the times accordingly. For the example I used 14 m/s (approx 50 km/h) as the initial speed and 20:00 as an arbitrary start time, the points are 250 m apart (created with the Points along Geometry tool on a line. The tool automatically adds a distance column to the point layer).

Assuming distance is meters and speed is meters per second:


with_variable('speed', 14,                             -- set the speed variable
    with_variable('start_time', make_time(20, 0 ,0),   -- set a start time variable
        CASE 
            WHEN "distance" = 0                        
            THEN @start_time                           -- input the start time at zero distance
            
            ELSE @start_time + to_interval(to_string("distance" / @speed) || ' seconds')  -- add the elapsed interval (distance/speed) to the start time and calculate the remaining rows 
        END
    )
)

enter image description here

Change speed to 20 m/s: enter image description here

Note: for the labels in the images, I removed the milliseconds component from the time.

7
  • Thank you so much for your answer!! But, by saying Time field type, do you mean Date? Sorry for stupid questions, I just start working in QGIS :( Jan 11, 2022 at 20:30
  • 1
    That's up to you :) QGIS offers both. I just chose time for the example. You can add the interval to a DateTime field and it will work the same.
    – Matt
    Jan 11, 2022 at 20:46
  • In Field Calculator, there is no Time field type, sadly. There are only Whole number(integer), Whole number(integer 64 bit), Decimal number(real), String and Date. How can I add the field with Time field type? I tried the Date but it didn't work as I expected. Because, with the Date, the time difference can't be seen, since it shows only date as days, not seconds or minutes... Jan 15, 2022 at 18:17
  • 1
    You can use the Add field tool to add a Time (or DateTime) field.
    – Matt
    Jan 15, 2022 at 19:44
  • 1
    Good to hear :)
    – Matt
    Jan 15, 2022 at 22:11

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.