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



commit 5673dca525009063d6df5a05bc7fcb2793b0886a
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Sun Aug 25 03:33:39 2013 +0200

    RouteService: Add GraphHopper backend
    
    Add a GraphHopper implementation of the RouteService class.

 src/route.js        |    9 +---
 src/routeService.js |  102 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 100 insertions(+), 11 deletions(-)
---
diff --git a/src/route.js b/src/route.js
index 24b4f7c..5d4fe1c 100644
--- a/src/route.js
+++ b/src/route.js
@@ -92,12 +92,9 @@ const TurnPoint = new Lang.Class({
     },
 
     isDestination: function() {
-        if(   this._type === TurnPointType.START
-           || this._type === TurnPointType.VIA
-           || this._type === TurnPointType.STOP)
-            return true;
-        else
-            return false;
+        return this._type === TurnPointType.START
+            || this._type === TurnPointType.VIA
+            || this._type === TurnPointType.STOP;
     },
 
     getMarker: function() {
diff --git a/src/routeService.js b/src/routeService.js
index 2c87f02..29bff69 100644
--- a/src/routeService.js
+++ b/src/routeService.js
@@ -21,18 +21,20 @@
  */
 
 const Soup = imports.gi.Soup;
+const Champlain = imports.gi.Champlain;
 
 const Lang = imports.lang;
+const Utils = imports.utils;
 
 const Route = imports.route;
-const Polyline = imports.polyline;
+const EPAF = imports.epaf;
 const HTTP = imports.http;
 
 const Transportation = {
-    CAR:     0,
-    BIKE:    1,
-    FOOT:    2,
-    TRANSIT: 3
+    CAR:        0,
+    BIKE:       1,
+    PEDESTRIAN: 2,
+    TRANSIT:    3
 };
 
 const RouteService = new Lang.Class({
@@ -55,6 +57,7 @@ const RouteService = new Lang.Class({
         this._session.queue_message(msg, (function(session, message) {
             if (message.status_code === 200) {
                 let result = message.response_body.data;
+
                 callback(this._parseResult(result));
             } else {
                 log("Error: " + message.status_code);
@@ -63,3 +66,92 @@ const RouteService = new Lang.Class({
         }).bind(this));
     }
 });
+
+const GraphHopper = new Lang.Class({
+    Name: 'GraphHopper',
+    Extends: RouteService,
+
+    _init: function(url) {
+        this._baseURL = url || "http://graphhopper.com/routing/api/route?";;
+        this._locale = 'en_US'; // TODO: get this from env
+        this.parent();
+    },
+
+    _vehicle: function(transportationType) {
+        switch(transportationType) {
+            case Transportation.CAR:        return 'CAR';
+            case Transportation.BIKE:       return 'BIKE';
+            case 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',
+            vehicle: this._vehicle(transportation),
+            locale: this._locale,
+            point: points
+        });
+        let url = this._baseURL + query.toString();
+        Utils.debug("Sending route request to: " + url);
+        return url;
+    },
+
+    _parseResult: function(result) {
+        let route = JSON.parse(result).route,
+            directions = this._createDirections(route.instructions.indications),
+            coordinates = route.instructions.latLngs.map(function([lat, lng]) {
+                return new Champlain.Coordinate({ latitude:  lat,
+                                                  longitude: lng });
+            }),
+            turnPoints = this._createTurnPoints(directions,
+                                                coordinates,
+                                                route.instructions.distances,
+                                                route.instructions.descriptions),
+            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 new Route.Route({ coordinates: EPAF.decode(route.coordinates),
+                                 turnPoints:  turnPoints,
+                                 distance:    route.distance,
+                                 time:        route.time,
+                                 bbox:        bbox });
+    },
+
+    _createTurnPoints: function(directions, coordinates, distances, instructions) {
+        let result = directions.map(function(direction, i) {
+            return new Route.TurnPoint({ coordinate:  coordinates[i],
+                                         type:        Route.TurnPointType.NORMAL,
+                                         direction:   direction,
+                                         distance:    distances[i],
+                                         instruction: instructions[i] });
+        });
+        result[0].type  = Route.TurnPointType.START;
+        result[directions.length - 1].type = Route.TurnPointType.END;
+
+        return result;
+    },
+
+    _createDirections: function(indications) {
+        return indications.map(function(indication) {
+            switch(indication) {
+                case -3: return Route.Direction.SHARP_LEFT;
+                case -2: return Route.Direction.LEFT;
+                case -1: return Route.Direction.SLIGHT_LEFT;
+                case  0: return Route.Direction.CONTINUE;
+                case  1: return Route.Direction.SLIGHT_RIGHT;
+                case  2: return Route.Direction.RIGHT;
+                case  3: return Route.Direction.SHARP_RIGHT;
+                default: return null;
+            };
+        });
+    }
+});


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