]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/fossgis_valhalla.js
Merge remote-tracking branch 'upstream/pull/5613'
[rails.git] / app / assets / javascripts / index / directions / fossgis_valhalla.js
1 (function () {
2   function FOSSGISValhallaEngine(id, costing) {
3     var 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     return {
46       id: id,
47       creditline:
48       "<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
49       draggable: false,
50
51       getRoute: function (points, callback) {
52         return $.ajax({
53           url: OSM.FOSSGIS_VALHALLA_URL,
54           data: {
55             json: JSON.stringify({
56               locations: points.map(function (p) {
57                 return { lat: p.lat, lon: p.lng, radius: 5 };
58               }),
59               costing: costing,
60               directions_options: {
61                 units: "km",
62                 language: I18n.currentLocale()
63               }
64             })
65           },
66           dataType: "json",
67           success: function (data) {
68             var trip = data.trip;
69
70             if (trip.status === 0) {
71               var line = [];
72               var steps = [];
73               var distance = 0;
74               var time = 0;
75
76               trip.legs.forEach(function (leg) {
77                 var legLine = L.PolylineUtil.decode(leg.shape, {
78                   precision: 6
79                 });
80
81                 line = line.concat(legLine);
82
83                 leg.maneuvers.forEach(function (manoeuvre, idx) {
84                   var point = legLine[manoeuvre.begin_shape_index];
85
86                   steps.push([
87                     { lat: point[0], lng: point[1] },
88                     INSTR_MAP[manoeuvre.type],
89                     "<b>" + (idx + 1) + ".</b> " + manoeuvre.instruction,
90                     manoeuvre.length * 1000,
91                     []
92                   ]);
93                 });
94
95                 distance = distance + leg.summary.length;
96                 time = time + leg.summary.time;
97               });
98
99               callback(false, {
100                 line: line,
101                 steps: steps,
102                 distance: distance * 1000,
103                 time: time
104               });
105             } else {
106               callback(true);
107             }
108           },
109           error: function () {
110             callback(true);
111           }
112         });
113       }
114     };
115   }
116
117   OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_car", "auto"), true);
118   OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_bicycle", "bicycle"), true);
119   OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_foot", "pedestrian"), true);
120 }());