Precision metadata for HERE

This commit is contained in:
Juan Ignacio Sánchez Lara 2018-07-11 15:30:51 +02:00
parent 67fee1cce8
commit dbb4f9204a
2 changed files with 23 additions and 12 deletions

View File

@ -6,8 +6,14 @@ import requests
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
from exceptions import * from exceptions import *
from cartodb_services import PRECISION_PRECISE, PRECISION_INTERPOLATED
from cartodb_services.metrics import Traceable from cartodb_services.metrics import Traceable
PRECISION_BY_MATCH_TYPE = {
'pointAddress': PRECISION_PRECISE,
'interpolated': PRECISION_INTERPOLATED
}
class HereMapsGeocoder(Traceable): class HereMapsGeocoder(Traceable):
'A Here Maps Geocoder wrapper for python' 'A Here Maps Geocoder wrapper for python'
@ -82,11 +88,13 @@ class HereMapsGeocoder(Traceable):
try: try:
response = self._perform_request(params) response = self._perform_request(params)
result = response['Response']['View'][0]['Result'][0] result = response['Response']['View'][0]['Result'][0]
self._logger.debug('--> Result: {}'.format(result))
return [self._extract_lng_lat_from_result(result), return [self._extract_lng_lat_from_result(result),
self._extract_metadata_from_result(result)] self._extract_metadata_from_result(result)]
except IndexError: except IndexError:
return [[], {}] return [[], {}]
except KeyError: except KeyError as e:
self._logger.error('params: {}'.format(params), e)
raise MalformedResult() raise MalformedResult()
def _perform_request(self, params): def _perform_request(self, params):
@ -124,6 +132,10 @@ class HereMapsGeocoder(Traceable):
return [longitude, latitude] return [longitude, latitude]
def _extract_metadata_from_result(self, result): 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 { return {
'relevance': result['Relevance'] 'relevance': result['Relevance'],
'precision': precision
} }

View File

@ -70,18 +70,17 @@ class TestStreetFunctionsSetUp(TestCase):
} }
GOOGLE_METADATAS = { GOOGLE_METADATAS = {
'Plaza España, Barcelona': { 'Plaza España, Barcelona':
'relevance': 0.9, 'precision': 'precise' {'relevance': 0.9, 'precision': 'precise'},
}, 'Santiago Rusiñol 123, Valladolid':
'Santiago Rusiñol 123, Valladolid': { {'relevance': 0.8, 'precision': 'interpolated'}
'relevance': 0.8, 'precision': 'interpolated'
}
} }
HERE_METADATAS = { HERE_METADATAS = {
'Plaza España, Barcelona': { 'Plaza España, Barcelona':
'relevance': 1 {'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 = { TOMTOM_METADATAS = {
@ -98,7 +97,7 @@ class TestStreetFunctionsSetUp(TestCase):
METADATAS = { METADATAS = {
'google': GOOGLE_METADATAS, 'google': GOOGLE_METADATAS,
'here': HERE_METADATAS, 'heremaps': HERE_METADATAS,
'tomtom': TOMTOM_METADATAS, 'tomtom': TOMTOM_METADATAS,
'mapbox': MAPBOX_METADATAS 'mapbox': MAPBOX_METADATAS
} }