From 64a0616c978727b793e3a2aeed90957ed43a48bc Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Thu, 29 Nov 2018 12:12:02 +0100 Subject: [PATCH] Added options and units parameters to the routing functions --- server/extension/sql/100_routing_helper.sql | 15 ++++++++---- .../sql/105_route_between_points.sql | 24 +++++++++---------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/server/extension/sql/100_routing_helper.sql b/server/extension/sql/100_routing_helper.sql index b638f16..9625420 100644 --- a/server/extension/sql/100_routing_helper.sql +++ b/server/extension/sql/100_routing_helper.sql @@ -8,7 +8,9 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_mapbox_route_with_waypoi username TEXT, orgname TEXT, waypoints geometry(Point, 4326)[], - mode TEXT) + mode TEXT, + options text[] DEFAULT ARRAY[]::text[], + units text DEFAULT 'kilometers') RETURNS cdb_dataservices_server.simple_route AS $$ from cartodb_services.tools import ServiceManager from cartodb_services.mapbox import MapboxRouting @@ -69,11 +71,13 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_tomtom_route_with_waypoi username TEXT, orgname TEXT, waypoints geometry(Point, 4326)[], - mode TEXT) + mode TEXT, + options text[] DEFAULT ARRAY[]::text[], + units text DEFAULT 'kilometers') RETURNS cdb_dataservices_server.simple_route AS $$ from cartodb_services.tools import ServiceManager from cartodb_services.tomtom import TomTomRouting - from cartodb_services.tomtom.types import TRANSPORT_MODE_TO_TOMTOM + 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.refactor.service.tomtom_routing_config import TomTomRoutingConfigBuilder @@ -104,8 +108,11 @@ RETURNS cdb_dataservices_server.simple_route AS $$ waypoint_coords.append(Coordinate(lon,lat)) 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']) - resp = client.directions(waypoint_coords, profile) + resp = client.directions(waypoint_coords, profile=profile, route_type=route_type) if resp and resp.shape: shape_linestring = polyline_to_linestring(resp.shape) if shape_linestring: diff --git a/server/extension/sql/105_route_between_points.sql b/server/extension/sql/105_route_between_points.sql index 703e0dd..b6d69c7 100644 --- a/server/extension/sql/105_route_between_points.sql +++ b/server/extension/sql/105_route_between_points.sql @@ -24,16 +24,16 @@ RETURNS cdb_dataservices_server.simple_route AS $$ waypoints = [origin, destination] if user_routing_config.mapzen_provider: - mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4) as route;", ["text", "text", "geometry(Point, 4326)[]", "text"]) - result = plpy.execute(mapzen_plan, [username, orgname, waypoints, mode]) + mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4, $5, $6) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"]) + result = plpy.execute(mapzen_plan, [username, orgname, waypoints, mode, options, units]) return [result[0]['shape'],result[0]['length'], result[0]['duration']] elif user_routing_config.mapbox_provider: - mapbox_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapbox_route_with_waypoints($1, $2, $3, $4) as route;", ["text", "text", "geometry(Point, 4326)[]", "text"]) - result = plpy.execute(mapbox_plan, [username, orgname, waypoints, mode]) + mapbox_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapbox_route_with_waypoints($1, $2, $3, $4, $5, $6) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"]) + result = plpy.execute(mapbox_plan, [username, orgname, waypoints, mode, options, units]) return [result[0]['shape'],result[0]['length'], result[0]['duration']] elif user_routing_config.tomtom_provider: - tomtom_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_tomtom_route_with_waypoints($1, $2, $3, $4) as route;", ["text", "text", "geometry(Point, 4326)[]", "text"]) - result = plpy.execute(tomtom_plan, [username, orgname, waypoints, mode]) + tomtom_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_tomtom_route_with_waypoints($1, $2, $3, $4, $5, $6) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"]) + result = plpy.execute(tomtom_plan, [username, orgname, waypoints, mode, options, units]) return [result[0]['shape'],result[0]['length'], result[0]['duration']] else: raise Exception('Requested routing method is not available') @@ -63,16 +63,16 @@ RETURNS cdb_dataservices_server.simple_route AS $$ with metrics('cdb_route_with_waypoints', user_routing_config, logger, params): if user_routing_config.mapzen_provider: - mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4) as route;", ["text", "text", "geometry(Point, 4326)[]", "text"]) - result = plpy.execute(mapzen_plan, [username, orgname, waypoints, mode]) + mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"]) + result = plpy.execute(mapzen_plan, [username, orgname, waypoints, mode, options, units]) return [result[0]['shape'],result[0]['length'], result[0]['duration']] elif user_routing_config.mapbox_provider: - mapbox_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapbox_route_with_waypoints($1, $2, $3, $4) as route;", ["text", "text", "geometry(Point, 4326)[]", "text"]) - result = plpy.execute(mapbox_plan, [username, orgname, waypoints, mode]) + mapbox_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapbox_route_with_waypoints($1, $2, $3, $4) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"]) + result = plpy.execute(mapbox_plan, [username, orgname, waypoints, mode, options, units]) return [result[0]['shape'],result[0]['length'], result[0]['duration']] elif user_routing_config.tomtom_provider: - tomtom_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_tomtom_route_with_waypoints($1, $2, $3, $4) as route;", ["text", "text", "geometry(Point, 4326)[]", "text"]) - result = plpy.execute(tomtom_plan, [username, orgname, waypoints, mode]) + tomtom_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_tomtom_route_with_waypoints($1, $2, $3, $4) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"]) + result = plpy.execute(tomtom_plan, [username, orgname, waypoints, mode, options, units]) return [result[0]['shape'],result[0]['length'], result[0]['duration']] else: raise Exception('Requested routing method is not available')