Skip to content

Commit 9ec69cb

Browse files
tcliff111Tom Clifford
andauthored
feat!: Added Address Descriptors to Geocoding response. Refactored Geo… (#516)
* feat! Added Address Descriptors to Geocoding response. Refactored Geocoding response to allow fields outside the geocoding result to be exposed through the client. --------- Co-authored-by: Tom Clifford <thomasclifford@google.com>
1 parent 645e07d commit 9ec69cb

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ addressvalidation_result = gmaps.addressvalidation(['1600 Amphitheatre Pk'],
9292
regionCode='US',
9393
locality='Mountain View',
9494
enableUspsCass=True)
95+
96+
# Get an Address Descriptor of a location in the reverse geocoding response
97+
address_descriptor_result = gmaps.reverse_geocode((40.714224, -73.961452), enable_address_descriptor=True)
98+
9599
```
96100

97101
For more usage examples, check out [the tests](https://github.com/googlemaps/google-maps-services-python/tree/master/tests).

googlemaps/geocoding.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def geocode(client, address=None, place_id=None, components=None, bounds=None, r
4949
:param language: The language in which to return results.
5050
:type language: string
5151
52-
:rtype: list of geocoding results.
52+
:rtype: result dict with the following keys:
53+
status: status code
54+
results: list of geocoding results
5355
"""
5456

5557
params = {}
@@ -72,11 +74,11 @@ def geocode(client, address=None, place_id=None, components=None, bounds=None, r
7274
if language:
7375
params["language"] = language
7476

75-
return client._request("/maps/api/geocode/json", params).get("results", [])
77+
return client._request("/maps/api/geocode/json", params)
7678

7779

7880
def reverse_geocode(client, latlng, result_type=None, location_type=None,
79-
language=None):
81+
language=None, enable_address_descriptor=False):
8082
"""
8183
Reverse geocoding is the process of converting geographic coordinates into a
8284
human-readable address.
@@ -94,7 +96,10 @@ def reverse_geocode(client, latlng, result_type=None, location_type=None,
9496
:param language: The language in which to return results.
9597
:type language: string
9698
97-
:rtype: list of reverse geocoding results.
99+
:rtype: result dict with the following keys:
100+
status: status code
101+
results: list of reverse geocoding results
102+
address_descriptor: address descriptor for the target
98103
"""
99104

100105
# Check if latlng param is a place_id string.
@@ -113,4 +118,7 @@ def reverse_geocode(client, latlng, result_type=None, location_type=None,
113118
if language:
114119
params["language"] = language
115120

116-
return client._request("/maps/api/geocode/json", params).get("results", [])
121+
if enable_address_descriptor:
122+
params["enable_address_descriptor"] = "true"
123+
124+
return client._request("/maps/api/geocode/json", params)

tests/test_geocoding.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_simple_geocode(self):
4141
content_type="application/json",
4242
)
4343

44-
results = self.client.geocode("Sydney")
44+
results = self.client.geocode("Sydney").get("results", [])
4545

4646
self.assertEqual(1, len(responses.calls))
4747
self.assertURLEqual(
@@ -60,7 +60,7 @@ def test_reverse_geocode(self):
6060
content_type="application/json",
6161
)
6262

63-
results = self.client.reverse_geocode((-33.8674869, 151.2069902))
63+
results = self.client.reverse_geocode((-33.8674869, 151.2069902)).get("results", [])
6464

6565
self.assertEqual(1, len(responses.calls))
6666
self.assertURLEqual(
@@ -79,7 +79,7 @@ def test_geocoding_the_googleplex(self):
7979
content_type="application/json",
8080
)
8181

82-
results = self.client.geocode("1600 Amphitheatre Parkway, " "Mountain View, CA")
82+
results = self.client.geocode("1600 Amphitheatre Parkway, " "Mountain View, CA").get("results", [])
8383

8484
self.assertEqual(1, len(responses.calls))
8585
self.assertURLEqual(
@@ -105,7 +105,7 @@ def test_geocode_with_bounds(self):
105105
"southwest": (34.172684, -118.604794),
106106
"northeast": (34.236144, -118.500938),
107107
},
108-
)
108+
).get("results", [])
109109

110110
self.assertEqual(1, len(responses.calls))
111111
self.assertURLEqual(
@@ -125,7 +125,7 @@ def test_geocode_with_region_biasing(self):
125125
content_type="application/json",
126126
)
127127

128-
results = self.client.geocode("Toledo", region="es")
128+
results = self.client.geocode("Toledo", region="es").get("results", [])
129129

130130
self.assertEqual(1, len(responses.calls))
131131
self.assertURLEqual(
@@ -144,7 +144,7 @@ def test_geocode_with_component_filter(self):
144144
content_type="application/json",
145145
)
146146

147-
results = self.client.geocode("santa cruz", components={"country": "ES"})
147+
results = self.client.geocode("santa cruz", components={"country": "ES"}).get("results", [])
148148

149149
self.assertEqual(1, len(responses.calls))
150150
self.assertURLEqual(
@@ -165,7 +165,7 @@ def test_geocode_with_multiple_component_filters(self):
165165

166166
results = self.client.geocode(
167167
"Torun", components={"administrative_area": "TX", "country": "US"}
168-
)
168+
).get("results", [])
169169

170170
self.assertEqual(1, len(responses.calls))
171171
self.assertURLEqual(
@@ -191,7 +191,7 @@ def test_geocode_with_just_components(self):
191191
"administrative_area": "Helsinki",
192192
"country": "Finland",
193193
}
194-
)
194+
).get("results", [])
195195

196196
self.assertEqual(1, len(responses.calls))
197197
self.assertURLEqual(
@@ -211,7 +211,7 @@ def test_geocode_place_id(self):
211211
content_type="application/json",
212212
)
213213

214-
results = self.client.geocode(place_id="ChIJeRpOeF67j4AR9ydy_PIzPuM")
214+
results = self.client.geocode(place_id="ChIJeRpOeF67j4AR9ydy_PIzPuM").get("results", [])
215215

216216
self.assertEqual(1, len(responses.calls))
217217
self.assertURLEqual(
@@ -230,7 +230,7 @@ def test_simple_reverse_geocode(self):
230230
content_type="application/json",
231231
)
232232

233-
results = self.client.reverse_geocode((40.714224, -73.961452))
233+
results = self.client.reverse_geocode((40.714224, -73.961452)).get("results", [])
234234

235235
self.assertEqual(1, len(responses.calls))
236236
self.assertURLEqual(
@@ -253,7 +253,7 @@ def test_reverse_geocode_restricted_by_type(self):
253253
(40.714224, -73.961452),
254254
location_type="ROOFTOP",
255255
result_type="street_address",
256-
)
256+
).get("results", [])
257257

258258
self.assertEqual(1, len(responses.calls))
259259
self.assertURLEqual(
@@ -277,7 +277,7 @@ def test_reverse_geocode_multiple_location_types(self):
277277
(40.714224, -73.961452),
278278
location_type=["ROOFTOP", "RANGE_INTERPOLATED"],
279279
result_type="street_address",
280-
)
280+
).get("results", [])
281281

282282
self.assertEqual(1, len(responses.calls))
283283
self.assertURLEqual(
@@ -301,7 +301,7 @@ def test_reverse_geocode_multiple_result_types(self):
301301
(40.714224, -73.961452),
302302
location_type="ROOFTOP",
303303
result_type=["street_address", "route"],
304-
)
304+
).get("results", [])
305305

306306
self.assertEqual(1, len(responses.calls))
307307
self.assertURLEqual(
@@ -311,6 +311,28 @@ def test_reverse_geocode_multiple_result_types(self):
311311
responses.calls[0].request.url,
312312
)
313313

314+
@responses.activate
315+
def test_reverse_geocode_with_address_descriptors(self):
316+
responses.add(
317+
responses.GET,
318+
"https://maps.googleapis.com/maps/api/geocode/json",
319+
body='{"status":"OK","results":[], "address_descriptor":{ "landmarks": [ { "placeId": "id" } ] } }',
320+
status=200,
321+
content_type="application/json",
322+
)
323+
324+
response = self.client.reverse_geocode((-33.8674869, 151.2069902), enable_address_descriptor=True)
325+
326+
address_descriptor = response.get("address_descriptor", [])
327+
328+
self.assertEqual(1, len(address_descriptor["landmarks"]))
329+
self.assertEqual(1, len(responses.calls))
330+
self.assertURLEqual(
331+
"https://maps.googleapis.com/maps/api/geocode/json?"
332+
"latlng=-33.8674869,151.2069902&enable_address_descriptor=true&key=%s" % self.key,
333+
responses.calls[0].request.url,
334+
)
335+
314336
@responses.activate
315337
def test_partial_match(self):
316338
responses.add(
@@ -321,7 +343,7 @@ def test_partial_match(self):
321343
content_type="application/json",
322344
)
323345

324-
results = self.client.geocode("Pirrama Pyrmont")
346+
results = self.client.geocode("Pirrama Pyrmont").get("results", [])
325347

326348
self.assertEqual(1, len(responses.calls))
327349
self.assertURLEqual(
@@ -340,7 +362,7 @@ def test_utf_results(self):
340362
content_type="application/json",
341363
)
342364

343-
results = self.client.geocode(components={"postal_code": "96766"})
365+
results = self.client.geocode(components={"postal_code": "96766"}).get("results", [])
344366

345367
self.assertEqual(1, len(responses.calls))
346368
self.assertURLEqual(
@@ -359,7 +381,7 @@ def test_utf8_request(self):
359381
content_type="application/json",
360382
)
361383

362-
self.client.geocode(self.u("\\u4e2d\\u56fd")) # China
384+
self.client.geocode(self.u("\\u4e2d\\u56fd")).get("results", []) # China
363385
self.assertURLEqual(
364386
"https://maps.googleapis.com/maps/api/geocode/json?"
365387
"key=%s&address=%s" % (self.key, "%E4%B8%AD%E5%9B%BD"),

0 commit comments

Comments
 (0)