diff client/src/lib/geo.js @ 2264:627bfcbbf631

client: Draw fairway dimensions for all LoS and in more "cut cases".
author Raimund Renkert <raimund.renkert@intevation.de>
date Thu, 14 Feb 2019 13:53:54 +0100
parents ca33ad696594
children 940ae7c20326
line wrap: on
line diff
--- a/client/src/lib/geo.js	Thu Feb 14 12:21:25 2019 +0100
+++ b/client/src/lib/geo.js	Thu Feb 14 13:53:54 2019 +0100
@@ -26,7 +26,11 @@
   lineString as turfLineString,
   polygon as turfPolygon
 } from "@turf/helpers";
+import lineSplit from "@turf/line-split";
+import lineSlice from "@turf/line-slice";
 import lineIntersect from "@turf/line-intersect";
+import booleanWithin from "@turf/boolean-within";
+import booleanContains from "@turf/boolean-contains";
 
 const EARTHRADIUS = 6378137.0;
 
@@ -184,24 +188,58 @@
   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.");
+
+  var startPoint, endPoint
+
+  if (intersects.features.length == 2) {
+    let line1 = lineSlice(intersects.features[0].geometry, intersects.features[1].geometry, line);
+    startPoint = line1.geometry.coordinates[0];
+    endPoint = line1.geometry.coordinates[1];
+  } else if (booleanWithin(line, polygon)) {
+    startPoint = line.geometry.coordinates[0];
+    endPoint = line.geometry.coordinates[1];
   } 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
-      ]);
+    let split = lineSplit(line, polygon);
+    for (let feature of split.features) {
+      if (booleanWithin(feature, polygon)){
+        startPoint = feature.geometry.coordinates[0];
+        endPoint = feature.geometry.coordinates[1];
+      }
+      if (booleanContains(polygon, feature)){
+        startPoint = feature.geometry.coordinates[0];
+        endPoint = feature.geometry.coordinates[1];
+      }
     }
   }
+
+  let opts = { units: "kilometers" };
+  let pStartPoint = profileLine.getCoordinates()[0];
+  fairwayCoordinates.push([
+    distance(pStartPoint, startPoint, opts) * 1000,
+    distance(pStartPoint, endPoint, opts) * 1000,
+    depth
+  ]);
+
   return fairwayCoordinates;
 };
 
-export { generateFeatureRequest, prepareProfile, calculateFairwayCoordinates };
+const featureToFairwayCoordinates = (feature, profileLine) => {
+  // need to use EPSG:3857 which is the proj of vectorSource
+  var intersectingPolygon = feature
+    .getGeometry()
+    .clone()
+    .transform("EPSG:3857", "EPSG:4326");
+  const fairwayCoordinates = calculateFairwayCoordinates(
+    profileLine,
+    intersectingPolygon,
+    feature.get("min_depth") ? feature.get("min_depth") / 100 : 2.5
+  );
+  return fairwayCoordinates;
+};
+
+export {
+  generateFeatureRequest,
+  prepareProfile,
+  calculateFairwayCoordinates,
+  featureToFairwayCoordinates
+};