view client/src/fairway/store.js @ 1063:7ec2133c6404

client: add area measurement. simpify code * Add a third draw mode which can only be activated when no morphology is selected and we are already in LineString mode. It adds an area calculation. Because the Polygon drawMode ends on a double click, there needs to be an extra callback for this to run identify so that the area calculation is shown all times. * Add Bernhard as author to some files and also simplify copyright note. * Remove DRAWMODES in the code to simplify as this is just one indirection used once in stores/application.js. * Use mapState instead mapGetters to get the drawMode at all places to save some code lines.
author Bernhard Reiter <bernhard@intevation.de>
date Thu, 25 Oct 2018 23:16:53 +0200
parents f8a4ec146d47
children 28eb62f7c676
line wrap: on
line source

/*
 * This is Free Software under GNU Affero General Public License v >= 3.0
 * without warranty, see README.md and license for details.
 * 
 * SPDX-License-Identifier: AGPL-3.0-or-later
 * License-Filename: LICENSES/AGPL-3.0.txt
 * 
 * Copyright (C) 2018 by via donau 
 *   – Österreichische Wasserstraßen-Gesellschaft mbH
 * Software engineering by Intevation GmbH
 * 
 * Author(s):
 * Thomas Junk <thomas.junk@intevation.de>
 * Markus Kottländer <markuks.kottlaender@intevation.de>
 */
import Vue from "vue";
import { HTTP } from "../application/lib/http";
import { prepareProfile } from "../application/lib/geo";
import LineString from "ol/geom/LineString.js";
import { generateFeatureRequest } from "../application/lib/geo.js";

const DEMOLEVEL = 149.345;

const FairwayProfile = {
  namespaced: true,
  state: {
    availableSurveys: null,
    totalLength: 0,
    minAlt: 0,
    maxAlt: 0,
    currentProfile: {},
    waterLevels: [{ year: "2016", level: DEMOLEVEL, color: "#005DFF" }],
    selectedWaterLevel: DEMOLEVEL,
    fairwayCoordinates: [],
    startPoint: null,
    endPoint: null,
    selectedMorph: null
  },
  getters: {
    length: state => {
      return state.totalLength;
    }
  },
  mutations: {
    setSelectedMorph: (state, selectedMorph) => {
      state.selectedMorph = selectedMorph;
    },
    setAvailableSurveys: (state, surveys) => {
      state.availableSurveys = surveys;
    },
    setSelectedWaterLevel: (state, level) => {
      state.selectedWaterLevel = level;
    },
    profileLoaded: (state, answer) => {
      const { response, surveyDate } = answer;
      const { data } = response;
      const coordinates = data.geometry.coordinates;
      if (!coordinates) return;
      const startPoint = state.startPoint;
      const endPoint = state.endPoint;
      const geoJSON = data;
      const result = prepareProfile({ geoJSON, startPoint, endPoint });
      // Use Vue.set() to make new object properties rective
      // https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats
      Vue.set(state.currentProfile, surveyDate, result.points);
      state.minAlt = result.minAlt;
      state.maxAlt = result.maxAlt;
      state.totalLength = result.lengthPolyLine;
    },
    setStartPoint: (state, start) => {
      state.startPoint = start;
    },
    setEndPoint: (state, end) => {
      state.endPoint = end;
    },
    setFairwayCoordinates: (state, coordinates) => {
      state.fairwayCoordinates = coordinates;
    },
    clearCurrentProfile: state => {
      state.currentProfile = {};
    }
  },
  actions: {
    loadProfile({ commit, state }, date_info) {
      return new Promise((resolve, reject) => {
        const profileLine = new LineString([state.startPoint, state.endPoint]);
        const geoJSON = generateFeatureRequest(
          profileLine,
          state.selectedMorph.bottleneck_id,
          date_info
        );
        HTTP.post("/cross", geoJSON, {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            commit("profileLoaded", {
              response: response,
              surveyDate: date_info
            });
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    }
  }
};

export default FairwayProfile;