-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
212 lines (200 loc) · 8.82 KB
/
utils.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
LINE_NAMES,
LINE_PROPERTIES,
} from './common/constants/lines';
import {
point,
lineString,
nearestPointOnLine,
nearestPoint,
featureCollection,
pointToLineDistance,
} from '@turf/turf';
import proj4 from 'proj4';
// WMATA train coordinates are in Longitude/Latitude, WGS84. Fuck that noise tho.
const WMATA_PROJ = new proj4.Proj('EPSG:4326');
// Default Leaflet coordinates are in meters, global spherical mercators projection.
const LEAFLET_PROJ = new proj4.Proj('EPSG:3785');
const calcAngleDegrees = (p1, p2) => {
return Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI;
}
const mergeLines = (railLines, merges) => {
let dominantLine = null;
let subordinateLine = null;
let subordinateStart = null;
let subordinateEnd = null;
let dominateMiddle = null;
let mergeCoords = null;
merges.forEach(({
dominant,
subordinate,
dominantRange,
subordinateRange,
reverseDominant,
}) => {
dominantLine = railLines['features'].find(({properties: {NAME}}) => NAME === dominant);
subordinateLine = railLines['features'].find(({properties: {NAME}}) => NAME === subordinate);
subordinateStart = subordinateLine.geometry.coordinates.slice(0, subordinateRange[0]);
subordinateEnd = subordinateLine.geometry.coordinates.slice(
subordinateRange[1],
subordinateLine.geometry.coordinates.length - 1
);
dominateMiddle = dominantLine.geometry.coordinates.slice(dominantRange[0], dominantRange[1]);
if (reverseDominant) {
dominateMiddle = dominateMiddle.reverse();
}
mergeCoords = subordinateStart.concat(dominateMiddle, subordinateEnd);
subordinateLine.geometry.coordinates = mergeCoords;
})
};
// Alter the coords of the stations points to be exactly on a railLine
const snapStations = (railLines, stations) => {
const snappedStations = [];
LINE_NAMES.forEach(name => {
const {geometry: {coordinates}} = railLines.features.find(({properties: {NAME}}) => NAME === name);
const line = lineString(coordinates);
stations.filter(({LineCode1}) => LineCode1 === LINE_PROPERTIES[name]['code']).forEach(({Lat, Lon, ...rest}) => {
const nearestOnLine = nearestPointOnLine(line, point([Lon, Lat]));
snappedStations.push({
Lat: nearestOnLine.geometry.coordinates[1],
Lon: nearestOnLine.geometry.coordinates[0],
...rest,
})
});
});
return snappedStations;
}
// This ensures at animating the CSS rotation transform will spin the train
// over the shortest radius.
const normalizeAngleDiff = (a1, a2) => {
let diff = a2 - a1;
while (diff < -180) {
diff += 360;
}
while (diff > 180) {
diff -=360;
}
return diff;
};
const snapTrains = (railLines, nextTrains, currentTrains, potentiallyClearedTrainITTMap) => {
const normalizedTrains = [];
const snappedTrains = [];
const currentTrainITTList = currentTrains ? currentTrains.map(({properties: {ITT}}) => ITT) : [];
nextTrains.features.forEach(({geometry, attributes}) => {
const { TRACKLINE } = attributes;
const { x, y } = geometry;
const values = LINE_NAMES.map(l => LINE_PROPERTIES[l]);
const lineProperties = values.find(({ trackLineID }) => trackLineID === TRACKLINE);
const transformedGeometry = proj4.transform(LEAFLET_PROJ, WMATA_PROJ, [x, y]);
const trainGeojson = {
type: "Feature",
geometry: {
type: "Point",
coordinates: [transformedGeometry.y, transformedGeometry.x]
},
properties: {
...attributes
}
}
normalizedTrains.push(trainGeojson)
});
// Trains can flicker in and out if sensors derp. This is to buffer for that
// so that there isn't annoying flickering of trains on the front-end UI.
const nextTrainITTList = normalizedTrains.map(({properties: {ITT}}) => ITT);
currentTrainITTList.forEach(ITT => {
if (!nextTrainITTList.includes(ITT)) {
const pcITT = potentiallyClearedTrainITTMap[ITT];
if (typeof pcITT !== 'undefined') {
// A train gets three refreshes to prove it still exists
if (pcITT < 3) {
potentiallyClearedTrainITTMap[ITT] = pcITT + 1;
} else {
delete potentiallyClearedTrainITTMap[ITT];
}
} else {
potentiallyClearedTrainITTMap[ITT] = 1;
}
} else {
delete potentiallyClearedTrainITTMap[ITT];
}
})
const potentiallyClearedTrains = currentTrains.filter(currTrain => {
return Object.keys(potentiallyClearedTrainITTMap).includes(currTrain['properties']['ITT'])
});
LINE_NAMES.forEach(name => {
const {geometry: {coordinates}} = railLines.features.find(({properties: {NAME}}) => NAME === name);
const line = lineString(coordinates);
const lineFeatureCollection = featureCollection(coordinates.map(([Lat, Lon]) => point([Lat, Lon])));
normalizedTrains
.concat(potentiallyClearedTrains)
.filter(({properties: {TRACKLINE}}) => TRACKLINE === LINE_PROPERTIES[name]['trackLineID'])
.forEach(train => {
let [Lat, Lon] = train['geometry']['coordinates'];
const currentTrainInstance = currentTrains.find(({properties: {ITT}}) => ITT === train['properties']['ITT']);
// For some reason the API sometimes puts trains in the middle of the ocean :/
// When this happens don't move the train. Leave it at it's last position.
// If a train starts in the middle of the ocean then oh well.
if (Lat === 0 && Lon === 0 && currentTrainITTList.includes(train['properties']['ITT'])){
[Lat, Lon] = currentTrainInstance['geometry']['coordinates'];
}
// Get the nearest point on the railLine
const nearestOnLine = nearestPointOnLine(line, point([Lon, Lat]));
const nearestOnLineCoords = nearestOnLine.geometry.coordinates;
// Get the nearest point that exists within the set of coordinates that define the railLine
const nearest = nearestPoint(point([Lon, Lat]), lineFeatureCollection);
const {properties: {featureIndex}} = nearest;
const closestLineSegmentCandidates = [];
if (featureIndex > 0) {
closestLineSegmentCandidates.push({
indices: [featureIndex - 1, featureIndex],
l: lineString([coordinates[featureIndex - 1], coordinates[featureIndex]])
});
}
if (featureIndex < coordinates.length - 1) {
closestLineSegmentCandidates.push({
indices: [featureIndex, featureIndex + 1],
l: lineString([coordinates[featureIndex], coordinates[featureIndex + 1]])
});
}
let smallestDistance = Infinity;
let smallestIndex = 0;
closestLineSegmentCandidates.forEach(({indices, l}, index) => {
const dist = pointToLineDistance(point([Lon, Lat]), l);
if (dist < smallestDistance) {
smallestDistance = dist;
smallestIndex = index;
}
})
const closestLineSegment = closestLineSegmentCandidates[smallestIndex];
const snappedTrain = {...train};
snappedTrain['properties']['closestLineSegment'] = closestLineSegment;
snappedTrain['geometry']['coordinates'] = [
nearestOnLineCoords[1],
nearestOnLineCoords[0]
];
let nextRotation = calcAngleDegrees(
{
x: closestLineSegment.l.geometry.coordinates[0][1],
y: closestLineSegment.l.geometry.coordinates[0][0],
},
{
x: closestLineSegment.l.geometry.coordinates[1][1],
y: closestLineSegment.l.geometry.coordinates[1][0],
}
);
if (currentTrainInstance) {
const currentRotation = currentTrainInstance['properties']['rotationAngle'];
nextRotation = currentRotation - normalizeAngleDiff(nextRotation, currentRotation);
}
snappedTrain['properties']['rotationAngle'] = nextRotation;
snappedTrains.push(snappedTrain);
});
});
return snappedTrains;
}
export {
mergeLines,
snapStations,
snapTrains,
calcAngleDegrees,
};