]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/osrm.js
6e7a6deb71955020f9a4ea53e1f30194a9267037
[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 convertExitNumber = function(exitNum) {
64         var j = exitNum % 10,
65         k = exitNum % 100;
66         if (j === 1 && k !== 11) {
67             return exitNum + "st";
68         }
69         if (j === 2 && k !== 12) {
70             return exitNum + "nd";
71         }
72         if (j === 3 && k !== 13) {
73             return exitNum + "rd";
74         }
75         return exitNum + "th";
76       };
77       var transformed_steps = input_steps.map(function(step, idx) {
78         var maneuver_id;
79
80         // special case handling
81         switch (step.maneuver.type) {
82           case 'on ramp':
83           case 'off ramp':
84           case 'merge':
85           case 'end of road':
86           case 'fork':
87             maneuver_id = step.maneuver.type + ' ' + (step.maneuver.modifier.indexOf('left') >= 0 ? 'left' : 'right');
88             break;
89           case 'depart':
90           case 'arrive':
91           case 'roundabout':
92           case 'rotary':
93             maneuver_id = step.maneuver.type;
94             break;
95           case 'roundabout turn':
96           case 'turn':
97             maneuver_id = "turn " + step.maneuver.modifier;
98             break;
99           // for unknown types the fallback is turn
100           default:
101             maneuver_id = "turn " + step.maneuver.modifier;
102             break;
103         }
104         var template = INSTRUCTION_TEMPLATE[maneuver_id];
105
106         // convert lat,lng pairs to LatLng objects
107         var step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(function(a) { return L.latLng(a); }) ;
108         // append step_geometry on line
109         Array.prototype.push.apply(line, step_geometry);
110
111         var instText = "<b>" + (idx + 1) + ".</b> ";
112         var destinations = "<b>" + step.destinations + "</b>";
113         var namedRoad = true;
114         var name;
115
116         if (step.name && step.ref) {
117           name = "<b>" + step.name + " (" + step.ref + ")</b>";
118         } else if (step.name) {
119           name = "<b>" + step.name + "</b>";
120         } else if (step.ref) {
121           name = "<b>" + step.ref + "</b>";
122         } else {
123           name = I18n.t('javascripts.directions.instructions.unnamed');
124           namedRoad = false;
125         }
126
127         if (step.maneuver.type.match(/rotary|roundabout/)) {
128           if (step.maneuver.exit) {
129             instText += I18n.t(template + '_with_exit', { exit: convertExitNumber(step.maneuver.exit), name: name } );
130           } else {
131             instText += I18n.t(template + '_without_exit', { name: name } );
132           }
133         } else if (step.maneuver.type.match(/on ramp|off ramp/)) {
134           var params = {};
135           if (step.exits && step.maneuver.type.match(/off ramp/)) params.exit = step.exits;
136           if (step.destinations) params.directions = destinations;
137           if (namedRoad) params.directions = name;
138           if (Object.keys(params).length > 0) {
139             template = template + "_with_" + Object.keys(params).join("_");
140           }
141           instText += I18n.t(template, params);
142         } else {
143           instText += I18n.t(template + '_without_exit', { name: name });
144         }
145         return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry];
146       });
147
148       return transformed_steps;
149     },
150
151     getRoute: function (points, callback) {
152
153       var params = [
154         { name: "overview", value: "false" },
155         { name: "geometries", value: "polyline" },
156         { name: "steps", value: true }
157       ];
158
159
160       if (cachedHints.length === points.length) {
161         params.push({name: "hints", value: cachedHints.join(";")});
162       } else {
163         // invalidate cache
164         cachedHints = [];
165       }
166
167       var encoded_coords = points.map(function(p) {
168         return p.lng + ',' + p.lat;
169       }).join(';');
170
171       var req_url = OSM.OSRM_URL + encoded_coords;
172
173       var onResponse = function (data) {
174         if (data.code !== 'Ok')
175           return callback(true);
176
177         cachedHints = data.waypoints.map(function(wp) {
178           return wp.hint;
179         });
180
181         var line = [];
182         var transformLeg = function (leg) {
183           return this._transformSteps(leg.steps, line);
184         };
185
186         var steps = [].concat.apply([], data.routes[0].legs.map(transformLeg.bind(this)));
187
188         callback(false, {
189           line: line,
190           steps: steps,
191           distance: data.routes[0].distance,
192           time: data.routes[0].duration
193         });
194       };
195
196       return $.ajax({
197         url: req_url,
198         data: params,
199         dataType: "json",
200         success: onResponse.bind(this),
201         error: function () {
202           callback(true);
203         }
204       });
205     }
206   };
207 }
208
209 OSM.Directions.addEngine(new OSRMEngine(), true);