[gnome-maps/wip/routing2] WIP support the new GH format



commit cfd4c5d30f9100ca84ca131373e4d4e88626b755
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Sun Apr 20 19:59:08 2014 +0200

    WIP support the new GH format

 src/mainWindow.js   |   14 +++++-
 src/mapView.js      |    7 +++-
 src/route.js        |   22 ++-------
 src/routeService.js |  123 ++++++++++++++++++++++++++++----------------------
 4 files changed, 91 insertions(+), 75 deletions(-)
---
diff --git a/src/mainWindow.js b/src/mainWindow.js
index d53d784..3e85303 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -73,10 +73,11 @@ const MainWindow = new Lang.Class({
                 Application.routeService.getRoute([routeQuery.from, routeQuery.to],
                                                   routeQuery.transportation,
                                                   (function(err, result) {
-                                                      if(!err)
+                                                      if(!err) {
                                                           routeModel.update(result);
-                                                      else
-                                                          log("Couldn't do route'");
+                                                      } else {
+                                                          log(err);
+                                                      }
                                                   }));
             } else {
                 // TODO: implement
@@ -100,6 +101,13 @@ const MainWindow = new Lang.Class({
 
         this._overlay.add_overlay(new ZoomControl.ZoomControl(this.mapView));
         this._overlay.show_all();
+
+        const Geocode = imports.gi.GeocodeGlib;
+        
+        let berlin = new Geocode.Location({ latitude: 52.536273, longitude: 13.007813 });
+        let kiev = new Geocode.Location({ latitude:50.289339, longitude: 30.761719 });
+
+        routeQuery.setMany({ from: berlin, to: kiev });
     },
 
     _initSearchWidgets: function() {
diff --git a/src/mapView.js b/src/mapView.js
index 0a6f6ec..8373522 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -100,7 +100,10 @@ const MapView = new Lang.Class({
         this.geoclue.connect("location-changed",
                              this._updateUserLocation.bind(this));
 
-        routeModel.connect('update', this.showRoute.bind(this));
+        routeModel.connect('update', (function() {
+            log("route update");
+            this.showRoute(routeModel);
+        }).bind(this));
         routeModel.connect('reset',
                            this._routeLayer.remove_all.bind(this._routeLayer));
     },
@@ -195,6 +198,8 @@ const MapView = new Lang.Class({
 
     showRoute: function(route) {
         this._routeLayer.remove_all();
+        log("show route");
+        log(JSON.stringify(route));
         route.coordinates.forEach(function(coordinate) {
             this._routeLayer.add_node(coordinate);
         }, this);
diff --git a/src/route.js b/src/route.js
index dee6d7b..07f4c54 100644
--- a/src/route.js
+++ b/src/route.js
@@ -26,13 +26,9 @@ const Champlain = imports.gi.Champlain;
 const Utils = imports.utils;
 
 const TurnPointType = {
-    START: 0,
-    BASIC: 1,
-    VIA:   2,
-    END:   3
-};
-
-const Direction = {
+    END:         -3,
+    VIA:         -2,
+    START:       -1,
     LEFT:         0,
     SHARP_LEFT:   1,
     SLIGHT_LEFT:  2,
@@ -77,14 +73,6 @@ const Route = new Lang.Class({
             bbox.extend(latitude, longitude);
         }, this);
         return bbox;
-    },
-
-    setMany: function(obj) {
-        // TODO: implement
-    },
-
-    reset: function() {
-        // TODO: implement
     }
 });
 Utils.addSignalMethods(Route.prototype);
@@ -95,7 +83,6 @@ const TurnPoint = new Lang.Class({
     _init: function({ coordinate, type, direction, distance, instruction }) {
         this.coordinate = coordinate;
         this._type = type;
-        this.direction = direction;
         this.distance = distance;
         this.instruction = instruction;
     },
@@ -107,6 +94,7 @@ const TurnPoint = new Lang.Class({
     },
 
     getMarker: function() {
-        return undefined; // TODO: implement
+        // TODO: implement (should depend on isDestination above)
+        return undefined;
     }
 });
diff --git a/src/routeService.js b/src/routeService.js
index b13ffb2..6d7afec 100644
--- a/src/routeService.js
+++ b/src/routeService.js
@@ -23,7 +23,7 @@
 const Soup = imports.gi.Soup;
 const Champlain = imports.gi.Champlain;
 const GObject = imports.gi.GObject;
-const GeoCode = imports.gi.GeoCodeGlib;
+const GeoCode = imports.gi.GeocodeGlib;
 
 const Lang = imports.lang;
 const Utils = imports.utils;
@@ -67,6 +67,7 @@ const Query = new Lang.Class({
     _init: function(args) {
         this.parent(args);
         this._changeSignalId = this.connect('notify', this.emit.bind(this, 'change'));
+        this.reset();
     },
 
     reset: function() {
@@ -78,16 +79,30 @@ const Query = new Lang.Class({
     setMany: function(obj) {
         this.disconnect(this._changeSignalId);
 
-        for(let key in obj) {
-            if(this.hasOwnProperty(key))
-                this[key] = obj[key];
-        }
-
+        if(obj.hasOwnProperty("from"))
+            this.from = obj.from;
+        if(obj.hasOwnProperty("to"))
+            this.to = obj.to;
+        if(obj.hasOwnProperty("transportation"))
+            this.transportation = obj.transportation;
+        
         this._changeSignalId = this.connect('notify', this.emit.bind(this, 'change'));
         this.emit('change');
+    },
+    
+    toString: function() {
+        return "From: " + this.from +
+            "\nTo: " + this.to +
+            "\nTransportation" + this.transportation;
     }
 });
+Utils.addSignalMethods(Query.prototype);
 
+/*
+ * TODO: 
+ *  - remove this abstract class
+ *  - error handling
+ */      
 const RouteService = new Lang.Class({
     Name: 'RouteService',
     Abstract: true,
@@ -109,10 +124,9 @@ const RouteService = new Lang.Class({
             if (message.status_code === 200) {
                 let result = message.response_body.data;
 
-                callback(this._parseResult(result));
+                callback(undefined, this._parseResult(result));
             } else {
-                log("Error: " + message.status_code);
-                callback(null);
+                callback("Error: " + message.status_code);
             }
         }).bind(this));
     }
@@ -123,16 +137,17 @@ const GraphHopper = new Lang.Class({
     Extends: RouteService,
 
     _init: function(url) {
-        this._baseURL = url || "http://graphhopper.com/routing/api/route?";;
+        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 Transportation.CAR:        return 'CAR';
-            case Transportation.BIKE:       return 'BIKE';
-            case Transportation.PEDESTRIAN: return 'FOOT';
+            case Transportation.CAR:        return 'car';
+            case Transportation.BIKE:       return 'bike';
+            case Transportation.PEDESTRIAN: return 'foot';
             default:                        return null;
         }
     },
@@ -142,67 +157,67 @@ const GraphHopper = new Lang.Class({
             return [p.latitude, p.longitude].join(',');
         });
 
-        let query = new HTTP.Query({
-            type: 'json',
-            vehicle: this._vehicle(transportation),
-            locale: this._locale,
-            point: points
-        });
+        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) {
-        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),
+        // 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 new Route.Route({ coordinates: EPAF.decode(route.coordinates),
+        return new Route.Route({ coordinates: path,
                                  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;
+    _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);
+    },
 
-        return result;
+    _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 });
     },
 
-    _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;
-            };
-        });
+    _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]