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

@ -18,6 +18,7 @@ RETURNS cdb_dataservices_server.simple_route AS $$
from cartodb_services.mapbox.types import TRANSPORT_MODE_TO_MAPBOX
from cartodb_services.tools import Coordinate
from cartodb_services.tools.polyline import polyline_to_linestring
from cartodb_services.tools.normalize import options_to_dict
from cartodb_services.refactor.service.mapbox_routing_config import MapboxRoutingConfigBuilder
import cartodb_services
@ -46,6 +47,9 @@ RETURNS cdb_dataservices_server.simple_route AS $$
waypoint_coords.append(Coordinate(lon,lat))
profile = TRANSPORT_MODE_TO_MAPBOX.get(mode)
options_dict = options_to_dict(options)
if 'mode_type' in options_dict:
plpy.warning('Mapbox provider doesnt support route type parameter')
resp = client.directions(waypoint_coords, profile)
if resp and resp.shape:
@ -81,6 +85,7 @@ RETURNS cdb_dataservices_server.simple_route AS $$
from cartodb_services.tomtom.types import TRANSPORT_MODE_TO_TOMTOM, DEFAULT_ROUTE_TYPE, MODE_TYPE_TO_TOMTOM
from cartodb_services.tools import Coordinate
from cartodb_services.tools.polyline import polyline_to_linestring
from cartodb_services.tools.normalize import options_to_dict
from cartodb_services.refactor.service.tomtom_routing_config import TomTomRoutingConfigBuilder
import cartodb_services
@ -110,8 +115,9 @@ RETURNS cdb_dataservices_server.simple_route AS $$
profile = TRANSPORT_MODE_TO_TOMTOM.get(mode)
route_type = DEFAULT_ROUTE_TYPE
if 'mode_type' in options:
route_type = MODE_TYPE_TO_TOMTOM.get(options['mode_type'])
options_dict = options_to_dict(options)
if 'mode_type' in options_dict:
route_type = MODE_TYPE_TO_TOMTOM.get(options_dict['mode_type'])
resp = client.directions(waypoint_coords, profile=profile, route_type=route_type)
if resp and resp.shape:

View File

@ -20,6 +20,7 @@ RETURNS cdb_dataservices_server.simple_route AS $$
from cartodb_services.mapbox.types import TRANSPORT_MODE_TO_MAPBOX
from cartodb_services.tools import Coordinate
from cartodb_services.tools.polyline import polyline_to_linestring
from cartodb_services.tools.normalize import options_to_dict
from cartodb_services.refactor.service.mapbox_routing_config import MapboxRoutingConfigBuilder
import cartodb_services
@ -48,6 +49,9 @@ RETURNS cdb_dataservices_server.simple_route AS $$
waypoint_coords.append(Coordinate(lon,lat))
profile = TRANSPORT_MODE_TO_MAPBOX.get(mode)
options_dict = options_to_dict(options)
if 'mode_type' in options_dict:
plpy.warning('Mapbox provider doesnt support route type parameter')
resp = client.directions(waypoint_coords, profile)
if resp and resp.shape:
@ -83,6 +87,7 @@ RETURNS cdb_dataservices_server.simple_route AS $$
from cartodb_services.tomtom.types import TRANSPORT_MODE_TO_TOMTOM, DEFAULT_ROUTE_TYPE, MODE_TYPE_TO_TOMTOM
from cartodb_services.tools import Coordinate
from cartodb_services.tools.polyline import polyline_to_linestring
from cartodb_services.tools.normalize import options_to_dict
from cartodb_services.refactor.service.tomtom_routing_config import TomTomRoutingConfigBuilder
import cartodb_services
@ -112,8 +117,9 @@ RETURNS cdb_dataservices_server.simple_route AS $$
profile = TRANSPORT_MODE_TO_TOMTOM.get(mode)
route_type = DEFAULT_ROUTE_TYPE
if 'mode_type' in options:
route_type = MODE_TYPE_TO_TOMTOM.get(options['mode_type'])
options_dict = options_to_dict(options)
if 'mode_type' in options_dict:
route_type = MODE_TYPE_TO_TOMTOM.get(options_dict['mode_type'])
resp = client.directions(waypoint_coords, profile=profile, route_type=route_type)
if resp and resp.shape:

View File

@ -17,6 +17,7 @@ RETURNS cdb_dataservices_server.simple_route AS $$
from cartodb_services.mapbox.types import TRANSPORT_MODE_TO_MAPBOX
from cartodb_services.tools import Coordinate
from cartodb_services.tools.polyline import polyline_to_linestring
from cartodb_services.tools.normalize import options_to_dict
from cartodb_services.refactor.service.mapbox_routing_config import MapboxRoutingConfigBuilder
import cartodb_services
@ -45,6 +46,9 @@ RETURNS cdb_dataservices_server.simple_route AS $$
waypoint_coords.append(Coordinate(lon,lat))
profile = TRANSPORT_MODE_TO_MAPBOX.get(mode)
options_dict = options_to_dict(options)
if 'mode_type' in options_dict:
plpy.warning('Mapbox provider doesnt support route type parameter')
resp = client.directions(waypoint_coords, profile)
if resp and resp.shape:
@ -80,6 +84,7 @@ RETURNS cdb_dataservices_server.simple_route AS $$
from cartodb_services.tomtom.types import TRANSPORT_MODE_TO_TOMTOM, DEFAULT_ROUTE_TYPE, MODE_TYPE_TO_TOMTOM
from cartodb_services.tools import Coordinate
from cartodb_services.tools.polyline import polyline_to_linestring
from cartodb_services.tools.normalize import options_to_dict
from cartodb_services.refactor.service.tomtom_routing_config import TomTomRoutingConfigBuilder
import cartodb_services
@ -109,8 +114,9 @@ RETURNS cdb_dataservices_server.simple_route AS $$
profile = TRANSPORT_MODE_TO_TOMTOM.get(mode)
route_type = DEFAULT_ROUTE_TYPE
if 'mode_type' in options:
route_type = MODE_TYPE_TO_TOMTOM.get(options['mode_type'])
options_dict = options_to_dict(options)
if 'mode_type' in options_dict:
route_type = MODE_TYPE_TO_TOMTOM.get(options_dict['mode_type'])
resp = client.directions(waypoint_coords, profile=profile, route_type=route_type)
if resp and resp.shape:

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]))