Skip to content

Commit

Permalink
Merge pull request #140 from CartoDB/geocoder_agnostic
Browse files Browse the repository at this point in the history
Geocoder functions should be provider agnostic
  • Loading branch information
Mario de Frutos committed Apr 14, 2016
2 parents d5c8962 + b4c1991 commit ccd5daa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
5 changes: 4 additions & 1 deletion server/extension/sql/20_geocode_street.sql
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ RETURNS Geometry AS $$

try:
geocoder = MapzenGeocoder(user_geocoder_config.mapzen_app_key)
coordinates = geocoder.geocode(searchtext=searchtext, country=country)
country_iso3 = None
if country:
country_iso3 = country_to_iso3(country)
coordinates = geocoder.geocode(searchtext=searchtext, country=country_iso3)
if coordinates:
quota_service.increment_success_service_use()
plan = plpy.prepare("SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326); ", ["double precision", "double precision"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def __init__(self, app_key, base_url=BASE_URL):
self._url = base_url

@qps_retry
def geocode(self, searchtext, country=None):
request_params = self._build_requests_parameters(searchtext, country)
def geocode(self, searchtext, city=None, state_province=None, country=None):
request_params = self._build_requests_parameters(searchtext, city, state_province, country)
response = requests.get(self._url, params=request_params)
if response.status_code == requests.codes.ok:
return self.__parse_response(response.text)
Expand All @@ -27,15 +27,26 @@ def geocode(self, searchtext, country=None):
else:
response.raise_for_status()

def _build_requests_parameters(self, searchtext, country=None):
def _build_requests_parameters(self, searchtext, city=None,
state_province=None, country=None):
request_params = {}
request_params['text'] = searchtext
search_string = self._build_search_text(searchtext, city, state_province)
request_params['text'] = search_string
request_params['layers'] = 'address'
request_params['api_key'] = self._app_key
if country:
request_params['boundary.country'] = country
return request_params

def _build_search_text(self, searchtext, city, state_province):
search_string = searchtext
if city:
search_string = "{0}, {1}".format(search_string, city)
if state_province:
search_string = "{0}, {1}".format(search_string, state_province)

return search_string

def __parse_response(self, response):
try:
parsed_json_response = json.loads(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ def polyline_to_linestring(polyline):
geometry = None

return geometry

def country_to_iso3(country):
""" Convert country to its iso3 code """
try:
country_plan = plpy.prepare("SELECT adm0_a3 as iso3 FROM admin0_synonyms WHERE lower(regexp_replace($1, " \
"'[^a-zA-Z\u00C0-\u00ff]+', '', 'g'))::text = name_; ", ['text'])
country_result = plpy.execute(country_plan, [country], 1)
if country_result:
return country_result[0]['iso3']
else:
return None
except BaseException as e:
plpy.warning("Can't get the iso3 code from {0}: {1}".format(country, e))
return None
2 changes: 1 addition & 1 deletion server/lib/python/cartodb_services/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
setup(
name='cartodb_services',

version='0.4.3',
version='0.4.4',

description='CartoDB Services API Python Library',

Expand Down

0 comments on commit ccd5daa

Please sign in to comment.