]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/graphhopper.js
Merge remote-tracking branch 'upstream/pull/5642'
[rails.git] / app / assets / javascripts / index / directions / graphhopper.js
1 (function () {
2   function GraphHopperEngine(id, vehicleType) {
3     const GH_INSTR_MAP = {
4       "-3": 7, // sharp left
5       "-2": 6, // left
6       "-1": 5, // slight left
7       "0": 0, // straight
8       "1": 1, // slight right
9       "2": 2, // right
10       "3": 3, // sharp right
11       "4": 14, // finish reached
12       "5": 14, // via reached
13       "6": 10, // roundabout
14       "-7": 19, // keep left
15       "7": 18, // keep right
16       "-98": 4, // unknown direction u-turn
17       "-8": 4, // left u-turn
18       "8": 4 // right u-turn
19     };
20
21     function _processDirections(path) {
22       const line = L.PolylineUtil.decode(path.points);
23
24       const steps = path.instructions.map(function (instr, i) {
25         const num = `<b>${i + 1}.</b> `;
26         const lineseg = line
27           .slice(instr.interval[0], instr.interval[1] + 1)
28           .map(([lat, lng]) => ({ lat, lng }));
29         return [
30           lineseg[0],
31           GH_INSTR_MAP[instr.sign],
32           num + instr.text,
33           instr.distance,
34           lineseg
35         ]; // TODO does graphhopper map instructions onto line indices?
36       });
37       steps.at(-1)[1] = 14;
38
39       return {
40         line: line,
41         steps: steps,
42         distance: path.distance,
43         time: path.time / 1000,
44         ascend: path.ascend,
45         descend: path.descend
46       };
47     }
48
49     return {
50       id: id,
51       creditline: "<a href=\"https://www.graphhopper.com/\" target=\"_blank\">GraphHopper</a>",
52       draggable: false,
53
54       getRoute: function (points, signal) {
55         // GraphHopper Directions API documentation
56         // https://graphhopper.com/api/1/docs/routing/
57         const query = new URLSearchParams({
58           vehicle: vehicleType,
59           locale: I18n.currentLocale(),
60           key: "LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn",
61           elevation: false,
62           instructions: true,
63           turn_costs: vehicleType === "car"
64         });
65         points.forEach(p => query.append("point", p.lat + "," + p.lng));
66         return fetch(OSM.GRAPHHOPPER_URL + "?" + query, { signal })
67           .then(response => response.json())
68           .then(({ paths }) => {
69             if (!paths || paths.length === 0) throw new Error();
70             return _processDirections(paths[0]);
71           });
72       }
73     };
74   }
75
76   OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_car", "car"), true);
77   OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_bicycle", "bike"), true);
78   OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_foot", "foot"), true);
79 }());