diff --git a/server/lib/python/cartodb_services/cartodb_services/__init__.py b/server/lib/python/cartodb_services/cartodb_services/__init__.py index dd364b4..ef5caa4 100644 --- a/server/lib/python/cartodb_services/cartodb_services/__init__.py +++ b/server/lib/python/cartodb_services/cartodb_services/__init__.py @@ -35,6 +35,3 @@ def _reset(): GD = None from geocoder import run_street_point_geocoder, StreetPointBulkGeocoder - -PRECISION_PRECISE = 'precise' -PRECISION_INTERPOLATED = 'interpolated' diff --git a/server/lib/python/cartodb_services/cartodb_services/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/geocoder.py index c6043e4..47e6d3a 100644 --- a/server/lib/python/cartodb_services/cartodb_services/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/geocoder.py @@ -6,6 +6,13 @@ from collections import namedtuple import json +METADATA_RELEVANCE = 'relevance' +METADATA_PRECISION = 'precision' +METADATA_MATCH_TYPES = 'match_types' + +PRECISION_PRECISE = 'precise' +PRECISION_INTERPOLATED = 'interpolated' + def compose_address(street, city=None, state=None, country=None): return ', '.join(filter(None, [street, city, state, country])) diff --git a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py index 44b678c..2f82549 100644 --- a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py @@ -4,8 +4,7 @@ from urlparse import parse_qs from exceptions import MalformedResult -from cartodb_services import PRECISION_PRECISE, PRECISION_INTERPOLATED -from cartodb_services.geocoder import compose_address +from cartodb_services.geocoder import compose_address, METADATA_RELEVANCE, METADATA_PRECISION, METADATA_MATCH_TYPES, PRECISION_PRECISE, PRECISION_INTERPOLATED from cartodb_services.google.exceptions import InvalidGoogleCredentials from client_factory import GoogleMapsClientFactory @@ -84,9 +83,9 @@ class GoogleMapsGeocoder(): match_types = [MATCH_TYPE_BY_MATCH_LEVEL.get(match_level, None) for match_level in result['types']] return { - 'relevance': base_relevance * partial_factor, - 'precision': PRECISION_BY_LOCATION_TYPE[location_type], - 'match_types': filter(None, match_types) + METADATA_RELEVANCE: base_relevance * partial_factor, + METADATA_PRECISION: PRECISION_BY_LOCATION_TYPE[location_type], + METADATA_MATCH_TYPES: filter(None, match_types) } diff --git a/server/lib/python/cartodb_services/cartodb_services/here/bulk_geocoder.py b/server/lib/python/cartodb_services/cartodb_services/here/bulk_geocoder.py index 51b9ec9..b501c6e 100644 --- a/server/lib/python/cartodb_services/cartodb_services/here/bulk_geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/here/bulk_geocoder.py @@ -8,6 +8,7 @@ from collections import namedtuple from requests.adapters import HTTPAdapter from cartodb_services import StreetPointBulkGeocoder from cartodb_services.here import HereMapsGeocoder +from cartodb_services.geocoder import METADATA_RELEVANCE, METADATA_PRECISION, METADATA_MATCH_TYPES from cartodb_services.metrics import Traceable from cartodb_services.tools.exceptions import ServiceException @@ -138,9 +139,9 @@ class HereMapsBulkGeocoder(HereMapsGeocoder, StreetPointBulkGeocoder): results.append((row['recId'], [row['displayLongitude'], row['displayLatitude']], { - 'relevance': float(row['relevance']), - 'precision': precision, - 'match_types': [match_type] if match_type else [] + METADATA_RELEVANCE: float(row['relevance']), + METADATA_PRECISION: precision, + METADATA_MATCH_TYPES: [match_type] if match_type else [] })) return results 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 f52d2d7..52014b9 100644 --- a/server/lib/python/cartodb_services/cartodb_services/here/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/here/geocoder.py @@ -6,7 +6,7 @@ import requests from requests.adapters import HTTPAdapter from exceptions import * -from cartodb_services import PRECISION_PRECISE, PRECISION_INTERPOLATED +from cartodb_services.geocoder import METADATA_RELEVANCE, METADATA_PRECISION, METADATA_MATCH_TYPES, PRECISION_PRECISE, PRECISION_INTERPOLATED from cartodb_services.metrics import Traceable class HereMapsGeocoder(Traceable): @@ -147,7 +147,7 @@ class HereMapsGeocoder(Traceable): result.get('MatchType', 'pointAddress')] match_type = self.MATCH_TYPE_BY_MATCH_LEVEL.get(result['MatchLevel'], None) return { - 'relevance': result['Relevance'], - 'precision': precision, - 'match_types': [match_type] if match_type else [] + METADATA_RELEVANCE: result['Relevance'], + METADATA_PRECISION: precision, + METADATA_MATCH_TYPES: [match_type] if match_type else [] } diff --git a/server/lib/python/cartodb_services/cartodb_services/mapbox/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/mapbox/geocoder.py index a54d06a..59c707b 100644 --- a/server/lib/python/cartodb_services/cartodb_services/mapbox/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/mapbox/geocoder.py @@ -5,7 +5,7 @@ Python client for the Mapbox Geocoder service. import json import requests from mapbox import Geocoder -from cartodb_services import PRECISION_PRECISE, PRECISION_INTERPOLATED +from cartodb_services.geocoder import METADATA_RELEVANCE, METADATA_PRECISION, METADATA_MATCH_TYPES, PRECISION_PRECISE, PRECISION_INTERPOLATED from cartodb_services.metrics import Traceable from cartodb_services.tools.exceptions import ServiceException from cartodb_services.tools.qps import qps_retry @@ -93,9 +93,9 @@ class MapboxGeocoder(Traceable): match_types = [MATCH_TYPE_BY_MATCH_LEVEL.get(match_level, None) for match_level in result['place_type']] return { - 'relevance': self._normalize_relevance(float(result['relevance'])), - 'precision': precision, - 'match_types': filter(None, match_types) + METADATA_RELEVANCE: self._normalize_relevance(float(result['relevance'])), + METADATA_PRECISION: precision, + METADATA_MATCH_TYPES: filter(None, match_types) } def _normalize_relevance(self, relevance): 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 09ed69d..d1c5bed 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py @@ -5,7 +5,7 @@ import json import requests from uritemplate import URITemplate from math import tanh -from cartodb_services import PRECISION_PRECISE, PRECISION_INTERPOLATED +from cartodb_services.geocoder import METADATA_RELEVANCE, METADATA_PRECISION, METADATA_MATCH_TYPES, PRECISION_PRECISE, PRECISION_INTERPOLATED from cartodb_services.metrics import Traceable from cartodb_services.tools.exceptions import ServiceException from cartodb_services.tools.qps import qps_retry @@ -145,9 +145,9 @@ class TomTomGeocoder(Traceable): score = self._normalize_score(result['score']) match_type = MATCH_TYPE_BY_MATCH_LEVEL.get(result['type'], None) return { - 'relevance': score, - 'precision': self._precision_from_score(score), - 'match_types': [match_type] if match_type else [] + METADATA_RELEVANCE: score, + METADATA_PRECISION: self._precision_from_score(score), + METADATA_MATCH_TYPES: [match_type] if match_type else [] } def _normalize_score(self, score): diff --git a/test/integration/test_street_functions.py b/test/integration/test_street_functions.py index 9b134e7..8535139 100644 --- a/test/integration/test_street_functions.py +++ b/test/integration/test_street_functions.py @@ -384,6 +384,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): self.metadata['Plaza España, Barcelona'], self.metadata['Santiago Rusiñol 123, Valladolid'] ] + assert_equal(len(response['rows']), len(expected)) for r, e in zip(response['rows'], expected): self.assert_metadata(r['metadata'], e)