forked from googlemaps/google-maps-services-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirections.py
More file actions
127 lines (94 loc) · 4.01 KB
/
directions.py
File metadata and controls
127 lines (94 loc) · 4.01 KB
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
119
120
121
122
123
124
125
126
127
#
# Copyright 2014 Google Inc. All rights reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
"""Performs requests to the Google Maps Directions API."""
from googlemaps import common
from googlemaps import convert
def directions(ctx, 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):
"""Get directions between an origin point and a destination point.
:param ctx: Shared googlemaps.Context
:type ctx: googlemaps.Context
:param origin: The address or latitude/longitude value from which you wish
to calculate directions.
:type origin: basestring or dict or tuple
:param destination: The address or latitude/longitude value from which
you wish to calculate directions.
:type destination: basestring or dict or tuple
:param mode: Specifies the mode of transport to use when calculating
directions. One of "driving", "walking", "bicycling" or "transit"
:type mode: basestring
:param waypoints: Specifies an array of waypoints. Waypoints alter a
route by routing it through the specified location(s).
: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 basestring
:param language: The language in which to return results.
:type language: basestring
:param units: Specifies the unit system to use when displaying results.
"metric" or "imperial"
:type units: basestring
:param region: The region code, specified as a ccTLD ("top-level domain"
two-character value.
:type region: basestring
: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.
: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
:rtype: list of routes
"""
params = {
"origin": _convert_waypoint(origin),
"destination": _convert_waypoint(destination)
}
if mode:
if mode not in ["driving", "walking", "bicycling", "transit"]:
raise Exception("Invalid travel mode.")
params["mode"] = mode
if waypoints:
waypoints = convert.as_list(waypoints)
waypoints = [_convert_waypoint(waypoint) for waypoint in waypoints]
if optimize_waypoints:
waypoints = ["optimize:true"] + waypoints
params["waypoints"] = convert.join_list("|", 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)
return common._get(ctx, "/maps/api/directions/json", params)["routes"]
def _convert_waypoint(waypoint):
if not common._isstr(waypoint):
return convert.latlng(waypoint)
return waypoint