|
| 1 | +"""Performs requests to the Google Maps Distance Matrix API.""" |
| 2 | + |
| 3 | +from googlemaps import convert |
| 4 | +from googlemaps import common |
| 5 | +from googlemaps.convert import as_list |
| 6 | + |
| 7 | + |
| 8 | +def distance_matrix(ctx, origins, destinations, |
| 9 | + mode=None, language=None, avoid=None, units=None, |
| 10 | + departure_time=None): |
| 11 | + """ Gets travel distance and time for a matrix of origins and destinations. |
| 12 | +
|
| 13 | + :param ctx: Shared googlemaps.Context |
| 14 | + :type ctx: googlemaps.Context |
| 15 | +
|
| 16 | + :param origins: One or more addresses and/or latitude/longitude values, |
| 17 | + from which to calculate distance and time. If you pass an address |
| 18 | + as a string, the service will geocode the string and convert it to |
| 19 | + a latitude/longitude coordinate to calculate directions. |
| 20 | + :type origins: list of basestrings, dicts or tuples |
| 21 | +
|
| 22 | + :param destinations: One or more addresses and/or lat/lng values, to |
| 23 | + which to calculate distance and time. If you pass an address as a |
| 24 | + string, the service will geocode the string and convert it to a |
| 25 | + latitude/longitude coordinate to calculate directions. |
| 26 | + :type destinations: list of basestrings, dicts or tuples |
| 27 | +
|
| 28 | + :param mode: Specifies the mode of transport to use when calculating |
| 29 | + directions. Valid values are "driving", "walking" or "bicycling". |
| 30 | + :type mode: basestring |
| 31 | +
|
| 32 | + :param language: The language in which to return results. |
| 33 | + :type language: basestring |
| 34 | +
|
| 35 | + :param avoid: Indicates that the calculated route(s) should avoid the |
| 36 | + indicated features. Valid values are "tolls", "highways" or "ferries" |
| 37 | + :type avoid: basestring |
| 38 | +
|
| 39 | + :param units: Specifies the unit system to use when displaying results. |
| 40 | + Valid values are "metric" or "imperial" |
| 41 | + :type units: basestring |
| 42 | +
|
| 43 | + :param departure_time: Specifies the desired time of departure as seconds |
| 44 | + since midnight, January 1, 1970 UTC. The departure time may be |
| 45 | + specified by Google Maps API for Work customers to receive trip |
| 46 | + duration considering current traffic conditions. The departure_time |
| 47 | + must be set to within a few minutes of the current time. |
| 48 | + :type departure_time: int or datetime.datetime |
| 49 | +
|
| 50 | + :rtype: matrix of distances. Results are returned in rows, each row |
| 51 | + containing one origin paired with each destination. |
| 52 | + """ |
| 53 | + |
| 54 | + params = { |
| 55 | + "origins": _convert_path(origins), |
| 56 | + "destinations": _convert_path(destinations) |
| 57 | + } |
| 58 | + |
| 59 | + if mode: |
| 60 | + if mode not in ["driving", "walking", "bicycling"]: |
| 61 | + raise Exception("Invalid travel mode.") |
| 62 | + params["mode"] = mode |
| 63 | + |
| 64 | + if language: |
| 65 | + params["language"] = language |
| 66 | + |
| 67 | + if avoid: |
| 68 | + if avoid not in ["tolls", "highways", "ferries"]: |
| 69 | + raise Exception("Invalid route restriction.") |
| 70 | + params["avoid"] = avoid |
| 71 | + |
| 72 | + if units: |
| 73 | + params["units"] = units |
| 74 | + |
| 75 | + if departure_time: |
| 76 | + params["departure_time"] = convert.time(departure_time) |
| 77 | + |
| 78 | + return common._get(ctx, "/maps/api/distancematrix/json", params) |
| 79 | + |
| 80 | + |
| 81 | +def _convert_path(waypoints): |
| 82 | + # Handle the single-tuple case |
| 83 | + if type(waypoints) is tuple: |
| 84 | + waypoints = [waypoints] |
| 85 | + else: |
| 86 | + waypoints = as_list(waypoints) |
| 87 | + |
| 88 | + return convert.join_list("|", |
| 89 | + [(k if isinstance(k, basestring) else convert.latlng(k)) |
| 90 | + for k in waypoints]) |
0 commit comments