Refactor country conversion for TomTom geocoders

This commit is contained in:
cgonzalez 2021-01-27 10:50:33 +00:00
parent f995f2d6ec
commit 4278f66372
3 changed files with 3 additions and 13 deletions

View File

@ -275,10 +275,8 @@ $$ LANGUAGE @@plpythonu@@ STABLE PARALLEL RESTRICTED;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_tomtom_geocode_street_point(username TEXT, orgname TEXT, searchtext TEXT, city TEXT DEFAULT NULL, state_province TEXT DEFAULT NULL, country TEXT DEFAULT NULL) CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_tomtom_geocode_street_point(username TEXT, orgname TEXT, searchtext TEXT, city TEXT DEFAULT NULL, state_province TEXT DEFAULT NULL, country TEXT DEFAULT NULL)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from iso3166 import countries
from cartodb_services.tools import ServiceManager, QuotaExceededException from cartodb_services.tools import ServiceManager, QuotaExceededException
from cartodb_services.tomtom import TomTomGeocoder from cartodb_services.tomtom import TomTomGeocoder
from cartodb_services.tools.country import country_to_iso3
from cartodb_services.refactor.service.tomtom_geocoder_config import TomTomGeocoderConfigBuilder from cartodb_services.refactor.service.tomtom_geocoder_config import TomTomGeocoderConfigBuilder
import cartodb_services import cartodb_services
@ -290,15 +288,9 @@ RETURNS Geometry AS $$
service_manager.assert_within_limits() service_manager.assert_within_limits()
geocoder = TomTomGeocoder(service_manager.config.tomtom_api_key, service_manager.logger, service_manager.config.service_params) geocoder = TomTomGeocoder(service_manager.config.tomtom_api_key, service_manager.logger, service_manager.config.service_params)
country_iso3166 = None
if country:
country_iso3 = country_to_iso3(country)
if country_iso3:
country_iso3166 = countries.get(country_iso3).alpha2.lower()
coordinates = geocoder.geocode(searchtext=searchtext, city=city, coordinates = geocoder.geocode(searchtext=searchtext, city=city,
state_province=state_province, state_province=state_province,
country=country_iso3166) country=country)
if coordinates: if coordinates:
service_manager.quota_service.increment_success_service_use() service_manager.quota_service.increment_success_service_use()
plan = plpy.prepare("SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326); ", ["double precision", "double precision"]) plan = plpy.prepare("SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326); ", ["double precision", "double precision"])

View File

@ -5,7 +5,6 @@ from cartodb_services.geocoder import geocoder_error_response
from cartodb_services.tomtom import TomTomGeocoder from cartodb_services.tomtom import TomTomGeocoder
from cartodb_services.tools.qps import qps_retry from cartodb_services.tools.qps import qps_retry
from cartodb_services.tools.exceptions import ServiceException from cartodb_services.tools.exceptions import ServiceException
from cartodb_services.tools.country import country_to_iso3
class TomTomBulkGeocoder(TomTomGeocoder, StreetPointBulkGeocoder): class TomTomBulkGeocoder(TomTomGeocoder, StreetPointBulkGeocoder):
@ -37,8 +36,6 @@ class TomTomBulkGeocoder(TomTomGeocoder, StreetPointBulkGeocoder):
for search in searches: for search in searches:
(search_id, address, city, state, country) = search (search_id, address, city, state, country) = search
country = country_to_iso3(country) or country
address = address.encode('utf-8') if address else None address = address.encode('utf-8') if address else None
city = city.encode('utf-8') if city else None city = city.encode('utf-8') if city else None
state = state.encode('utf-8') if state else None state = state.encode('utf-8') if state else None

View File

@ -10,6 +10,7 @@ from cartodb_services.metrics import Traceable
from cartodb_services.tools.exceptions import ServiceException from cartodb_services.tools.exceptions import ServiceException
from cartodb_services.tools.qps import qps_retry from cartodb_services.tools.qps import qps_retry
from cartodb_services.tools.normalize import normalize from cartodb_services.tools.normalize import normalize
from cartodb_services.tools.country import country_to_iso3
HOST = 'https://api.tomtom.com' HOST = 'https://api.tomtom.com'
API_BASEURI = '/search/2' API_BASEURI = '/search/2'
@ -48,7 +49,7 @@ class TomTomGeocoder(Traceable):
def _request_uri(self, searchtext, country=None, apiKey=None): def _request_uri(self, searchtext, country=None, apiKey=None):
baseuri = REQUEST_BASEURI baseuri = REQUEST_BASEURI
if country: if country:
baseuri += '&countrySet={}'.format(country) baseuri += '&countrySet={}'.format(country_to_iso3(country) or country)
baseuri = baseuri + '&key={apiKey}' if apiKey else baseuri baseuri = baseuri + '&key={apiKey}' if apiKey else baseuri
return URITemplate(baseuri).expand(apiKey=apiKey, return URITemplate(baseuri).expand(apiKey=apiKey,
searchtext=searchtext.encode('utf-8')) searchtext=searchtext.encode('utf-8'))