Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added an option to pass countrycode to opencage #406

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/providers/OpenCage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Parameters
- `location`: Your search location you want geocoded.
- `key`: (optional) use your own API Key from OpenCage.
- `maxRows`: (default=1) Max number of results to fetch
- `countrycode`: (default=None) A string representing the country codes to restrict the search to (e.g. 'ca,us')
- `method`: (default=geocode) Use the following:

- geocode
Expand Down
5 changes: 4 additions & 1 deletion geocoder/opencage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class OpenCageResult(OneResult):

def __init__(self, json_content):
# create safe shortcuts
self._geometry = json_content.get('geometry', {})
Expand Down Expand Up @@ -404,6 +403,10 @@ def _build_params(self, location, provider_key, **kwargs):
if language:
base_params['language'] = language

countrycode = kwargs.get('countrycode', None)
if countrycode:
base_params['countrycode'] = countrycode

return base_params

def _catch_errors(self, json_response):
Expand Down
23 changes: 19 additions & 4 deletions tests/test_opencage.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,38 @@ def test_opencage():


def test_issue_292():
g = geocoder.opencage('AirportClinic M - MediCare Flughafen München Medizinisches Zentrum', countrycode='DE', language='de', no_annotations=1)
g = geocoder.opencage(
'AirportClinic M - MediCare Flughafen München Medizinisches Zentrum',
countrycode='DE',
language='de',
no_annotations=1)
assert g.ok


def test_opencage_no_language_param():
""" Expected result :
https://api.opencagedata.com/geocode/v1/json=Ottawa,Ontario&key=YOUR-API-KEY
"""
g = geocoder.opencage(location)
assert 'language' not in g.url


def test_opencage_language_param():
""" Expected result :
https://api.opencagedata.com/geocode/v1/json=Ottawa,Ontario&key=YOUR-API-KEY&language=de
"""
g = geocoder.opencage(location, language='de')
assert 'language=de' in g.url.split('&')


def test_opencage_countrycode_param():
""" Expected result:
https://api.opencagedata.com/geocode/v1/json?q=Ottawa,Ontario&key=YOUR-API-KEY&countrycode=ca"
"""
g = geocoder.opencage(location, countrycode='ca')
assert 'countrycode=ca' in g.url.split('&')


def test_opencage_multi_result():
g = geocoder.opencage(location, maxRows=5)
assert len(g) > 1
Expand All @@ -66,9 +81,11 @@ def test_opencage_address():
assert (g.remaining_api_calls > 0 and g.remaining_api_calls != 999999)
assert (g.limit_api_calls > 0 and g.remaining_api_calls != 999999)


def test_opencage_paid():
# Paid API keys can be set to unlimited and have rate limit information ommitted from the response
url = 'http://api.opencagedata.com/geocode/v1/json?query=The+Happy+Goat%2C+Ottawa&limit=1&key=' + os.environ.get('OPENCAGE_API_KEY')
url = 'http://api.opencagedata.com/geocode/v1/json?query=The+Happy+Goat%2C+Ottawa&limit=1&key=' + os.environ.get(
'OPENCAGE_API_KEY')
data_file = 'tests/results/opencagedata_paid.json'
with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
mocker.get(url, text=input.read())
Expand All @@ -81,8 +98,6 @@ def test_opencage_paid():
assert result.limit_api_calls == 999999




def test_opencage_reverse():
""" Expected result :
https://api.opencagedata.com/geocode/v1/json?q=45.4215296,-75.6971930&key=YOUR-API-KEY
Expand Down