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