]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/osrm.js
Merge remote-tracking branch 'upstream/pull/1802'
[rails.git] / app / assets / javascripts / index / directions / osrm.js
1 // OSRM car engine
2 // Doesn't yet support hints
3
4 function OSRMEngine() {
5   var cachedHints = [];
6
7   return {
8     id: "osrm_car",
9     creditline: '<a href="http://project-osrm.org/" target="_blank">OSRM</a>',
10     draggable: true,
11
12     _transformSteps: function(input_steps, line) {
13       var INSTRUCTION_TEMPLATE = {
14         'continue': 'javascripts.directions.instructions.continue',
15         'merge right': 'javascripts.directions.instructions.merge_right',
16         'merge left': 'javascripts.directions.instructions.merge_left',
17         'off ramp right': 'javascripts.directions.instructions.offramp_right',
18         'off ramp left': 'javascripts.directions.instructions.offramp_left',
19         'on ramp right': 'javascripts.directions.instructions.onramp_right',
20         'on ramp left': 'javascripts.directions.instructions.onramp_left',
21         'fork right': 'javascripts.directions.instructions.fork_right',
22         'fork left': 'javascripts.directions.instructions.fork_left',
23         'end of road right': 'javascripts.directions.instructions.endofroad_right',
24         'end of road left': 'javascripts.directions.instructions.endofroad_left',
25         'turn straight': 'javascripts.directions.instructions.continue',
26         'turn slight right': 'javascripts.directions.instructions.slight_right',
27         'turn right': 'javascripts.directions.instructions.turn_right',
28         'turn sharp right': 'javascripts.directions.instructions.sharp_right',
29         'turn uturn': 'javascripts.directions.instructions.uturn',
30         'turn sharp left': 'javascripts.directions.instructions.sharp_left',
31         'turn left': 'javascripts.directions.instructions.turn_left',
32         'turn slight left': 'javascripts.directions.instructions.slight_left',
33         'roundabout': 'javascripts.directions.instructions.roundabout',
34         'rotary': 'javascripts.directions.instructions.roundabout',
35         'depart': 'javascripts.directions.instructions.start',
36         'arrive': 'javascripts.directions.instructions.destination',
37       };
38       var ICON_MAP = {
39         'continue': 0,
40         'merge right': 21,
41         'merge left': 20,
42         'off ramp right': 24,
43         'off ramp left': 25,
44         'on ramp right': 2,
45         'on ramp left': 6,
46         'fork right': 18,
47         'fork left': 19,
48         'end of road right': 22,
49         'end of road left': 23,
50         'turn straight': 0,
51         'turn slight right': 1,
52         'turn right': 2,
53         'turn sharp right': 3,
54         'turn uturn': 4,
55         'turn slight left': 5,
56         'turn left': 6,
57         'turn sharp left': 7,
58         'roundabout': 10,
59         'rotary': 10,
60         'depart': 8,
61         'arrive': 14
62       };
63       var numToWord = function(num) {
64         if(num > 10) {
65           num = 11;
66         }
67         return ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "gt_ten"][num];
68       };
69       var transformed_steps = input_steps.map(function(step, idx) {
70         var maneuver_id;
71
72         // special case handling
73         switch (step.maneuver.type) {
74           case 'on ramp':
75           case 'off ramp':
76           case 'merge':
77           case 'end of road':
78           case 'fork':
79             maneuver_id = step.maneuver.type + ' ' + (step.maneuver.modifier.indexOf('left') >= 0 ? 'left' : 'right');
80             break;
81           case 'depart':
82           case 'arrive':
83           case 'roundabout':
84           case 'rotary':
85             maneuver_id = step.maneuver.type;
86             break;
87           case 'roundabout turn':
88           case 'turn':
89             maneuver_id = "turn " + step.maneuver.modifier;
90             break;
91           // for unknown types the fallback is turn
92           default:
93             maneuver_id = "turn " + step.maneuver.modifier;
94             break;
95         }
96         var template = INSTRUCTION_TEMPLATE[maneuver_id];
97
98         // convert lat,lng pairs to LatLng objects
99         var step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(function(a) { return L.latLng(a); }) ;
100         // append step_geometry on line
101         Array.prototype.push.apply(line, step_geometry);
102
103         var instText = "<b>" + (idx + 1) + ".</b> ";
104         var destinations = "<b>" + step.destinations + "</b>";
105         var namedRoad = true;
106         var name;
107
108         if (step.name && step.ref) {
109           name = "<b>" + step.name + " (" + step.ref + ")</b>";
110         } else if (step.name) {
111           name = "<b>" + step.name + "</b>";
112         } else if (step.ref) {
113           name = "<b>" + step.ref + "</b>";
114         } else {
115           name = I18n.t('javascripts.directions.instructions.unnamed');
116           namedRoad = false;
117         }
118
119         if (step.maneuver.type.match(/rotary|roundabout/)) {
120           if (step.maneuver.exit) {
121             if (step.maneuver.exit <= 10) {
122               instText += I18n.t(template + '_with_exit_ordinal', { exit: I18n.t('javascripts.directions.instructions.exit_counts.' + numToWord(step.maneuver.exit)), name: name });
123             } else {
124               instText += I18n.t(template + '_with_exit', { exit: step.maneuver.exit, name: name });
125             }
126           } else {
127             instText += I18n.t(template + '_without_exit', { name: name });
128           }
129         } else if (step.maneuver.type.match(/on ramp|off ramp/)) {
130           var params = {};
131           if (step.exits && step.maneuver.type.match(/off ramp/)) params.exit = step.exits;
132           if (step.destinations) params.directions = destinations;
133           if (namedRoad) params.directions = name;
134           if (Object.keys(params).length > 0) {
135             template = template + "_with_" + Object.keys(params).join("_");
136           }
137           instText += I18n.t(template, params);
138         } else {
139           instText += I18n.t(template + '_without_exit', { name: name });
140         }
141         return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry];
142       });
143
144       return transformed_steps;
145     },
146
147     getRoute: function (points, callback) {
148
149       var params = [
150         { name: "overview", value: "false" },
151         { name: "geometries", value: "polyline" },
152         { name: "steps", value: true }
153       ];
154
155
156       if (cachedHints.length === points.length) {
157         params.push({name: "hints", value: cachedHints.join(";")});
158       } else {
159         // invalidate cache
160         cachedHints = [];
161       }
162
163       var encoded_coords = points.map(function(p) {
164         return p.lng + ',' + p.lat;
165       }).join(';');
166
167       var req_url = OSM.OSRM_URL + encoded_coords;
168
169       var onResponse = function (data) {
170         if (data.code !== 'Ok')
171           return callback(true);
172
173         cachedHints = data.waypoints.map(function(wp) {
174           return wp.hint;
175         });
176
177         var line = [];
178         var transformLeg = function (leg) {
179           return this._transformSteps(leg.steps, line);
180         };
181
182         var steps = [].concat.apply([], data.routes[0].legs.map(transformLeg.bind(this)));
183
184         callback(false, {
185           line: line,
186           steps: steps,
187           distance: data.routes[0].distance,
188           time: data.routes[0].duration
189         });
190       };
191
192       return $.ajax({
193         url: req_url,
194         data: params,
195         dataType: "json",
196         success: onResponse.bind(this),
197         error: function () {
198           callback(true);
199         }
200       });
201     }
202   };
203 }
204
205 OSM.Directions.addEngine(new OSRMEngine(), true);