-
Notifications
You must be signed in to change notification settings - Fork 0
/
directions.py
118 lines (101 loc) · 4.93 KB
/
directions.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from googlemaps import convert
def directions(client, origin, destination,
mode=None, waypoints=None, alternatives=False, avoid=None,
language=None, units=None, region=None, departure_time=None,
arrival_time=None, optimize_waypoints=False, transit_mode=None,
transit_routing_preference=None, traffic_model=None):
"""Get directions between an origin point and a destination point.
:param origin: The address or latitude/longitude value from which you wish
to calculate directions.
:type origin: string, dict, list, or tuple
:param destination: The address or latitude/longitude value from which
you wish to calculate directions. You can use a place_id as destination
by putting 'place_id:' as a prefix in the passing parameter.
:type destination: string, dict, list, or tuple
:param mode: Specifies the mode of transport to use when calculating
directions. One of "driving", "walking", "bicycling" or "transit"
:type mode: string
:param waypoints: Specifies an array of waypoints. Waypoints alter a
route by routing it through the specified location(s). To influence
route without adding stop prefix the waypoint with `via`, similar to
`waypoints = ["via:San Francisco", "via:Mountain View"]`.
:type waypoints: a single location, or a list of locations, where a
location is a string, dict, list, or tuple
:param alternatives: If True, more than one route may be returned in the
response.
:type alternatives: bool
:param avoid: Indicates that the calculated route(s) should avoid the
indicated features.
:type avoid: list or string
:param language: The language in which to return results.
:type language: string
:param units: Specifies the unit system to use when displaying results.
"metric" or "imperial"
:type units: string
:param region: The region code, specified as a ccTLD ("top-level domain"
two-character value.
:type region: string
:param departure_time: Specifies the desired time of departure.
:type departure_time: int or datetime.datetime
:param arrival_time: Specifies the desired time of arrival for transit
directions. Note: you can't specify both departure_time and
arrival_time.
:type arrival_time: int or datetime.datetime
:param optimize_waypoints: Optimize the provided route by rearranging the
waypoints in a more efficient order.
:type optimize_waypoints: bool
:param transit_mode: Specifies one or more preferred modes of transit.
This parameter may only be specified for requests where the mode is
transit. Valid values are "bus", "subway", "train", "tram", "rail".
"rail" is equivalent to ["train", "tram", "subway"].
:type transit_mode: string or list of strings
:param transit_routing_preference: Specifies preferences for transit
requests. Valid values are "less_walking" or "fewer_transfers"
:type transit_routing_preference: string
:param traffic_model: Specifies the predictive travel time model to use.
Valid values are "best_guess" or "optimistic" or "pessimistic".
The traffic_model parameter may only be specified for requests where
the travel mode is driving, and where the request includes a
departure_time.
:type units: string
:rtype: list of routes
"""
params = {
"origin": convert.latlng(origin),
"destination": convert.latlng(destination)
}
if mode:
# NOTE(broady): the mode parameter is not validated by the Maps API
# server. Check here to prevent silent failures.
if mode not in ["driving", "walking", "bicycling", "transit"]:
raise ValueError("Invalid travel mode.")
params["mode"] = mode
if waypoints:
waypoints = convert.location_list(waypoints)
if optimize_waypoints:
waypoints = "optimize:true|" + waypoints
params["waypoints"] = waypoints
if alternatives:
params["alternatives"] = "true"
if avoid:
params["avoid"] = convert.join_list("|", avoid)
if language:
params["language"] = language
if units:
params["units"] = units
if region:
params["region"] = region
if departure_time:
params["departure_time"] = convert.time(departure_time)
if arrival_time:
params["arrival_time"] = convert.time(arrival_time)
if departure_time and arrival_time:
raise ValueError("Should not specify both departure_time and"
"arrival_time.")
if transit_mode:
params["transit_mode"] = convert.join_list("|", transit_mode)
if transit_routing_preference:
params["transit_routing_preference"] = transit_routing_preference
if traffic_model:
params["traffic_model"] = traffic_model
return client._request("/maps/api/directions/json", params).get("routes", [])