-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathosrm.py
39 lines (19 loc) · 937 Bytes
/
osrm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
"""
import requests
import json
def call_request (data, num_cluster):
url = 'http://router.project-osrm.org/table/v1/driving/'
# define data, destination and source index
waypoints = ';'.join(map(lambda pt: '{},{}'.format(*reversed(pt)), data))
dest = ';'.join(map(lambda pt: '{}'.format(pt), range(num_cluster)))
src = ';'.join(map(lambda pt: '{}'.format(pt), range(num_cluster, len(data))))
# request accepts maximum of 8192 bytes
try:
r = requests.get('{}{}?sources={}&destinations={}&annotations=duration,distance'.format(url, waypoints, src, dest))
dictContent = json.loads(r.content.decode('utf-8'))
distance = dictContent['distances']
duration = dictContent['durations']
except Exception as e :
print(e)
return (distance, duration)