forked from googlemaps/google-maps-services-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroads.py
More file actions
125 lines (90 loc) · 4.12 KB
/
Copy pathroads.py
File metadata and controls
125 lines (90 loc) · 4.12 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
#
# Copyright 2015 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 Roads API."""
import googlemaps
from googlemaps import convert
_ROADS_BASE_URL = "https://roads.googleapis.com"
def snap_to_roads(client, path, interpolate=False):
"""Snaps a path to the most likely roads travelled.
Takes up to 100 GPS points collected along a route, and returns a similar
set of data with the points snapped to the most likely roads the vehicle
was traveling along.
:param path: The path to be snapped.
:type path: a single location, or a list of locations, where a
location is a string, dict, list, or tuple
:param interpolate: Whether to interpolate a path to include all points
forming the full road-geometry. When true, additional interpolated
points will also be returned, resulting in a path that smoothly follows
the geometry of the road, even around corners and through tunnels.
Interpolated paths may contain more points than the original path.
:type interpolate: bool
:rtype: A list of snapped points.
"""
params = {"path": convert.location_list(path)}
if interpolate:
params["interpolate"] = "true"
return client._get("/v1/snapToRoads", params,
base_url=_ROADS_BASE_URL,
accepts_clientid=False,
extract_body=_roads_extract)["snappedPoints"]
def speed_limits(client, place_ids):
"""Returns the posted speed limit (in km/h) for given road segments.
:param place_ids: The Place ID of the road segment. Place IDs are returned
by the snap_to_roads function. You can pass up to 100 Place IDs.
:type place_ids: str or list
:rtype: list of speed limits.
"""
params = [("placeId", place_id) for place_id in convert.as_list(place_ids)]
return client._get("/v1/speedLimits", params,
base_url=_ROADS_BASE_URL,
accepts_clientid=False,
extract_body=_roads_extract)["speedLimits"]
def snapped_speed_limits(client, path):
"""Returns the posted speed limit (in km/h) for given road segments.
The provided points will first be snapped to the most likely roads the
vehicle was traveling along.
:param path: The path of points to be snapped.
:type path: a single location, or a list of locations, where a
location is a string, dict, list, or tuple
:rtype: dict with a list of speed limits and a list of the snapped points.
"""
params = {"path": convert.location_list(path)}
return client._get("/v1/speedLimits", params,
base_url=_ROADS_BASE_URL,
accepts_clientid=False,
extract_body=_roads_extract)
def _roads_extract(resp):
"""Extracts a result from a Roads API HTTP response."""
try:
j = resp.json()
except:
if resp.status_code != 200:
raise googlemaps.exceptions.HTTPError(resp.status_code)
raise googlemaps.exceptions.ApiError("UNKNOWN_ERROR",
"Received a malformed response.")
if "error" in j:
error = j["error"]
status = error["status"]
if status == "RESOURCE_EXHAUSTED":
raise googlemaps.exceptions._RetriableRequest()
if "message" in error:
raise googlemaps.exceptions.ApiError(status, error["message"])
else:
raise googlemaps.exceptions.ApiError(status)
if resp.status_code != 200:
raise googlemaps.exceptions.HTTPError(resp.status_code)
return j