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