[gnome-maps/wip/routing2: 14/22] Add RouteService module



commit 23dccbf34087a96ff9f5851d0c16fd165b187d8a
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Sun May 4 01:52:11 2014 +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             |  142 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+), 0 deletions(-)
---
diff --git a/src/gnome-maps.js.gresource.xml b/src/gnome-maps.js.gresource.xml
index 3a4b706..589f17a 100644
--- a/src/gnome-maps.js.gresource.xml
+++ b/src/gnome-maps.js.gresource.xml
@@ -17,6 +17,7 @@
     <file>placeStore.js</file>
     <file>route.js</file>
     <file>routeQuery.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..aa77525
--- /dev/null
+++ b/src/routeService.js
@@ -0,0 +1,142 @@
+/* -*- 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 GLib = imports.gi.GLib;
+
+const Lang = imports.lang;
+const Utils = imports.utils;
+const _ = imports.gettext.gettext;
+
+const Route = imports.route;
+const RouteQuery = imports.routeQuery;
+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  = GLib.get_language_names()[0];
+        this._route   = new Route.Route();
+        this._query   = new RouteQuery.RouteQuery();
+
+        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);
+        let 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));
+    },
+
+    _buildURL: function(viaPoints, transportation) {
+        let points = viaPoints.map(function(p) {
+            return [p.latitude, p.longitude].join(',');
+        });
+        let vehicle = RouteQuery.Transportation.toString(transportation);
+        let query = new HTTP.Query({ type:    'json',
+                                     key:     this._key,
+                                     vehicle: vehicle,
+                                     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];
+        let path       = EPAF.decode(route.points);
+        let turnPoints = this._createTurnPoints(path, route.instructions);
+        let 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;
+        let min  = Route.TurnPointType.SHARP_LEFT;
+        let max  = Route.TurnPointType.VIA;
+        if(min <= type && type <= max)
+            return type;
+        else
+            return undefined;
+    }
+});


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