From ac0103e8e8eb6ee5a8ecce183bc44dfb5bc4d977 Mon Sep 17 00:00:00 2001 From: G <30966740+g-mc@users.noreply.github.com> Date: Wed, 20 Jun 2018 18:24:55 -0600 Subject: [PATCH 1/3] Use LocationIQ's `reverse` endpoint instead * Import `Location` to properly split given latitude/longitude * Modify `_URL` to use `reverse` endpoint instead * Modify `_build_params` for `reverse` endpoint * Modify `_adapt_results` to put single JSON response in a list * Modify coordinates in `LocationIQReverse` example (from unittest) --- geocoder/locationiq_reverse.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/geocoder/locationiq_reverse.py b/geocoder/locationiq_reverse.py index 5f0e1387..7d097301 100644 --- a/geocoder/locationiq_reverse.py +++ b/geocoder/locationiq_reverse.py @@ -2,6 +2,8 @@ # coding: utf8 from __future__ import absolute_import + +from geocoder.location import Location from geocoder.locationiq import LocationIQQuery @@ -9,6 +11,21 @@ class LocationIQReverse(LocationIQQuery): provider = 'locationiq' method = 'reverse' + _URL = 'https://locationiq.org/v1/reverse.php' + + def _build_params(self, location, provider_key, **kwargs): + location = Location(location) + return { + 'format': 'json', + 'key': provider_key, + 'lat': location.latitude, + 'lon': location.longitude, + } + + def _adapt_results(self, json_response): + return [json_response] + + if __name__ == '__main__': - g = LocationIQReverse("45.3, -75.4") + g = LocationIQReverse("45.421106, -75.690308") g.debug() From 77f17eff5c78c296e4f4857af8e2e94fc893cda4 Mon Sep 17 00:00:00 2001 From: G <30966740+g-mc@users.noreply.github.com> Date: Wed, 20 Jun 2018 18:26:10 -0600 Subject: [PATCH 2/3] Update example JSON response from the `reverse` endpoint instead --- tests/results/locationiq_reverse.json | 61 ++++++++++++--------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/tests/results/locationiq_reverse.json b/tests/results/locationiq_reverse.json index 5554366d..646ead5a 100644 --- a/tests/results/locationiq_reverse.json +++ b/tests/results/locationiq_reverse.json @@ -1,33 +1,28 @@ -[ - { - "place_id": "85985426", - "licence": "API \u00a9 LocationIQ.org CC BY 4.0, Data \u00a9 OpenStreetMap contributors, ODbL 1.0", - "osm_type": "way", - "osm_id": "68588664", - "boundingbox": [ - "45.4201768", - "45.4214404", - "-75.6908211", - "-75.6893108" - ], - "lat": "45.4208154", - "lon": "-75.6901176990294", - "display_name": "Ottawa City Hall, 110, Laurier Avenue West, Golden Triangle, Centretown, Somerset, Ottawa, Ontario, K2P 2K1, Canada", - "class": "building", - "type": "yes", - "importance": 0.31303438301933, - "address": { - "building": "Ottawa City Hall", - "house_number": "110", - "road": "Laurier Avenue West", - "neighbourhood": "Golden Triangle", - "suburb": "Centretown", - "city_district": "Somerset", - "city": "Ottawa", - "state": "Ontario", - "postcode": "K2P 2K1", - "country": "Canada", - "country_code": "ca" - } - } -] +{ + "place_id": "90622554", + "licence": "\u00a9 LocationIQ.org CC BY 4.0, Data \u00a9 OpenStreetMap contributors, ODbL 1.0", + "osm_type": "way", + "osm_id": "68588664", + "lat": "45.4208154", + "lon": "-75.6901176990294", + "display_name": "Ottawa City Hall, 110, Laurier Avenue West, Golden Triangle, Centretown, Somerset, Ottawa, Ontario, K2P 2K1, Canada", + "address": { + "address29": "Ottawa City Hall", + "house_number": "110", + "road": "Laurier Avenue West", + "neighbourhood": "Golden Triangle", + "suburb": "Centretown", + "city_district": "Somerset", + "city": "Ottawa", + "state": "Ontario", + "postcode": "K2P 2K1", + "country": "Canada", + "country_code": "ca" + }, + "boundingbox": [ + "45.4201768", + "45.4214404", + "-75.6908211", + "-75.6893108" + ] +} From 7069fd23558b37f9f7e5ee701e333f6e5fce0c68 Mon Sep 17 00:00:00 2001 From: G <30966740+g-mc@users.noreply.github.com> Date: Wed, 20 Jun 2018 18:27:11 -0600 Subject: [PATCH 3/3] Update URL to use `reverse` endpoint instead --- tests/test_locationiq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_locationiq.py b/tests/test_locationiq.py index 399a9837..2c7f39bc 100644 --- a/tests/test_locationiq.py +++ b/tests/test_locationiq.py @@ -44,7 +44,7 @@ def test_locationiq_multi_result(): def test_locationiq_reverse(): - url = 'https://locationiq.org/v1/search.php?q=45.421106%2C+-75.690308&format=json&addressdetails=1&key=TEST_KEY' + url = 'https://locationiq.org/v1/reverse.php?format=json&key=TEST_KEY&lat=45.421106&lon=-75.690308' data_file = 'tests/results/locationiq_reverse.json' with requests_mock.Mocker() as mocker, open(data_file, 'r') as ip: mocker.get(url, text=ip.read())