[gnome-maps/wip/mlundblad/transit-plugin-resrobot: 6/10] WIP: add module with transit walking route utils



commit a7a854a424990ce91557f2c187339786b0b053e7
Author: Marcus Lundblad <ml update uu se>
Date:   Thu Oct 10 21:57:29 2019 +0200

    WIP: add module with transit walking route utils
    
    Broken out convienience functions from the
    OpenTripPlanner plugin to be able to re-use
    in other plugins.

 src/graphHopperTransit.js            | 107 +++++++++++++++++++++++++++++++++++
 src/org.gnome.Maps.src.gresource.xml |   1 +
 2 files changed, 108 insertions(+)
---
diff --git a/src/graphHopperTransit.js b/src/graphHopperTransit.js
new file mode 100644
index 00000000..27bb6201
--- /dev/null
+++ b/src/graphHopperTransit.js
@@ -0,0 +1,107 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2019 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+/**
+ * Utilities to use GraphHopper to perform walking routes for use in
+ * transit itineraries, for plugins not natively supporting turn-by-turn
+ * routing for walking legs
+ */
+
+const Champlain = imports.gi.Champlain;
+
+const Application = imports.application;
+const Location = imports.location;
+const Place = imports.place;
+const RouteQuery = imports.routeQuery;
+const TransitPlan = imports.transitPlan;
+
+/* Creates a new walking leg given start and end places, and a route
+ * obtained from GraphHopper. If the route is undefined (which happens if
+ * GraphHopper failed to obtain a walking route, approximate it with a
+ * straight line. */
+function createWalkingLeg(from, to, fromName, toName, route) {
+    let fromLocation = from.place.location;
+    let toLocation = to.place.location;
+    let fromCoordinate = [fromLocation.latitude, fromLocation.longitude];
+    let toCoordinate = [toLocation.latitude, toLocation.longitude];
+    let polyline = route ? route.path :
+                           createStraightPolyline(fromLocation, toLocation);
+    let distance = route ? route.distance :
+                           fromLocation.get_distance_from(toLocation) * 1000;
+    /* as an estimate for approximated straight-line walking legs,
+     * assume a speed of 1 m/s to allow some extra time */
+    let duration = route ? route.time / 1000 : distance;
+    let walkingInstructions = route ? route.turnPoints : null;
+
+    return new TransitPlan.Leg({ fromCoordinate: fromCoordinate,
+                                 toCoordinate: toCoordinate,
+                                 from: fromName,
+                                 to: toName,
+                                 isTransit: false,
+                                 polyline: polyline,
+                                 duration: duration,
+                                 distance: distance,
+                                 walkingInstructions: walkingInstructions });
+}
+
+// create a straight-line "as the crow flies" polyline between two places
+function createStraightPolyline(fromLoc, toLoc) {
+    return [new Champlain.Coordinate({ latitude: fromLoc.latitude,
+                                       longitude: fromLoc.longitude }),
+            new Champlain.Coordinate({ latitude: toLoc.latitude,
+                                       longitude: toLoc.longitude })];
+}
+
+var _walkingRoutes = [];
+
+/* fetches walking route and stores the route for the given coordinate
+ * pair to avoid requesting the same route over and over from GraphHopper
+ */
+function fetchWalkingRoute(points, callback) {
+    let index = points[0].place.location.latitude + ',' +
+                points[0].place.location.longitude + ';' +
+                points[1].place.location.latitude + ',' +
+                points[1].place.location.longitude;
+    let route = _walkingRoutes[index];
+
+    if (!route) {
+        Application.routingDelegator.graphHopper.fetchRouteAsync(points,
+                                          RouteQuery.Transportation.PEDESTRIAN,
+                                          (newRoute) => {
+            _walkingRoutes[index] = newRoute;
+            callback(newRoute);
+        });
+    } else {
+        callback(route);
+    }
+}
+
+// create a query point from a bare coordinate (lat, lon pair)
+function createQueryPointForCoord(coord) {
+    let location = new Location.Location({ latitude: coord[0],
+                                           longitude: coord[1],
+                                           accuracy: 0 });
+    let place = new Place.Place({ location: location });
+    let point = new RouteQuery.QueryPoint();
+
+    point.place = place;
+    return point;
+}
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index 94e50990..5ec3d065 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -24,6 +24,7 @@
     <file>gpxShapeLayer.js</file>
     <file>graphHopper.js</file>
     <file>graphHopperGeocode.js</file>
+    <file>graphHopperTransit.js</file>
     <file>hvt.js</file>
     <file>http.js</file>
     <file>instructionRow.js</file>


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