]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/fossgis_valhalla.js
Use svg symbols for routing icons
[rails.git] / app / assets / javascripts / index / directions / fossgis_valhalla.js
1 (function () {
2   function FOSSGISValhallaEngine(modeId, costing) {
3     const INSTR_MAP = [
4       "straight", // kNone = 0;
5       "start", // kStart = 1;
6       "start", // kStartRight = 2;
7       "start", // kStartLeft = 3;
8       "destination", // kDestination = 4;
9       "destination", // kDestinationRight = 5;
10       "destination", // kDestinationLeft = 6;
11       "straight", // kBecomes = 7;
12       "straight", // kContinue = 8;
13       "slight-right", // kSlightRight = 9;
14       "right", // kRight = 10;
15       "sharp-right", // kSharpRight = 11;
16       "u-turn", // kUturnRight = 12;
17       "u-turn", // kUturnLeft = 13;
18       "sharp-left", // kSharpLeft = 14;
19       "left", // kLeft = 15;
20       "slight-left", // kSlightLeft = 16;
21       "straight", // kRampStraight = 17;
22       "exit-right", // kRampRight = 18;
23       "exit-left", // kRampLeft = 19;
24       "exit-right", // kExitRight = 20;
25       "exit-left", // kExitLeft = 21;
26       "straight", // kStayStraight = 22;
27       "slight-right", // kStayRight = 23;
28       "slight-left", // kStayLeft = 24;
29       "merge-left", // kMerge = 25;
30       "roundabout", // kRoundaboutEnter = 26;
31       "roundabout", // kRoundaboutExit = 27;
32       "ferry", // kFerryEnter = 28;
33       "straight", // kFerryExit = 29;
34       null, // kTransit = 30;
35       null, // kTransitTransfer = 31;
36       null, // kTransitRemainOn = 32;
37       null, // kTransitConnectionStart = 33;
38       null, // kTransitConnectionTransfer = 34;
39       null, // kTransitConnectionDestination = 35;
40       null, // kPostTransitConnectionDestination = 36;
41       "merge-right", // kMergeRight = 37;
42       "merge-left" // kMergeLeft = 38;
43     ];
44
45     function _processDirections(tripLegs) {
46       let line = [];
47       let steps = [];
48       let distance = 0;
49       let time = 0;
50
51       for (const leg of tripLegs) {
52         const legLine = L.PolylineUtil.decode(leg.shape, {
53           precision: 6
54         });
55
56         const legSteps = leg.maneuvers.map(function (manoeuvre, idx) {
57           const num = `<b>${idx + 1}.</b> `;
58           const lineseg = legLine
59             .slice(manoeuvre.begin_shape_index, manoeuvre.end_shape_index + 1)
60             .map(([lat, lng]) => ({ lat, lng }));
61           return [
62             lineseg[0],
63             INSTR_MAP[manoeuvre.type],
64             num + manoeuvre.instruction,
65             manoeuvre.length * 1000,
66             lineseg
67           ];
68         });
69
70         line = line.concat(legLine);
71         steps = steps.concat(legSteps);
72         distance += leg.summary.length;
73         time += leg.summary.time;
74       }
75
76       return {
77         line: line,
78         steps: steps,
79         distance: distance * 1000,
80         time: time
81       };
82     }
83
84     return {
85       mode: modeId,
86       provider: "fossgis_valhalla",
87       creditline:
88       "<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
89       draggable: false,
90
91       getRoute: function (points, signal) {
92         const query = new URLSearchParams({
93           json: JSON.stringify({
94             locations: points.map(function (p) {
95               return { lat: p.lat, lon: p.lng, radius: 5 };
96             }),
97             costing: costing,
98             directions_options: {
99               units: "km",
100               language: I18n.currentLocale()
101             }
102           })
103         });
104         return fetch(OSM.FOSSGIS_VALHALLA_URL + "?" + query, { signal })
105           .then(response => response.json())
106           .then(({ trip }) => {
107             if (trip.status !== 0) throw new Error();
108             return _processDirections(trip.legs);
109           });
110       }
111     };
112   }
113
114   OSM.Directions.addEngine(new FOSSGISValhallaEngine("car", "auto"), true);
115   OSM.Directions.addEngine(new FOSSGISValhallaEngine("bicycle", "bicycle"), true);
116   OSM.Directions.addEngine(new FOSSGISValhallaEngine("foot", "pedestrian"), true);
117 }());