-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
67 lines (52 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const turfMeta = require('@turf/meta');
const turfHelpers = require('@turf/helpers');
const length = require('@turf/length').default;
const rhumbDistance = require('@turf/rhumb-distance').default;
const pointToLineDistance = require('@turf/point-to-line-distance').default;
const RouteFinder = require('geojson-path-finder');
const marnet = require('./data/marnet_densified.json');
const routefinder = new RouteFinder(marnet);
module.exports = function searoute(origin, destination, units = 'nm') {
try {
let snappedOrigin = snapToNetwork(origin),
snappedDestination = snapToNetwork(destination);
let route = routefinder.findPath(snappedOrigin, snappedDestination);
if (route == null) {
console.log("No route found");
return null;
}
let lineString = turfHelpers.lineString(route.path)
lineString.properties.units = units;
lineString.properties.length = units == 'nm'
? length(lineString, { units: 'miles' }) * 1.15078
: length(lineString, { units: units });
return lineString;
} catch (err) {
throw err;
}
}
function snapToNetwork(point) {
var nearestLineIndex = 0,
distance = 30000;
turfMeta.featureEach(marnet, function (feature, ftIndex) {
let dist = pointToLineDistance(point, feature, { units: 'kilometers' })
if (dist < distance) {
distance = dist;
nearestLineIndex = ftIndex;
}
});
var nearestVertexDist = null,
nearestCoord = null;
console.log(nearestLineIndex)
turfMeta.coordEach(marnet.features[nearestLineIndex], function (currentCoord) {
let distToVertex = rhumbDistance(point, currentCoord);
if (!nearestVertexDist) {
nearestVertexDist = distToVertex;
nearestCoord = currentCoord;
} else if (distToVertex < nearestVertexDist) {
nearestVertexDist = distToVertex;
nearestCoord = currentCoord;
}
});
return turfHelpers.point(nearestCoord);
}