~ubuntu-branches/ubuntu/vivid/gnome-maps/vivid

« back to all changes in this revision

Viewing changes to src/routeService.js

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson
  • Date: 2014-09-22 23:04:28 UTC
  • mfrom: (1.1.3) (2.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20140922230428-zvgvmk5pea6eavcz
Tags: 3.14.0-1
* New upstream release.
* Drop debian/patches/gjs-readwrite-flags.patch, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
 
2
/* vim: set et ts=4 sw=4: */
 
3
/*
 
4
 * Copyright (c) 2013 Mattias Bengtsson.
 
5
 *
 
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.
 
10
 *
 
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
 
14
 * for more details.
 
15
 *
 
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
 
19
 *
 
20
 * Author: Mattias Bengtsson <mattias.jc.bengtsson@gmail.com>
 
21
 */
 
22
 
 
23
const Champlain = imports.gi.Champlain;
 
24
const GLib = imports.gi.GLib;
 
25
const Soup = imports.gi.Soup;
 
26
 
 
27
const Lang = imports.lang;
 
28
const _ = imports.gettext.gettext;
 
29
 
 
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;
 
37
 
 
38
const GraphHopper = new Lang.Class({
 
39
    Name: 'GraphHopper',
 
40
 
 
41
    get query() {
 
42
        return this._query;
 
43
    },
 
44
 
 
45
    get route() {
 
46
        return this._route;
 
47
    },
 
48
 
 
49
    _init: function() {
 
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();
 
56
 
 
57
        this.query.connect('notify', (function() {
 
58
            if (this.query.isValid())
 
59
                this.fetchRoute(this.query.filledPoints,
 
60
                                this._query.transportation);
 
61
        }).bind(this));
 
62
 
 
63
        this.parent();
 
64
    },
 
65
 
 
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) {
 
70
            try {
 
71
                let result = this._parseMessage(message);
 
72
                if (!result) {
 
73
                    Application.notificationManager.showMessage(_("No route found."));
 
74
                    this.route.reset();
 
75
                } else {
 
76
                    let route = this._createRoute(result.paths[0]);
 
77
                    this.route.update(route);
 
78
                }
 
79
            } catch(e) {
 
80
                Application.notificationManager.showMessage(_("Route request failed."));
 
81
                log(e);
 
82
            }
 
83
        }).bind(this));
 
84
    },
 
85
 
 
86
    _buildURL: function(points, transportation) {
 
87
        let locations = points.map(function(point) {
 
88
            return [point.place.location.latitude, point.place.location.longitude].join(',');
 
89
        });
 
90
        let vehicle = RouteQuery.Transportation.toString(transportation);
 
91
        let query = new HTTP.Query({ type:    'json',
 
92
                                     key:     this._key,
 
93
                                     vehicle: vehicle,
 
94
                                     locale:  this._locale,
 
95
                                     point:   locations,
 
96
                                     debug:   Utils.debugEnabled
 
97
                                   });
 
98
        let url = this._baseURL + query.toString();
 
99
        Utils.debug("Sending route request to: " + url);
 
100
        return url;
 
101
    },
 
102
 
 
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));
 
111
        }
 
112
        if (status_code !== 200)
 
113
            return null;
 
114
 
 
115
        let result = JSON.parse(response_body.data);
 
116
 
 
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);
 
123
                });
 
124
            }
 
125
            return null;
 
126
        }
 
127
 
 
128
        return result;
 
129
    },
 
130
 
 
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();
 
135
 
 
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]);
 
139
 
 
140
        return { path:       path,
 
141
                 turnPoints: turnPoints,
 
142
                 distance:   route.distance,
 
143
                 time:       route.time,
 
144
                 bbox:       bbox };
 
145
    },
 
146
 
 
147
    _createTurnPoints: function(path, instructions) {
 
148
        let startPoint = new Route.TurnPoint({ coordinate:  path[0],
 
149
                                               type:        Route.TurnPointType.START,
 
150
                                               distance:    0,
 
151
                                               instruction: _("Start!"),
 
152
                                               time:        0
 
153
                                             });
 
154
        let rest = instructions.map(this._createTurnPoint.bind(this, path));
 
155
        return [startPoint].concat(rest);
 
156
    },
 
157
 
 
158
    _createTurnPoint: function(path, { text, distance, time, interval, sign }) {
 
159
        return new Route.TurnPoint({ coordinate:  path[interval[0]],
 
160
                                     type:        this._createTurnPointType(sign),
 
161
                                     distance:    distance,
 
162
                                     instruction: text,
 
163
                                     time:        time });
 
164
    },
 
165
 
 
166
    _createTurnPointType: function(sign) {
 
167
        let type = sign + 3;
 
168
        let min  = Route.TurnPointType.SHARP_LEFT;
 
169
        let max  = Route.TurnPointType.VIA;
 
170
        if (min <= type && type <= max)
 
171
            return type;
 
172
        else
 
173
            return undefined;
 
174
    }
 
175
});