From a70560e566941445cce469b1821314b33fc8fa8f Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 5 Jul 2016 11:12:15 +0200 Subject: [PATCH] Minimal Mapzen Time-Distance Matrix client --- .../cartodb_services/mapzen/__init__.py | 1 + .../cartodb_services/mapzen/matrix_client.py | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 server/lib/python/cartodb_services/cartodb_services/mapzen/matrix_client.py diff --git a/server/lib/python/cartodb_services/cartodb_services/mapzen/__init__.py b/server/lib/python/cartodb_services/cartodb_services/mapzen/__init__.py index cc651c2..e3dca35 100644 --- a/server/lib/python/cartodb_services/cartodb_services/mapzen/__init__.py +++ b/server/lib/python/cartodb_services/cartodb_services/mapzen/__init__.py @@ -1,3 +1,4 @@ from routing import MapzenRouting, MapzenRoutingResponse from isolines import MapzenIsolines from geocoder import MapzenGeocoder +from matrix_client import MatrixClient diff --git a/server/lib/python/cartodb_services/cartodb_services/mapzen/matrix_client.py b/server/lib/python/cartodb_services/cartodb_services/mapzen/matrix_client.py new file mode 100644 index 0000000..0c2d1a8 --- /dev/null +++ b/server/lib/python/cartodb_services/cartodb_services/mapzen/matrix_client.py @@ -0,0 +1,29 @@ +import requests +import json + +class MatrixClient: + + ONE_TO_MANY_URL = 'https://matrix.mapzen.com/one_to_many' + + def __init__(self, matrix_key): + self._matrix_key = matrix_key + + """Get distances and times to a set of locations. + See https://mapzen.com/documentation/matrix/api-reference/ + + Args: + locations Array of {lat: y, lon: x} + costing Costing model to use + + Returns: + A dict with one_to_many, units and locations + """ + def one_to_many(self, locations, costing): + request_params = { + 'json': json.dumps({'locations': locations}), + 'costing': costing, + 'api_key': self._matrix_key + } + response = requests.get(self.ONE_TO_MANY_URL, params=request_params) + + return response.json()