Compare commits
8 Commits
python-0.9
...
python-0.9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0533018326 | ||
|
|
e380d51bec | ||
|
|
6058960ec5 | ||
|
|
336d8be977 | ||
|
|
75557837b0 | ||
|
|
e7c35457e1 | ||
|
|
80963e2589 | ||
|
|
19d6cacdb3 |
@@ -14,6 +14,8 @@ class HereMapsGeocoder:
|
|||||||
STAGING_GEOCODE_JSON_URL = 'https://geocoder.cit.api.here.com/6.2/geocode.json'
|
STAGING_GEOCODE_JSON_URL = 'https://geocoder.cit.api.here.com/6.2/geocode.json'
|
||||||
DEFAULT_MAXRESULTS = 1
|
DEFAULT_MAXRESULTS = 1
|
||||||
DEFAULT_GEN = 9
|
DEFAULT_GEN = 9
|
||||||
|
READ_TIMEOUT = 60
|
||||||
|
CONNECT_TIMEOUT = 10
|
||||||
|
|
||||||
ADDRESS_PARAMS = [
|
ADDRESS_PARAMS = [
|
||||||
'city',
|
'city',
|
||||||
@@ -85,7 +87,8 @@ class HereMapsGeocoder:
|
|||||||
'gen': self.gen
|
'gen': self.gen
|
||||||
}
|
}
|
||||||
request_params.update(params)
|
request_params.update(params)
|
||||||
response = requests.get(self.host, params=request_params)
|
response = requests.get(self.host, params=request_params,
|
||||||
|
timeout=(self.CONNECT_TIMEOUT, self.READ_TIMEOUT))
|
||||||
if response.status_code == requests.codes.ok:
|
if response.status_code == requests.codes.ok:
|
||||||
return json.loads(response.text)
|
return json.loads(response.text)
|
||||||
elif response.status_code == requests.codes.bad_request:
|
elif response.status_code == requests.codes.bad_request:
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ class HereMapsRoutingIsoline:
|
|||||||
PRODUCTION_ROUTING_BASE_URL = 'https://isoline.route.api.here.com'
|
PRODUCTION_ROUTING_BASE_URL = 'https://isoline.route.api.here.com'
|
||||||
STAGING_ROUTING_BASE_URL = 'https://isoline.route.cit.api.here.com'
|
STAGING_ROUTING_BASE_URL = 'https://isoline.route.cit.api.here.com'
|
||||||
ISOLINE_PATH = '/routing/7.2/calculateisoline.json'
|
ISOLINE_PATH = '/routing/7.2/calculateisoline.json'
|
||||||
|
READ_TIMEOUT = 60
|
||||||
|
CONNECT_TIMEOUT = 10
|
||||||
|
|
||||||
ACCEPTED_MODES = {
|
ACCEPTED_MODES = {
|
||||||
"walk": "pedestrian",
|
"walk": "pedestrian",
|
||||||
@@ -50,7 +52,8 @@ class HereMapsRoutingIsoline:
|
|||||||
data_range,
|
data_range,
|
||||||
range_type,
|
range_type,
|
||||||
parsed_options)
|
parsed_options)
|
||||||
response = requests.get(self._url, params=request_params)
|
response = requests.get(self._url, params=request_params,
|
||||||
|
timeout=(self.CONNECT_TIMEOUT, self.READ_TIMEOUT))
|
||||||
if response.status_code == requests.codes.ok:
|
if response.status_code == requests.codes.ok:
|
||||||
return self.__parse_isolines_response(response.text)
|
return self.__parse_isolines_response(response.text)
|
||||||
elif response.status_code == requests.codes.bad_request:
|
elif response.status_code == requests.codes.bad_request:
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ class MapzenGeocoder:
|
|||||||
'A Mapzen Geocoder wrapper for python'
|
'A Mapzen Geocoder wrapper for python'
|
||||||
|
|
||||||
BASE_URL = 'https://search.mapzen.com/v1/search'
|
BASE_URL = 'https://search.mapzen.com/v1/search'
|
||||||
|
READ_TIMEOUT = 60
|
||||||
|
CONNECT_TIMEOUT = 10
|
||||||
|
|
||||||
def __init__(self, app_key, logger, base_url=BASE_URL):
|
def __init__(self, app_key, logger, base_url=BASE_URL):
|
||||||
self._app_key = app_key
|
self._app_key = app_key
|
||||||
@@ -24,7 +26,8 @@ class MapzenGeocoder:
|
|||||||
state_province,
|
state_province,
|
||||||
country, search_type)
|
country, search_type)
|
||||||
try:
|
try:
|
||||||
response = requests.get(self._url, params=request_params)
|
response = requests.get(self._url, params=request_params,
|
||||||
|
timeout=(self.CONNECT_TIMEOUT, self.READ_TIMEOUT))
|
||||||
if response.status_code == requests.codes.ok:
|
if response.status_code == requests.codes.ok:
|
||||||
return self.__parse_response(response.text)
|
return self.__parse_response(response.text)
|
||||||
elif response.status_code == requests.codes.bad_request:
|
elif response.status_code == requests.codes.bad_request:
|
||||||
@@ -41,6 +44,12 @@ class MapzenGeocoder:
|
|||||||
"state_province": state_province})
|
"state_province": state_province})
|
||||||
raise ServiceException('Error trying to geocode {0} using mapzen'.format(searchtext),
|
raise ServiceException('Error trying to geocode {0} using mapzen'.format(searchtext),
|
||||||
response)
|
response)
|
||||||
|
except requests.Timeout as te:
|
||||||
|
# In case of timeout we want to stop the job because the server
|
||||||
|
# could be down
|
||||||
|
self._logger.error('Timeout connecting to Mapzen geocoding server')
|
||||||
|
raise ServiceException('Error trying to geocode {0} using mapzen'.format(searchtext),
|
||||||
|
None)
|
||||||
except requests.ConnectionError as e:
|
except requests.ConnectionError as e:
|
||||||
# Don't raise the exception to continue with the geocoding job
|
# Don't raise the exception to continue with the geocoding job
|
||||||
self._logger.error('Error connecting to Mapzen geocoding server',
|
self._logger.error('Error connecting to Mapzen geocoding server',
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ class MapzenIsolines:
|
|||||||
def calculate_isoline(self, origin, costing_model, isorange, upper_rmax, cost_variable, unit_factor=1.0):
|
def calculate_isoline(self, origin, costing_model, isorange, upper_rmax, cost_variable, unit_factor=1.0):
|
||||||
|
|
||||||
# NOTE: not for production
|
# NOTE: not for production
|
||||||
self._logger.debug('Calculate isoline', data={"origin": origin, "costing_model": costing_model, "isorange": isorange})
|
# self._logger.debug('Calculate isoline', data={"origin": origin, "costing_model": costing_model, "isorange": isorange})
|
||||||
|
|
||||||
# Formally, a solution is an array of {angle, radius, lat, lon, cost} with cardinality NUMBER_OF_ANGLES
|
# Formally, a solution is an array of {angle, radius, lat, lon, cost} with cardinality NUMBER_OF_ANGLES
|
||||||
# we're looking for a solution in which abs(cost - isorange) / isorange <= TOLERANCE
|
# we're looking for a solution in which abs(cost - isorange) / isorange <= TOLERANCE
|
||||||
@@ -105,14 +105,16 @@ class MapzenIsolines:
|
|||||||
|
|
||||||
response = self._matrix_client.one_to_many([origin] + location_estimates, costing_model)
|
response = self._matrix_client.one_to_many([origin] + location_estimates, costing_model)
|
||||||
costs = [None] * self.NUMBER_OF_ANGLES
|
costs = [None] * self.NUMBER_OF_ANGLES
|
||||||
|
if not response:
|
||||||
|
# In case the matrix client doesn't return any data
|
||||||
|
break
|
||||||
|
|
||||||
for idx, c in enumerate(response['one_to_many'][0][1:]):
|
for idx, c in enumerate(response['one_to_many'][0][1:]):
|
||||||
if c[cost_variable]:
|
if c[cost_variable]:
|
||||||
costs[idx] = c[cost_variable]*unit_factor
|
costs[idx] = c[cost_variable]*unit_factor
|
||||||
else:
|
else:
|
||||||
costs[idx] = isorange
|
costs[idx] = isorange
|
||||||
|
|
||||||
# self._logger.debug('i = %d, costs = %s' % (i, costs))
|
|
||||||
|
|
||||||
errors = [(cost - isorange) / float(isorange) for cost in costs]
|
errors = [(cost - isorange) / float(isorange) for cost in costs]
|
||||||
max_abs_error = max([abs(e) for e in errors])
|
max_abs_error = max([abs(e) for e in errors])
|
||||||
if max_abs_error <= self.TOLERANCE:
|
if max_abs_error <= self.TOLERANCE:
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ class MatrixClient:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
ONE_TO_MANY_URL = 'https://matrix.mapzen.com/one_to_many'
|
ONE_TO_MANY_URL = 'https://matrix.mapzen.com/one_to_many'
|
||||||
|
READ_TIMEOUT = 60
|
||||||
|
CONNECT_TIMEOUT = 10
|
||||||
|
|
||||||
def __init__(self, matrix_key, logger):
|
def __init__(self, matrix_key, logger):
|
||||||
self._matrix_key = matrix_key
|
self._matrix_key = matrix_key
|
||||||
@@ -41,9 +43,10 @@ class MatrixClient:
|
|||||||
'costing': costing,
|
'costing': costing,
|
||||||
'api_key': self._matrix_key
|
'api_key': self._matrix_key
|
||||||
}
|
}
|
||||||
response = requests.get(self.ONE_TO_MANY_URL, params=request_params)
|
response = requests.get(self.ONE_TO_MANY_URL, params=request_params,
|
||||||
|
timeout=(self.CONNECT_TIMEOUT, self.READ_TIMEOUT))
|
||||||
|
|
||||||
if not requests.codes.ok:
|
if response.status_code != requests.codes.ok:
|
||||||
self._logger.error('Error trying to get matrix distance from mapzen',
|
self._logger.error('Error trying to get matrix distance from mapzen',
|
||||||
data={"response_status": response.status_code,
|
data={"response_status": response.status_code,
|
||||||
"response_reason": response.reason,
|
"response_reason": response.reason,
|
||||||
@@ -52,6 +55,22 @@ class MatrixClient:
|
|||||||
"response_headers": response.headers,
|
"response_headers": response.headers,
|
||||||
"locations": locations,
|
"locations": locations,
|
||||||
"costing": costing})
|
"costing": costing})
|
||||||
raise ServiceException("Error trying to get matrix distance from mapzen", response)
|
# In case 4xx error we return empty because the error comes from
|
||||||
|
# the provided info by the user and we don't want to top the
|
||||||
|
# isolines generation
|
||||||
|
if response.status_code == requests.codes.bad_request:
|
||||||
|
return {}
|
||||||
|
elif response.status_code == 504:
|
||||||
|
# Due to some unsolved problems in the Mapzen Matrix API we're
|
||||||
|
# getting randomly 504, probably timeouts. To avoid raise an
|
||||||
|
# exception in all the jobs, for now we're going to return
|
||||||
|
# empty in that case
|
||||||
|
return {}
|
||||||
|
else:
|
||||||
|
raise ServiceException("Error trying to get matrix distance from mapzen", response)
|
||||||
|
|
||||||
return response.json()
|
# response could return with empty json
|
||||||
|
try:
|
||||||
|
return response.json()
|
||||||
|
except:
|
||||||
|
return {}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ class MapzenRouting:
|
|||||||
'A Mapzen Routing wrapper for python'
|
'A Mapzen Routing wrapper for python'
|
||||||
|
|
||||||
PRODUCTION_ROUTING_BASE_URL = 'https://valhalla.mapzen.com/route'
|
PRODUCTION_ROUTING_BASE_URL = 'https://valhalla.mapzen.com/route'
|
||||||
|
READ_TIMEOUT = 60
|
||||||
|
CONNECT_TIMEOUT = 10
|
||||||
|
|
||||||
ACCEPTED_MODES = {
|
ACCEPTED_MODES = {
|
||||||
"walk": "pedestrian",
|
"walk": "pedestrian",
|
||||||
@@ -43,7 +45,8 @@ class MapzenRouting:
|
|||||||
mode_param,
|
mode_param,
|
||||||
units)
|
units)
|
||||||
request_params = self.__parse_request_parameters(json_request_params)
|
request_params = self.__parse_request_parameters(json_request_params)
|
||||||
response = requests.get(self._url, params=request_params)
|
response = requests.get(self._url, params=request_params,
|
||||||
|
timeout=(self.CONNECT_TIMEOUT, self.READ_TIMEOUT))
|
||||||
if response.status_code == requests.codes.ok:
|
if response.status_code == requests.codes.ok:
|
||||||
return self.__parse_routing_response(response.text)
|
return self.__parse_routing_response(response.text)
|
||||||
elif response.status_code == requests.codes.bad_request:
|
elif response.status_code == requests.codes.bad_request:
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from setuptools import setup, find_packages
|
|||||||
setup(
|
setup(
|
||||||
name='cartodb_services',
|
name='cartodb_services',
|
||||||
|
|
||||||
version='0.9.2',
|
version='0.9.4',
|
||||||
|
|
||||||
description='CartoDB Services API Python Library',
|
description='CartoDB Services API Python Library',
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user