forked from perliedman/geojson-path-finder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
weight-functions.js
60 lines (46 loc) · 1.33 KB
/
weight-functions.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
"use strict";
const point = require('turf-point');
const distance = require('@turf/distance').default;
module.exports = {
travelTimeWeightFn: travelTimeWeightFn,
};
function travelTimeWeightFn(a, b, props) {
const highwaySpeeds = {
motorway: 110,
trunk: 90,
primary: 80,
secondary: 70,
tertiary: 50,
unclassified: 50,
road: 50,
residential: 30,
service: 30,
living_street: 20
};
const unknowns = {};
let d = distance(point(a), point(b)) * 1000,
factor = 0.9,
type = props.highway,
forwardSpeed,
backwardSpeed;
if (props.maxspeed) {
forwardSpeed = backwardSpeed = Number(props.maxspeed);
} else {
let linkIndex = type.indexOf('_link');
if (linkIndex >= 0) {
type = type.substring(0, linkIndex);
factor *= 0.7;
}
forwardSpeed = backwardSpeed = highwaySpeeds[type] * factor;
if (!forwardSpeed) {
unknowns[type] = true;
}
}
if (props.oneway && props.oneway !== 'no' || props.junction && props.junction === 'roundabout') {
backwardSpeed = null;
}
return {
forward: forwardSpeed && (d / (forwardSpeed / 3.6)),
backward: backwardSpeed && (d / (backwardSpeed / 3.6)),
};
}