[gnome-maps/wip/routing2: 1/8] Add RouteService module



commit aa252c75af375a313167c9100bbc2d6025b1058e
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             |  211 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 212 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..227e040
--- /dev/null
+++ b/src/routeService.js
@@ -0,0 +1,211 @@
+/* -*- 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 RouteError = new Lang.Class({
+    Name: 'RouteError',
+
+    _init: function(message, debugMessages) {
+        this._message       = message;
+        this._debugMessages = debugMessages;
+    },
+
+    debug: function() {
+        this._debugMessages.forEach(Utils.debug);
+    },
+
+    toString: function() {
+        return this._message;
+    }
+});
+
+const ParseMsgError = new Lang.Class({
+    Name: 'ParseMsgError',
+    Extends: RouteError,
+
+    _init: function(httpCode, httpBody, errors) {
+        let debugMsgs = ["HTTP code: "  + httpCode];
+        if(errors.length > 0) {
+            debugMsgs.push("Errors: {");
+            errors.forEach(function({ details, msg }, i) {
+                debugMsgs.push("    Message[" + (i + 1) + "]: " + msg);
+                debugMsgs.push("    Details[" + (i + 1) + "]: " + details);
+            });
+            debugMsgs.push("}");
+        }
+        debugMsgs.push("HTTP Body: {\n" + httpBody + "\n}");
+        this.parent(_("The route search result had error(s)."),
+                    debugMsgs);
+    }
+});
+
+const CreateRouteError = new Lang.Class({
+    Name: 'CreateRouteError',
+    Extends: RouteError,
+
+    _init: function(path) {
+        this.parent(_("Couldn't parse the route result JSON."),
+                    [JSON.stringify(path)]);
+    }
+});
+
+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) {
+            try {
+                let result = this._parseMessage(message);
+                let route  = this._createRoute(result);
+                this.route.update(route);
+            } catch(e) {
+                if(e instanceof RouteError) {
+                    log(e);
+                    e.debug();
+                } else
+                    throw e;
+            }
+        }).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,
+                                     debug:   Utils.debugEnabled
+                                   });
+        let url = this._baseURL + query.toString();
+        Utils.debug("Sending route request to: " + url);
+        return url;
+    },
+
+    _parseMessage: function({ status_code, response_body }) {
+        let errors = [];
+        if (status_code === 200 && response_body) {
+            let result = JSON.parse(response_body.data);
+            let info   = result.info;
+            let paths  = result.paths;
+            if(paths && paths[0])
+                return paths[0];
+            else if(info && info.errors) {
+                errors = info.errors;
+            }
+        }
+        throw new ParseMsgError(status_code, response_body.data, errors);
+    },
+
+    _createRoute: function(result) {
+        try {
+            let path       = EPAF.decode(result.points);
+            let turnPoints = this._createTurnPoints(path, result.instructions);
+            let bbox       = new Champlain.BoundingBox();
+
+            // GH does lonlat-order and Champlain latlon-order
+            bbox.extend(result.bbox[1], result.bbox[0]);
+            bbox.extend(result.bbox[3], result.bbox[2]);
+
+            return { path:       path,
+                     turnPoints: turnPoints,
+                     distance:   result.distance,
+                     time:       result.time,
+                     bbox:       bbox };
+        } catch (e) {
+            throw new CreateRouteError(result);
+        }
+    },
+
+    _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]