allow caching routing by destination and travel mode; also export all travel methods to csv column
This commit is contained in:
parent
f2118c9bc4
commit
10ae25e0d3
5 changed files with 190 additions and 84 deletions
|
|
@ -3,7 +3,6 @@ import os
|
|||
from typing import Any
|
||||
import requests
|
||||
from rec.utils import nextMonday
|
||||
from collections import defaultdict
|
||||
|
||||
url = "https://routes.googleapis.com/directions/v2:computeRoutes"
|
||||
API_KEY_ENVIRONMENT_VARIABLE = "ROUTING_API_KEY"
|
||||
|
|
@ -72,42 +71,3 @@ def transit_route(
|
|||
return r.json()
|
||||
|
||||
raise Exception(r.json())
|
||||
|
||||
|
||||
def extract_time(d, limit: int = 2):
|
||||
res = []
|
||||
for route in d["routes"]:
|
||||
distance = route["distanceMeters"]
|
||||
duration = int(route["duration"].strip("s"))
|
||||
duration_static = int(route["staticDuration"].strip("s"))
|
||||
|
||||
steps = route["legs"][0]["steps"]
|
||||
initial_walk_duration = 0
|
||||
used_transit = False
|
||||
duration_per_transit = defaultdict(lambda: 0)
|
||||
distance_per_transit = defaultdict(lambda: 0)
|
||||
number_of_transit_stops = 0
|
||||
|
||||
for step in steps:
|
||||
if used_transit == False and step["travelMode"] == "WALK":
|
||||
initial_walk_duration += int(step["staticDuration"].strip("s"))
|
||||
else:
|
||||
used_transit = True
|
||||
duration_per_transit[step["travelMode"]] += int(
|
||||
step["staticDuration"].strip("s"))
|
||||
distance_per_transit[step["travelMode"]] += step.get(
|
||||
"distanceMeters", 0)
|
||||
if step["travelMode"] == "TRANSIT":
|
||||
number_of_transit_stops += 1
|
||||
|
||||
res.append({
|
||||
"duration": duration,
|
||||
"distance": distance,
|
||||
"duration_static": duration_static,
|
||||
"initial_walk_duration": initial_walk_duration,
|
||||
"duration_per_transit": dict(duration_per_transit),
|
||||
"distance_per_transit": dict(distance_per_transit),
|
||||
"number_of_transit_stops": number_of_transit_stops,
|
||||
})
|
||||
|
||||
return res[:limit]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue