-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
340 lines (309 loc) · 10.2 KB
/
app.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
const fs = require('fs')
const turf = require('@turf/turf')
const mapboxgl = require('mapbox-gl')
const restclient = require('restler');
const moment = require('moment');
moment().format();
const apiKeyFile = require('./keys.js')
const airportCodes = JSON.parse(fs.readFileSync('data/airportCodes.json', 'utf-8'))
const airportData = JSON.parse(fs.readFileSync('data/airportData.json', 'utf-8'))
var FlightID
const fxml_url = 'http://flightxml.flightaware.com/json/FlightXML2/';
const username = 'themapsmith';
const apiKey = apiKeyFile.apiKey;
mapboxgl.accessToken = "pk.eyJ1IjoidGhlbWFwc21pdGgiLCJhIjoiYTdmMDdiZjYxNzNmNzFiOGVjZDJiYzI5MGQ5N2VlMmQifQ.fUnAp-76Ka0d3v4oMNPhFw";
const map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/satellite-v9",
center: [-96, 38],
zoom: 3
});
// page init
init()
function init() {
document.getElementById('submit').addEventListener('click', function() {
getSchedules()
})
document.getElementById('beginFlight').addEventListener('click', function() {
beginFlight()
})
}
function getSchedules() {
// set O/D and store in session storage
var origin = document.getElementById('origin').value
sessionStorage.setItem('origin', origin)
var dest = document.getElementById('dest').value
sessionStorage.setItem('dest', dest)
// for not making API calls in dev
if (origin == 'AUS' && dest == 'EWR') {
console.log('Reading schedule from local storage');
ausewr = JSON.parse(fs.readFileSync('storage/AUS-EWR-1522422120.json', 'utf-8'))
var randomRoute = Math.floor(Math.random() * 5)
var ident = ausewr.AirlineFlightSchedulesResult.data[randomRoute].ident
var departureTime = ausewr.AirlineFlightSchedulesResult.data[randomRoute].departuretime
getFlightID(ident, departureTime)
} else {
var startUnixSecond = moment().subtract(7, 'days').unix()
var endUnixSecond = moment().subtract(1, 'days').unix()
console.log('Fetching flights between ' + origin + ' and ' + dest);
// get flights between given airports
restclient.get(fxml_url + 'AirlineFlightSchedules', {
username: username,
password: apiKey,
query: {
startDate: startUnixSecond,
endDate: endUnixSecond,
origin: origin,
destination: dest,
howMany: 5,
offset: 0
}
}).on('success', function(result, response) {
if (result) {
var ident = result.AirlineFlightSchedulesResult.data[0].ident
var departureTime = result.AirlineFlightSchedulesResult.data[0].departuretime
var filename = 'storage/' + origin + '-' + dest + '-' + departureTime + '.json'
fs.writeFile(filename, JSON.stringify(result), getFlightID(ident, departureTime))
}
})
}
}
function getFlightID(ident, departureTime) {
var filename = 'storage/' + ident + '-' + departureTime + '.json'
if (fs.existsSync(filename)) {
console.log('Getting FlightID from local storage');
var GetFlightID = JSON.parse(fs.readFileSync(filename, 'utf-8'))
FlightID = GetFlightID.GetFlightIDResult
getLastTrack(FlightID)
} else {
console.log("getting ID");
restclient.get(fxml_url + 'GetFlightID', {
username: username,
password: apiKey,
query: {
ident: ident,
departureTime: departureTime
}
}).on('success', function(result, response) {
var FlightID = result.GetFlightIDResult
sessionStorage.setItem('FlightID', JSON.stringify(FlightID,null,2))
fs.writeFile(filename, JSON.stringify(result, null, 2), getLastTrack(FlightID))
})
}
}
function getLastTrack(FlightID) {
var filename = 'storage/' + FlightID + '.json'
if (fs.existsSync(filename)) {
console.log('Reading track from local storage');
var GetHistoricalTrackResult = JSON.parse(fs.readFileSync(filename, 'utf-8'))
var track = GetHistoricalTrackResult.GetHistoricalTrackResult.data
// addPolys(FlightID)
parseTrack(track)
} else {
console.log('getting track');
restclient.get(fxml_url + 'GetHistoricalTrack', {
username: username,
password: apiKey,
query: {
faFlightID: FlightID
}
}).on('success', function(result, response) {
var track = result.GetHistoricalTrackResult.data
fs.writeFile(filename, JSON.stringify(result, null, 2), addPolys(FlightID))
// TODO: Future: pass path to the path-polys function
// while in dev, just grab the polys and plop them in the map
})
.on('error', function(error) {
console.log(error);
})
}
}
function parseTrack(track) {
console.log('parsing track');
var trackInfo = []
var lineCoords = []
for (var i = 0; i <= track.length; i++) {
if (i == track.length) {
sessionStorage.setItem('trackInfo', JSON.stringify(trackInfo))
makePathFeature(trackInfo, lineCoords)
} else {
var each = {}
var pair = []
pair.push(Number(track[i].longitude))
pair.push(Number(track[i].latitude))
each.pair = pair
each.properties = {}
each.properties.altitude = track[i].altitude * 100
each.properties.timestamp = track[i].timestamp
each.properties.groundspeed = track[i].groundspeed
trackInfo.push(each)
lineCoords.push(pair)
}
}
}
function makePathFeature(trackInfo, lineCoords) {
// make simple line
var line = turf.lineString(lineCoords)
var rightLine = turf.lineOffset(line, 300000, {units: 'feet'})
rightLine.properties.name = 'left line'
var leftLine = turf.lineOffset(line, -300000, {units: 'feet'})
leftLine.properties.name = 'right line'
sessionStorage.setItem('flightPath', JSON.stringify(line))
sessionStorage.setItem('leftLine', JSON.stringify(leftLine))
sessionStorage.setItem('rightLine', JSON.stringify(rightLine))
// set necessary line attributes for rendering
var lineFeature = {}
var pathFeatureID = origin + ' to ' + dest
lineFeature.id = pathFeatureID
lineFeature.type = 'line'
lineFeature.source = {}
lineFeature.source.type = "geojson"
lineFeature.properties = {}
lineFeature.source.data = line
lineFeature.layout = {}
lineFeature.layout['line-join'] = 'round'
lineFeature.layout['line-cap'] = 'round'
lineFeature.paint = {}
lineFeature.paint['line-color'] = '#ffdd00'
lineFeature.paint['line-width'] = 9
lineFeature.paint['line-opacity'] = 0.25
// add line to map
if (!map.getLayer(pathFeatureID)) {
map.addLayer(lineFeature);
}
// determine initial view bearing from start/end coords
var targetPoint = turf.point(lineCoords.pop())
var originPoint = turf.point(lineCoords[0])
var bearing = turf.bearing(originPoint, targetPoint)
// fly to initial view
map.flyTo({
center: originPoint.geometry.coordinates,
zoom: 7,
bearing: bearing,
pitch: 60
})
makePointFeatures(trackInfo)
}
function makePointFeatures(trackInfo){
// make an array of turf.point objects
var points = []
for (var i = 0; i <= trackInfo.length; i++) {
if (i == trackInfo.length) {
sessionStorage.setItem('points', JSON.stringify(points))
bufferPoints(points)
} else {
var point = turf.point(trackInfo[i].pair, trackInfo[i].properties)
points.push(point)
}
}
}
function bufferPoints(points){
var geojson = {
'type': 'FeatureCollection',
'features': []
}
for (var j = 0; j <= points.length; j++) {
if (j == points.length) {
var filename = 'storage/' + FlightID + '-polys.json'
if (fs.existsSync(filename)) {
addPolys(geojson)
} else {
fs.writeFile(filename, JSON.stringify(geojson, null, 2), 'utf-8', addPolys(geojson))
}
} else {
var buffered = turf.buffer(points[j], 1, { units: 'miles' });
geojson.features.push(buffered)
}
}
}
function addPolys(geojson) {
var extrudedProperties = {
// See the Mapbox Style Specification for details on data expressions.
// https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions
'fill-extrusion-color': 'yellow',
'fill-extrusion-height': ['get', 'altitude'],
'fill-extrusion-opacity': 0.5
};
map.addLayer({
"id": FlightID,
"type": "fill-extrusion",
"properties": {},
"paint": extrudedProperties,
"source": {
"type": 'geojson',
"data": geojson
}
})
}
function beginFlight() {
var leftLine = JSON.parse(sessionStorage.leftLine)
var coordinates = leftLine.geometry.coordinates
var line = JSON.parse(sessionStorage.leftLine)
line.geometry.coordinates = [coordinates[0]]
// TODO: fly straight to the first point at z20
map.addSource('trace', {
type: 'geojson',
data: line
});
map.addLayer({
"id": "trace",
"type": "line",
"source": "trace",
"paint": {
"line-color": "yellow",
"line-opacity": 0.85,
"line-width": 12
}
});
var aniOptions = {
duration: 1000
}
// on a regular basis, add more coordinates from the saved list and update the map
var i = 0;
var timer = window.setInterval(function() {
var points = JSON.parse(sessionStorage.points)
if (i < coordinates.length) {
var around = points[i].geometry.coordinates
var alt = points[i].properties.altitude
var zoom = 10
// make sure there is an altitude value
if (alt) {
// set zoom level by alt
if (alt < 1000) {
zoom = 20
} else if (alt > 1000 && alt < 2000) {
zoom = 15
} else if (alt > 2000 && alt < 8000) {
zoom = 13
} else if (alt > 8000 && alt < 12000) {
zoom = 12
} else if (alt > 12000 && alt < 15000) {
zoom = 11
} else if (alt > 15000 && alt < 25000) {
zoom = 10
} else if (alt > 25000) {
zoom = 9
} else {
console.log('Altitude value was invalid: ' + alt);
}
}
line.geometry.coordinates.push(coordinates[i]);
map.getSource('trace').setData(line);
var prevCoord = coordinates[i-1]
if (prevCoord) {
map.easeTo({
center: coordinates[i],
zoom: zoom,
duration: 1000,
bearing: turf.bearing(prevCoord, coordinates[i]) - 90,
easing: function (t) { return t; },
animate: true
})
}
// map.panTo(coordinates[i], aniOptions);
i++;
} else {
window.clearInterval(timer);
}
}, 500);
}