]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/fossgis_osrm.js
Merge remote-tracking branch 'upstream/pull/5121'
[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) {
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         "ferry": "ferry",
34         "depart": "start",
35         "arrive": "destination"
36       };
37       function numToWord(num) {
38         return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num - 1];
39       }
40
41       const instrPrefix = "javascripts.directions.instructions.";
42       let template = instrPrefix + INSTRUCTION_TEMPLATE[step.maneuverId];
43
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         return OSM.i18n.t(template, { name: name });
61       }
62       if (step.maneuver.type.match(/^(rotary|roundabout)$/)) {
63         if (!step.maneuver.exit) {
64           return OSM.i18n.t(template + "_without_exit", { name: name });
65         }
66         if (step.maneuver.exit > 10) {
67           return OSM.i18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name });
68         }
69         return OSM.i18n.t(template + "_with_exit_ordinal", { exit: OSM.i18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name });
70       }
71       if (!step.maneuver.type.match(/^(on ramp|off ramp)$/)) {
72         return OSM.i18n.t(template + "_without_exit", { name: name });
73       }
74       const params = {};
75       if (step.exits && step.maneuver.type.match(/^(off ramp)$/)) params.exit = step.exits;
76       if (step.destinations) params.directions = destinations;
77       if (namedRoad) params.directions = name;
78       if (Object.keys(params).length > 0) {
79         template = template + "_with_" + Object.keys(params).join("_");
80       }
81       return OSM.i18n.t(template, params);
82     }
83
84     function _processDirections(leg) {
85       function getManeuverId({ maneuver, mode, intersections }) {
86         // special case handling
87         if (mode === "ferry") return "ferry";
88         if (intersections.some(i => i.classes?.includes("ferry"))) return "ferry";
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         "ferry": "ferry",
136         "depart": "start",
137         "arrive": "destination"
138       };
139
140       for (const step of leg.steps) step.maneuverId = getManeuverId(step);
141
142       const steps = leg.steps.map(step => [
143         ICON_MAP[step.maneuverId],
144         getInstructionText(step),
145         step.distance,
146         L.PolylineUtil.decode(step.geometry, { precision: 5 })
147       ]);
148
149       return {
150         line: steps.flatMap(step => step[3]),
151         steps,
152         distance: leg.distance,
153         time: leg.duration,
154         credit: "OSRM (FOSSGIS)",
155         creditlink: "https://routing.openstreetmap.de/about.html"
156       };
157     }
158
159     return {
160       mode: modeId,
161       provider: "fossgis_osrm",
162       draggable: true,
163
164       getRoute: function (points, signal) {
165         const query = new URLSearchParams({
166           overview: "false",
167           geometries: "polyline",
168           steps: true
169         });
170
171         if (cachedHints.length === points.length) {
172           query.set("hints", cachedHints.join(";"));
173         } else {
174           // invalidate cache
175           cachedHints = [];
176         }
177
178         const req_path = "routed-" + vehicleType + "/route/v1/driving/" + points.map(p => p.lng + "," + p.lat).join(";");
179
180         return fetch(OSM.FOSSGIS_OSRM_URL + req_path + "?" + query, { signal })
181           .then(response => response.json())
182           .then(response => {
183             if (response.code !== "Ok") throw new Error();
184             cachedHints = response.waypoints.map(wp => wp.hint);
185             return _processDirections(response.routes[0].legs[0]);
186           });
187       }
188     };
189   }
190
191   OSM.Directions.addEngine(new FOSSGISOSRMEngine("car", "car"), true);
192   OSM.Directions.addEngine(new FOSSGISOSRMEngine("bicycle", "bike"), true);
193   OSM.Directions.addEngine(new FOSSGISOSRMEngine("foot", "foot"), true);
194 }());