[gnome-maps/wip/mlundblad/otp-use-walking-routes] WIP: openTripPlanner: Use walk route...



commit afb7a1553da5d3e86e714854941795b84bde0604
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Sep 23 22:59:56 2019 +0200

    WIP: openTripPlanner: Use walk route...

 src/transitplugins/openTripPlanner.js | 186 ++++++++++++++++++++++------------
 1 file changed, 124 insertions(+), 62 deletions(-)
---
diff --git a/src/transitplugins/openTripPlanner.js b/src/transitplugins/openTripPlanner.js
index 346bb8f..667a2da 100644
--- a/src/transitplugins/openTripPlanner.js
+++ b/src/transitplugins/openTripPlanner.js
@@ -116,6 +116,7 @@ var OpenTripPlanner = class OpenTripPlanner {
         this._query = Application.routeQuery;
         this._baseUrl = params.baseUrl;
         this._router = params.router || 'default';
+        this._onlyTransitData = params.onlyTransitData || false;
         this._walkingRoutes = [];
         this._extendPrevious = false;
     }
@@ -372,25 +373,13 @@ var OpenTripPlanner = class OpenTripPlanner {
         return date.format('%F');
     }
 
-    // create parameter map for the request, given query and options
-    _createParams(stops) {
-        let params = { fromPlace: stops[0].id,
-                       toPlace: stops.last().id };
-        let intermediatePlaces = [];
-
-        for (let i = 1; i < stops.length - 1; i++) {
-            intermediatePlaces.push(stops[i].id);
-        }
-        if (intermediatePlaces.length > 0)
-            params.intermediatePlaces = intermediatePlaces;
+    _getPlaceParamFromLocation(location) {
+        return location.latitude + ',' + location.longitude;
+    }
 
+    _addCommonParams(params) {
         params.numItineraries = 5;
         params.showIntermediateStops = true;
-        /* set walking speed for transfers to a slightly lower value to
-         * compensate for running OTP with only transit data, giving straight-
-         * line walking paths
-         */
-        params.walkSpeed = 1.0;
 
         let time = this._query.time;
         let date = this._query.date;
@@ -436,47 +425,107 @@ var OpenTripPlanner = class OpenTripPlanner {
         let options = this._query.transitOptions;
         if (options && !options.showAllTransitTypes)
             params.mode = this._getModes(options);
+    }
+
+    _createParamsWithLocations() {
+        let points = this._query.filledPoints;
+        let params = {
+            fromPlace: this._getPlaceParamFromLocation(points[0].place.location),
+            toPlace: this._getPlaceParamFromLocation(points[points.length - 1].place.location) };
+        let intermediatePlaces = [];
+
+        for (let i = 1; i < points.length - 1; i++) {
+            let location = points[i].location;
+            intermediatePlaces.push(this._getPlaceParamFromLocation(location));
+        }
+        if (intermediatePlaces)
+            params.intermediatePlaces = intermediatePlaces;
+
+        this._addCommonParams(params);
 
         return params;
     }
 
-    _fetchRoutes(callback) {
-        this._fetchTransitStops((stops) => {
-            let points = this._query.filledPoints;
+    // create parameter map for the request, given query and options
+    _createParamsWithStops(stops) {
+        let params = { fromPlace: stops[0].id,
+                       toPlace: stops.last().id };
+        let intermediatePlaces = [];
 
-            if (!stops) {
-                callback(null);
-                return;
-            }
+        for (let i = 1; i < stops.length - 1; i++) {
+            intermediatePlaces.push(stops[i].id);
+        }
+        if (intermediatePlaces.length > 0)
+            params.intermediatePlaces = intermediatePlaces;
 
-            /* if there's only a start and end stop (no intermediate stops)
-             * and those stops are identical, reject the routing, since this
-             * means there would be no point in transit, and OTP would give
-             * some bizarre option like boarding transit, go one stop and then
-             * transfer to go back the same route
-             */
-            if (stops.length === 2 && stops[0].id === stops[1].id) {
+        /* set walking speed for transfers to a slightly lower value to
+         * compensate for running OTP with only transit data, giving straight-
+         * line walking paths
+         */
+        params.walkSpeed = 1.0;
+
+        this._addCommonParams(params);
+
+        return params;
+    }
+
+    _fetchPlan(params, callback) {
+        let query = new HTTP.Query(params);
+        let uri = new Soup.URI(this._getRouterUrl() + '/plan?' +
+                               query.toString());
+        let request = new Soup.Message({ method: 'GET', uri: uri });
+
+        Utils.debug('uri: ' + this._getRouterUrl() + '/plan?' + query.toString());
+
+        request.request_headers.append('Accept', 'application/json');
+        this._session.queue_message(request, (obj, message) => {
+            if (message.status_code !== Soup.Status.OK) {
+                Utils.debug('Failed to get route plan from router ' +
+                            this._router + ' ' + message);
                 callback(null);
-                return;
+            } else {
+                try {
+                    let result = JSON.parse(message.response_body.data);
+
+                    callback(result);
+                } catch (e) {
+                    Utils.debug('Error parsing result: ' + e);
+                    callback(null);
+                }
             }
+        });
+    }
 
-            let params = this._createParams(stops);
-            let query = new HTTP.Query(params);
-            let uri = new Soup.URI(this._getRouterUrl() + '/plan?' +
-                                   query.toString());
-            let request = new Soup.Message({ method: 'GET', uri: uri });
+    _fetchRoutes(callback) {
+        if (this._onlyTransitData) {
+            this._fetchTransitStops((stops) => {
+                let points = this._query.filledPoints;
 
-            request.request_headers.append('Accept', 'application/json');
-            this._session.queue_message(request, (obj, message) => {
-                if (message.status_code !== Soup.Status.OK) {
-                    Utils.debug('Failed to get route plan from router ' +
-                                this._router + ' ' + message);
+                if (!stops) {
                     callback(null);
-                } else {
-                    callback(JSON.parse(message.response_body.data));
+                    return;
                 }
+
+                /* if there's only a start and end stop (no intermediate stops)
+                 * and those stops are identical, reject the routing, since this
+                 * means there would be no point in transit, and OTP would give
+                 * some bizarre option like boarding transit, go one stop and then
+                 * transfer to go back the same route
+                 */
+                if (stops.length === 2 && stops[0].id === stops[1].id) {
+                    callback(null);
+                    return;
+                }
+
+                let params = this._createParamsWithStops(stops);
+
+                this._fetchPlan(params, callback);
             });
-        });
+        } else {
+            let params = this._createParamsWithLocations();
+
+            this._fetchPlan(params, callback);
+        }
     }
 
     _reset() {
@@ -510,6 +559,8 @@ var OpenTripPlanner = class OpenTripPlanner {
                 let itineraries = [];
                 let plan = route.plan;
 
+                Utils.debug('route: ' + JSON.stringify(route, null, 2));
+
                 if (plan && plan.itineraries) {
                     itineraries =
                         itineraries.concat(
@@ -522,7 +573,10 @@ var OpenTripPlanner = class OpenTripPlanner {
                      * results */
                     this._noRouteFound();
                 } else {
-                    this._recalculateItineraries(itineraries);
+                    if (this._onlyTransitData)
+                        this._recalculateItineraries(itineraries);
+                    else
+                        this._updateWithNewItineraries(itineraries);
                 }
             } else {
                 this._noRouteFound();
@@ -577,6 +631,30 @@ var OpenTripPlanner = class OpenTripPlanner {
         return true;
     }
 
+    /**
+     * Update plan with new itineraries, setting the new itineraries if it's
+     * the first fetch for a query, or extending the existing ones if it's
+     * a request to load more
+     */
+    _updateWithNewItineraries(itineraries) {
+        /* sort itineraries, by departure time ascending if querying
+         * by leaving time, by arrival time descending when querying
+         * by arriving time
+         */
+        if (this._query.arriveBy)
+            itineraries.sort(TransitPlan.sortItinerariesByArrivalDesc);
+        else
+            itineraries.sort(TransitPlan.sortItinerariesByDepartureAsc);
+
+        let newItineraries =
+            this._extendPrevious ? this.plan.itineraries.concat(itineraries) :
+                                   itineraries;
+
+        // reset the "load more results" flag
+        this._extendPrevious = false;
+        this.plan.update(newItineraries);
+    }
+
     _recalculateItinerariesRecursive(itineraries, index) {
         if (index < itineraries.length) {
             this._recalculateItinerary(itineraries[index], (itinerary) => {
@@ -602,23 +680,7 @@ var OpenTripPlanner = class OpenTripPlanner {
 
             if (filteredItineraries.length > 0) {
                 filteredItineraries.forEach((itinerary) => itinerary.adjustTimings());
-
-                /* sort itineraries, by departure time ascending if querying
-                 * by leaving time, by arrival time descending when querying
-                 * by arriving time
-                 */
-                if (this._query.arriveBy)
-                    filteredItineraries.sort(TransitPlan.sortItinerariesByArrivalDesc);
-                else
-                    filteredItineraries.sort(TransitPlan.sortItinerariesByDepartureAsc);
-
-                let newItineraries = this._extendPrevious ?
-                                     this.plan.itineraries.concat(filteredItineraries) :
-                                     filteredItineraries;
-
-                // reset the "load more results" flag
-                this._extendPrevious = false;
-                this.plan.update(newItineraries);
+                this._updateWithNewItineraries(filteredItineraries);
             } else {
                 this._noRouteFound();
             }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]