Skip to content

Commit a95f4a6

Browse files
authored
Merge pull request #25 from insspb/vlsd_master
Added an option to pass countrycode to opencage
2 parents a532572 + 55a0adb commit a95f4a6

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

docs/source/providers/OpenCage.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Parameters
5151
- `location`: Your search location you want geocoded.
5252
- `key`: (optional) use your own API Key from OpenCage.
5353
- `maxRows`: (default=1) Max number of results to fetch
54+
- `countrycode`: (default=None) A string representing the country codes to restrict the search to (e.g. 'ca,us')
5455
- `method`: (default=geocode) Use the following:
5556

5657
- geocode

geocoder/opencage.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212

1313
class OpenCageResult(OneResult):
14-
1514
def __init__(self, json_content):
1615
# create safe shortcuts
1716
self._geometry = json_content.get('geometry', {})
@@ -404,6 +403,10 @@ def _build_params(self, location, provider_key, **kwargs):
404403
if language:
405404
base_params['language'] = language
406405

406+
countrycode = kwargs.get('countrycode', None)
407+
if countrycode:
408+
base_params['countrycode'] = countrycode
409+
407410
return base_params
408411

409412
def _catch_errors(self, json_response):

tests/test_opencage.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,38 @@ def test_opencage():
2828

2929

3030
def test_issue_292():
31-
g = geocoder.opencage('AirportClinic M - MediCare Flughafen München Medizinisches Zentrum', countrycode='DE', language='de', no_annotations=1)
31+
g = geocoder.opencage(
32+
'AirportClinic M - MediCare Flughafen München Medizinisches Zentrum',
33+
countrycode='DE',
34+
language='de',
35+
no_annotations=1)
3236
assert g.ok
3337

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

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

54+
55+
def test_opencage_countrycode_param():
56+
""" Expected result:
57+
https://api.opencagedata.com/geocode/v1/json?q=Ottawa,Ontario&key=YOUR-API-KEY&countrycode=ca"
58+
"""
59+
g = geocoder.opencage(location, countrycode='ca')
60+
assert 'countrycode=ca' in g.url.split('&')
61+
62+
4863
def test_opencage_multi_result():
4964
g = geocoder.opencage(location, maxRows=5)
5065
assert len(g) > 1
@@ -66,9 +81,11 @@ def test_opencage_address():
6681
assert (g.remaining_api_calls > 0 and g.remaining_api_calls != 999999)
6782
assert (g.limit_api_calls > 0 and g.remaining_api_calls != 999999)
6883

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

83100

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

0 commit comments

Comments
 (0)