Skip to content
Snippets Groups Projects
Commit dacee3fa authored by markn92's avatar markn92
Browse files

stupid but works so far. osmr has problems with high frequency of requests.

parent e39259d7
No related branches found
No related tags found
No related merge requests found
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment