# Calculate distances between consecutive points in QGIS

I have a large dataset containing GPS points of various vehicles that are recorded every 10 seconds.

I have added a new `id` column to these GPS points in order to be able to connect them via the Points to Path tool by using the `id` field as order field.

``````car_id     LAT         LON         id
2891       4.899883    52.367300   1
2891       4.900583    52.366983   2
2891       4.901033    52.366900   3
2891       4.901100    52.366867   4
...        ...         ...
2891       4.901572    52.367967   82
5892       4.901794    52.369302   83
5892       4.901749    52.369442   84
5892       4.901829    52.361554   85
``````

The resulting GPS trajectory dataset contains outlying observations (the straight lines that jump across large distances not along with the road network) which I want to remove. These route points are impossible because it would require acceleration/speed that these vehicles can not achieve.

In order to detect these unrealistically travelled distances, I would like to calculate the distance between each point to the next point in sequence (`id`) for each unique `car_id`. This mean that the distance to the end point of each `car_id` should be zero, since there is no subsequent recorded GPS point (see below). Eventually I want to set a maximum distance depending on how far it is possible for a vehicle to get in 10 seconds (150 meters?) and filter out distances above this threshold.

``````car_id     LAT         LON         id      DISTANCE
2891       4.899883    52.367300   1       13
2891       4.900583    52.366983   2       2
2891       4.901033    52.366900   3       14
2891       4.901100    52.366867   4       10
...        ...         ...         ...
2891       4.901572    52.367967   82      0
5892       4.901794    52.369302   83      9
5892       4.901749    52.369442   84      12
5892       4.901829    52.361554   85      6
``````

Is this the best way to get rid of these outliers, and achievable in QGIS?

• Removing lines whose length is bigger than 150(for example) doesn't solve the problem? Commented Feb 11, 2020 at 12:20
• The output of the 'Points to path' algorithm results in a single datafile where the lines are not individually identifiable.. What do you propose to get the length of each individual line? Commented Feb 11, 2020 at 12:34
• You can use Explode lines tool. It gives you all individual lines. Commented Feb 11, 2020 at 12:39
• On a side note, the lat and long have been swapped
– JGH
Commented Feb 11, 2020 at 12:46
• Be careful when removing point/line: it works ok if the outliers are lonely. It fails if you have 2 or more consecutive ones. You may want to consider not only the "next" point but rather points outside of the max distance that the car can reach in 5 minutes (or else), then to remove groups of outliers. Don't forget to consider that the very first point could be the outlier
– JGH
Commented Feb 11, 2020 at 12:49

1. To keep your `id` attached to the points / vertices through line generation and other algorithms, you can add it as the M (or Z) dimension (algorithms Set M value / Set Z value) to your input GPS points layer. The `id` will then become a part of the geometry, and can be retrieved later using the Extract M values / Extract Z values algorithms.