]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/fossgis_valhalla.js
Merge remote-tracking branch 'upstream/pull/5621'
[rails.git] / app / assets / javascripts / index / directions / fossgis_valhalla.js
1 (function () {
2   function FOSSGISValhallaEngine(id, costing) {
3     const INSTR_MAP = [
4       0, // kNone = 0;
5       8, // kStart = 1;
6       8, // kStartRight = 2;
7       8, // kStartLeft = 3;
8       14, // kDestination = 4;
9       14, // kDestinationRight = 5;
10       14, // kDestinationLeft = 6;
11       0, // kBecomes = 7;
12       0, // kContinue = 8;
13       1, // kSlightRight = 9;
14       2, // kRight = 10;
15       3, // kSharpRight = 11;
16       4, // kUturnRight = 12;
17       4, // kUturnLeft = 13;
18       7, // kSharpLeft = 14;
19       6, // kLeft = 15;
20       5, // kSlightLeft = 16;
21       0, // kRampStraight = 17;
22       24, // kRampRight = 18;
23       25, // kRampLeft = 19;
24       24, // kExitRight = 20;
25       25, // kExitLeft = 21;
26       0, // kStayStraight = 22;
27       1, // kStayRight = 23;
28       5, // kStayLeft = 24;
29       20, // kMerge = 25;
30       10, // kRoundaboutEnter = 26;
31       10, // kRoundaboutExit = 27;
32       17, // kFerryEnter = 28;
33       0, // 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       21, // kMergeRight = 37;
42       20 // 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       id: id,
86       creditline:
87       "<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
88       draggable: false,
89
90       getRoute: function (points, callback) {
91         const data = {
92           json: JSON.stringify({
93             locations: points.map(function (p) {
94               return { lat: p.lat, lon: p.lng, radius: 5 };
95             }),
96             costing: costing,
97             directions_options: {
98               units: "km",
99               language: I18n.currentLocale()
100             }
101           })
102         };
103         return $.ajax({
104           url: OSM.FOSSGIS_VALHALLA_URL,
105           data,
106           dataType: "json",
107           success: function ({ trip }) {
108             if (trip.status === 0) {
109               callback(false, _processDirections(trip.legs));
110             } else {
111               callback(true);
112             }
113           },
114           error: function () {
115             callback(true);
116           }
117         });
118       }
119     };
120   }
121
122   OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_car", "auto"), true);
123   OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_bicycle", "bicycle"), true);
124   OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_foot", "pedestrian"), true);
125 }());