Routes API implemented for travel time
This commit is contained in:
parent
5349088ba0
commit
ce9956ea52
7 changed files with 5632 additions and 1 deletions
60
code/routing_routing.py
Normal file
60
code/routing_routing.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import requests
|
||||
from utils import nextMonday
|
||||
|
||||
API_KEY = 'AIzaSyBoBHzeQFgR7O-NlNsuHXQcC1B7ccEHpl8'
|
||||
url = "https://routes.googleapis.com/directions/v2:computeRoutes"
|
||||
|
||||
|
||||
def travel_time(origin_lat:float, origin_lon:float, dest_lat:float, dest_lon:float):
|
||||
monday9am = nextMonday()
|
||||
|
||||
header = {
|
||||
"X-Goog-Api-Key": API_KEY,
|
||||
"Content-Type": "application/json",
|
||||
"X-Goog-FieldMask": "routes.distanceMeters,routes.duration,routes.staticDuration,routes.legs.stepsOverview",
|
||||
}
|
||||
|
||||
body = {
|
||||
"origin":{
|
||||
"location":{
|
||||
"latLng":{
|
||||
"latitude": origin_lat,
|
||||
"longitude": origin_lon
|
||||
}
|
||||
}
|
||||
},
|
||||
"destination":{
|
||||
"location":{
|
||||
"latLng":{
|
||||
"latitude": dest_lat,
|
||||
"longitude": dest_lon
|
||||
}
|
||||
}
|
||||
},
|
||||
"travelMode": "TRANSIT",
|
||||
# "2023-10-15T15:01:23.045123456Z"
|
||||
"departureTime": monday9am.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
|
||||
"computeAlternativeRoutes": False,
|
||||
# "routeModifiers": {
|
||||
# "avoidTolls": false,
|
||||
# "avoidHighways": false,
|
||||
# "avoidFerries": false
|
||||
# },
|
||||
"languageCode": "en-US",
|
||||
"units": "METRIC"
|
||||
}
|
||||
|
||||
r = requests.post(url, json=body, headers=header)
|
||||
if r.status_code == 200:
|
||||
return r.json()
|
||||
|
||||
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)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue