From bf0348f93ced3c2bede6992fabd465eddf46f3ec Mon Sep 17 00:00:00 2001 From: oesparza Date: Tue, 15 Dec 2020 15:44:46 +0100 Subject: [PATCH] Trip option addition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This pull request is intended to extend leaflet_routing_machine with the implementation of the trip service. Trip service is an alternative to regular routing which returns an optimised route (TSP algorithm) to connect all waypoints. Documentation and parameters of trip service are explained in http://project-osrm.org/docs/v5.22.0/api/#trip-service These are the modifications applied: Changes in osrm-v1.js: Lines 21-35: Add new properties to control if the service calls the trip (optimised) or routing urls tripOptions: properties for the options available in trip service Line 73 : Assign the end-point url depending on optimised. Lines 169-185: Parse and trip service response format Lines 365-379: Build end-point url with the appropriate parameters depending on routing/trip Changes in line.js Line 125: When optimise=true the result doesn´t need to follow the order of the entry coordinates. With this change we force the process to recalculate the indices of the entry waypoints in the response coordinates. --- src/line.js | 2 +- src/osrm-v1.js | 58 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/line.js b/src/line.js index c8043f73..f1cb5b78 100644 --- a/src/line.js +++ b/src/line.js @@ -122,7 +122,7 @@ _getWaypointIndices: function() { if (!this._wpIndices) { - this._wpIndices = this._route.waypointIndices || this._findWaypointIndices(); + this._wpIndices = this._findWaypointIndices(); } return this._wpIndices; diff --git a/src/osrm-v1.js b/src/osrm-v1.js index ffbebac1..52076819 100644 --- a/src/osrm-v1.js +++ b/src/osrm-v1.js @@ -18,6 +18,7 @@ */ module.exports = L.Class.extend({ options: { + optimised: false, serviceUrl: 'https://router.project-osrm.org/route/v1', profile: 'driving', timeout: 30 * 1000, @@ -25,6 +26,13 @@ alternatives: true, steps: true }, + tripOptions: { + roundtrip:true, + source:"first", + geometries: "polyline", + destination:"last", + steps: true + }, polylinePrecision: 5, useHints: true, suppressDemoServerWarning: false, @@ -62,7 +70,7 @@ i, xhr; - options = L.extend({}, this.options.routingOptions, options); + options = L.extend({}, this.options.optimised ? this.options.routingOptions : this.options.tripOptions, options) ; url = this.buildRouteUrl(waypoints, options); if (this.options.requestParameters) { url += L.Util.getParamString(this.options.requestParameters, url); @@ -158,12 +166,23 @@ actualWaypoints = this._toWaypoints(inputWaypoints, response.waypoints); - for (i = 0; i < response.routes.length; i++) { - route = this._convertRoute(response.routes[i]); - route.inputWaypoints = inputWaypoints; - route.waypoints = actualWaypoints; - route.properties = {isSimplified: !options || !options.geometryOnly || options.simplifyGeometry}; - alts.push(route); + if (this.options.optimised) { + for (i = 0; i < response.trips.length; i++) { + route = this._convertRoute(response.trips[i]); + route.inputWaypoints = inputWaypoints; + route.waypoints = actualWaypoints; + route.properties = { isSimplified: !options || !options.geometryOnly || options.simplifyGeometry }; + alts.push(route); + } + } + else { + for (i = 0; i < response.routes.length; i++) { + route = this._convertRoute(response.routes[i]); + route.inputWaypoints = inputWaypoints; + route.waypoints = actualWaypoints; + route.properties = {isSimplified: !options || !options.geometryOnly || options.simplifyGeometry}; + alts.push(route); + } } this._saveHintData(response.waypoints, inputWaypoints); @@ -334,9 +353,7 @@ var locs = [], hints = [], wp, - latLng, - computeInstructions, - computeAlternative = true; + latLng; for (var i = 0; i < waypoints.length; i++) { wp = waypoints[i]; @@ -345,14 +362,21 @@ hints.push(this._hints.locations[this._locationKey(latLng)] || ''); } - computeInstructions = - true; - - return this.options.serviceUrl + '/' + this.options.profile + '/' + - locs.join(';') + '?' + + if (this.options.optimised) + return this.options.serviceUrl + '/' + this.options.profile + '/' + + locs.join(';') + '?' + + (options.geometryOnly ? (options.simplifyGeometry ? '' : 'overview=full') : 'overview=false') + + '&geometries='+ this.options.tripOptions.geometries + + '&steps=' + this.options.tripOptions.steps.toString() + + '&source=' + this.options.tripOptions.source + + '&destination=' + this.options.tripOptions.destination + + '&roundtrip=' + this.options.tripOptions.roundtrip; + else + return this.options.serviceUrl + '/' + this.options.profile + '/' + + locs.join(';') + '?' + (options.geometryOnly ? (options.simplifyGeometry ? '' : 'overview=full') : 'overview=false') + - '&alternatives=' + computeAlternative.toString() + - '&steps=' + computeInstructions.toString() + + '&alternatives=' + this.options.routingOptions.alternatives.toString() + + '&steps=' + this.options.routingOptions.steps.toString() + (this.options.useHints ? '&hints=' + hints.join(';') : '') + (options.allowUTurns ? '&continue_straight=' + !options.allowUTurns : ''); },