From dbb4f9204aabd2ea8a9cd78ab753f12607006d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Wed, 11 Jul 2018 15:30:51 +0200 Subject: [PATCH] Precision metadata for HERE --- .../cartodb_services/here/geocoder.py | 16 ++++++++++++++-- test/integration/test_street_functions.py | 19 +++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/here/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/here/geocoder.py index 8139e1a..44d695d 100644 --- a/server/lib/python/cartodb_services/cartodb_services/here/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/here/geocoder.py @@ -6,8 +6,14 @@ import requests from requests.adapters import HTTPAdapter from exceptions import * +from cartodb_services import PRECISION_PRECISE, PRECISION_INTERPOLATED from cartodb_services.metrics import Traceable +PRECISION_BY_MATCH_TYPE = { + 'pointAddress': PRECISION_PRECISE, + 'interpolated': PRECISION_INTERPOLATED +} + class HereMapsGeocoder(Traceable): 'A Here Maps Geocoder wrapper for python' @@ -82,11 +88,13 @@ class HereMapsGeocoder(Traceable): try: response = self._perform_request(params) result = response['Response']['View'][0]['Result'][0] + self._logger.debug('--> Result: {}'.format(result)) return [self._extract_lng_lat_from_result(result), self._extract_metadata_from_result(result)] except IndexError: return [[], {}] - except KeyError: + except KeyError as e: + self._logger.error('params: {}'.format(params), e) raise MalformedResult() def _perform_request(self, params): @@ -124,6 +132,10 @@ class HereMapsGeocoder(Traceable): return [longitude, latitude] def _extract_metadata_from_result(self, result): + # See https://stackoverflow.com/questions/51285622/missing-matchtype-at-here-geocoding-responses + precision = PRECISION_BY_MATCH_TYPE[ + result.get('MatchType', 'pointAddress')] return { - 'relevance': result['Relevance'] + 'relevance': result['Relevance'], + 'precision': precision } diff --git a/test/integration/test_street_functions.py b/test/integration/test_street_functions.py index 1b8430d..cfc4547 100644 --- a/test/integration/test_street_functions.py +++ b/test/integration/test_street_functions.py @@ -70,18 +70,17 @@ class TestStreetFunctionsSetUp(TestCase): } GOOGLE_METADATAS = { - 'Plaza España, Barcelona': { - 'relevance': 0.9, 'precision': 'precise' - }, - 'Santiago Rusiñol 123, Valladolid': { - 'relevance': 0.8, 'precision': 'interpolated' - } + 'Plaza España, Barcelona': + {'relevance': 0.9, 'precision': 'precise'}, + 'Santiago Rusiñol 123, Valladolid': + {'relevance': 0.8, 'precision': 'interpolated'} } HERE_METADATAS = { - 'Plaza España, Barcelona': { - 'relevance': 1 - } + 'Plaza España, Barcelona': + {'relevance': 1, 'precision': 'precise'}, + 'Santiago Rusiñol 123, Valladolid': + {'relevance': 0.89, 'precision': 'precise'} # Wrong. See https://stackoverflow.com/questions/51285622/missing-matchtype-at-here-geocoding-responses } TOMTOM_METADATAS = { @@ -98,7 +97,7 @@ class TestStreetFunctionsSetUp(TestCase): METADATAS = { 'google': GOOGLE_METADATAS, - 'here': HERE_METADATAS, + 'heremaps': HERE_METADATAS, 'tomtom': TOMTOM_METADATAS, 'mapbox': MAPBOX_METADATAS }