-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosrm_distance.py
58 lines (43 loc) · 1.65 KB
/
osrm_distance.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# fn = function
# -----------------------------------------------------------------------------
# Dependencies
# -----------------------------------------------------------------------------
import requests
import json
import time
import polyline
# -----------------------------------------------------------------------------
# fn1:
# -----------------------------------------------------------------------------
def get_route(pickup_lon, pickup_lat, dropoff_lon, dropoff_lat):
"""
Measure the route distance betweeen two waypoints.
dependencies
----------
polyline
Parameters
----------
pickup_lon, pickup_lat: cordinates of the starting waypoint
dropoff_lon, dropoff_lat: cordiantes of the ending waypoint
Returns
-------
dictionary for the modified OSRM output for the http call
"""
loc = "{},{};{},{}".format(pickup_lon, pickup_lat, dropoff_lon, dropoff_lat)
#OSRM call, refer docs for additional arguments
url = "http://127.0.0.1:5000/route/v1/driving/"
r = requests.get(url + loc)
if r.status_code!= 200:
return {}
res = r.json()
# decode all the points between waypoints using Googles polyline fn
routes = polyline.decode(res['routes'][0]['geometry'])
start_point = [res['waypoints'][0]['location'][1], res['waypoints'][0]['location'][0]]
end_point = [res['waypoints'][1]['location'][1], res['waypoints'][1]['location'][0]]
distance = res['routes'][0]['distance']
out = {'route':routes,
'start_point':start_point,
'end_point':end_point,
'distance':distance
}
return out