[gnome-maps/wip/routing2: 7/8] RouteService: Add GraphHopper backend
- From: Mattias Bengtsson <mattiasb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/routing2: 7/8] RouteService: Add GraphHopper backend
- Date: Sun, 6 Apr 2014 23:26:59 +0000 (UTC)
commit bb0dcd8d32654d9d85b07a31c19c0e92cf07e4a6
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 | 101 ++++++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 99 insertions(+), 11 deletions(-)
---
diff --git a/src/route.js b/src/route.js
index 8edec58..e7bf128 100644
--- a/src/route.js
+++ b/src/route.js
@@ -77,12 +77,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 16e44c1..7003bfc 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({
@@ -63,3 +65,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]