]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/fossgis_valhalla.js
Merge remote-tracking branch 'upstream/pull/4883'
[rails.git] / app / assets / javascripts / index / directions / fossgis_valhalla.js
1 function FOSSGISValhallaEngine(id, costing) {
2   var INSTR_MAP = [
3     0, // kNone = 0;
4     8, // kStart = 1;
5     8, // kStartRight = 2;
6     8, // kStartLeft = 3;
7     14, // kDestination = 4;
8     14, // kDestinationRight = 5;
9     14, // kDestinationLeft = 6;
10     0, // kBecomes = 7;
11     0, // kContinue = 8;
12     1, // kSlightRight = 9;
13     2, // kRight = 10;
14     3, // kSharpRight = 11;
15     4, // kUturnRight = 12;
16     4, // kUturnLeft = 13;
17     7, // kSharpLeft = 14;
18     6, // kLeft = 15;
19     5, // kSlightLeft = 16;
20     0, // kRampStraight = 17;
21     24, // kRampRight = 18;
22     25, // kRampLeft = 19;
23     24, // kExitRight = 20;
24     25, // kExitLeft = 21;
25     0, // kStayStraight = 22;
26     1, // kStayRight = 23;
27     5, // kStayLeft = 24;
28     20, // kMerge = 25;
29     10, // kRoundaboutEnter = 26;
30     10, // kRoundaboutExit = 27;
31     17, // kFerryEnter = 28;
32     0, // kFerryExit = 29;
33     null, // kTransit = 30;
34     null, // kTransitTransfer = 31;
35     null, // kTransitRemainOn = 32;
36     null, // kTransitConnectionStart = 33;
37     null, // kTransitConnectionTransfer = 34;
38     null, // kTransitConnectionDestination = 35;
39     null, // kPostTransitConnectionDestination = 36;
40     21, // kMergeRight = 37;
41     20 // kMergeLeft = 38;
42   ];
43
44   return {
45     id: id,
46     creditline:
47       "<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
48     draggable: false,
49
50     getRoute: function (points, callback) {
51       return $.ajax({
52         url: OSM.FOSSGIS_VALHALLA_URL,
53         data: {
54           json: JSON.stringify({
55             locations: points.map(function (p) {
56               return { lat: p.lat, lon: p.lng, radius: 5 };
57             }),
58             costing: costing,
59             directions_options: {
60               units: "km",
61               language: I18n.currentLocale()
62             }
63           })
64         },
65         dataType: "json",
66         success: function (data) {
67           var trip = data.trip;
68
69           if (trip.status === 0) {
70             var line = [];
71             var steps = [];
72             var distance = 0;
73             var time = 0;
74
75             trip.legs.forEach(function (leg) {
76               var legLine = L.PolylineUtil.decode(leg.shape, {
77                 precision: 6
78               });
79
80               line = line.concat(legLine);
81
82               leg.maneuvers.forEach(function (manoeuvre, idx) {
83                 var point = legLine[manoeuvre.begin_shape_index];
84
85                 steps.push([
86                   { lat: point[0], lng: point[1] },
87                   INSTR_MAP[manoeuvre.type],
88                   "<b>" + (idx + 1) + ".</b> " + manoeuvre.instruction,
89                   manoeuvre.length * 1000,
90                   []
91                 ]);
92               });
93
94               distance = distance + leg.summary.length;
95               time = time + leg.summary.time;
96             });
97
98             callback(false, {
99               line: line,
100               steps: steps,
101               distance: distance * 1000,
102               time: time
103             });
104           } else {
105             callback(true);
106           }
107         },
108         error: function () {
109           callback(true);
110         }
111       });
112     }
113   };
114 }
115
116 OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_car", "auto"), true);
117 OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_bicycle", "bicycle"), true);
118 OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_foot", "pedestrian"), true);