MKuranowski/pyroutelib3

lenght and time of a route

maurofaccin opened this issue · 4 comments

Great and helpful code.
Is I don't understand from your code if there is a possibility to compute the total length and the estimation of the time needed to follow the proposed route.

Cheers

pyroutelib3 is not meant to do this.
It's just a basic implementation of the A* algorithm, not a point-to-point navigation.

If you want to get the full length of route you can just iterate over each node pair in route and add their distance to a variable, like this.

# other code
status, route = router.doRoute(start, end) # Find the route - a list of OSM nodes
if status == 'success':
    total = 0.0
    for idx in range(1, len(route)):
        total += router.distance(route[idx - 1], route[idx])
    # total now holds total distance in kilometers

Once you have the total length you can multiply it by some average speed.

Hi @MKuranowski,

your example above didn't seem to work properly, I get the error:

File "D:\PyCharm\ITCS\venv\lib\site-packages\pyroutelib3\util.py", line 44, in distHaversine
    lat1, lon1 = map(math.radians, n1)
TypeError: 'int' object is not iterable

It seems like the distance method does not expect node IDs but their GPS positions. Thus, your example should look like

length = 0.0
for i in range(1, len(node_sequence)):
    pos1 = self._router.rnodes[node_sequence[i - 1]]
    pos2 = self._router.rnodes[node_sequence[i]]

    length += self._router.distance(pos1, pos2)

This works fine for me and contains the distance in kilometers from start to end point.

Best
Sebastian

@sebastianknopf this example is 4 years old, the interface of pyroutelib3 slightly changed in 1.7

I noticed, but for others facing this issue, it might be helpful anyway.