[gnome-maps/wip/mlundblad/transit-routing: 1/5] WIP: Add module to query an OpenTripPlanner instance



commit 4a5029f6b2a1d0d3f6222d2e8b46d20eb30d1f1f
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Feb 15 23:18:14 2016 +0100

    WIP: Add module to query an OpenTripPlanner instance

 src/openTripPlanner.js               |  423 ++++++++++++++++++++++++++++++++++
 src/org.gnome.Maps.src.gresource.xml |    2 +
 src/transitPlan.js                   |  224 ++++++++++++++++++
 3 files changed, 649 insertions(+), 0 deletions(-)
---
diff --git a/src/openTripPlanner.js b/src/openTripPlanner.js
new file mode 100644
index 0000000..46e41d5
--- /dev/null
+++ b/src/openTripPlanner.js
@@ -0,0 +1,423 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2016 Marcus Lundblad
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+const Lang = imports.lang;
+
+const Soup = imports.gi.Soup;
+
+const Application = imports.application;
+const EPAF = imports.epaf;
+const HTTP = imports.http;
+const Location = imports.location;
+const Place = imports.place;
+const RouteQuery = imports.routeQuery;
+const TransitPlan = imports.transitPlan;
+const Utils = imports.utils;
+
+/* base URL used for testing against a local OTP instance for now */
+const BASE_URL = 'http://localhost:8080/otp';
+
+/* timeout after which the routers data is considered stale and we will force
+   a reload (24 hours) */
+const ROUTERS_TIMEOUT = 24 * 60 * 60 * 1000;
+
+/* minimum distance when an explicit walk route will be requested to suppliment
+   the transit route */
+const MIN_WALK_ROUTING_DISTANCE = 100;
+
+const OpenTripPlanner = new Lang.Class({
+    Name: 'OpenTripPlanner',
+
+    _init: function() {
+        this._session = new Soup.Session();
+        /* initially set routers as updated far back in the past to force
+           a download when first request */
+        this._routersUpdatedTimestamp = 0;
+        this._query = Application.routeQuery;
+        this._plan = new TransitPlan.Plan();
+    },
+
+    get plan() {
+        return this._plan;
+    },
+
+    connect: function() {
+        this._signalHandler = this._query.connect('notify::points', (function() {
+            if (this._query.isValid())
+                this.fetchRoute();
+        }).bind(this));
+    },
+
+    disconnect: function() {
+        if (this._signalHandler !== 0) {
+            this._query.disconnect(this._signalHandler);
+            this._signalHandler = 0;
+        }
+    },
+
+    _fetchRouters: function(callback) {
+        let currentTime = (new Date()).getTime();
+
+        if (currentTime - this._routersUpdatedTimestamp < ROUTERS_TIMEOUT) {
+            callback(true);
+        } else {
+            let uri = new Soup.URI(BASE_URL + '/routers');
+            let request = new Soup.Message({ method: 'GET', uri: uri });
+
+            request.request_headers.append('Accept', 'application/json');
+            this._session.queue_message(request, (function(obj, message) {
+                if (message.status_code !== Soup.Status.OK) {
+                    callback(false);
+                    return;
+                }
+
+                Utils.debug('routers: ' + message.response_body.data);
+                try {
+                    this._routers = JSON.parse(message.response_body.data);
+                    this._routersUpdatedTimestamp = (new Date()).getTime();
+                    callback(true);
+                } catch (e) {
+                    Utils.debug('Failed to parse router information');
+                    callback(false);
+                }
+            }).bind(this));
+        }
+    },
+
+    _getRoutersForPlace: function(place) {
+        let routers = [];
+
+        Utils.debug('_getRotersForPlace');
+        Utils.debug('place coords: ' + place.location.latitude + ', ' + place.location.longitude);
+
+        this._routers.routerInfo.forEach((function(routerInfo) {
+            Utils.debug('checking router: ' + routerInfo.routerId);
+
+            /* TODO: only check bounding rectangle for now
+               should we try to do a finer-grained check using the bounding
+               polygon (if OTP gives one for the routers).
+               And should we add some margins to allow routing from just outside
+               a network (walking distance)? */
+            if (place.location.latitude >= routerInfo.lowerLeftLatitude &&
+                place.location.latitude <= routerInfo.upperRightLatitude &&
+                place.location.longitude >= routerInfo.lowerLeftLongitude &&
+                place.location.longitude <= routerInfo.upperRightLongitude)
+                routers.push(routerInfo.routerId);
+        }));
+
+        return routers;
+    },
+
+    /* Note: this is theoretically slow (O(n*m)), but we will have filtered
+       possible routers for the starting and ending query point, so they should
+       be short (in many cases just one element) */
+    _routerIntersection: function(routers1, routers2) {
+        return routers1.filter(function(n) {
+            return routers2.indexOf(n) != -1;
+        });
+    },
+
+    _formatPlaceQueryParam: function(place) {
+        return '%s,%s'.format(place.location.latitude, place.location.longitude);
+    },
+
+    _fetchRoutesRecursive: function(routers, index, result, callback) {
+        let points = this._query.filledPoints;
+        let params = {fromPlace: this._formatPlaceQueryParam(points[0].place),
+                      toPlace: this._formatPlaceQueryParam(points[points.length - 1].place)
+                      };
+
+        Utils.debug('fetching plans for router with index ' + index);
+        Utils.debug('number of points: ' + points.length);
+
+        let intermediatePlaces = [];
+        for (let i = 1; i < points.length - 1; i++) {
+            Utils.debug('intermediatePlace: ' + this._formatPlaceQueryParam(points[i].place));
+            intermediatePlaces.push(this._formatPlaceQueryParam(points[i].place));
+        }
+        if (intermediatePlaces.length > 0)
+            params.intermediatePlaces = intermediatePlaces;
+
+        Utils.debug('intermediatePlaces: ' + intermediatePlaces);
+
+        //params.numItineraries = 10;
+
+        let query = new HTTP.Query(params);
+        let uri = new Soup.URI(BASE_URL + '/routers/' + routers[index] +
+                               '/plan?' + query.toString());
+        let request = new Soup.Message({ method: 'GET', uri: uri });
+
+        Utils.debug('URI: ' + uri.to_string(true));
+
+        request.request_headers.append('Accept', 'application/json');
+        this._session.queue_message(request, (function(obj, message) {
+            Utils.debug('callback: ' + callback);
+            if (message.status_code !== Soup.Status.OK)
+                Utils.debug('Failed to get route plan from router ' +
+                            routers[index]);
+            else
+                result.push(JSON.parse(message.response_body.data))
+
+            if (index < routers.length - 1)
+                this._fetchRoutesRecursive(routers, index + 1, result,
+                                           callback);
+            else {
+                Utils.debug('calling callback at index ' + index);
+                callback(result);
+            }
+        }).bind(this));
+    },
+
+    _fetchRoutes: function(routers, callback) {
+        Utils.debug('_fetchRoutes with length: ' + routers.length);
+        this._fetchRoutesRecursive(routers, 0, [], callback);
+    },
+
+    _reset: function() {
+        if (this._query.latest)
+            this._query.latest.place = null;
+        else
+            this.plan.reset();
+    },
+
+    fetchRoute: function() {
+        this._fetchRouters((function(success) {
+            if (success) {
+                let points = this._query.filledPoints;
+                let routers = this._getRoutersForPoints(points);
+
+                if (routers.length > 0) {
+                    Utils.debug('about to fetch routes');
+                    this._fetchRoutes(routers, (function(routes) {
+                        let itineraries = [];
+                        routes.forEach((function(plan) {
+                            Utils.debug('plan: ' + JSON.stringify(plan, null, 2));
+                            Utils.debug('itineraries: ' + itineraries);
+                            if (plan.plan && plan.plan.itineraries) {
+                                Utils.debug('creating itineraries');
+                                itineraries =
+                                    itineraries.concat(
+                                        this._createItineraries(plan.plan.itineraries));
+                                Utils.debug('itineraries.length: ' + itineraries.length);
+                            }
+                        }).bind(this));
+                        Utils.debug('found ' + itineraries.length + ' itineraries');
+                        if (itineraries.length === 0) {
+                            Application.notificationManager.showMessage(_("No route found."));
+                            this._reset();
+                        } else {
+                            itineraries.forEach((function(itinerary) {
+                                Utils.debug(itinerary.toString());
+                            }));
+                            this._recalculateItineraries(itineraries);
+                        }
+                    }).bind(this));
+
+                } else {
+                    Application.notificationManager.showMessage(_("No route found."));
+                    this._reset();
+                }
+            } else {
+                Application.notificationManager.showMessage(_("Route request failed."));
+                this._reset();
+            }
+        }).bind(this));
+    },
+
+    _recalculateItineraries: function(itineraries) {
+        this._recalculateItinerariesRecursive(itineraries, 0);
+    },
+
+    _recalculateItinerariesRecursive: function(itineraries, index) {
+        if (index < itineraries.length) {
+            this._recalculateItinerary(itineraries[index], (function(itinerary) {
+                itineraries[index] = itinerary;
+                this._recalculateItinerariesRecursive(itineraries, index + 1);
+            }).bind(this));
+        } else {
+            this.plan.update(itineraries);
+        }
+    },
+
+    _createWalkingLeg: function(from, to, route) {
+        let polyline = EPAF.decode(route.paths[0].points);
+
+        return new TransitPlan.Leg({fromCoordinate: from,
+                                    toCoordinate: to,
+                                    isTransit: false,
+                                    polyline: polyline,
+                                    walkingInstructions: route.paths[0]});
+    },
+
+    _recalculateItinerary: function(itinerary, callback) {
+        /* special case, if there's just one leg of an itinerary, and that leg
+           leg is a non-transit (walking), recalculate the route in its entire
+           using walking */
+        if (itinerary.legs.length === 1 && !itinerary.legs[0].transit) {
+            Application.routeService.fetchRouteAsync(this._query.filledPoints,
+                                                     RouteQuery.Transportation.PEDESTRIAN,
+                                                     (function(route) {
+                Utils.debug('Walking route: ' + JSON.stringify(route, '', 2));
+                let from = this._query.filledPoints[0];
+                let to = this._query.filledPoints[this._query.filledPoints.length - 1];
+                let leg = this._createWalkingLeg(from, to, route);
+                let newItinerary =
+                    new TransitPlan.Itinerary({departure: itinerary.departure,
+                                               duration: route.paths[0].time / 1000,
+                                               legs: [leg]});
+                callback(newItinerary);
+            }).bind(this));
+        } else {
+            /* TODO: replace walk legs with GraphHopper-generated paths (hence the
+               callback nature of this. Filter out unrealistic itineraries (having
+               walking segments not possible in reasonable time, due to our running
+               of OTP with only transit data). */
+            this._recalculateItineraryRecursive(itinerary, 0, callback);
+        }
+    },
+
+    _createQueryPointForCoords: function(latitude, longitude) {
+        let location = new Location.Location({ latitude: latitude,
+                                               longitude: longitude,
+                                               accuracy: 0 });
+        let place = new Place.Place({ location: location });
+        let point = new RouteQuery.QueryPoint();
+
+        point.place = place;
+        return point;
+    },
+
+    _recalculateItineraryRecursive: function(itinerary, index, callback) {
+        if (index < itinerary.legs.length) {
+            let leg = itinerary.legs[index];
+            if (index === 0) {
+                let from = this._query.filledPoints[0];
+                if (!leg.transit) {
+                    /* if the first leg of the intinerary returned by OTP is a
+                       walking one, recalculate it with GH using the actual
+                       starting coordinate from the input query */
+                    let to = this._createQueryPointForCoords(leg.toCoordinate[0],
+                                                             leg.toCoordinate[1]);
+                    Application.routeService.fetchRouteAsync([from, to],
+                                                             RouteQuery.Transportation.PEDESTRIAN,
+                                                             (function(route) {
+                        let leg = this._createWalkingLeg(from, to, route);
+                        itinerary.legs[index] = leg;
+                        this._recalculateItineraryRecursive(itinerary,
+                                                            index + 1,
+                                                            callback);
+                    }).bind(this));
+                } else {
+                    /* TODO: introduce an additional walking leg calculated
+                       by GH in case the OTP starting point as far enough from
+                       the original starting point */
+                    let to = this._createQueryPointForCoords(leg.fromCoordinate[0],
+                                                             leg.fromCoordinate[1]);
+                    let fromLoc = from.place.location;
+                    let toLoc = to.place.location;
+                    let distance = fromLoc.get_distance_from(toLoc) * 1000;
+                    Utils.debug('distance from original starting point to start of transit route: ' + 
distance);
+
+                    if (distance >= MIN_WALK_ROUTING_DISTANCE) {
+                        Application.routeService.fetchRouteAsync([from, to],
+                                                                 RouteQuery.Transportation.PEDESTRIAN,
+                                                                 (function(route) {
+                            let leg = this._createWalkingLeg(from, to, route);
+                            itinerary.legs.unshift(leg);
+                            /* now, next index will be two steps up, since we
+                               inserted a new leg */
+                             this._recalculateItineraryRecursive(itinerary,
+                                                                 index + 2,
+                                                                 callback);
+                        }).bind(this));
+                    } else {
+                        this._recalculateItineraryRecursive(itinerary, index + 1,
+                                                            callback);
+                    }
+                }
+            } else if (index === itinerary.legs.length - 1) {
+                /* TODO: similarily, calculate a precise walking walking leg
+                   with GH */
+                this._recalculateItineraryRecursive(itinerary, index + 1, callback);
+            } else {
+                /* TODO: similar as the above, but for a leg in the "middle"
+                   of an itinerary */
+                this._recalculateItineraryRecursive(itinerary, index + 1, callback);
+            }
+        } else {
+            callback(itinerary);
+        }
+    },
+
+    _getRoutersForPoints: function(points) {
+        Utils.debug('sucessfully fetched routers list, points.length ' + points.length);
+        let startRouters = this._getRoutersForPlace(points[0].place);
+        let endRouters =
+            this._getRoutersForPlace(points[points.length - 1].place);
+
+        Utils.debug('routers at start point: ' + startRouters);
+        Utils.debug('routers at end point: ' + endRouters);
+        let intersectingRouters =
+            this._routerIntersection(startRouters, endRouters);
+
+        Utils.debug('intersecting routers: ' + intersectingRouters);
+
+        return intersectingRouters;
+    },
+
+    _createItineraries: function(itineraries) {
+        return itineraries.map((function(itinerary) {
+                                    return this._createItinerary(itinerary);
+                                }).bind(this));
+    },
+
+    _createItinerary: function(itinerary) {
+        let legs = this._createLegs(itinerary.legs);
+        return new TransitPlan.Itinerary({ duration:  itinerary.duration,
+                                           transfers: itinerary.transfers,
+                                           departure: itinerary.startTime,
+                                           arrival:   itinerary.endTime,
+                                           legs:      legs});
+    },
+
+    _createLegs: function(legs) {
+        return legs.map((function(leg) {
+            return this._createLeg(leg);
+        }).bind(this));
+    },
+
+    _createLeg: function(leg) {
+        let polyline = EPAF.decode(leg.legGeometry.points);
+        return new TransitPlan.Leg({ departure:      leg.from.departure,
+                                     arrival:        leg.to.arrival,
+                                     from:           leg.from.name,
+                                     to:             leg.to.name,
+                                     fromCoordinate: [leg.from.lat,
+                                                      leg.from.lon],
+                                     toCoordinate:   [leg.to.lat,
+                                                      leg.to.lon],
+                                     route:          leg.route,
+                                     routeType:      leg.routeType,
+                                     polyline:       polyline,
+                                     isTransit:      leg.transitLeg});
+    }
+})
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index ecd8ef4..6acd706 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -34,6 +34,7 @@
     <file>mapView.js</file>
     <file>mapWalker.js</file>
     <file>notification.js</file>
+    <file>openTripPlanner.js</file>
     <file>notificationManager.js</file>
     <file>osmAccountDialog.js</file>
     <file>osmConnection.js</file>
@@ -71,6 +72,7 @@
     <file>socialPlaceMatcher.js</file>
     <file>storedRoute.js</file>
     <file>togeojson/togeojson.js</file>
+    <file>transitPlan.js</file>
     <file>translations.js</file>
     <file>turnPointMarker.js</file>
     <file>userLocationBubble.js</file>
diff --git a/src/transitPlan.js b/src/transitPlan.js
new file mode 100644
index 0000000..1b9e96e
--- /dev/null
+++ b/src/transitPlan.js
@@ -0,0 +1,224 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2016 Marcus Lundblad
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+const Lang = imports.lang;
+
+const Champlain = imports.gi.Champlain;
+const GObject = imports.gi.GObject;
+
+/*
+ * These constants corresponds to the routeType attribute of transit legs
+ * in the OpenTripPlanner API.
+ */
+const RouteType = {
+    NON_TRANSIT: -1,
+    TRAM:        0,
+    SUBWAY:      1,
+    TRAIN:       2,
+    BUS:         3,
+    FERRY:       4,
+    CABLE_CAR:   5,
+    GONDOLA:     6,
+    FUNICULAR:   7
+};
+
+const Plan = new Lang.Class({
+    Name: 'Plan',
+    Extends: GObject.Object,
+    Signals: {
+        'update': {},
+        'reset': {}
+    },
+
+    _init: function(params) {
+        this.parent(params);
+        this.reset();
+    },
+
+    get itineraries() {
+        return this._itineraries;
+    },
+
+    update: function(itineraries) {
+        this._itineraries = itineraries;
+        this.bbox = this._createBBox();
+        this.emit('update');
+    },
+
+    reset: function() {
+        this._itineraries = [];
+        this.bbox = null;
+        this.emit('reset');
+    },
+
+    _createBBox: function() {
+        let bbox = new Champlain.BoundingBox();
+        this._itineraries.forEach(function(itinerary) {
+            itinerary.legs.forEach(function(leg) {
+                bbox.extend(leg.fromCoordinate[0],
+                            leg.fromCoordinate[1]);
+                bbox.extend(leg.toCoordinate[0],
+                            leg.toCoordinate[1]);
+            });
+        });
+        return bbox;
+    }
+});
+
+const Itinerary = new Lang.Class({
+    Name: 'Itinerary',
+
+    _init: function(params) {
+        this._duration = params.duration;
+        delete params.duration;
+
+        this._departure = params.departure;
+        delete params.departure;
+
+        this._arrival = params.arrival;
+        delete params.arrival;
+
+        this._transfers = params.transfers;
+        delete params.transfers;
+
+        this._legs = params.legs;
+        delete params.legs;
+    },
+
+    get duration() {
+        return this._duration;
+    },
+
+    get departure() {
+        return this._departure;
+    },
+
+    get arrival() {
+        return this._arrival;
+    },
+
+    get transfers() {
+        return this._transfers;
+    },
+
+    get legs() {
+        return this._legs;
+    },
+
+    toString: function() {
+        let start = new Date();
+        let end = new Date();
+        let durationString =
+            this.duration >= 60 * 60 ? '%d h %d min'.format(this.duration / (60 * 60),
+                                                            this.duration % (60 * 60) / 60) :
+                                       '%d min'.format(this.duration / 60);
+        start.setTime(this.departure);
+        end.setTime(this.arrival);
+        return 'Itinerary: \nDeparture: ' + start + '\nArrival: ' + end +
+               '\nduration: ' + durationString + ', ' + this.transfers + ' transfers';
+    }
+});
+
+const Leg = new Lang.Class({
+    Name: 'Leg',
+
+    _init: function(params) {
+        this._route = params.route;
+        delete params.route;
+
+        this._routeType = params.routeType;
+        delete params.routeType;
+
+        this._departure = params.departure;
+        delete params.departure;
+
+        this._arrival = params.arrival;
+        delete params.arrival;
+
+        this._polyline = params.polyline;
+        delete params.polyline;
+
+        this._fromCoordinate = params.fromCoordinate;
+        delete params.fromCoordinate;
+
+        this._toCoordinate = params.toCoordinate;
+        delete params.toCoordinate;
+
+        this._from = params.from;
+        delete params.from;
+
+        this._to = params.to;
+        delete params.to;
+
+        this._headSign = params.headSign;
+        delete params.headSign;
+
+        this._isTransit = params.isTransit;
+        delete params.isTransit;
+
+        this._walkingInstructions = params.walkingInstructions;
+    },
+
+    get route() {
+        return this._route;
+    },
+
+    get routeType() {
+        return this._routeType;
+    },
+
+    get departure() {
+        return this._departure;
+    },
+
+    get arrival() {
+        return this._arrival;
+    },
+
+    get polyline() {
+        return this._polyline;
+    },
+
+    get fromCoordinate() {
+        return this._fromCoordinate;
+    },
+
+    get toCoordinate() {
+        return this._toCoordinate;
+    },
+
+    get from() {
+        return this._fromName;
+    },
+
+    get to() {
+        return this._toName;
+    },
+
+    get headSign() {
+        return this._headSign;
+    },
+
+    get transit() {
+        return this._isTransit;
+    }
+});


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