changeset 1165:a44ac5193b38

fixed code duplication moved logic to create cross profile into fairway store
author Markus Kottlaender <markus@intevation.de>
date Wed, 14 Nov 2018 11:14:58 +0100
parents dc99decbf9e3
children 0cddb3b24d24
files client/src/drawtool/Drawtool.vue client/src/fairway/Fairwayprofile.vue client/src/store/fairway.js
diffstat 3 files changed, 68 insertions(+), 135 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/drawtool/Drawtool.vue	Wed Nov 14 09:03:20 2018 +0100
+++ b/client/src/drawtool/Drawtool.vue	Wed Nov 14 11:14:58 2018 +0100
@@ -45,12 +45,7 @@
  */
 import { mapState, mapGetters } from "vuex";
 import { getLength, getArea } from "ol/sphere.js";
-import LineString from "ol/geom/LineString.js";
 import Draw from "ol/interaction/Draw.js";
-import { displayError } from "../application/lib/errors.js";
-import { calculateFairwayCoordinates } from "../application/lib/geo.js";
-
-const DEMODATA = 2.5;
 
 export default {
   name: "drawtool",
@@ -166,67 +161,7 @@
       this.$store.commit("map/cutTool", null);
     },
     cutEnd(event) {
-      const length = getLength(event.feature.getGeometry());
-      this.$store.commit("map/setCurrentMeasurement", {
-        quantity: "Length",
-        unitSymbol: "m",
-        value: Math.round(length * 10) / 10
-      });
-
-      // if a survey has been selected, request a profile
-      // TODO an improvement could be to check if the line intersects
-      // with the bottleneck area's polygon before trying the server request
-      if (this.selectedSurvey) {
-        this.$store.commit("fairwayprofile/clearCurrentProfile");
-        console.log("requesting profile for", this.selectedSurvey);
-        const inputLineString = event.feature.getGeometry().clone();
-        inputLineString.transform("EPSG:3857", "EPSG:4326");
-        const [start, end] = inputLineString
-          .getCoordinates()
-          .map(coords => coords.map(coord => parseFloat(coord.toFixed(8))));
-        this.$store.commit("fairwayprofile/setStartPoint", start);
-        this.$store.commit("fairwayprofile/setEndPoint", end);
-        const profileLine = new LineString([start, end]);
-        this.$store
-          .dispatch("fairwayprofile/loadProfile", this.selectedSurvey)
-          .then(() => {
-            this.getLayerByName("Fairway Dimensions")
-              .data.getSource()
-              .forEachFeatureIntersectingExtent(
-                // need to use EPSG:3857 which is the proj of vectorSource
-                profileLine
-                  .clone()
-                  .transform("EPSG:4326", "EPSG:3857")
-                  .getExtent(),
-                feature => {
-                  // transform back to prepare for usage
-                  var intersectingPolygon = feature
-                    .getGeometry()
-                    .clone()
-                    .transform("EPSG:3857", "EPSG:4326");
-                  const fairwayCoordinates = calculateFairwayCoordinates(
-                    profileLine,
-                    intersectingPolygon,
-                    DEMODATA
-                  );
-                  this.$store.commit(
-                    "fairwayprofile/setFairwayCoordinates",
-                    fairwayCoordinates
-                  );
-                }
-              );
-          })
-          .then(() => {
-            this.$store.commit("application/showSplitscreen", true);
-          })
-          .catch(error => {
-            const { status, data } = error.response;
-            displayError({
-              title: "Backend Error",
-              message: `${status}: ${data.message || data}`
-            });
-          });
-      }
+      this.$store.dispatch("fairwayprofile/cut", event.feature);
     }
   },
   mounted() {
--- a/client/src/fairway/Fairwayprofile.vue	Wed Nov 14 09:03:20 2018 +0100
+++ b/client/src/fairway/Fairwayprofile.vue	Wed Nov 14 11:14:58 2018 +0100
@@ -126,11 +126,8 @@
 import { displayError, displayInfo } from "../application/lib/errors.js";
 import Feature from "ol/Feature";
 import LineString from "ol/geom/LineString";
-import { getLength } from "ol/sphere.js";
-import { calculateFairwayCoordinates } from "../application/lib/geo.js";
 
 const GROUND_COLOR = "#4A2F06";
-const DEMODATA = 2.5;
 
 export default {
   name: "fairwayprofile",
@@ -497,74 +494,9 @@
           cutLayer.data.getSource().addFeature(cut);
 
           // draw diagram
-          this.loadProfile(cut);
+          this.$store.dispatch("fairwayprofile/cut", cut);
         }
       }
-    },
-    loadProfile(cut) {
-      // TODO: find central place for this logic, duplicated code from Drawtool.vue
-      const length = getLength(cut.getGeometry());
-      this.$store.commit("map/setCurrentMeasurement", {
-        quantity: "Length",
-        unitSymbol: "m",
-        value: Math.round(length * 10) / 10
-      });
-
-      // if a survey has been selected, request a profile
-      // TODO an improvement could be to check if the line intersects
-      // with the bottleneck area's polygon before trying the server request
-      if (this.selectedSurvey) {
-        this.$store.commit("fairwayprofile/clearCurrentProfile");
-        console.log("requesting profile for", this.selectedSurvey);
-        const inputLineString = cut.getGeometry().clone();
-        inputLineString.transform("EPSG:3857", "EPSG:4326");
-        const [start, end] = inputLineString
-          .getCoordinates()
-          .map(coords => coords.map(coord => parseFloat(coord.toFixed(8))));
-        this.$store.commit("fairwayprofile/setStartPoint", start);
-        this.$store.commit("fairwayprofile/setEndPoint", end);
-        const profileLine = new LineString([start, end]);
-        this.$store
-          .dispatch("fairwayprofile/loadProfile", this.selectedSurvey)
-          .then(() => {
-            this.getLayerByName("Fairway Dimensions")
-              .data.getSource()
-              .forEachFeatureIntersectingExtent(
-                // need to use EPSG:3857 which is the proj of vectorSource
-                profileLine
-                  .clone()
-                  .transform("EPSG:4326", "EPSG:3857")
-                  .getExtent(),
-                feature => {
-                  // transform back to prepare for usage
-                  var intersectingPolygon = feature
-                    .getGeometry()
-                    .clone()
-                    .transform("EPSG:3857", "EPSG:4326");
-                  const fairwayCoordinates = calculateFairwayCoordinates(
-                    profileLine,
-                    intersectingPolygon,
-                    DEMODATA
-                  );
-                  this.$store.commit(
-                    "fairwayprofile/setFairwayCoordinates",
-                    fairwayCoordinates
-                  );
-                }
-              );
-          })
-          .then(() => {
-            this.$store.commit("application/showSplitscreen", true);
-          })
-          .catch(error => {
-            console.log(error);
-            const { status, data } = error.response;
-            displayError({
-              title: "Backend Error",
-              message: `${status}: ${data.message || data}`
-            });
-          });
-      }
     }
   },
   mounted() {
--- a/client/src/store/fairway.js	Wed Nov 14 09:03:20 2018 +0100
+++ b/client/src/store/fairway.js	Wed Nov 14 11:14:58 2018 +0100
@@ -18,8 +18,12 @@
 import { prepareProfile } from "../application/lib/geo";
 import LineString from "ol/geom/LineString.js";
 import { generateFeatureRequest } from "../application/lib/geo.js";
+import { getLength } from "ol/sphere.js";
+import { calculateFairwayCoordinates } from "../application/lib/geo.js";
+import { displayError } from "../application/lib/errors.js";
 
 const DEMOLEVEL = 149.345;
+const DEMODATA = 2.5;
 
 export default {
   namespaced: true,
@@ -125,6 +129,68 @@
             reject(error);
           });
       });
+    },
+    cut({ commit, dispatch, rootState, rootGetters }, cut) {
+      const length = getLength(cut.getGeometry());
+      commit(
+        "map/setCurrentMeasurement",
+        {
+          quantity: "Length",
+          unitSymbol: "m",
+          value: Math.round(length * 10) / 10
+        },
+        { root: true }
+      );
+
+      // if a survey has been selected, request a profile
+      // TODO an improvement could be to check if the line intersects
+      // with the bottleneck area's polygon before trying the server request
+      if (rootState.bottlenecks.selectedSurvey) {
+        commit("clearCurrentProfile");
+        const inputLineString = cut.getGeometry().clone();
+        inputLineString.transform("EPSG:3857", "EPSG:4326");
+        const [start, end] = inputLineString
+          .getCoordinates()
+          .map(coords => coords.map(coord => parseFloat(coord.toFixed(8))));
+        commit("setStartPoint", start);
+        commit("setEndPoint", end);
+        const profileLine = new LineString([start, end]);
+        dispatch("loadProfile", rootState.bottlenecks.selectedSurvey)
+          .then(() => {
+            rootGetters["map/getLayerByName"]("Fairway Dimensions")
+              .data.getSource()
+              .forEachFeatureIntersectingExtent(
+                // need to use EPSG:3857 which is the proj of vectorSource
+                profileLine
+                  .clone()
+                  .transform("EPSG:4326", "EPSG:3857")
+                  .getExtent(),
+                feature => {
+                  // transform back to prepare for usage
+                  var intersectingPolygon = feature
+                    .getGeometry()
+                    .clone()
+                    .transform("EPSG:3857", "EPSG:4326");
+                  const fairwayCoordinates = calculateFairwayCoordinates(
+                    profileLine,
+                    intersectingPolygon,
+                    DEMODATA
+                  );
+                  commit("setFairwayCoordinates", fairwayCoordinates);
+                }
+              );
+          })
+          .then(() => {
+            commit("application/showSplitscreen", true, { root: true });
+          })
+          .catch(error => {
+            const { status, data } = error.response;
+            displayError({
+              title: "Backend Error",
+              message: `${status}: ${data.message || data}`
+            });
+          });
+      }
     }
   }
 };