diff --git a/evrouting/osm.py b/evrouting/osm.py index 19d8d6d4c3bae5670e4732e8c7d8168006cdea76..59b4cd05852a082a031d147f88135b8e4f8c2bf2 100644 --- a/evrouting/osm.py +++ b/evrouting/osm.py @@ -14,12 +14,12 @@ Added : import copy import xml.sax +import time from math import radians, cos, sin, asin, sqrt from collections import namedtuple import networkx as nx -import aiohttp -import asyncio +import requests from evrouting.graph_tools import DISTANCE_KEY @@ -141,26 +141,35 @@ def read_osm(osm_xml_data, G.nodes[n_id]['id'] = n.id coordinates_map[n_id] = (n.lon, n.lat) - asyncio.run(augment_distances(G, query_url)) + augment_distances(G, query_url) G = nx.relabel_nodes(G, coordinates_map) return G -async def augment_distances(G, url_factory): +def augment_distances(G, url_factory): # Estimate the length of each way - async with aiohttp.ClientSession() as session: - for u, v, d in G.edges(data=True): - url = url_factory( - 'route', - [ - (G.nodes[u]['lat'], G.nodes[u]['lon']), - (G.nodes[v]['lat'], G.nodes[v]['lon']) - ]) - async with session.get(url) as resp: - resp.raise_for_status() - resp = await resp.json() - duration = resp['routes'][0]['duration'] - G.add_weighted_edges_from([(u, v, duration)], weight=DISTANCE_KEY) + i = 0 + for u, v, d in G.edges(data=True): + i += 1 + url = url_factory( + 'route', + [ + (G.nodes[u]['lat'], G.nodes[u]['lon']), + (G.nodes[v]['lat'], G.nodes[v]['lon']) + ]) + try: + resp = requests.get(url, timeout=0.1) + except requests.exceptions.Timeout: + print('Timeout at request: ', i) + raise + + if resp.status_code == 200: + resp = resp.json() + else: + continue + duration = resp['routes'][0]['duration'] + time.sleep(0.005) + G.add_weighted_edges_from([(u, v, duration)], weight=DISTANCE_KEY) class Node(object):