Mapzen isochtrones integration

This commit is contained in:
Mario de Frutos
2016-11-29 12:57:23 +01:00
parent 4b714b3845
commit 77f4f3e7ff
10 changed files with 293 additions and 32 deletions
@@ -2,3 +2,4 @@ from routing import MapzenRouting, MapzenRoutingResponse
from isolines import MapzenIsolines
from geocoder import MapzenGeocoder
from matrix_client import MatrixClient
from isochrones import MapzenIsochrones
@@ -49,7 +49,7 @@ class MapzenGeocoder(Traceable):
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')
self._logger.error('Timeout connecting to Mapzen geocoding server', te)
raise ServiceException('Error trying to geocode {0} using mapzen'.format(searchtext),
None)
except requests.ConnectionError as e:
@@ -0,0 +1,128 @@
import requests
import json
import re
from exceptions import WrongParams, MalformedResult, ServiceException
from qps import qps_retry
class MapzenIsochrones:
'A Mapzen Isochrones wrapper for python'
BASE_URL = 'https://matrix.mapzen.com/isochrone'
READ_TIMEOUT = 60
CONNECT_TIMEOUT = 10
ACCEPTED_MODES = {
"walk": "pedestrian",
"car": "car"
}
def __init__(self, app_key, logger, base_url=BASE_URL):
self._app_key = app_key
self._url = base_url
self._logger = logger
@qps_retry
def isochrone(self, locations, costing, ranges):
request_params = self._parse_request_params(locations, costing,
ranges)
try:
response = requests.get(self._url, params=request_params,
timeout=(self.CONNECT_TIMEOUT,
self.READ_TIMEOUT))
if response.status_code is requests.codes.ok:
return self._parse_response(response)
elif response.status_code == requests.codes.bad_request:
return []
else:
self._logger.error('Error trying to get isochrones from mapzen',
data={"response_status": response.status_code,
"response_reason": response.reason,
"response_content": response.text,
"reponse_url": response.url,
"response_headers": response.headers,
"locations": locations,
"costing": costing})
raise ServiceException('Error trying to get isochrones from mapzen',
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 isochrones server', exception=te)
raise ServiceException('Error trying to calculate isochrones using mapzen',
None)
except requests.ConnectionError as e:
# Don't raise the exception to continue with the geocoding job
self._logger.error('Error connecting to Mapzen isochrones server',
exception=e)
return []
def _parse_request_params(self, locations, costing, ranges):
if costing in self.ACCEPTED_MODES:
mode_source = self.ACCEPTED_MODES[costing]
else:
raise WrongParams("{0} is not an accepted mode".format(costing))
contours = []
for r in ranges:
# range is in seconds but mapzen uses minutes
range_minutes = r / 60
contours.append({"time": range_minutes, "color": 'tbd'})
request_params = {
'json': json.dumps({'locations': [locations],
'costing': mode_source,
'contours': contours}),
'api_key': self._app_key
}
return request_params
def _parse_response(self, response):
try:
json_response = response.json()
isochrones = []
for feature in json_response['features']:
# Coordinates could have more than one isochrone. For the
# moment we're getting the first polygon only
coordinates = feature['geometry']['coordinates']
duration = feature['properties']['contour']
mapzen_response = MapzenIsochronesResponse(coordinates,
duration)
isochrones.append(mapzen_response)
return isochrones
except IndexError:
return []
except KeyError:
self._logger.error('Non existing key for mapzen isochrones response',
data={"response_status": response.status_code,
"response_reason": response.reason,
"response_content": response.text,
"reponse_url": response.url,
"response_headers": response.headers})
raise MalformedResult()
except ValueError:
# JSON decode error
self._logger.error('JSON decode error for Mapzen isochrones',
data={"response_status": response.status_code,
"response_reason": response.reason,
"response_content": response.text,
"reponse_url": response.url,
"response_headers": response.headers})
return []
class MapzenIsochronesResponse:
def __init__(self, coordinates, duration):
self._coordinates = coordinates
self._duration = duration
@property
def coordinates(self):
return self._coordinates
@property
def duration(self):
return self._duration
@@ -19,6 +19,24 @@ def polyline_to_linestring(polyline):
return geometry
def coordinates_to_polygon(coordinates):
"""Convert a Mapzen coordinates to a PostGIS polygon"""
result_coordinates = []
for coordinate in coordinates:
result_coordinates.append("%s %s" % (coordinate[0], coordinate[1]))
wkt_coordinates = ','.join(result_coordinates)
try:
sql = "SELECT ST_MakePolygon(ST_GeomFromText('LINESTRING({0})', 4326)) as geom".format(wkt_coordinates)
geometry = plpy.execute(sql, 1)[0]['geom']
except BaseException as e:
plpy.warning("Can't generate POLYGON from coordinates: {0}".format(e))
geometry = None
return geometry
def country_to_iso3(country):
""" Convert country to its iso3 code """
try:
@@ -14,7 +14,6 @@ try:
except ImportError:
pass
class Logger:
LEVELS = {'debug': 1, 'info': 2, 'warning': 3, 'error': 4}
@@ -66,7 +65,7 @@ class Logger:
if self._rollbar_activated():
try:
if exception:
rollbar.report_exc_info(exception, extra_data=data,
rollbar.report_exc_info(sys.exc_info(), extra_data=data,
level=level)
else:
rollbar.report_message(text, level, extra_data=data)
@@ -102,7 +101,7 @@ class Logger:
def _parse_log_extra_data(self, exception, data):
extra_data = {}
if exception:
type_, value_, traceback_ = exception
type_, value_, traceback_ = sys.exc_info()
exception_traceback = traceback.format_tb(traceback_)
extra_data = {"exception_type": type_, "exception_message": value_,
"exception_traceback": exception_traceback,