comparison client/src/application/lib/geo.js @ 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
comparison
equal deleted inserted replaced
1028:dd67e46366ef 1029:1fdeb9404afb
20 * 20 *
21 */ 21 */
22 22
23 import { GeoJSON } from "ol/format.js"; 23 import { GeoJSON } from "ol/format.js";
24 import Feature from "ol/Feature"; 24 import Feature from "ol/Feature";
25 import distance from "@turf/distance";
26 import {
27 lineString as turfLineString,
28 polygon as turfPolygon
29 } from "@turf/helpers";
30 import lineIntersect from "@turf/line-intersect";
25 31
26 const EARTHRADIUS = 6378137.0; 32 const EARTHRADIUS = 6378137.0;
27 33
28 /** 34 /**
29 * Converts to radiant 35 * Converts to radiant
170 date: date_info 176 date: date_info
171 }); 177 });
172 return new GeoJSON({ geometryName: "geometry" }).writeFeature(feature); 178 return new GeoJSON({ geometryName: "geometry" }).writeFeature(feature);
173 }; 179 };
174 180
175 export { generateFeatureRequest, prepareProfile }; 181 const calculateFairwayCoordinates = (profileLine, fairwayGeometry, depth) => {
182 // both geometries have to be in EPSG:4326
183 // uses turfjs distance() function
184 let fairwayCoordinates = [];
185 var line = turfLineString(profileLine.getCoordinates());
186 var polygon = turfPolygon(fairwayGeometry.getCoordinates());
187 var intersects = lineIntersect(line, polygon);
188 var l = intersects.features.length;
189 if (l % 2 != 0) {
190 console.log("Ignoring fairway because profile only intersects once.");
191 } else {
192 for (let i = 0; i < l; i += 2) {
193 let pStartPoint = profileLine.getCoordinates()[0];
194 let fStartPoint = intersects.features[i].geometry.coordinates;
195 let fEndPoint = intersects.features[i + 1].geometry.coordinates;
196 let opts = { units: "kilometers" };
197
198 fairwayCoordinates.push([
199 distance(pStartPoint, fStartPoint, opts) * 1000,
200 distance(pStartPoint, fEndPoint, opts) * 1000,
201 depth
202 ]);
203 }
204 }
205 return fairwayCoordinates;
206 };
207
208 export { generateFeatureRequest, prepareProfile, calculateFairwayCoordinates };