2 // Doesn't yet support hints
5 function FOSSGISOSRMEngine(modeId, vehicleType, srv) {
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",
35 "arrive": "destination"
38 function numToWord(num) {
39 return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num - 1];
42 const instrPrefix = "javascripts.directions.instructions.";
43 let template = instrPrefix + INSTRUCTION_TEMPLATE[step.maneuverId];
45 const destinations = "<b>" + step.destinations + "</b>";
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>";
56 name = OSM.i18n.t(instrPrefix + "unnamed");
60 if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) {
61 return OSM.i18n.t(template, { name: name });
64 if (step.maneuver.type.match(/^(rotary|roundabout)$/)) {
65 if (!step.maneuver.exit) {
66 return OSM.i18n.t(template + "_without_exit", { name: name });
69 if (step.maneuver.exit > 10) {
70 return OSM.i18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name });
73 return OSM.i18n.t(template + "_with_exit_ordinal", { exit: OSM.i18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name });
76 if (!step.maneuver.type.match(/^(on ramp|off ramp)$/)) {
77 return OSM.i18n.t(template + "_without_exit", { name: name });
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;
86 if (Object.keys(params).length > 0) {
87 template = template + "_with_" + Object.keys(params).join("_");
90 return OSM.i18n.t(template, params);
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";
99 switch (maneuver.type) {
105 return maneuver.type + " " + (maneuver.modifier.indexOf("left") >= 0 ? "left" : "right");
110 case "exit roundabout":
112 return maneuver.type;
113 case "roundabout turn":
115 return "turn " + maneuver.modifier;
116 // for unknown types the fallback is turn
118 return "turn " + maneuver.modifier;
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",
141 "turn sharp left": "sharp-left",
142 "roundabout": "roundabout",
143 "rotary": "roundabout",
144 "exit roundabout": "roundabout",
145 "exit rotary": "roundabout",
148 "arrive": "destination"
151 for (const step of leg.steps) step.maneuverId = getManeuverId(step);
153 const steps = leg.steps.map(step => [
154 ICON_MAP[step.maneuverId],
155 getInstructionText(step),
157 L.PolylineUtil.decode(step.geometry, { precision: 5 })
161 line: steps.flatMap(step => step[3]),
163 distance: leg.distance,
170 provider: "fossgis_osrm",
173 getRoute: function (points, signal) {
174 const query = new URLSearchParams({
176 geometries: "polyline",
180 if (cachedHints.length === points.length) {
181 query.set("hints", cachedHints.join(";"));
187 const demoQuery = new URLSearchParams({ srv: srv });
189 for (const { lat, lng } of points) {
190 demoQuery.append("loc", [lat, lng]);
194 credit: "OSRM (FOSSGIS)",
195 creditlink: "https://routing.openstreetmap.de/about.html",
196 demolink: "https://routing.openstreetmap.de/?" + demoQuery
198 const req_path = "routed-" + vehicleType + "/route/v1/driving/" + points.map(p => p.lng + "," + p.lat).join(";");
200 return fetch(OSM.FOSSGIS_OSRM_URL + req_path + "?" + query, { signal })
201 .then(response => response.json())
203 if (response.code !== "Ok") throw new Error();
205 cachedHints = response.waypoints.map(wp => wp.hint);
207 return { ... _processDirections(response.routes[0].legs[0]), ...meta };
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);