diff --git a/server/extension/cdb_dataservices_server--0.34.2--0.35.0.sql b/server/extension/cdb_dataservices_server--0.34.2--0.35.0.sql index 29f46c6..5ae8a23 100644 --- a/server/extension/cdb_dataservices_server--0.34.2--0.35.0.sql +++ b/server/extension/cdb_dataservices_server--0.34.2--0.35.0.sql @@ -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: diff --git a/server/extension/cdb_dataservices_server--0.35.0.sql b/server/extension/cdb_dataservices_server--0.35.0.sql index f68498c..4314dbc 100644 --- a/server/extension/cdb_dataservices_server--0.35.0.sql +++ b/server/extension/cdb_dataservices_server--0.35.0.sql @@ -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: diff --git a/server/extension/sql/100_routing_helper.sql b/server/extension/sql/100_routing_helper.sql index 9625420..e5f18ad 100644 --- a/server/extension/sql/100_routing_helper.sql +++ b/server/extension/sql/100_routing_helper.sql @@ -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: diff --git a/server/lib/python/cartodb_services/cartodb_services/tomtom/routing.py b/server/lib/python/cartodb_services/cartodb_services/tomtom/routing.py index bde251c..a863e25 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tomtom/routing.py +++ b/server/lib/python/cartodb_services/cartodb_services/tomtom/routing.py @@ -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) diff --git a/server/lib/python/cartodb_services/cartodb_services/tomtom/types.py b/server/lib/python/cartodb_services/cartodb_services/tomtom/types.py index 67b1f34..71ea26f 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tomtom/types.py +++ b/server/lib/python/cartodb_services/cartodb_services/tomtom/types.py @@ -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 } diff --git a/server/lib/python/cartodb_services/cartodb_services/tools/normalize.py b/server/lib/python/cartodb_services/cartodb_services/tools/normalize.py index d95f293..dac8066 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tools/normalize.py +++ b/server/lib/python/cartodb_services/cartodb_services/tools/normalize.py @@ -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]))