changeset 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 bf10a7f990cc
files client/src/application/lib/geo.js client/src/map/Maplayer.vue
diffstat 2 files changed, 36 insertions(+), 28 deletions(-) [+]
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 };
--- a/client/src/map/Maplayer.vue	Wed Oct 24 14:42:04 2018 +0200
+++ b/client/src/map/Maplayer.vue	Wed Oct 24 14:50:25 2018 +0200
@@ -58,6 +58,7 @@
 } from "@turf/helpers";
 import lineIntersect from "@turf/line-intersect";
 import { displayError } from "../application/lib/errors.js";
+import { calculateFairwayCoordinates } from "../application/lib/geo.js";
 
 const DEMODATA = 2.5;
 
@@ -207,7 +208,7 @@
           .getGeometry()
           .clone()
           .transform("EPSG:3857", "EPSG:4326");
-        const fairwayCoordinates = this.calculateFairwayCoordinates(
+        const fairwayCoordinates = calculateFairwayCoordinates(
           profileLine,
           intersectingPolygon,
           DEMODATA
@@ -223,32 +224,6 @@
         featureCallback
       );
     },
-    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;
-    },
     activateInteraction() {
       const interaction = this.createInteraction(this.drawMode);
       this.interaction = interaction;