Adding the duration and distance aggregation from the routes api
This commit is contained in:
parent
ce9956ea52
commit
83b3f1d3fd
2 changed files with 119 additions and 75 deletions
|
|
@ -1,70 +1,84 @@
|
|||
{
|
||||
"routes": [
|
||||
{
|
||||
"legs": [
|
||||
"routes": [
|
||||
{
|
||||
"stepsOverview": {
|
||||
"multiModalSegments": [
|
||||
{
|
||||
"stepStartIndex": 0,
|
||||
"stepEndIndex": 2,
|
||||
"navigationInstruction": {
|
||||
"instructions": "Walk to Finsbury Park"
|
||||
},
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"stepStartIndex": 3,
|
||||
"stepEndIndex": 3,
|
||||
"navigationInstruction": {
|
||||
"instructions": "Subway towards Brixton"
|
||||
},
|
||||
"travelMode": "TRANSIT"
|
||||
},
|
||||
{
|
||||
"stepStartIndex": 4,
|
||||
"stepEndIndex": 4,
|
||||
"navigationInstruction": {
|
||||
"instructions": "Walk to Euston"
|
||||
},
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"stepStartIndex": 5,
|
||||
"stepEndIndex": 5,
|
||||
"navigationInstruction": {
|
||||
"instructions": "Subway towards Morden via Bank"
|
||||
},
|
||||
"travelMode": "TRANSIT"
|
||||
},
|
||||
{
|
||||
"stepStartIndex": 6,
|
||||
"stepEndIndex": 6,
|
||||
"navigationInstruction": {
|
||||
"instructions": "Walk to Bank Station"
|
||||
},
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"stepStartIndex": 7,
|
||||
"stepEndIndex": 7,
|
||||
"navigationInstruction": {
|
||||
"instructions": "Light rail towards Woolwich Arsenal"
|
||||
},
|
||||
"travelMode": "TRANSIT"
|
||||
},
|
||||
{
|
||||
"stepStartIndex": 8,
|
||||
"stepEndIndex": 9,
|
||||
"travelMode": "WALK"
|
||||
}
|
||||
]
|
||||
}
|
||||
"legs": [
|
||||
{
|
||||
"steps": [
|
||||
{
|
||||
"distanceMeters": 6,
|
||||
"staticDuration": "5s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 158,
|
||||
"staticDuration": "133s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 19,
|
||||
"staticDuration": "16s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 102,
|
||||
"staticDuration": "87s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"staticDuration": "30s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 133,
|
||||
"staticDuration": "60s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 5202,
|
||||
"staticDuration": "392s",
|
||||
"travelMode": "TRANSIT"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 21,
|
||||
"staticDuration": "30s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 5627,
|
||||
"staticDuration": "600s",
|
||||
"travelMode": "TRANSIT"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 45,
|
||||
"staticDuration": "60s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 10907,
|
||||
"staticDuration": "1320s",
|
||||
"travelMode": "TRANSIT"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 63,
|
||||
"staticDuration": "64s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 70,
|
||||
"staticDuration": "54s",
|
||||
"travelMode": "WALK"
|
||||
},
|
||||
{
|
||||
"distanceMeters": 85,
|
||||
"staticDuration": "73s",
|
||||
"travelMode": "WALK"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"distanceMeters": 22438,
|
||||
"duration": "3463s",
|
||||
"staticDuration": "3463s"
|
||||
}
|
||||
],
|
||||
"distanceMeters": 22438,
|
||||
"duration": "3463s",
|
||||
"staticDuration": "3463s"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import requests
|
||||
from utils import nextMonday
|
||||
from collections import defaultdict
|
||||
|
||||
API_KEY = 'AIzaSyBoBHzeQFgR7O-NlNsuHXQcC1B7ccEHpl8'
|
||||
url = "https://routes.googleapis.com/directions/v2:computeRoutes"
|
||||
|
|
@ -11,7 +12,7 @@ def travel_time(origin_lat:float, origin_lon:float, dest_lat:float, dest_lon:flo
|
|||
header = {
|
||||
"X-Goog-Api-Key": API_KEY,
|
||||
"Content-Type": "application/json",
|
||||
"X-Goog-FieldMask": "routes.distanceMeters,routes.duration,routes.staticDuration,routes.legs.stepsOverview",
|
||||
"X-Goog-FieldMask": "routes.distanceMeters,routes.duration,routes.staticDuration,routes.legs.steps.distanceMeters,routes.legs.steps.staticDuration,routes.legs.steps.travelMode",
|
||||
}
|
||||
|
||||
body = {
|
||||
|
|
@ -50,11 +51,40 @@ def travel_time(origin_lat:float, origin_lon:float, dest_lat:float, dest_lon:flo
|
|||
|
||||
raise Exception(r.json())
|
||||
|
||||
if __name__ == "__main__":
|
||||
origin = 51.5635664310333, -0.1107173751570373 # home
|
||||
dest = 51.50475678313417, 0.04915321000190009 # london city airport
|
||||
d = travel_time(origin[0], origin[1], dest[0], dest[1])
|
||||
import json
|
||||
with open('code/json/routing_routeapi2.json', 'w') as f:
|
||||
json.dump(d, f)
|
||||
def extract_time(d):
|
||||
r = d['routes'][0]
|
||||
print(r.keys())
|
||||
distance = r['distanceMeters']
|
||||
duration = r['duration']
|
||||
duration_static = r['staticDuration']
|
||||
|
||||
steps = r['legs'][0]['steps']
|
||||
# print(steps)
|
||||
duration_per_transit = defaultdict(lambda: 0)
|
||||
distance_per_transit = defaultdict(lambda: 0)
|
||||
|
||||
for step in steps:
|
||||
duration_per_transit[step['travelMode']] += int(step['staticDuration'].strip('s'))
|
||||
distance_per_transit[step['travelMode']] += step.get('distanceMeters', 0)
|
||||
|
||||
|
||||
print(f"dis {distance}, dur {duration}, duration per transit {dict(duration_per_transit)}, distance per transit {dict(distance_per_transit)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import json
|
||||
with open('code/json/routing_routeapi.json', 'r') as f:
|
||||
d = json.load(f)
|
||||
|
||||
extract_time(d)
|
||||
|
||||
|
||||
|
||||
# if __name__ == "__main__":
|
||||
# origin = 51.5635664310333, -0.1107173751570373 # home
|
||||
# dest = 51.50475678313417, 0.04915321000190009 # london city airport
|
||||
# d = travel_time(origin[0], origin[1], dest[0], dest[1])
|
||||
# import json
|
||||
# with open('code/json/routing_routeapi.json', 'w') as f:
|
||||
# json.dump(d, f)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue