forked from googlemaps/google-maps-services-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance_matrix.py
More file actions
107 lines (84 loc) · 3.89 KB
/
Copy pathdistance_matrix.py
File metadata and controls
107 lines (84 loc) · 3.89 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
#
# 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 Distance Matrix API."""
from googlemaps import convert
from googlemaps import common
from googlemaps.convert import as_list
def distance_matrix(ctx, origins, destinations,
mode=None, language=None, avoid=None, units=None,
departure_time=None):
""" Gets travel distance and time for a matrix of origins and destinations.
:param ctx: Shared googlemaps.Context
:type ctx: googlemaps.Context
:param origins: One or more addresses and/or latitude/longitude values,
from which to calculate distance and time. If you pass an address
as a string, the service will geocode the string and convert it to
a latitude/longitude coordinate to calculate directions.
:type origins: list of basestrings, dicts or tuples
:param destinations: One or more addresses and/or lat/lng values, to
which to calculate distance and time. If you pass an address as a
string, the service will geocode the string and convert it to a
latitude/longitude coordinate to calculate directions.
:type destinations: list of basestrings, dicts or tuples
:param mode: Specifies the mode of transport to use when calculating
directions. Valid values are "driving", "walking" or "bicycling".
:type mode: basestring
:param language: The language in which to return results.
:type language: basestring
:param avoid: Indicates that the calculated route(s) should avoid the
indicated features. Valid values are "tolls", "highways" or "ferries"
:type avoid: basestring
:param units: Specifies the unit system to use when displaying results.
Valid values are "metric" or "imperial"
:type units: basestring
:param departure_time: Specifies the desired time of departure as seconds
since midnight, January 1, 1970 UTC. The departure time may be
specified by Google Maps API for Work customers to receive trip
duration considering current traffic conditions. The departure_time
must be set to within a few minutes of the current time.
:type departure_time: int or datetime.datetime
:rtype: matrix of distances. Results are returned in rows, each row
containing one origin paired with each destination.
"""
params = {
"origins": _convert_path(origins),
"destinations": _convert_path(destinations)
}
if mode:
if mode not in ["driving", "walking", "bicycling"]:
raise Exception("Invalid travel mode.")
params["mode"] = mode
if language:
params["language"] = language
if avoid:
if avoid not in ["tolls", "highways", "ferries"]:
raise Exception("Invalid route restriction.")
params["avoid"] = avoid
if units:
params["units"] = units
if departure_time:
params["departure_time"] = convert.time(departure_time)
return common._get(ctx, "/maps/api/distancematrix/json", params)
def _convert_path(waypoints):
# Handle the single-tuple case
if type(waypoints) is tuple:
waypoints = [waypoints]
else:
waypoints = as_list(waypoints)
return convert.join_list("|",
[(k if common._isstr(k) else convert.latlng(k))
for k in waypoints])