1
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
2
/* vim: set et ts=4 sw=4: */
4
* Copyright (c) 2013 Mattias Bengtsson.
6
* GNOME Maps is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by the
8
* Free Software Foundation; either version 2 of the License, or (at your
9
* option) any later version.
11
* GNOME Maps is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16
* You should have received a copy of the GNU General Public License along
17
* with GNOME Maps; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
* Author: Mattias Bengtsson <mattias.jc.bengtsson@gmail.com>
23
const Champlain = imports.gi.Champlain;
24
const GLib = imports.gi.GLib;
25
const Soup = imports.gi.Soup;
27
const Lang = imports.lang;
28
const _ = imports.gettext.gettext;
30
const Application = imports.application;
31
const Config = imports.config;
32
const EPAF = imports.epaf;
33
const HTTP = imports.http;
34
const Route = imports.route;
35
const RouteQuery = imports.routeQuery;
36
const Utils = imports.utils;
38
const GraphHopper = new Lang.Class({
50
this._session = new Soup.Session({ user_agent : Config.USER_AGENT });
51
this._key = "VCIHrHj0pDKb8INLpT4s5hVadNmJ1Q3vi0J4nJYP";
52
this._baseURL = "http://graphhopper.com/api/1/route?";
53
this._locale = GLib.get_language_names()[0];
54
this._route = new Route.Route();
55
this._query = new RouteQuery.RouteQuery();
57
this.query.connect('notify', (function() {
58
if (this.query.isValid())
59
this.fetchRoute(this.query.filledPoints,
60
this._query.transportation);
66
fetchRoute: function(points, transportationType) {
67
let url = this._buildURL(points, transportationType);
68
let msg = Soup.Message.new('GET', url);
69
this._session.queue_message(msg, (function(session, message) {
71
let result = this._parseMessage(message);
73
Application.notificationManager.showMessage(_("No route found."));
76
let route = this._createRoute(result.paths[0]);
77
this.route.update(route);
80
Application.notificationManager.showMessage(_("Route request failed."));
86
_buildURL: function(points, transportation) {
87
let locations = points.map(function(point) {
88
return [point.place.location.latitude, point.place.location.longitude].join(',');
90
let vehicle = RouteQuery.Transportation.toString(transportation);
91
let query = new HTTP.Query({ type: 'json',
96
debug: Utils.debugEnabled
98
let url = this._baseURL + query.toString();
99
Utils.debug("Sending route request to: " + url);
103
_parseMessage: function({ status_code, response_body, uri }) {
104
if (status_code === 500) {
105
log("Internal server error.\n"
106
+ "This is most likely a bug in GraphHopper");
107
log("Please file a bug at "
108
+ "https://github.com/graphhopper/graphhopper/issues\n"
109
+ "with the the following Graphopper request URL included:\n");
110
log(uri.to_string(false));
112
if (status_code !== 200)
115
let result = JSON.parse(response_body.data);
117
if (!Array.isArray(result.paths)) {
118
Utils.debug("No route found");
119
if (result.info && Array.isArray(result.info.errors)) {
120
result.info.errors.forEach(function({ message, details }) {
121
Utils.debug("Message: " + message);
122
Utils.debug("Details: " + details);
131
_createRoute: function(route) {
132
let path = EPAF.decode(route.points);
133
let turnPoints = this._createTurnPoints(path, route.instructions);
134
let bbox = new Champlain.BoundingBox();
136
// GH does lonlat-order and Champlain latlon-order
137
bbox.extend(route.bbox[1], route.bbox[0]);
138
bbox.extend(route.bbox[3], route.bbox[2]);
141
turnPoints: turnPoints,
142
distance: route.distance,
147
_createTurnPoints: function(path, instructions) {
148
let startPoint = new Route.TurnPoint({ coordinate: path[0],
149
type: Route.TurnPointType.START,
151
instruction: _("Start!"),
154
let rest = instructions.map(this._createTurnPoint.bind(this, path));
155
return [startPoint].concat(rest);
158
_createTurnPoint: function(path, { text, distance, time, interval, sign }) {
159
return new Route.TurnPoint({ coordinate: path[interval[0]],
160
type: this._createTurnPointType(sign),
166
_createTurnPointType: function(sign) {
168
let min = Route.TurnPointType.SHARP_LEFT;
169
let max = Route.TurnPointType.VIA;
170
if (min <= type && type <= max)