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