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