Precision metadata for Google
This commit is contained in:
@@ -35,3 +35,6 @@ def _reset():
|
|||||||
GD = None
|
GD = None
|
||||||
|
|
||||||
from geocoder import run_street_point_geocoder, StreetPointBulkGeocoder
|
from geocoder import run_street_point_geocoder, StreetPointBulkGeocoder
|
||||||
|
|
||||||
|
PRECISION_PRECISE = 'precise'
|
||||||
|
PRECISION_INTERPOLATED = 'interpolated'
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
from urlparse import parse_qs
|
from urlparse import parse_qs
|
||||||
|
|
||||||
from exceptions import MalformedResult
|
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
|
||||||
from cartodb_services.google.exceptions import InvalidGoogleCredentials
|
from cartodb_services.google.exceptions import InvalidGoogleCredentials
|
||||||
from client_factory import GoogleMapsClientFactory
|
from client_factory import GoogleMapsClientFactory
|
||||||
@@ -16,6 +17,12 @@ RELEVANCE_BY_LOCATION_TYPE = {
|
|||||||
'RANGE_INTERPOLATED': 0.8,
|
'RANGE_INTERPOLATED': 0.8,
|
||||||
'APPROXIMATE': 0.7
|
'APPROXIMATE': 0.7
|
||||||
}
|
}
|
||||||
|
PRECISION_BY_LOCATION_TYPE = {
|
||||||
|
'ROOFTOP': PRECISION_PRECISE,
|
||||||
|
'GEOMETRIC_CENTER': PRECISION_PRECISE,
|
||||||
|
'RANGE_INTERPOLATED': PRECISION_INTERPOLATED,
|
||||||
|
'APPROXIMATE': PRECISION_INTERPOLATED
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class GoogleMapsGeocoder():
|
class GoogleMapsGeocoder():
|
||||||
@@ -64,7 +71,8 @@ class GoogleMapsGeocoder():
|
|||||||
partial_match = result.get('partial_match', False)
|
partial_match = result.get('partial_match', False)
|
||||||
partial_factor = PARTIAL_FACTOR if partial_match else 1
|
partial_factor = PARTIAL_FACTOR if partial_match else 1
|
||||||
return {
|
return {
|
||||||
'relevance': base_relevance * partial_factor
|
'relevance': base_relevance * partial_factor,
|
||||||
|
'precision': PRECISION_BY_LOCATION_TYPE[location_type]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -69,30 +69,38 @@ class TestStreetFunctionsSetUp(TestCase):
|
|||||||
'mapbox': MAPBOX_POINTS
|
'mapbox': MAPBOX_POINTS
|
||||||
}
|
}
|
||||||
|
|
||||||
HERE_RELEVANCES = {
|
GOOGLE_METADATAS = {
|
||||||
'Plaza España, Barcelona': 1
|
'Plaza España, Barcelona': {
|
||||||
|
'relevance': 0.9, 'precision': 'precise'
|
||||||
|
},
|
||||||
|
'Santiago Rusiñol 123, Valladolid': {
|
||||||
|
'relevance': 0.8, 'precision': 'interpolated'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MAPBOX_RELEVANCES = HERE_RELEVANCES.copy()
|
HERE_METADATAS = {
|
||||||
MAPBOX_RELEVANCES.update({
|
'Plaza España, Barcelona': {
|
||||||
'Plaza España, Barcelona': 0.75
|
'relevance': 1
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TOMTOM_RELEVANCES = MAPBOX_RELEVANCES.copy()
|
TOMTOM_METADATAS = {
|
||||||
TOMTOM_RELEVANCES.update({
|
'Plaza España, Barcelona': {
|
||||||
'Plaza España, Barcelona': 0.85
|
'relevance': 0.85
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|
||||||
GOOGLE_RELEVANCES = HERE_RELEVANCES.copy()
|
MAPBOX_METADATAS = {
|
||||||
GOOGLE_RELEVANCES.update({
|
'Plaza España, Barcelona': {
|
||||||
'Plaza España, Barcelona': 0.9
|
'relevance': 0.75
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RELEVANCES = {
|
METADATAS = {
|
||||||
'google': GOOGLE_RELEVANCES,
|
'google': GOOGLE_METADATAS,
|
||||||
'here': HERE_RELEVANCES,
|
'here': HERE_METADATAS,
|
||||||
'tomtom': TOMTOM_RELEVANCES,
|
'tomtom': TOMTOM_METADATAS,
|
||||||
'mapbox': MAPBOX_RELEVANCES
|
'mapbox': MAPBOX_METADATAS
|
||||||
}
|
}
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -112,7 +120,7 @@ class TestStreetFunctionsSetUp(TestCase):
|
|||||||
provider = response['rows'][0]['provider']
|
provider = response['rows'][0]['provider']
|
||||||
self.fixture_points = self.FIXTURE_POINTS[provider]
|
self.fixture_points = self.FIXTURE_POINTS[provider]
|
||||||
|
|
||||||
self.relevances = self.RELEVANCES[provider]
|
self.metadata = self.METADATAS[provider]
|
||||||
|
|
||||||
|
|
||||||
def _run_authenticated(self, query):
|
def _run_authenticated(self, query):
|
||||||
@@ -353,19 +361,25 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp):
|
|||||||
assert_equal(len(response['rows']), count)
|
assert_equal(len(response['rows']), count)
|
||||||
assert_not_equal(response['rows'][0]['st_x'], None)
|
assert_not_equal(response['rows'][0]['st_x'], None)
|
||||||
|
|
||||||
def test_relevance(self):
|
def test_metadata(self):
|
||||||
query = "select metadata " \
|
query = "select metadata " \
|
||||||
"FROM cdb_dataservices_client.cdb_bulk_geocode_street_point(" \
|
"FROM cdb_dataservices_client.cdb_bulk_geocode_street_point(" \
|
||||||
"'select 1 as cartodb_id, ''Spain'' as country, " \
|
"'select 1 as cartodb_id, ''Spain'' as country, " \
|
||||||
"''Barcelona'' as city, " \
|
"''Barcelona'' as city, " \
|
||||||
"''Plaza España'' as street' " \
|
"''Plaza España'' as street " \
|
||||||
|
"UNION " \
|
||||||
|
"select 2 as cartodb_id, ''Spain'' as country, " \
|
||||||
|
"''Valladolid'' as city, " \
|
||||||
|
"''Calle Santiago Rusiñol 123'' as street' " \
|
||||||
", 'street', 'city', NULL, 'country')"
|
", 'street', 'city', NULL, 'country')"
|
||||||
response = self._run_authenticated(query)
|
response = self._run_authenticated(query)
|
||||||
|
|
||||||
relevance = response['rows'][0]['metadata']['relevance']
|
expected = [
|
||||||
expected_relevance = self.relevances['Plaza España, Barcelona']
|
self.metadata['Plaza España, Barcelona'],
|
||||||
assert_true(isclose(relevance, expected_relevance, 0.05),
|
self.metadata['Santiago Rusiñol 123, Valladolid']
|
||||||
'{} not close to {}'.format(relevance, expected_relevance))
|
]
|
||||||
|
for r, e in zip(response['rows'], expected):
|
||||||
|
self.assert_metadata(r['metadata'], e)
|
||||||
|
|
||||||
def _run_authenticated(self, query):
|
def _run_authenticated(self, query):
|
||||||
authenticated_query = "{}&api_key={}".format(query,
|
authenticated_query = "{}&api_key={}".format(query,
|
||||||
@@ -384,3 +398,12 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp):
|
|||||||
assert_equal(len(points_a_by_cartodb_id), len(points_b_by_cartodb_id))
|
assert_equal(len(points_a_by_cartodb_id), len(points_b_by_cartodb_id))
|
||||||
for cartodb_id, point in points_a_by_cartodb_id.iteritems():
|
for cartodb_id, point in points_a_by_cartodb_id.iteritems():
|
||||||
assert_close_enough(point, points_b_by_cartodb_id[cartodb_id])
|
assert_close_enough(point, points_b_by_cartodb_id[cartodb_id])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def assert_metadata(metadata, expected):
|
||||||
|
relevance = metadata['relevance']
|
||||||
|
expected_relevance = expected['relevance']
|
||||||
|
assert_true(isclose(relevance, expected_relevance, 0.05),
|
||||||
|
'{} not close to {}'.format(relevance, expected_relevance))
|
||||||
|
|
||||||
|
assert_equal(metadata['precision'], expected['precision'])
|
||||||
|
|||||||
Reference in New Issue
Block a user