]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/fossgis_valhalla.js
consistent naming of FOSSGIS routers
[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     11, // kRoundaboutExit = 27;
31     17, // kFerryEnter = 28;
32     0, // kFerryExit = 29;
33     ...Array(7).fill(), // irrelevant transit maneuvers
34     21, // kMergeRight = 37;
35     20 // kMergeLeft = 38;
36   ];
37
38   return {
39     id: id,
40     creditline:
41       "<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
42     draggable: false,
43
44     getRoute: function (points, callback) {
45       return $.ajax({
46         url: OSM.FOSSGIS_VALHALLA_URL,
47         data: {
48           json: JSON.stringify({
49             locations: points.map(function (p) {
50               return { lat: p.lat, lon: p.lng };
51             }),
52             costing: costing,
53             directions_options: {
54               units: "km",
55               language: I18n.currentLocale()
56             }
57           })
58         },
59         dataType: "json",
60         success: function (data) {
61           var trip = data.trip;
62
63           if (trip.status === 0) {
64             var line = [];
65             var steps = [];
66             var distance = 0;
67             var time = 0;
68
69             trip.legs.forEach(function (leg) {
70               var legLine = L.PolylineUtil.decode(leg.shape, {
71                 precision: 6
72               });
73
74               line = line.concat(legLine);
75
76               leg.maneuvers.forEach(function (manoeuvre, idx) {
77                 var point = legLine[manoeuvre.begin_shape_index];
78
79                 steps.push([
80                   { lat: point[0], lng: point[1] },
81                   INSTR_MAP[manoeuvre.type],
82                   "<b>" + (idx + 1) + ".</b> " + manoeuvre.instruction,
83                   manoeuvre.length * 1000,
84                   []
85                 ]);
86               });
87
88               distance = distance + leg.summary.length;
89               time = time + leg.summary.time;
90             });
91
92             callback(false, {
93               line: line,
94               steps: steps,
95               distance: distance * 1000,
96               time: time
97             });
98           } else {
99             callback(true);
100           }
101         },
102         error: function () {
103           callback(true);
104         }
105       });
106     }
107   };
108 }
109
110   OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_car", "auto"), true);
111   OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_bicycle", "bicycle"), true);
112   OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_foot", "pedestrian"), true);