-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracker.html
1808 lines (1725 loc) · 66.4 KB
/
tracker.html
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="author"
content="Geoff R. McLane">
<meta charset="UTF-8">
<title>
2D Tracker 7
</title><!--
,MMN, ...................................................................................
,MMM, CROSSFEED CLIENT MAPPING TESTSUITE: FULL SCREEN MAP
JMMN,
.gg, MMMM, These files are written for testing and develop
.MMN .....jMMMMa....... the crossfeed client (written by Geoff McLane) of FlightGear
.TMMMMMMMMMMMMMMMMMMMMMMM@ Multiplayer Server (written by Oliver Schroeder).
.MMF `?WMMMM"7??!` See http://www.fgx.ch for more information.
?""` dMMM' (c) 2013 Geoff R. McLane, Paris
.MMM' Licence: GPLv2 or later
.MMM' http://www.gnu.org/licenses/gpl.txt
.MMM' ............................................
Please do not remove this copyright and license information from this source in any case.
For OpenLayers (FreeBSD license) and jQuery (MIT license) please follow these links:
http://openlayers.org, http://jquery.org
........................................................................................
20140921 - 0.0.7 - Add param url=http://domain.com:port/flights.json to set url fetch
20131215 - 0.0.6 - Add Hazuki mpserver15 previous track of the callsign
20131210 - 0.0.5 - switch to using ils/navaid loaded CSV search - depreciate json ils/navaid array
20131126 - 0.0.4 - do all xmlhttp together, add red aircraft, add blue aircraft
20131116 - Version 0.0.1 - starting to look good ;=))
-->
<style type="text/css">
html, body, #basicMap {
margin: 0;
width: 100%;
height: 100%;
}
#status_flights {
position: absolute;
font-size: 12px;
bottom: 1em;
left: 10em;
width: 600px;
z-index: 1000;
vertical-align: middle;
border: 1px solid #000000;
}
.p.nob {
border: 0;
padding: 0;
}
#centerAir {
position: absolute;
bottom: 3em;
left: 1em;
z-index: 1000;
<!-- border: 2px solid #FF0000; -->
}
</style>
<script src="js/OpenLayers.js"
type="text/javascript">
</script>
<script src="js/jquery-2.0.3.js"
type="text/javascript">
</script>
<script src="js/geodesic.js"
type="text/javascript">
</script>
<script type="text/javascript">
// CONSTANTS
// 1 knot (kt) = 1.85200 kilometer per hour (kph)
var KTS2KPH = 1.85200;
var NM2KM = 1.852;
var startTime = new Date();
var FGx = FGx || {};
// maybe these should be configurabale
var pred_secs = 5 * 60; // was 120; // length of prediction path
var add_marker = false;
var add_helipads = true;
var show_waiting = false; // mainly a http callback diagnostic
var upd_to_log = false;
var feed_ms = 1800;
//var json_url = 'http://crossfeed.fgx.ch/flights.json';
var json_url = 'http://192.168.1.34:5555/flights.json';
var close_nm = 100; // paint close planes within this range (nm)
// program variables
var def_lat = 52.32;
var def_lon = 13.41;
var def_zoom = 12; // hard to choose then best init zoom
// TODO: maybe store the last zoom in a cookie, and use that?
// initial parameters - never changed
var m_fid, m_callsign, m_model, m_lat, m_lon, m_hdg, m_spd, m_zoom;
// current parameter - this update
var c_lat, c_lon, c_hdg, c_spd, c_alt, c_date;
// previous parameter - from the above at update
var p_lat, p_lon, p_hdg, p_spd, p_alt, p_date;
var calc_dist = 0;
var fromProjection, toProjection, markerLayer, lineLayer, circleLayer, vectorLayer;
var pointLayer, trackLayer; // for historic track
var ILSLayer;
var m_ilat, m_ilon, dest_lat, dest_lon, curr_zoom;
var lineFeat2 = null;
var update_msg = '';
var circle_style = {
strokeColor: '#0000ff',
strokeOpacity: 0.5,
strokeWidth: 2,
fill: false
};
var red_style = {
strokeColor: '#ff0000',
strokeOpacity: 0.5,
strokeWidth: 2,
fill: false
};
var vor_style = {
strokeColor: '#ff0000',
strokeOpacity: 0.5,
strokeWidth: 2,
fill: false
};
var ndb_style = {
strokeColor: '#a0000ff',
strokeOpacity: 0.2,
strokeWidth: 1,
fill: true
};
var nav_style = {
strokeColor: '#000000',
strokeOpacity: 0.8,
strokeWidth: 2,
fill: false
};
// Heliport
var green_style = {
strokeColor: '#004000',
strokeOpacity: 0.5,
strokeWidth: 2,
fill: false
};
var blue_style = {
strokeColor: '#0000ff',
strokeOpacity: 0.5,
strokeWidth: 2,
fill: false
};
var ils_style = {
strokeColor: '#ff0000',
strokeOpacity: 0.5,
strokeWidth: 1
};
var curr_lat1, curr_lon1, curr_lat2, curr_lon2; // current bound of map
var airFound = []; // indexes of airports found in blunds
var airPainted = [];
var heliFound = [];
var heliPainted = [];
var navFound = [];
var navPainted = []; // indexes painted
var doneAir = false;
var doneNav = false;
var doneHeli = false;
var icaoFound = []; // icao files to load
var ilsPainted = [];
// ondemand loading of ICAO.json
var jsonLoaded = {}; // loaded json ICAO files
var icaoDone = []; // do not repeatedly load a failed file
var icaoRequests = 0; // requests for ICAO.json
var plane, air_icon, close_icon;
var center_aircraft = true;
var done_flights_json = false;
var done_tracker_json = false;
var tr_start; // start time to get FGTracker flight data
function valInArray(s,a ) {
var i, t;
var max = a.length;
for (i = 0; i < max; i++) {
t = a[i];
if ( s == t )
return true;
}
return false;
}
function centAir(val) {
center_aircraft = val;
}
function init(fid,callsign,model,lon,lat,alt,hdg,spd,zoom) {
// set some defaults, required to create the map
if (lat.length == 0) { lat = def_lat; }
if (lon.length == 0) { lon = def_lon; }
if (zoom.length == 0) { zoom = def_zoom; }
if (hdg.length == 0) { hdg = 0; }
if (spd.length == 0) { spd = 0; }
// others can be undefinded
if (fid.length)
m_fid = fid;
else
m_fid = 'not_defined'; // of course this means the flight is NOT found later
if (callsign.length)
m_callsign = callsign;
else
m_callsign = 'not_defined';
if (model.length)
m_model = model;
else
m_model = 'not_defined';
// these have been checked and validated
// keep initial values
m_spd = spd;
m_hdg = hdg;
m_lat = lat;
m_lon = lon;
m_alt = alt;
// startTime already kept
m_ilat = lat;
m_ilon = lon;
m_zoom = zoom;
c_spd = spd;
c_hdg = hdg;
c_lat = lat;
c_lon = lon;
c_alt = alt;
c_date = startTime;
p_spd = spd;
p_hdg = hdg;
p_lat = lat;
p_lon = lon;
p_alt = alt;
p_date = startTime;
document.title = "Tracking "+callsign+" in "+model;
fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
map = new OpenLayers.Map("basicMap", {
projection: new OpenLayers.Projection("EPSG:3857"),
// this sets wgs84/4326 as default for display coords
displayProjection: new OpenLayers.Projection("EPSG:4326") });
var mapnik = new OpenLayers.Layer.OSM();
var position = new OpenLayers.LonLat(lon,lat).transform( fromProjection, toProjection);
map.addLayer(mapnik);
map.addControl(new OpenLayers.Control.Permalink('permalink'));
map.addControl(new OpenLayers.Control.MousePosition());
map.addControl(new OpenLayers.Control.ScaleLine());
map.addControl(new OpenLayers.Control.LayerSwitcher());
// add a marker to the initial position
markerLayer = new OpenLayers.Layer.Markers( "Marker Layer" );
//markerLayer.setOpacity(0.3); // guestimate only
map.addLayer(markerLayer);
ILSLayer = new OpenLayers.Layer.Vector("ILS Layer");
map.addLayer(ILSLayer);
//map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path));
// add circles - DID NOT WORK
// circleLayer = new OpenLayers.Layer.Vector("Circles");
// map.addLayer(circleLayer);
// circleLayer.addPolygon(circle);
vectorLayer = new OpenLayers.Layer.Vector("Airport Layer");
map.addLayer(vectorLayer);
var labelStyle = new OpenLayers.Style(
{
externalGraphic: "images/background_trans.png",
//externalGraphic: "images/label_background.gif",
graphicWidth: "${labelWidth}",
graphicHeight: "13",
graphicOpacity: 0.99,
graphicXOffset: "${labelOffset}",
graphicYOffset: 14,
fillColor: "#000000",
fillOpacity: 0.0,
strokeWidth: 0.0,
label : "${icao}",
//fontColor: "#CCCCCC",
fontColor: "#000000",
fontSize: "11px",
fontFamily: "\"DejaVuSansMonoBold\",monospace",
fontWeight: "normal",
labelAlign: "center",
labelXOffset: "0",
labelYOffset: "-22"
}
);
airportLabel = new OpenLayers.Layer.Vector("Label Layer", {styleMap: labelStyle});
map.addLayer(airportLabel);
if (add_marker) {
// add a big fat 'marker;' to initial position - classic style, except above setOpacity(0-1);
var mark = new OpenLayers.Marker(position);
markerLayer.addMarker(mark);
} else { // or a small circle, BUT this seems to FAIL????
//var vectorLayer = new OpenLayers.Layer.Vector("Overlay");
//map.addLayer(vectorLayer);
var radius = 1000; // TODO: what SIZE should the circle be? probably should DEPEND on zoom???
var sides = 30; // docs say '20 approximates a circle'... so ...
var point = new OpenLayers.Geometry.Point(position.lon, position.lat);
var circle = OpenLayers.Geometry.Polygon.createRegularPolygon(
point,
radius,
sides,
0 );
// circleLayer.addPolygon(circle); // THIS FAILED!!!
// lineFeat2 = new OpenLayers.Feature.Vector(line2, null, style2);
// var featurecircle = new OpenLayers.Feature.Vector(circle); // default is borwn, filled circle, so
// add blue, non-filled circle... TODO: Add user configuration...
var featurecircle = new OpenLayers.Feature.Vector(circle, null, circle_style);
vectorLayer.addFeatures([featurecircle]);
}
// to add line drawing
lineLayer = new OpenLayers.Layer.Vector("Line Layer");
map.addLayer(lineLayer);
// for historic tacker info
pointLayer = new OpenLayers.Layer.Vector("Point Layer");
map.addLayer(pointLayer);
trackLayer = new OpenLayers.Layer.Vector("Track Layer");
map.addLayer(trackLayer);
map.setCenter(position, zoom );
// add the predicted path
//if (spd > 10) // no, causes a problem???
// add_predicted( lat, lon, hdg, spd );
map.events.register('zoomend', undefined, zoomChanged);
map.events.register('moveend', undefined, moveEnded);
// add label at initial position
addLabel(callsign, lon, lat);
var air_size = new OpenLayers.Size(40,40);
var air_off = new OpenLayers.Pixel(-(air_size.w/2), -air_size.h/2);
air_icon = new OpenLayers.Icon('textures/fg_generic_red.gif',air_size,air_off);
close_icon = new OpenLayers.Icon('textures/fg_generic_blue.gif',air_size,air_off);
plane = new OpenLayers.Marker(position, air_icon);
// from : http://coherent-labs.com/cryengine-3-minimap-made-with-coherent-ui/
plane.icon.imageDiv.style.webkitTransform = "rotate(" + hdg + "deg)";
markerLayer.addMarker(plane);
} // end init()
// blue track style
var style = {
strokeColor: '#0000ff',
strokeOpacity: 0.5,
strokeWidth: 3
};
// red predicted track style for next ?? secs
var style2 = {
strokeColor: '#FF0000',
strokeOpacity: 0.5,
strokeWidth: 2
};
var closePlanes = [];
var paintedPlanes = [];
var paintedFeatues = [];
/**
* add a predicted track
* calculated and painted as a red line
*/
function add_predicted( lat, lon, hdg, spd ) {
var secs = pred_secs;
if (curr_zoom < 8)
secs *= 4;
var dist_km = spd * KTS2KPH * (secs / 3600);
var p = getDirect( lat, lon, hdg, dist_km );
dest_lat = p._lat;
dest_lon = p._lon;
var point2 = new Array(
new OpenLayers.Geometry.Point(lon, lat).transform( fromProjection, toProjection ),
new OpenLayers.Geometry.Point(dest_lon, dest_lat).transform( fromProjection, toProjection )
);
var line2 = new OpenLayers.Geometry.LineString(point2);
if (lineFeat2 !== null) {
// remove last pediction line
lineLayer.removeFeatures([lineFeat2]);
}
lineFeat2 = new OpenLayers.Feature.Vector(line2, null, style2);
lineLayer.addFeatures([lineFeat2]);
}
function update_position(lat,lon,hdg,alt,spd) {
curr_zoom = map.getZoom();
markerLayer.removeMarker(plane); // remove last plane marker
var points = new Array(
new OpenLayers.Geometry.Point(p_lon, p_lat).transform( fromProjection, toProjection ),
new OpenLayers.Geometry.Point(lon, lat).transform( fromProjection, toProjection )
);
var line = new OpenLayers.Geometry.LineString(points);
var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
lineLayer.addFeatures([lineFeature]);
// save to previous
p_lon = c_lon;
p_lat = c_lat;
p_hdg = c_hdg;
p_spd = c_spd;
p_alt = c_alt;
p_date = c_date;
c_lon = lon;
c_lat = lat;
c_hdg = hdg;
c_alt = alt;
c_spd = spd;
c_date = new Date();
// project the flight forward for the next ?, say 30 seconds
if (spd > 10) {
add_predicted( lat, lon, hdg, spd );
}
// move plane
var lonlat = new OpenLayers.LonLat(lon,lat).transform( fromProjection, toProjection );
plane = new OpenLayers.Marker(lonlat, air_icon);
if (p_hdg != c_hdg) // maybe (if (abs(p_hdg-c_hdg) > 0.1) or something
plane.icon.imageDiv.style.webkitTransform = "rotate(" + c_hdg + "deg)";
markerLayer.addMarker(plane);
if (center_aircraft)
map.setCenter( lonlat, curr_zoom );
}
function add_nearby(data,lat,lon,fid) {
var max, i, pln, flt;
// could maybe move and re-orient, rather than delete
max = paintedPlanes.length;
for (i = 0; i < max; i++) {
pln = paintedPlanes[i];
markerLayer.removeMarker(pln); // remove last plane marker
}
paintedPlanes = [];
var dist = close_nm * NM2KM; // dist from tracked object, in km
var p1 = getDirect(lat,lon,0,dist);
var p2 = getDirect(lat,lon,90,dist);
var p3 = getDirect(lat,lon,180,dist);
var p4 = getDirect(lat,lon,270,dist);
// TODO: Maybe this BBOX could be reduce to the visible screen,
closePlanes = [];
$(data.flights).each(function (i) {
var tfid = this.fid;
if (tfid != fid) {
var tlat = this.lat;
var tlon = this.lon;
if ((tlat <= p1._lat) && (tlat >= p3._lat) &&
(tlon <= p2._lon) && (tlon >= p4._lon)) {
closePlanes.push(this);
}
}
});
max = closePlanes.length;
for (i = 0; i < max; i++) {
flt = closePlanes[i];
var lonlat = new OpenLayers.LonLat(flt.lon,flt.lat).transform( fromProjection, toProjection );
pln = new OpenLayers.Marker(lonlat, close_icon);
pln.icon.imageDiv.style.webkitTransform = "rotate(" + flt.hdg + "deg)";
markerLayer.addMarker(pln);
paintedPlanes.push(pln);
}
//if (max) console.log("painted "+max+" close planes");
}
// *** json data stuff ************************************************************************
function workData() {
// we do not care about the "success" label, we check for
// request loading state directly
if ((this.readyState == 4) && (this.status == "200")) {
// JSON parsing the response string
var found = false;
var lon, lat, hdg, alt, spd;
var data = JSON.parse(this.responseText);
$(data.flights).each(function (i) {
var fid = this.fid;
// var cs = this.callsign;
// var model = this.model;
// I guess checking the fid should be enough
if (fid == m_fid) {
lon = this.lon;
lat = this.lat;
hdg = this.hdg;
alt = this.alt_ft;
spd = this.spd_kts;
found = true;
return false; // out of here - found ourselves
}
});
curr_zoom = map.getZoom();
if (found) {
// want to find nearby aircraft
add_nearby(data,lat,lon,m_fid);
update_position(lat,lon,hdg,alt,spd);
var d = new Date();
var ms = d.valueOf() - startTime.valueOf();
var elap = ms2hhmmss(ms);
var ccnt = paintedPlanes.length; // planes 'close' to the tracker
update_msg = m_callsign+':'+m_model+': llahs='+lon+','+lat+','+alt+','+hdg+','+spd;
if (ccnt)
update_msg += ',c='+ccnt;
update_msg += ',z='+curr_zoom+' '+elap;
if (upd_to_log) console.log(update_msg);
$("#status_flights").html(update_msg);
setTimeout("sendRequest('"+json_url+"')", feed_ms)
} else {
// no longer in the data - flight ended - what to do - close window???
update_msg = "Update: fid="+m_fid+":"+m_callsign+":"+m_model+": NO LONGER IN DATA! flight ended.";
console.log(update_msg);
$("#status_flights").html("<strong>"+update_msg+"<\/strong>");
// TODO: Decide whether to CLOSE the window... maybe not
// alert(update_msg+"\r\nSuggest window be CLOSED!\r\nNo further updates will be added.");
}
}
}
// XMLHttpRequest handling
function getXMLHttpRequest() {
var httpReq = null;
if (window.XMLHttpRequest) {
httpReq = new XMLHttpRequest();
}
else if (typeof ActiveXObject != "undefined") {
httpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
return httpReq;
}
function sendRequest2( url, callback ) {
var req = getXMLHttpRequest();
req.onreadystatechange = callback;
req.open("GET", url)
req.send(null);
}
function sendRequest(url) {
sendRequest2( url, workData );
}
// *** end jason data *************************************************************************
// miscellanious
function zoom2Radius(z) {
var rad = 50000;
var d, f, l, m;
if (z < 6) {
d = (6 - z);
l = Math.log(d);
m = Math.pow(2,l) * 2;
//m = Math.pow(2,d);
//f = d * 2;
f = d * m;
//f = d * d * 2;
rad *= f;
} else if (z > 6) {
d = (z - 6);
l = Math.log(d);
m = Math.pow(2,l) * 2;
//m = Math.pow(2,d);
f = d * m;
//f = d * d * 2;
rad /= f;
}
return rad;
}
function ms2hhmmss( ms ) {
if (ms < 1000) {
return ''+ms+' ms';
}
var secs = Math.floor( ms / 1000 );
ms -= (secs * 1000);
if (secs < 60) {
var stg = ''+((ms / 1000).toFixed(2));
stg = stg.substr(1); // drop the zero
return ''+secs+stg+' secs';
}
var mins = Math.floor(secs / 60);
secs -= (mins * 60);
if (ms > 500)
secs++;
if (secs >= 60) {
secs -= 60;
mins++;
}
if (mins < 60) {
if (secs < 10)
secs = '0'+secs;
return ''+mins+':'+secs+' mm:ss';
}
var hours = Math.floor(mins / 60);
mins -= (hours * 60);
if (mins < 10)
mins = '0'+mins;
if (secs < 10)
secs = '0'+secs;
return ''+hours+':'+mins+':'+secs+' hh:mm:ss';
}
/**
* function requestFile( fname, callback );
* @param (string) File name to load
* @param (function) Callback funtion on state change
* ========== load data files ========
*/
function requestFile( fname, callback ) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open( 'GET', fname, true );
xmlhttp.onreadystatechange = callback;
xmlhttp.send( null );
}
/**
* Object.size = function(obj)
* @param (object) Object to get the size of
* @return (int) Length of the object
*/
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
// === status line display ===
/**
* function showStatus();
* Add some information to the status line
*/
function showStatus() {
var acntl = FGx.airports.length;
var acntf = airFound.length;
var acntp = airPainted.length;
var hcntl = FGx.helipads.length;
var hcntf = heliFound.length;
var hcntp = heliPainted.length;
var ncntl = FGx.navaids.length;
var ncntf = navFound.length;
var ncntp = navPainted.length;
var icntf = icaoFound.length;
var ccntp = paintedPlanes.length;
var jcnt = Object.size(jsonLoaded);
var z = map.getZoom();
// see how long all that took
var d = new Date();
var ms = d.valueOf() - startTime.valueOf();
var elap = ms2hhmmss(ms);
//var stg = ""+lat1+","+lon1+","+lat2+","+lon2;
var msg = "Loaded "+acntl+" airports f/p="+acntf+"/"+acntp;
msg += ", "+hcntl+" heli f/p="+hcntf+"/"+hcntp;
msg += ", "+ncntl+" navaids f/p="+ncntf+"/"+ncntp;
if (icntf)
msg += ", icao "+icntf;
if (jcnt)
msg += ", json "+jcnt;
if (ccntp)
msg += ", close "+ccntp;
msg += ", z="+z;
msg += ' '+elap;
$("#status_flights").html(msg);
// console.log(msg);
}
// === end status line ===
// === get airports and navaids ===
/**
* function addLabel(icao, lon, lat);
* @param (string) ICAO to paint
* @param (number) Degree of longitude
* @param (number) Degree of latitude
* Add a label for an airport contianing ICAO
*/
function addLabel(icao, lon, lat) {
var point = new OpenLayers.Geometry.Point(lon,lat).transform( fromProjection, toProjection);
var labelFeature = new OpenLayers.Feature.Vector(point);
labelFeature.id = "label"+icao;
labelFeature.attributes.icao = icao;
// This calculates label background width and offset
labelWidth = 8 * icao.length;
labelOffset = labelWidth / 2 * -1.0;
labelFeature.attributes.labelWidth = labelWidth;
labelFeature.attributes.labelOffset = labelOffset;
airportLabel.addFeatures([labelFeature]);
return labelFeature;
}
/**
* function getNavPosition(i)
* @param (integer) Index into navaids loaded
* @return (LonLat) OpenLayers LonLat of navaid
*/
function getNavPosition(i) {
var nav = FGx.navaids[i];
var nlat = parseFloat(nav[1]);
var nlon = parseFloat(nav[2]);
var pos = new OpenLayers.LonLat(nlon,nlat).transform( fromProjection, toProjection );
return pos;
}
function paintLine( lat1, lon1, lat2, lon2, style )
{
var points = new Array(
new OpenLayers.Geometry.Point(lon1,lat1).transform( fromProjection, toProjection ),
new OpenLayers.Geometry.Point(lon2,lat2).transform( fromProjection, toProjection )
);
var line = new OpenLayers.Geometry.LineString(points);
var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
lineLayer.addFeatures([lineFeature]);
}
function paintVORRose( nlat, nlon ) {
var p1, p2, hdg, dist1, dist2;
dist1 = 3.25;
dist2 = 0.4;
for (hdg = 0; hdg < 360; hdg += 10) {
if ((hdg == 0)||(hdg == 90)||(hdg == 180)||(hdg == 270))
dist2 = 0.4;
else
dist2 = 3.0;
p1 = getDirect( nlat, nlon, hdg, dist1 ); // outter point
p2 = getDirect( nlat, nlon, hdg, dist2 ); // inner point
paintLine( p2._lat, p2._lon, p1._lat, p1._lon, blue_style );
}
}
function paintNDBRose( nlat, nlon ) {
var p1, p2, hdg, dist1, dist2;
dist1 = 3.25;
dist2 = 0.4;
for (hdg = 0; hdg < 360; hdg += 45) {
if ((hdg == 0)||(hdg == 90)||(hdg == 180)||(hdg == 270))
dist2 = 0.4;
else
dist2 = 3.0;
p1 = getDirect( nlat, nlon, hdg, dist1 ); // outter point
p2 = getDirect( nlat, nlon, hdg, dist2 ); // inner point
paintLine( p2._lat, p2._lon, p1._lat, p1._lon, blue_style );
}
}
/**
* function paintNavaids( navs );
* @param (array) Navaids found in bounding box
* Add a display of certain types of navaids onto the map
* TODO: Lots of experimenting... ended up just adding an image in most cases
*/
function paintNavaids( navs ) {
var cnt = navs.length;
var i, j, pos, ind, typ, nav;
var icon, marker;
var point, circle, featurecircle;
var nlat, nlon;
var z = map.getZoom();
//if (z < 5) {
// return 0;
//}
var radius = 500; // TODO: what SIZE should the circle be? probably should DEPEND on zoom???
var sides = 20; // docs say '20 approximates a circle'... so ...
var style = circle_style;
var add = true;
var img = '';
var painted = 0;
var done = false;
for (i = 0; i < cnt; i++) {
ind = navs[i];
if (valInArray(ind,navPainted))
continue;
nav = FGx.navaids[ind];
typ = parseInt(nav[0]);
nlat = parseFloat(nav[1]);
nlon = parseFloat(nav[2]);
pos = getNavPosition(ind);
radius = 200;
style = nav_style;
add = true;
img = '';
done = false;
if (typ == 2) {
if (z < 8)
img = 'NDB-50x50.gif';
else
img = 'NDB-95x95.gif';
//markerLayer.addMarker(new OpenLayers.Marker(pos,ndb_icon.clone));
radius = 400;
style = ndb_style;
//style = circle_style;
add = false;
} else if (typ == 3) {
// Three types of VOR, VORTAC, VOR-DME, and good old VOR - check name
//markerLayer.addMarker(new OpenLayers.Marker(pos,vor_icon.clone));
radius = 500;
style = vor_style;
if (z < 8)
img = 'VOR-50x43.gif'; // 'standard' VOR
else
img = 'VOR-95x83.gif'; // 'standard' VOR
var name = nav[10]; // get the 'name'
var fnd = name.match(/VORTAC/g);
if (fnd !== null) {
if (z < 8)
img = 'VORTAC-50x43.gif';
else
img = 'VORTAC-97x85.gif';
} else {
fnd = name.match(/DME/g);
if (fnd !== null) {
if (z < 8)
img = 'VOR-DME-50x43.gif';
else
img = 'VOR-DME-95x83.gif';
}
}
add = false;
} else if ((typ == 4)||(typ == 5)||(typ == 6)) {
// Localiser and Glideslope components of ILS (LDA, SDF)
// this will come from the ICAO.json file
add = false;
} else if ((typ == 7)||(typ == 8)||(typ == 9)) {
// OM, MM, IM markers
radius = 200;
style = nav_style;
if (z > 7) {
if (typ == 7)
img = 'OM.png';
else if (typ == 8)
img = 'MM.png';
else
img = 'IM.png';
}
add = false;
} else if (typ == 12) {
// DME of ILS
radius = 600;
style = circle_style;
add = false;
} else if (typ == 13) {
radius = 600;
style = circle_style;
add = false;
}
done = false;
if (img.length) {
icon = new OpenLayers.Icon('img/'+img);
marker = new OpenLayers.Marker(pos, icon);
markerLayer.addMarker(marker);
painted++;
done = true;
} else if (add) {
point = new OpenLayers.Geometry.Point(pos.lon, pos.lat);
circle = OpenLayers.Geometry.Polygon.createRegularPolygon(
point,
radius,
sides,
0 );
featurecircle = new OpenLayers.Feature.Vector(circle, null, style);
vectorLayer.addFeatures([featurecircle]);
painted++;
done = true;
}
if (done) {
radius = 600;
if ((typ == 2)||(typ == 3))
style = blue_style;
else
style = red_style;
point = new OpenLayers.Geometry.Point(pos.lon, pos.lat);
circle = OpenLayers.Geometry.Polygon.createRegularPolygon(
point,
radius,
sides,
0 );
featurecircle = new OpenLayers.Feature.Vector(circle, null, style);
if (z >= 8)
vectorLayer.addFeatures([featurecircle]);
if ((typ == 2)||(typ == 3)) {
radius = 5000; // this should be according to the range maybe, and/or zoom
style = blue_style;
point = new OpenLayers.Geometry.Point(pos.lon, pos.lat);
circle = OpenLayers.Geometry.Polygon.createRegularPolygon(
point,
radius,
sides,
0 );
featurecircle = new OpenLayers.Feature.Vector(circle, null, style);
//if (typ == 3) {
if (z >= 8) {
vectorLayer.addFeatures([featurecircle]);
if (typ == 3)
paintVORRose( nlat, nlon );
else
paintNDBRose( nlat, nlon );
}
}
}
navPainted.push(ind);
}
return painted;
}
/**
* function searchNavAids( lat1, lon1, lat2, lon2, type );
* @param (Float) Degrees of hightest (N) latitude
* @param (Float) Degrees of least (W) longitude
* @param (Float) Degrees of lowest (S) latitude
* @param (Float) Degrees of most (E) longitude
* examples nav csv
* 0 1 2 3 4 5 6 7 8 9 10
* 4,39.98091100,-075.87781400,660,10850,18,281.662, IMQS,40N, 29,"ILS-cat-I"
* 6,42.88151400,129.43913900, 624,10870,10,300081.620,IJA, ZYYJ,09,"GS"
* 7,39.96071900,-075.75077800,660,0, 0, 281.662 ,----,40N, 29,"OM"
* 9,30.56114200,103.93897200,1624,0, 0, 21.873 ,----,ZUUU,02,"IM"
*/
function searchNavAids( lat1, lon1, lat2, lon2 ) {
var max = FGx.navaids.length;
var i, nav, nlat, nlon, type, icao;
newFound = [];
for (i = 1; i < max; i++) {
nav = FGx.navaids[i];
nlat = parseFloat(nav[1]);
nlon = parseFloat(nav[2]);
if ((nlat <= lat1)&&(nlat >= lat2)&&
(nlon >= lon1)&&(nlon <= lon2)) {
if (valInArray(i,navFound)) {
// already in list
} else {
navFound.push(i); // just need an index
newFound.push(i);
type = parseInt(nav[0]);
if ((type >= 4)&&(type <= 9)) {
// these have an ICAO
icao = nav[8];
if (icao.length && !valInArray(icao,icaoFound))
icaoFound.push(icao);
}
}
}
}
return newFound; // return the found
}
function callbackNavaids() {
if ( this.readyState == 4 ) {
var dataLines = this.responseText;
dataLines = dataLines.split(/\r\n|\n/);
var max = dataLines.length;
var separator = ',';
for ( var i = 1; i < max; i++ ) {
FGx.navaids.push( dataLines[i].split( separator ) );
}
max = FGx.navaids.length;
var nava = searchNavAids( curr_lat1, curr_lon1, curr_lat2, curr_lon2 );
var d = new Date();
var ms = d.valueOf() - startTime.valueOf();
console.log( 'Loaded '+max+', found '+nava.length+' in '+ms+' ms');
showStatus();
doneNav = true;
checkStage2();
} else {
FGx.callbackCount++;
if (show_waiting)
console.log(FGx.callbackCount+': Waiting for navaid load... ');
}
}
/**
* function paintHelipads( airs, navs );
* @param (array) Helipads found in bounding box
* Add a display of an helipad onto the map
* At this time the ICAO json file may not be loaded, else maybe
* add outlines of the runways, a nice circle and H for helipads
*
*/
function paintHelipads( airs ) {
var max = airs.length;
var i, ind, air, cnt, alat, alon, icao, pos, point, circle, feat;
var data, rwysa, helia, rada, nava, name, j, maxj, rwy, info;
var circ2, feat2;
cnt = 0;
var z = map.getZoom();
for (i = 0; i < max; i++) {
ind = airs[i];
if (valInArray(ind,heliPainted))
continue;
air = FGx.helipads[ind];
icao = air[0];
alat = parseFloat(air[2]);