Skip to content

Commit

Permalink
Allow multiple components of same type in GoogleV3 geocoder. (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
pratheekrebala committed May 9, 2020
1 parent fbd3713 commit 3182f07
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
22 changes: 19 additions & 3 deletions geopy/geocoders/googlev3.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,19 @@ def _format_components_param(components):
"""
Format the components dict to something Google understands.
"""
component_items = []

if isinstance(components, dict):
component_items = components.items()
elif isinstance(components, list):
component_items = components
else:
raise ValueError(
'`components` parameter must be of type `dict` or `list`')

return "|".join(
(":".join(item) for item in components.items())
)
(":".join(item) for item in component_items)
)

def geocode(
self,
Expand Down Expand Up @@ -203,8 +213,14 @@ def geocode(
:param str region: The region code, specified as a ccTLD
("top-level domain") two-character value.
:param dict components: Restricts to an area. Can use any combination
:param Union[dict,list] components: Restricts to an area. Can use any combination
of: route, locality, administrative_area, postal_code, country.
Pass a list of tuples if you want to specify multiple components of the same type
e.g.:
>>> [('administrative_area','VA'), ('administrative_area':'Arlington')]
:param str place_id: Retrieve a Location using a Place ID.
Cannot be not used with ``query`` or ``bounds`` parameters.
Expand Down
46 changes: 41 additions & 5 deletions test/geocoders/googlev3.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def test_format_components_param(self):
"""
f = GoogleV3._format_components_param
self.assertEqual(f({}), '')
self.assertEqual(f([]), '')

self.assertEqual(f({'country': 'FR'}), 'country:FR')
output = f({'administrative_area': 'CA', 'country': 'FR'})
# the order the dict is iterated over is not important
Expand All @@ -152,13 +154,18 @@ def test_format_components_param(self):
), output
)

with self.assertRaises(AttributeError):
f(None)
self.assertEqual(f([('country', 'FR')]), 'country:FR')
output = f([
('administrative_area','CA'),
('administrative_area', 'Los Angeles'),
('country', 'US')
])
self.assertEqual(output,'administrative_area:CA|administrative_area:Los Angeles|country:US')

with self.assertRaises(AttributeError):
f([])
with self.assertRaises(ValueError):
f(None)

with self.assertRaises(AttributeError):
with self.assertRaises(ValueError):
f('administrative_area:CA|country:FR')

def test_geocode(self):
Expand Down Expand Up @@ -195,6 +202,18 @@ def test_geocode_with_conflicting_components(self):
expect_failure=True
)

self.geocode_run(
{
"query": "santa cruz",
"components": [
('administrative_area', 'CA'),
('country', 'FR')
]
},
{},
expect_failure=True
)

def test_components(self):
"""
GoogleV3.geocode with components
Expand All @@ -209,6 +228,16 @@ def test_components(self):
{"latitude": 28.4636296, "longitude": -16.2518467},
)

self.geocode_run(
{
"query": "santa cruz",
"components": [
('country', 'ES')
]
},
{"latitude": 28.4636296, "longitude": -16.2518467},
)

def test_components_without_query(self):
self.geocode_run(
{
Expand All @@ -217,6 +246,13 @@ def test_components_without_query(self):
{"latitude": 46.227638, "longitude": 2.213749},
)

self.geocode_run(
{
"components": [("city", "Paris"), ("country", "FR")],
},
{"latitude": 46.227638, "longitude": 2.213749},
)

def test_reverse(self):
self.reverse_run(
{"query": self.new_york_point, "exactly_one": True},
Expand Down

0 comments on commit 3182f07

Please sign in to comment.