From 2af92045427318f1b6d6463a21b93043996b033b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Tue, 10 Jul 2018 21:21:42 +0200 Subject: [PATCH] Relevance metadata for TomTom --- .../cartodb_services/tomtom/bulk_geocoder.py | 10 ++++---- .../cartodb_services/tomtom/geocoder.py | 25 ++++++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/tomtom/bulk_geocoder.py b/server/lib/python/cartodb_services/cartodb_services/tomtom/bulk_geocoder.py index a3d8583..c8ad803 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tomtom/bulk_geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/tomtom/bulk_geocoder.py @@ -38,18 +38,18 @@ class TomTomBulkGeocoder(TomTomGeocoder, StreetPointBulkGeocoder): state = state.encode('utf-8') if state else None country = country.encode('utf-8') if country else None self._logger.debug('--> Sending serial search: {}'.format(search)) - coordinates = self.geocode(searchtext=address, city=city, + result = self.geocode_meta(searchtext=address, city=city, state_province=state, country=country) self._logger.debug('--> result sent') - results.append((search_id, coordinates, [])) + results.append((search_id, result[0], result[1])) return results def _batch_geocode(self, searches): location = self._send_batch(searches) - xy_results = self._download_results(location) + full_results = self._download_results(location) results = [] - for s, r in zip(searches, xy_results): - results.append((s[0], r, [])) + for s, r in zip(searches, full_results): + results.append((s[0], r[0], r[1])) self._logger.debug('--> results: {}'.format(results)) return results diff --git a/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py index b4e82cb..b064ea5 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py @@ -18,6 +18,7 @@ ENTRY_RESULTS = 'results' ENTRY_POSITION = 'position' ENTRY_LON = 'lon' ENTRY_LAT = 'lat' +EMPTY_RESPONSE = [[], {}] class TomTomGeocoder(Traceable): @@ -62,6 +63,11 @@ class TomTomGeocoder(Traceable): @qps_retry(qps=5) def geocode(self, searchtext, city=None, state_province=None, country=None): + return self.geocode_meta(searchtext, city, state_province, country)[0] + + @qps_retry(qps=5) + def geocode_meta(self, searchtext, city=None, state_province=None, + country=None): if searchtext: searchtext = searchtext.decode('utf-8') if city: @@ -72,7 +78,7 @@ class TomTomGeocoder(Traceable): country = country.decode('utf-8') if not self._validate_input(searchtext, city, state_province, country): - return [] + return EMPTY_RESPONSE address = [] if searchtext and searchtext.strip(): @@ -98,15 +104,15 @@ class TomTomGeocoder(Traceable): # Don't raise the exception to continue with the geocoding job self._logger.error('Error connecting to TomTom geocoding server', exception=ce) - return [] + return EMPTY_RESPONSE def _parse_response(self, status_code, text): if status_code == requests.codes.ok: return self._parse_geocoder_response(text) elif status_code == requests.codes.bad_request: - return [] + return EMPTY_RESPONSE elif status_code == requests.codes.unprocessable_entity: - return [] + return EMPTY_RESPONSE else: msg = 'Unknown response {}: {}'.format(str(status_code), text) raise ServiceException(msg, None) @@ -117,7 +123,14 @@ class TomTomGeocoder(Traceable): if json_response and json_response[ENTRY_RESULTS]: result = json_response[ENTRY_RESULTS][0] - return self._extract_lng_lat_from_feature(result) + return [ + self._extract_lng_lat_from_feature(result), + self._extract_metadata_from_result(result) + ] else: - return [] + return EMPTY_RESPONSE + def _extract_metadata_from_result(self, result): + return { + 'relevance': result['score'] # TODO: normalize + }