'Calculate walking times across route

I am trying to calculate travel distances across a walkable street network I've downloaded using OSMNX. I have generated a route from an origin to a destination using NetworkX's nx.shortest_path function. However, whenever I calculate my route setting "travel_time" as weight, I get inaccurate results. I was wondering if this might have to do with the "speed_kph" attribute in the network. Is there a way to manually set speeds to equate walking speeds (e.g. 5 kph?)?



Solution 1:[1]

You are using the nx.shortest_path_length function and the travel_time attribute. Note the travel_time attribute is not present by default in your graph. In order to add it you need to use ox.add_edge_travel_times.

The Networkx function shortest_path_length states in it's documentation:

weight - If None, every edge has weight/distance/cost 1. If a string, use this edge attribute as the edge weight. Any edge attribute not present defaults to 1.

Since the travel_time is not present you get the shortest path which has the minimum edges.

This can be verified by trying:

weight = nx.shortest_path_length(walk_network, origin_nn, destination_nn, weight = 'fake_key')

Since usually in walking you assume a constant speed, a possible solution would be to use the edge length and calculate the speed separately.

For example:

import osmnx as ox
import networkx as nx

WALKING_SPEED = 1.4 # m/s average walkihg speed

walk_network = ox.graph.graph_from_address('Piccadilly Circus, London', dist = 1000, network_type = 'walk')


origin = (-0.13492669985021463, 51.51016491413415)
destination = (-0.12516, 51.51112)

origin_nn = ox.distance.nearest_nodes(walk_network, origin[0], origin[1])
destination_nn = ox.distance.nearest_nodes(walk_network, destination[0], destination[1])


length = nx.shortest_path_length(walk_network, origin_nn, destination_nn, weight = 'length')
duration = length / WALKING_SPEED / 60
print('The shortest walking route is {:.2f} meters and will take {:.2f} minutes'.format(length, duration))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michal Yanko