[gnome-maps/wip/routing: 54/59] RouteService: Add GraphHopper support



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

    RouteService: Add GraphHopper support
    
    Add support for routing via GraphHopper HTTP REST interface.

 src/routeService.js |   88 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 88 insertions(+), 0 deletions(-)
---
diff --git a/src/routeService.js b/src/routeService.js
index 16e44c1..3a2f98b 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 Polyline = imports.polyline;
@@ -63,3 +65,89 @@ 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';
+        this.parent();
+    },
+
+    _vehicle: function(transportationType) {
+        switch(transportationType) {
+            case Transportation.CAR:     return 'CAR';
+            case Transportation.BIKE:    return 'BIKE';
+            case Transportation.FOOT:    return 'FOOT';
+            case Transportation.TRANSIT: return '';
+        }
+        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),
+            instructions = this._createInstructions(route.instructions.indications,
+                                                    route.instructions.descriptions,
+                                                    route.instructions.distances),
+            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: Polyline.decode(route.coordinates),
+                                 instructions: instructions,
+                                 distance: route.distance,
+                                 time: route.time,
+                                 bbox: bbox });
+    },
+
+    _createInstructions: function(directions, distances, descriptions) {
+        let result = directions.map(function(direction, i) {
+            // GH doesn't provide coordinates of turns yet
+            return new Route.Instruction({ coordinate: null, 
+                                           type: Route.InstructionType.NORMAL,
+                                           direction: direction,
+                                           distance: distances[i],
+                                           description: descriptions[i] });
+        });
+        result[0].type = Route.InstructionType.START;
+        result[directions.length - 1].type = Route.InstructionType.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;
+            };
+            return null;
+        });
+    }
+});
+


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