-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.js
137 lines (118 loc) · 4.36 KB
/
route.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import log from 'loglevel';
import * as geometry from 'spherical-geometry-js';
// segment a line into a list segments with heading and distance
function segment(coordinates) {
let segments = []
let fromPos = geometry.convertLatLng(coordinates[0])
for (let i = 1; i < coordinates.length; i++) {
let toPos = geometry.convertLatLng(coordinates[i])
let heading = geometry.computeHeading(fromPos, toPos)
let distance = geometry.computeDistanceBetween(fromPos, toPos)
segments.push({ fromPos, heading, distance, toPos })
fromPos = toPos
}
return segments
}
function deduplicateCoordinates(coordinates) {
let i = 0, j = 1
let result = [coordinates[i]]
while (j < coordinates.length) {
if ((result[i][0] != coordinates[j][0]) || (result[i][1] != coordinates[j][1])) {
result.push(coordinates[j])
i++
}
j++
}
return result
}
function buildRoundTripSegments(coordinates, roadWidth, keepTo) {
const turningAngle = keepTo === "Right" ? -90 : 90
let segments = segment(coordinates)
let seg1, seg2;
for (let curIdx = segments.length - 1; curIdx >= -1; curIdx--) {
if (curIdx == segments.length - 1) seg1 = { // first turnover
fromPos: segments[curIdx].toPos,
toPos: geometry.computeOffset(segments[curIdx].toPos, roadWidth, segments[curIdx].heading + turningAngle),
heading: segments[curIdx].heading + turningAngle,
distance: roadWidth,
}
if (curIdx >= 0) seg2 = {
fromPos: geometry.computeOffset(segments[curIdx].toPos, roadWidth, segments[curIdx].heading + turningAngle),
toPos: geometry.computeOffset(segments[curIdx].fromPos, roadWidth, segments[curIdx].heading + turningAngle),
heading: segments[curIdx].heading - 180,
distance: segments[curIdx].distance,
}
else seg2 = { // last turnover
fromPos: seg1.toPos, toPos: segments[0].fromPos,
heading: seg1.heading + turningAngle,
distance: roadWidth,
}
const intPos = intersectionPos(seg1, seg2)
if (intPos) {
segments.push({
fromPos: seg1.fromPos, toPos: intPos, heading: seg1.heading,
distance: geometry.computeDistanceBetween(seg1.fromPos, intPos)
})
seg1 = {
fromPos: intPos, toPos: seg2.toPos, heading: seg2.heading,
distance: geometry.computeDistanceBetween(intPos, seg2.toPos)
}
} else { // no intersection position
segments.push(seg1)
segments.push({
fromPos: seg1.toPos, toPos: seg2.fromPos,
heading: geometry.computeHeading(seg1.toPos, seg2.fromPos),
distance: geometry.computeDistanceBetween(seg1.toPos, seg2.fromPos)
})
seg1 = { ...seg2 }
}
if (curIdx === -1) {
segments.push(seg1)
}
}
return segments
}
// based on http://paulbourke.net/geometry/pointlineplane/javascript.txt
function intersectionPos(seg1, seg2) {
if (seg1.toPos === seg2.fromPos) return seg1.toPos
const x1 = seg1.fromPos.lng(), x2 = seg1.toPos.lng()
const x3 = seg2.fromPos.lng(), x4 = seg2.toPos.lng()
const y1 = seg1.fromPos.lat(), y2 = seg1.toPos.lat()
const y3 = seg2.fromPos.lat(), y4 = seg2.toPos.lat()
// Check if none of the lines are of length 0
if ((x1 === x2 && y1 === y2) || (x3 === x4 && y3 === y4)) {
return false
}
const denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))
// Lines are parallel
if (denominator === 0) {
return false
}
const ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator
const ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator
// is the intersection along the segments
if (ua < 0 || ua > 1 || ub < 0 || ub > 1) {
return false
}
// Return a object with the x and y coordinates of the intersection
const x = x1 + ua * (x2 - x1)
const y = y1 + ua * (y2 - y1)
return geometry.convertLatLng({ x, y })
}
function route2segments(route) {
if (!route.type) route.type = "RoundTripLoop"
if (!route.roadWidth) route.roadWidth = 20
if (!route.keepTo) route.keepTo = "Right"
let coords = deduplicateCoordinates(route.coordinates)
if (route.type === "RoundTripLoop")
route["segments"] = buildRoundTripSegments(coords, route.roadWidth, route.keepTo)
else if (route.type === "OneWayLoop") {
coords.push(coords[0])
route["segments"] = segment(coords)
}
else {
log.error(`Invalid type "${route.type}" of route "${route.id}"`)
process.exit(1)
}
}
export { route2segments }