]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions/fossgis_valhalla.js
Label boundaries by regional official designations
[rails.git] / app / assets / javascripts / index / directions / fossgis_valhalla.js
index bc093fea47b642b2451b25fd0c3207eb74c2181b..493d0907208bcea6fd7406ac0d4ec79afb39aa70 100644 (file)
@@ -1,36 +1,36 @@
 (function () {
-  function FOSSGISValhallaEngine(id, costing) {
-    var INSTR_MAP = [
-      0, // kNone = 0;
-      8, // kStart = 1;
-      8, // kStartRight = 2;
-      8, // kStartLeft = 3;
-      14, // kDestination = 4;
-      14, // kDestinationRight = 5;
-      14, // kDestinationLeft = 6;
-      0, // kBecomes = 7;
-      0, // kContinue = 8;
-      1, // kSlightRight = 9;
-      2, // kRight = 10;
-      3, // kSharpRight = 11;
-      4, // kUturnRight = 12;
-      4, // kUturnLeft = 13;
-      7, // kSharpLeft = 14;
-      6, // kLeft = 15;
-      5, // kSlightLeft = 16;
-      0, // kRampStraight = 17;
-      24, // kRampRight = 18;
-      25, // kRampLeft = 19;
-      24, // kExitRight = 20;
-      25, // kExitLeft = 21;
-      0, // kStayStraight = 22;
-      1, // kStayRight = 23;
-      5, // kStayLeft = 24;
-      20, // kMerge = 25;
-      10, // kRoundaboutEnter = 26;
-      10, // kRoundaboutExit = 27;
-      17, // kFerryEnter = 28;
-      0, // kFerryExit = 29;
+  function FOSSGISValhallaEngine(modeId, costing) {
+    const INSTR_MAP = [
+      "straight", // kNone = 0;
+      "start", // kStart = 1;
+      "start", // kStartRight = 2;
+      "start", // kStartLeft = 3;
+      "destination", // kDestination = 4;
+      "destination", // kDestinationRight = 5;
+      "destination", // kDestinationLeft = 6;
+      "straight", // kBecomes = 7;
+      "straight", // kContinue = 8;
+      "slight-right", // kSlightRight = 9;
+      "right", // kRight = 10;
+      "sharp-right", // kSharpRight = 11;
+      "u-turn", // kUturnRight = 12;
+      "u-turn", // kUturnLeft = 13;
+      "sharp-left", // kSharpLeft = 14;
+      "left", // kLeft = 15;
+      "slight-left", // kSlightLeft = 16;
+      "straight", // kRampStraight = 17;
+      "exit-right", // kRampRight = 18;
+      "exit-left", // kRampLeft = 19;
+      "exit-right", // kExitRight = 20;
+      "exit-left", // kExitLeft = 21;
+      "straight", // kStayStraight = 22;
+      "slight-right", // kStayRight = 23;
+      "slight-left", // kStayLeft = 24;
+      "merge-left", // kMerge = 25;
+      "roundabout", // kRoundaboutEnter = 26;
+      "roundabout", // kRoundaboutExit = 27;
+      "ferry", // kFerryEnter = 28;
+      "straight", // kFerryExit = 29;
       null, // kTransit = 30;
       null, // kTransitTransfer = 31;
       null, // kTransitRemainOn = 32;
       null, // kTransitConnectionTransfer = 34;
       null, // kTransitConnectionDestination = 35;
       null, // kPostTransitConnectionDestination = 36;
-      21, // kMergeRight = 37;
-      20 // kMergeLeft = 38;
+      "merge-right", // kMergeRight = 37;
+      "merge-left" // kMergeLeft = 38;
     ];
 
-    return {
-      id: id,
-      creditline:
-      "<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
-      draggable: false,
+    function _processDirections(tripLegs) {
+      let line = [];
+      let steps = [];
+      let distance = 0;
+      let time = 0;
 
-      getRoute: function (points, callback) {
-        return $.ajax({
-          url: OSM.FOSSGIS_VALHALLA_URL,
-          data: {
-            json: JSON.stringify({
-              locations: points.map(function (p) {
-                return { lat: p.lat, lon: p.lng, radius: 5 };
-              }),
-              costing: costing,
-              directions_options: {
-                units: "km",
-                language: I18n.currentLocale()
-              }
-            })
-          },
-          dataType: "json",
-          success: function (data) {
-            var trip = data.trip;
-
-            if (trip.status === 0) {
-              var line = [];
-              var steps = [];
-              var distance = 0;
-              var time = 0;
-
-              trip.legs.forEach(function (leg) {
-                var legLine = L.PolylineUtil.decode(leg.shape, {
-                  precision: 6
-                });
+      for (const leg of tripLegs) {
+        const legLine = L.PolylineUtil.decode(leg.shape, {
+          precision: 6
+        });
 
-                line = line.concat(legLine);
+        const legSteps = leg.maneuvers.map(function (manoeuvre, idx) {
+          const num = `<b>${idx + 1}.</b> `;
+          const lineseg = legLine
+            .slice(manoeuvre.begin_shape_index, manoeuvre.end_shape_index + 1)
+            .map(([lat, lng]) => ({ lat, lng }));
+          return [
+            lineseg[0],
+            INSTR_MAP[manoeuvre.type],
+            num + manoeuvre.instruction,
+            manoeuvre.length * 1000,
+            lineseg
+          ];
+        });
 
-                leg.maneuvers.forEach(function (manoeuvre, idx) {
-                  var point = legLine[manoeuvre.begin_shape_index];
+        line = line.concat(legLine);
+        steps = steps.concat(legSteps);
+        distance += leg.summary.length;
+        time += leg.summary.time;
+      }
 
-                  steps.push([
-                    { lat: point[0], lng: point[1] },
-                    INSTR_MAP[manoeuvre.type],
-                    "<b>" + (idx + 1) + ".</b> " + manoeuvre.instruction,
-                    manoeuvre.length * 1000,
-                    []
-                  ]);
-                });
+      return {
+        line: line,
+        steps: steps,
+        distance: distance * 1000,
+        time: time
+      };
+    }
 
-                distance = distance + leg.summary.length;
-                time = time + leg.summary.time;
-              });
+    return {
+      mode: modeId,
+      provider: "fossgis_valhalla",
+      creditline:
+      "<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
+      draggable: false,
 
-              callback(false, {
-                line: line,
-                steps: steps,
-                distance: distance * 1000,
-                time: time
-              });
-            } else {
-              callback(true);
+      getRoute: function (points, signal) {
+        const query = new URLSearchParams({
+          json: JSON.stringify({
+            locations: points.map(function (p) {
+              return { lat: p.lat, lon: p.lng, radius: 5 };
+            }),
+            costing: costing,
+            directions_options: {
+              units: "km",
+              language: I18n.currentLocale()
             }
-          },
-          error: function () {
-            callback(true);
-          }
+          })
         });
+        return fetch(OSM.FOSSGIS_VALHALLA_URL + "?" + query, { signal })
+          .then(response => response.json())
+          .then(({ trip }) => {
+            if (trip.status !== 0) throw new Error();
+            return _processDirections(trip.legs);
+          });
       }
     };
   }
 
-  OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_car", "auto"), true);
-  OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_bicycle", "bicycle"), true);
-  OSM.Directions.addEngine(new FOSSGISValhallaEngine("fossgis_valhalla_foot", "pedestrian"), true);
+  OSM.Directions.addEngine(new FOSSGISValhallaEngine("car", "auto"), true);
+  OSM.Directions.addEngine(new FOSSGISValhallaEngine("bicycle", "bicycle"), true);
+  OSM.Directions.addEngine(new FOSSGISValhallaEngine("foot", "pedestrian"), true);
 }());