-
Notifications
You must be signed in to change notification settings - Fork 11
/
igcviewer.js
349 lines (308 loc) · 12.2 KB
/
igcviewer.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
341
342
343
344
345
346
347
348
349
(function ($) {
'use strict';
var igcFile = null;
var barogramPlot = null;
var altitudeConversionFactor = 1.0; // Conversion from metres to required units
function positionDisplay(position) {
function toDegMins(degreevalue) {
var wholedegrees= Math.floor(degreevalue);
var minutevalue = (60*(degreevalue-wholedegrees)).toFixed(3);
return wholedegrees + '\u00B0\u00A0' + minutevalue + '\u00B4';
}
var positionLatitude= toDegMins(Math.abs(position[0]));
var positionLongitude=toDegMins(Math.abs(position[1]));
if(position[0] > 0) {
positionLatitude += "N";
}
else {
positionLatitude += "S";
}
if(position[1] > 0) {
positionLongitude += "E";
}
else {
positionLongitude += "W";
}
return positionLatitude + ", " + positionLongitude;
}
function pad(n) {
return (n < 10) ? ("0" + n.toString()) : n.toString();
}
function plotBarogram() {
var nPoints = igcFile.recordTime.length;
var pressureBarogramData = [];
var gpsBarogramData = [];
var j;
var timestamp;
for (j = 0; j < nPoints; j++) {
timestamp = igcFile.recordTime[j].getTime();
pressureBarogramData.push([timestamp, igcFile.pressureAltitude[j] * altitudeConversionFactor]);
gpsBarogramData.push([timestamp, igcFile.gpsAltitude[j] * altitudeConversionFactor]);
}
var baro = $.plot($('#barogram'), [{
label: 'Pressure altitude',
data: pressureBarogramData
}, {
label: 'GPS altitude',
data: gpsBarogramData
}], {
axisLabels: {
show: true
},
xaxis: {
axisLabel: 'Time',
tickFormatter: function (t, axis) {
return moment(t).format('HH:mm');
},
ticks: function (axis) {
var ticks = [];
var startMoment = moment(axis.min);
var endMoment = moment(axis.max);
var durationMinutes = endMoment.diff(startMoment, 'minutes');
var interval;
if (durationMinutes <= 10) {
interval = 1;
}
if (durationMinutes <= 50) {
interval = 5;
}
else if (durationMinutes <= 100) {
interval = 10;
}
else if (durationMinutes <= 150) {
interval = 15;
}
else if (durationMinutes <= 300) {
interval = 30;
}
else if (durationMinutes <= 600) {
interval = 60;
}
else {
interval = 120;
}
var tick = startMoment.clone();
tick.minutes(0).seconds(0);
while (tick < endMoment) {
if (tick > startMoment) {
ticks.push(tick.valueOf());
}
tick.add(interval, 'minutes');
}
return ticks;
}
},
yaxis: {
axisLabel: 'Altitude / ' + $('#altitudeUnits').val()
},
crosshair: {
mode: 'xy'
},
grid: {
clickable: true,
autoHighlight: false
}
});
return baro;
}
function updateTimeline (timeIndex, mapControl) {
var currentPosition = igcFile.latLong[timeIndex];
var positionText=positionDisplay(currentPosition);
var unitName = $('#altitudeUnits').val();
$('#timePositionDisplay').text(
moment(igcFile.recordTime[timeIndex]).format('HH:mm:ss') + ': ' +
(igcFile.pressureAltitude[timeIndex] * altitudeConversionFactor).toFixed(0) + ' ' +
unitName + ' (barometric) / ' +
(igcFile.gpsAltitude[timeIndex] * altitudeConversionFactor).toFixed(0) + ' ' +
unitName + ' (GPS); ' +
positionText
);
mapControl.setTimeMarker(timeIndex);
barogramPlot.lockCrosshair({
x: igcFile.recordTime[timeIndex].getTime(),
y: igcFile.pressureAltitude[timeIndex] * altitudeConversionFactor
});
}
function displayIgc(mapControl) {
// Display the headers.
var displayDate = moment(igcFile.recordTime[0]).format('LL');
var headerTable = $('#headerInfo tbody');
headerTable.html('')
.append(
$('<tr></tr>').append($('<th></th>').text('Date'))
.append($('<td></td>').text(displayDate))
);
var headerName;
var headerIndex;
for (headerIndex = 0; headerIndex < igcFile.headers.length; headerIndex++) {
headerTable.append(
$('<tr></tr>').append($('<th></th>').text(igcFile.headers[headerIndex].name))
.append($('<td></td>').text(igcFile.headers[headerIndex].value))
);
}
// Show the task declaration if it is present.
if (igcFile.task.coordinates.length > 0) {
//eliminate anything with empty start line coordinates
if(igcFile.task.coordinates[0][0] !==0) {
$('#task').show();
var taskList = $('#task ul').first().html('');
var j;
//Now add TP numbers. Change to unordered list
if(igcFile.task.takeoff.length > 0) {
taskList.append($('<li> </li>').text("Takeoff: " + igcFile.task.takeoff));
}
for (j =0; j <igcFile.task.names.length; j++) {
switch(j) {
case 0:
taskList.append($('<li> </li>').text("Start: " + igcFile.task.names[j]));
break;
case ( igcFile.task.names.length-1):
taskList.append($('<li> </li>').text("Finish: " + igcFile.task.names[j]));
break;
default:
taskList.append($('<li></li>').text("TP" + (j).toString() + ": " + igcFile.task.names[j]));
}
}
if(igcFile.task.landing.length > 0) {
taskList.append($('<li> </li>').text("Landing: : " + igcFile.task.landing));
}
mapControl.addTask(igcFile.task.coordinates, igcFile.task.names);
}
}
else {
$('#task').hide();
}
// Reveal the map and graph. We have to do this before
// setting the zoom level of the map or plotting the graph.
$('#igcFileDisplay').show();
mapControl.addTrack(igcFile.latLong);
barogramPlot = plotBarogram(igcFile);
$('#timeSlider').prop('max', igcFile.recordTime.length - 1);
updateTimeline(0, mapControl);
}
function storePreference(name, value) {
if (window.localStorage) {
try {
localStorage.setItem(name, value);
}
catch (e) {
// If permission is denied, ignore the error.
}
}
}
$(document).ready(function () {
var mapControl = createMapControl('map');
var timeZoneSelect = $('#timeZoneSelect');
$.each(moment.tz.names(), function(index, name) {
timeZoneSelect.append(
$('<option></option>', { value: name }).text(name));
});
timeZoneSelect.change(function () {
var selectedZone = $(this).val();
moment.tz.setDefault(selectedZone);
if (igcFile !== null) {
barogramPlot = plotBarogram();
updateTimeline($('#timeSlider').val(), mapControl);
$('#headerInfo td').first().text(moment(igcFile.recordTime[0]).format('LL'));
}
storePreference('timeZone', selectedZone);
});
$('#fileControl').change(function () {
if (this.files.length > 0) {
var reader = new FileReader();
reader.onload = function(e) {
try {
$('#errorMessage').text('');
mapControl.reset();
$('#timeSlider').val(0);
igcFile = parseIGC(this.result);
displayIgc(mapControl);
} catch (ex) {
if (ex instanceof IGCException) {
$('#errorMessage').text(ex.message);
}
else {
throw ex;
}
}
};
reader.readAsText(this.files[0]);
}
});
$('#altitudeUnits').change(function (e, raisedProgrammatically) {
var altitudeUnit = $(this).val();
if (altitudeUnit === 'feet') {
altitudeConversionFactor = 3.2808399;
}
else {
altitudeConversionFactor = 1.0;
}
if (igcFile !== null) {
barogramPlot = plotBarogram();
updateTimeline($('#timeSlider').val(), mapControl);
}
if (!raisedProgrammatically) {
storePreference("altitudeUnit", altitudeUnit);
}
});
// We need to handle the 'change' event for IE, but
// 'input' for Chrome and Firefox in order to update smoothly
// as the range input is dragged.
$('#timeSlider').on('input', function() {
var t = parseInt($(this).val(), 10);
updateTimeline(t, mapControl);
});
$('#timeSlider').on('change', function() {
var t = parseInt($(this).val(), 10);
updateTimeline(t, mapControl);
});
$('#timeBack').click(function() {
var slider = $('#timeSlider');
var curTime = parseInt(slider.val(), 10);
curTime--;
if(curTime < 0) {
curTime = 0;
}
slider.val(curTime);
updateTimeline(curTime, mapControl);
});
$('#timeForward').click(function() {
var slider = $('#timeSlider');
var curTime = parseInt(slider.val(), 10);
var maxval= slider.prop('max');
curTime++;
if(curTime > maxval) {
curTime = maxval;
}
slider.val(curTime);
updateTimeline(curTime, mapControl);
});
$('#barogram').on('plotclick', function (event, pos, item) {
console.log('plot click');
if (item) {
updateTimeline(item.dataIndex, mapControl);
$('#timeSlider').val(item.dataIndex);
}
});
// Load preferences from local storage, if available.
var altitudeUnit = '';
var timeZone = '';
if (window.localStorage) {
try {
altitudeUnit = localStorage.getItem('altitudeUnit');
if (altitudeUnit) {
$('#altitudeUnits').val(altitudeUnit).trigger('change', true);
}
timeZone = localStorage.getItem('timeZone');
}
catch (e) {
// If permission is denied, ignore the error.
}
}
if (!timeZone) {
timeZone = 'UTC';
}
timeZoneSelect.val(timeZone);
moment.tz.setDefault(timeZone);
});
}(jQuery));