diff client/src/application/lib/geo.js @ 1029:1fdeb9404afb

refac: extract calculation of fairway coordinates
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 24 Oct 2018 14:50:25 +0200
parents dd67e46366ef
children
line wrap: on
line diff
--- a/client/src/application/lib/geo.js	Wed Oct 24 14:42:04 2018 +0200
+++ b/client/src/application/lib/geo.js	Wed Oct 24 14:50:25 2018 +0200
@@ -22,6 +22,12 @@
 
 import { GeoJSON } from "ol/format.js";
 import Feature from "ol/Feature";
+import distance from "@turf/distance";
+import {
+  lineString as turfLineString,
+  polygon as turfPolygon
+} from "@turf/helpers";
+import lineIntersect from "@turf/line-intersect";
 
 const EARTHRADIUS = 6378137.0;
 
@@ -172,4 +178,31 @@
   return new GeoJSON({ geometryName: "geometry" }).writeFeature(feature);
 };
 
-export { generateFeatureRequest, prepareProfile };
+const calculateFairwayCoordinates = (profileLine, fairwayGeometry, depth) => {
+  // both geometries have to be in EPSG:4326
+  // uses turfjs distance() function
+  let fairwayCoordinates = [];
+  var line = turfLineString(profileLine.getCoordinates());
+  var polygon = turfPolygon(fairwayGeometry.getCoordinates());
+  var intersects = lineIntersect(line, polygon);
+  var l = intersects.features.length;
+  if (l % 2 != 0) {
+    console.log("Ignoring fairway because profile only intersects once.");
+  } else {
+    for (let i = 0; i < l; i += 2) {
+      let pStartPoint = profileLine.getCoordinates()[0];
+      let fStartPoint = intersects.features[i].geometry.coordinates;
+      let fEndPoint = intersects.features[i + 1].geometry.coordinates;
+      let opts = { units: "kilometers" };
+
+      fairwayCoordinates.push([
+        distance(pStartPoint, fStartPoint, opts) * 1000,
+        distance(pStartPoint, fEndPoint, opts) * 1000,
+        depth
+      ]);
+    }
+  }
+  return fairwayCoordinates;
+};
+
+export { generateFeatureRequest, prepareProfile, calculateFairwayCoordinates };