# HG changeset patch # User Thomas Junk # Date 1540385425 -7200 # Node ID 1fdeb9404afb72a0ca7250f3e6d372167f172724 # Parent dd67e46366ef1df219532b2e99293cb9fe1a0e3d refac: extract calculation of fairway coordinates diff -r dd67e46366ef -r 1fdeb9404afb client/src/application/lib/geo.js --- 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 }; diff -r dd67e46366ef -r 1fdeb9404afb client/src/map/Maplayer.vue --- 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;