Include options parsing functions

This commit is contained in:
Mario de Frutos
2018-11-29 14:15:20 +01:00
parent 850497ef79
commit 0ec1e051be
6 changed files with 50 additions and 13 deletions

View File

@@ -11,7 +11,7 @@ from cartodb_services.tools.coordinates import (validate_coordinates,
marshall_coordinates)
from cartodb_services.tools.exceptions import ServiceException
from cartodb_services.tools.qps import qps_retry
from types import (DEFAULT_PROFILE, DEFAULT_ROUTE_TYPE, VALID_PROFILES, DEFAULT_DEPARTAT)
from types import (DEFAULT_PROFILE, DEFAULT_ROUTE_TYPE, VALID_PROFILES, VALID_ROUTE_TYPE, DEFAULT_DEPARTAT)
BASEURI = ('https://api.tomtom.com/routing/1/calculateRoute/'
'{coordinates}'
@@ -62,6 +62,14 @@ class TomTomRouting(Traceable):
valid_profiles=', '.join(
[x for x in VALID_PROFILES])))
def _validate_route_type(self, route_type):
if route_type not in VALID_ROUTE_TYPE:
raise ValueError('{route_type} is not a valid route type. '
'Valid route types are: {valid_route_types}'.format(
route_type=route_type,
valid_route_types=', '.join(
[x for x in VALID_ROUTE_TYPE])))
def _marshall_coordinates(self, coordinates):
return ':'.join(['{lat},{lon}'.format(lat=coordinate.latitude,
lon=coordinate.longitude)
@@ -95,6 +103,7 @@ class TomTomRouting(Traceable):
def directions(self, waypoints, profile=DEFAULT_PROFILE,
date_time=DEFAULT_DEPARTAT, route_type=DEFAULT_ROUTE_TYPE):
self._validate_profile(profile)
self._validate_route_type(route_type)
validate_coordinates(waypoints, NUM_WAYPOINTS_MIN, NUM_WAYPOINTS_MAX)
coordinates = self._marshall_coordinates(waypoints)

View File

@@ -6,12 +6,16 @@ PROFILE_DRIVING = 'car'
PROFILE_CYCLING = 'bicycle'
PROFILE_WALKING = 'pedestrian'
DEFAULT_PROFILE = PROFILE_DRIVING
ROUTE_TYPE_FAST = 'fastest'
ROUTE_TYPE_SHORT = 'shortest'
DEFAULT_DEPARTAT = 'now'
VALID_PROFILES = [PROFILE_DRIVING,
PROFILE_CYCLING,
PROFILE_WALKING]
VALID_ROUTE_TYPE = [ROUTE_TYPE_SHORT,
ROUTE_TYPE_FAST]
MAX_SPEEDS = {
PROFILE_WALKING: 3.3333333, # In m/s, assuming 12km/h walking speed
@@ -20,13 +24,13 @@ MAX_SPEEDS = {
}
TRANSPORT_MODE_TO_TOMTOM = {
'car': 'car',
'walk': 'pedestrian',
'bicycle': 'bicycle',
'car': PROFILE_DRIVING,
'walk': PROFILE_WALKING,
'bicycle': PROFILE_CYCLING,
}
DEFAULT_ROUTE_TYPE = 'shortest'
DEFAULT_ROUTE_TYPE = ROUTE_TYPE_SHORT
MODE_TYPE_TO_TOMTOM = {
'shortest': 'shortest',
'fastest': 'fastest'
'shortest': ROUTE_TYPE_SHORT,
'fastest': ROUTE_TYPE_SHORT
}

View File

@@ -1,3 +1,9 @@
from itertools import chain
def normalize(str_input):
return str_input.replace('"', '"') \
.replace(';', ',')
def options_to_dict(options):
options_list = list(chain(*[option.split('=') for option in options]))
return dict(zip(options_list[::2],options_list[1::2]))