[gnome-maps/wip/routing2: 7/11] RouteService: Add GraphHopper backend



commit 43deec772b2de276b60068ac5022c6c323dcf0d3
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 |   86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 86 insertions(+), 0 deletions(-)
---
diff --git a/src/routeService.js b/src/routeService.js
index 89221f9..b4512d1 100644
--- a/src/routeService.js
+++ b/src/routeService.js
@@ -21,8 +21,11 @@
  */
 
 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;
@@ -55,3 +58,86 @@ 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!"),
+                                               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._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]