[gnome-maps/wip/routing2: 7/11] RouteService: Add GraphHopper backend
- From: Mattias Bengtsson <mattiasb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/routing2: 7/11] RouteService: Add GraphHopper backend
- Date: Mon, 28 Apr 2014 17:13:23 +0000 (UTC)
commit 8f3155b96a394aa9aaefe4a2a32d8d2c5e26d71c
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date: Tue Apr 22 04:41:06 2014 +0200
RouteService: Add GraphHopper backend
Add a GraphHopper implementation of the RouteService class.
https://bugzilla.gnome.org/show_bug.cgi?id=728695
src/routeService.js | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 92 insertions(+), 0 deletions(-)
---
diff --git a/src/routeService.js b/src/routeService.js
index 89221f9..9854451 100644
--- a/src/routeService.js
+++ b/src/routeService.js
@@ -21,8 +21,10 @@
*/
const Soup = imports.gi.Soup;
+const Champlain = imports.gi.Champlain;
const Lang = imports.lang;
+const Utils = imports.utils;
const Route = imports.route;
const EPAF = imports.epaf;
@@ -55,3 +57,93 @@ const RouteService = new Lang.Class({
}).bind(this));
}
});
+
+const GraphHopper = new Lang.Class({
+ Name: 'GraphHopper',
+ Extends: RouteService,
+
+ _init: function(url) {
+ this._key = "VCIHrHj0pDKb8INLpT4s5hVadNmJ1Q3vi0J4nJYP";
+ this._baseURL = url || "http://graphhopper.com/api/1/route?";
+ this._locale = 'en_US'; // TODO: get this from env
+ this.parent();
+ },
+
+ _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!", // localize
+ time: 0
+ }),
+ 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._createType(sign),
+ distance: distance,
+ instruction: text,
+ time: time });
+ },
+
+ _createType: function(sign) {
+ switch(sign) {
+ case -3: return Route.TurnPointType.SHARP_LEFT;
+ case -2: return Route.TurnPointType.LEFT;
+ case -1: return Route.TurnPointType.SLIGHT_LEFT;
+ case 0: return Route.TurnPointType.CONTINUE;
+ case 1: return Route.TurnPointType.SLIGHT_RIGHT;
+ case 2: return Route.TurnPointType.RIGHT;
+ case 3: return Route.TurnPointType.SHARP_RIGHT;
+ case 4: return Route.TurnPointType.END;
+ case 5: return Route.TurnPointType.VIA;
+ default: return null;
+ };
+ }
+});
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]