Calculation of walking distances using OSM data in Python returns 0

41 views Asked by At

Description: I'm working on a project where I need to calculate walking distances between various locations using OpenStreetMap (OSM) data in Python. I'm using the osmnx library to retrieve pedestrian network data and attempting to compute walking distances based on this data.

Issue: Despite implementing the code, the calculated walking distances are consistently returning 0, which is unexpected. I'm uncertain about what might be causing this issue. I tried different codes.

G_pedestrian = ox.graph_from_place('Netherlands', network_type='walk',
                     custom_filter='["highway"~"footway|steps|path|pedestrian|living_street"]')
import pickle
with open('G_Gedestrian.pkl', 'wb') as f:
    pickle.dump(G_pedestrian, f)
# subset of my dataset
df = {
    'ArrivalPostalCode': [9481, 9901, 9761, 9761, 9301],
    'HaversineDistance': [0.000000, 28.405535, 30.298913, 0.000000, 14.426884],
    'postal_code_arr': [9481, 9901, 9761, 9761, 9301],
    'LatitudeArrival': [53.0726, 53.3226, 53.1367, 53.1367, 53.1380],
    'LongitudeArrival': [6.5768, 6.8560, 6.5633, 6.5633, 6.4224],
    'postal_code_dep': [9481, 9761, 9501, 9761, 9482],
    'LatitudeDeparture': [53.0726, 53.1367, 52.9939, 53.1367, 53.0796],
    'LongitudeDeparture': [6.5768, 6.5633, 6.9495, 6.5633, 6.615]}



def calculate_walking_distance(G, origin, destination):
    try:
        walking_distance = nx.shortest_path_length(G, origin, destination, weight='length')
        return walking_distance
    except nx.NetworkXNoPath:
        return np.nan

walking_distances = []
for index, row in df.iterrows():
    origin = ox.distance.nearest_nodes(G_pedestrian, row['LongitudeDeparture'], row['LatitudeDeparture'])
    destination = ox.distance.nearest_nodes(G_pedestrian, row['LongitudeArrival'], row['LatitudeArrival'])
    walking_distance = calculate_walking_distance(G_pedestrian, origin, destination)
    walking_distances.append(walking_distance)

df['WalkingDistance'] = walking_distances

I've obtained the pedestrian network graph using osmnx and saved it to a pickle file named 'G_Gedestrian.pkl'. I'm using the calculate_walking_distance function to compute the shortest path length between two nodes on the pedestrian network graph using NetworkX's shortest_path_length function. I'm then iterating over my DataFrame df containing departure and arrival coordinates to calculate walking distances between each pair of locations.

Additionally, I've already calculated the Haversine distance between the departure and arrival locations. In some cases, this Haversine distance is as large as 200 km. However, the code consistently returns 0 for the calculated walking distances, which is unexpected given the substantial distances involved.

Expected Outcome: I expect the code to accurately compute walking distances between locations based on the pedestrian network data obtained from OSM. However, it consistently returns 0 for all calculated distances, which is incorrect. It's important to note that OSM generally provides high-quality data for the Netherlands, including detailed pedestrian network information. Therefore, I am expecting to retrieve reasonable values for walking distances in most observations..

I would appreciate any insights or suggestions on what might be causing the issue and how to resolve it. Thanks in advance

0

There are 0 answers