A Python library for route analysis

Bruno Hugentobler Lippert
3 min readMar 2, 2021

This is the first version of the Python library RouteUtils, whose goal is to analyze user positions in a route taking into account GPS inaccuracy.

The problem

When analyzing user’s GPS data we have to deal with the inaccuracy of the GPS, most of the time the navigation is used in places with good internet signal, but sometimes users go to places with bad or no internet. Since the GPS position is a composition of the GPS service and internet triangulation, we have to deal with this inaccuracy when analyzing user data.

Algorithm

To know if a user point is inside the route given a margin (GPS inaccuracy) we developed the follow algorithm.

  1. First, we have the user position and the given route

2. Then we calculate the distance between the user point and the closest point inside each vertex until we found a distance less than the given margin or we run out of vertex.

3. Then, we have to find if the user position is on the route sides, then we will be sure that the user position is inside the route with the given margin.

4. To do so, we calculate the angles between the vectors formed between each edge and each edge and the user location, as shown in the below image.

If both angles are equal or less than 90º then the user position is inside the route with the given margin.

RouteUtils

RouteUtils has been recently created Python library with the goal to simplify the analysis of user GPS data with a given route. To do so, in the first version we have only a method capable of determining if a user GPS position it’s inside a given route taking into account a margin. The method is called isPointInsideRoute, and receives four parameters to work:

  • Latitude of user position
  • Longitude of user position
  • The route
  • A given margin

Latitude and Longitude must be in decimal coordinates, ex: -24.123321, -50.123321

The route is the inside points of a GeoJson LineString, ex:

If the route GeoJson’s it’s like below, the route passed by it’s only the bold area.

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
-51.108398,
-10.001310
],
[
-51.080932,
-10.196000
],
[
-51.163330,
-10.266276
],
[
-51.306152,
-10.374361
]
]

}
}
]
}

The margin is an optional parameter with default value of 10 meters.

As a result, the method gives back a boolean with the result if the point is, or not, inside the route taking into account the given margin

The full code and an example of usage can be acessed in the GitHub repository here.

--

--