[gnome-maps/wip/routing2: 4/7] Add RouteService module



commit 243ef83cab5b0daaacb11d13cfcc307dbf721e67
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             |  149 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 150 insertions(+), 0 deletions(-)
---
diff --git a/src/gnome-maps.js.gresource.xml b/src/gnome-maps.js.gresource.xml
index a46aa9e..0663567 100644
--- a/src/gnome-maps.js.gresource.xml
+++ b/src/gnome-maps.js.gresource.xml
@@ -16,6 +16,7 @@
     <file>path.js</file>
     <file>placeStore.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..4f1596d
--- /dev/null
+++ b/src/routeService.js
@@ -0,0 +1,149 @@
+/* -*- 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',
+
+    get query() {
+        return this._query;
+    },
+
+    get route() {
+        return this._route;
+    },
+
+    _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._route = new Route.Route();
+        this._query = new Route.Query();
+
+        this._query.connect('change', (function() {
+            if(this._query.from && this._query.to)
+                this.fetchRoute([this._query.from, this._query.to],
+                                this._query.transportation);
+            else
+                this._route.reset();
+        }).bind(this));
+
+        this.parent();
+    },
+
+    fetchRoute: function(viaPoints, transportationType) {
+        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;
+                this.route.update(this._parseResult(result));
+            } else {
+                log("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._createTurnPointType(sign),
+                                     distance:    distance,
+                                     instruction: text,
+                                     time:        time });
+    },
+
+    _createTurnPointType: function(sign) {
+        let type = sign + 3,
+            min  = Route.TurnPointType.SHARP_LEFT,
+            max  = Route.TurnPointType.VIA;
+        return min <= type && type <= max
+            ? type
+            : undefined;
+    }
+});


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