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