Skip to content

Commit

Permalink
Add language parameter to the Pelias geocoder (#378)
Browse files Browse the repository at this point in the history
Both methods `geocode` and `reverse` accept the `language`
parameter which will be passed to the pelias-api as GET
parameter `lang`.

This applies to `GeocodeEarth` as well.
  • Loading branch information
mir06 authored and KostyaEsmukov committed Dec 17, 2019
1 parent 564b9cc commit 980bf17
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions geopy/geocoders/pelias.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def geocode(
timeout=DEFAULT_SENTINEL,
boundary_rect=None,
country_bias=None,
language=False
):
"""
Return a location point by address.
Expand All @@ -157,6 +158,12 @@ def geocode(
.. versionadded:: 1.19.0
:param str language: Preferred language in which to return results.
Either uses standard
`RFC2616 <http://www.ietf.org/rfc/rfc2616.txt>`_
accept-language string or a simple comma-separated
list of language codes.
:rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
``exactly_one=False``.
"""
Expand Down Expand Up @@ -194,6 +201,9 @@ def geocode(
if country_bias:
params['boundary.country'] = country_bias

if language:
params["lang"] = language

url = "?".join((self.geocode_api, urlencode(params)))
logger.debug("%s.geocode_api: %s", self.__class__.__name__, url)
return self._parse_json(
Expand All @@ -205,6 +215,7 @@ def reverse(
query,
exactly_one=True,
timeout=DEFAULT_SENTINEL,
language=False
):
"""
Return an address by location point.
Expand All @@ -222,6 +233,12 @@ def reverse(
exception. Set this only if you wish to override, on this call
only, the value set during the geocoder's initialization.
:param str language: Preferred language in which to return results.
Either uses standard
`RFC2616 <http://www.ietf.org/rfc/rfc2616.txt>`_
accept-language string or a simple comma-separated
list of language codes.
:rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
``exactly_one=False``.
"""
Expand All @@ -234,6 +251,9 @@ def reverse(
'point.lon': lon,
}

if language:
params['lang'] = language

if self.api_key:
params.update({
'api_key': self.api_key
Expand Down
38 changes: 38 additions & 0 deletions test/geocoders/pelias.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,51 @@ def test_boundary_rect_deprecated(self):
)
self.assertEqual(1, len(w))

def test_geocode_language_parameter(self):
query = "Graben 7, Wien"
result_geocode = self.geocode_run(
{"query": query, "language": "de"}, {}
)
self.assertEqual(
result_geocode.raw['properties']['country'],
"Österreich"
)
result_geocode = self.geocode_run(
{"query": query, "language": "en"}, {}
)
self.assertEqual(
result_geocode.raw['properties']['country'],
"Austria"
)

def test_reverse_language_parameter(self):
query = "48.198674, 16.348388"
result_reverse_de = self.reverse_run(
{"query": query, "exactly_one": True, "language": "de"},
{},
)
self.assertEqual(
result_reverse_de.raw['properties']['country'],
"Österreich"
)

result_reverse_en = self.reverse_run(
{"query": query, "exactly_one": True, "language": "en"},
{},
)
self.assertTrue(
result_reverse_en.raw['properties']['country'],
"Austria"
)


@unittest.skipUnless(
bool(env.get('PELIAS_DOMAIN')),
"No PELIAS_DOMAIN env variable set"
)
class PeliasTestCase(BasePeliasTestCase, GeocoderTestBase):

@classmethod
def make_geocoder(cls, **kwargs):
return Pelias(env.get('PELIAS_DOMAIN'), api_key=env.get('PELIAS_KEY'),
**kwargs)

0 comments on commit 980bf17

Please sign in to comment.