[gnome-maps/wip/routing2: 5/9] Add RouteService module



commit f2b68212fb2a42b684c531c8c49483e191da643a
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Fri Aug 23 06:15:55 2013 +0200

    Add RouteService module
    
    Add a RouteService module with a GraphHopper class as only implementation.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=728695

 src/gnome-maps.js.gresource.xml |    1 +
 src/routeService.js             |  128 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+), 0 deletions(-)
---
diff --git a/src/gnome-maps.js.gresource.xml b/src/gnome-maps.js.gresource.xml
index 65f569c..274effb 100644
--- a/src/gnome-maps.js.gresource.xml
+++ b/src/gnome-maps.js.gresource.xml
@@ -16,6 +16,7 @@
     <file>placeStore.js</file>
     <file>epaf.js</file>
     <file>route.js</file>
+    <file>routeService.js</file>
     <file>searchPopup.js</file>
     <file>settings.js</file>
     <file>sidebar.js</file>
diff --git a/src/routeService.js b/src/routeService.js
new file mode 100644
index 0000000..ee08cc6
--- /dev/null
+++ b/src/routeService.js
@@ -0,0 +1,128 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2013 Mattias Bengtsson.
+ *
+ * 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: Mattias Bengtsson <mattias jc bengtsson gmail com>
+ */
+
+const Soup = imports.gi.Soup;
+const Champlain = imports.gi.Champlain;
+
+const Lang = imports.lang;
+const Utils = imports.utils;
+const _ = imports.gettext.gettext;
+
+const Route = imports.route;
+const EPAF = imports.epaf;
+const HTTP = imports.http;
+
+const GraphHopper = new Lang.Class({
+    Name: 'GraphHopper',
+
+    _init: function() {
+        this._session = new Soup.Session({ user_agent : "gnome-maps" });
+        this._key = "VCIHrHj0pDKb8INLpT4s5hVadNmJ1Q3vi0J4nJYP";
+        this._baseURL = "http://graphhopper.com/api/1/route?";;
+        this._locale = 'en_US'; // TODO: get this from env
+        this.parent();
+    },
+
+    getRoute: function(viaPoints, transportationType, callback) {
+        let url = this._buildURL(viaPoints, transportationType),
+            msg = Soup.Message.new('GET', url);
+        this._session.queue_message(msg, (function(session, message) {
+            if (message.status_code === 200) {
+                let result = message.response_body.data;
+                callback(undefined, this._parseResult(result));
+            } else {
+                callback("Error: " + message.status_code);
+            }
+        }).bind(this));
+    },
+
+    _vehicle: function(transportationType) {
+        switch(transportationType) {
+        case Route.Transportation.CAR:        return 'car';
+        case Route.Transportation.BIKE:       return 'bike';
+        case Route.Transportation.PEDESTRIAN: return 'foot';
+        default:                              return null;
+        }
+    },
+
+    _buildURL: function(viaPoints, transportation) {
+        let points = viaPoints.map(function(p) {
+            return [p.latitude, p.longitude].join(',');
+        });
+
+        let query = new HTTP.Query({ type: 'json',
+                                     key: this._key,
+                                     vehicle: this._vehicle(transportation),
+                                     locale: this._locale,
+                                     point: points
+                                   });
+        let url = this._baseURL + query.toString();
+        Utils.debug("Sending route request to: " + url);
+        return url;
+    },
+
+    // TODO: error handling
+    _parseResult: function(result) {
+        // Always the first path until GH has alternate routes support
+        let route = JSON.parse(result).paths[0],
+            path = EPAF.decode(route.points),
+            turnPoints = this._createTurnPoints(path, route.instructions),
+            bbox = new Champlain.BoundingBox();
+
+        // GH does lonlat-order and Champlain latlon-order
+        bbox.extend(route.bbox[1], route.bbox[0]);
+        bbox.extend(route.bbox[3], route.bbox[2]);
+
+        return { path:        path,
+                 turnPoints:  turnPoints,
+                 distance:    route.distance,
+                 time:        route.time,
+                 bbox:        bbox };
+    },
+
+    _createTurnPoints: function(path, instructions) {
+        let startPoint = new Route.TurnPoint({ coordinate:  path[0],
+                                               type:        Route.TurnPointType.START,
+                                               distance:    0,
+                                               instruction: _("Start!"),
+                                               time:        0
+                                             });
+        let rest = instructions.map(this._createTurnPoint.bind(this, path));
+        return [startPoint].concat(rest);
+    },
+
+    _createTurnPoint: function(path, { text, distance, time, interval, sign }) {
+        return new Route.TurnPoint({ coordinate:  path[interval[0]],
+                                     type:        this._signToTurnPointType(sign),
+                                     distance:    distance,
+                                     instruction: text,
+                                     time:        time });
+    },
+
+    _signToTurnPointType: function(sign) {
+        let type     = sign + 3,
+            turnType = Route.TurnPointType;
+        return turnType.SHARP_LEFT <= type && type <= turnType.VIA
+            ? type
+            : undefined;
+    }
+});


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