Skip to content

Commit

Permalink
Allowing optional language for geocode. (#503)
Browse files Browse the repository at this point in the history
The mapbox api allows to specify the language used
in geocoding results. The test executes a query, which
return the state and country name in German to verify
the implementation. This test is inspired by the Bing
culture reverse geocoding tests.
  • Loading branch information
dennisstritzke committed Jul 31, 2022
1 parent 5cbaa91 commit 60fe5e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions geopy/geocoders/mapbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def geocode(
timeout=DEFAULT_SENTINEL,
proximity=None,
country=None,
language=None,
bbox=None
):
"""
Expand All @@ -126,6 +127,11 @@ def geocode(
:type country: str or list
:param str language: This parameter controls the language of the text supplied in
responses, and also affects result scoring, with results matching the user’s
query in the requested language being preferred over results that match in
another language. You can pass two letters country codes (ISO 639-1).
:param bbox: The bounding box of the viewport within which
to bias geocode results more prominently.
Example: ``[Point(22, 180), Point(-22, -180)]``.
Expand Down Expand Up @@ -153,6 +159,9 @@ def geocode(
p = Point(proximity)
params['proximity'] = "%s,%s" % (p.longitude, p.latitude)

if language:
params['language'] = language

quoted_query = quote(query.encode('utf-8'))
url = "?".join((self.api % dict(query=quoted_query),
urlencode(params)))
Expand Down
8 changes: 8 additions & 0 deletions test/geocoders/mapbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ async def test_geocode_exactly_one_false(self):
{},
)
assert len(list_result) >= 3

async def test_geocode_language(self):
res = await self.geocode_run(
{"query": "Frankfurt", "language": "DE"},
{}
)

assert "Frankfurt am Main, Hessen, Deutschland" in res.address

0 comments on commit 60fe5e8

Please sign in to comment.