From dacee3fafa668f20d4682d09d1d8ed807faf114c Mon Sep 17 00:00:00 2001 From: "niehues.mark@gmail.com" <niehues.mark@gmail.com> Date: Wed, 22 Apr 2020 18:00:29 +0200 Subject: [PATCH] stupid but works so far. osmr has problems with high frequency of requests. --- evrouting/osm.py | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/evrouting/osm.py b/evrouting/osm.py index 19d8d6d..59b4cd0 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): -- GitLab