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