-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXFMS_DATA.cpp
3407 lines (3011 loc) · 112 KB
/
XFMS_DATA.cpp
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
#include "XFMS_DATA.h"
#include <airway.h>
#include <airport_data.h>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <iostream>
#include <list>
#include <QString>
#include <QFile>
#include <QDir>
#include <QFileInfoList>
#include <QTextStream>
#include "LMATH.h"
#include "coremag.h"
#include <algorithm>
#include "dialogsettings.h"
#include <QDebug>
class RUNWAY
{
private:
static void swap(RUNWAY& me, const RUNWAY& r)
{
me.type = r.type;
me.length = r.length;
me.width = r.width;
me.rwyA = r.rwyA;
me.rwyB = r.rwyB;
me.lightA = r.lightA;
me.lightB = r.lightB;
me.surface = r.surface;
me.start = r.start;
me.end = r.end;
}
public:
int type; //WAYPOINT::TYPE_RUNWAY or WAYPOINT::TYPE_HELIPAD
double length; //Length of runway/helipad
double width; //Width of runway/helipad
QString rwyA; //Runway identifer
QString rwyB; //Runway opposite identifer
int lightA; //Runway approach lightning
int lightB; //Runway approach lightning opposite
int surface; //Runway surface
CPoint start; //Coordinates of runway start threshold
CPoint end; //Coordinates of runway end threshold
RUNWAY(const QString& _rwyA, const CPoint& _startA, int _lightA, const QString _rwyB, const CPoint& _startB, int _lightB, double _rwyWidth, int _surface, int _type)
{
type = _type;
rwyA = _rwyA;
start = _startA;
lightA = _lightA;
rwyB = _rwyB;
end = _startB;
lightB = _lightB;
surface = _surface;
width = _rwyWidth;
length = LMATH::calc_distance(start, end)*1000;
}
RUNWAY(const QString & _rwy, const CPoint& _startA, double _length, double _width, int _surface, int _type)
{
type = _type;
rwyA = _rwy;
start = _startA;
length = _length;
width = _width;
surface = _surface;
}
RUNWAY(const RUNWAY& r)
{
swap(*this, r);
}
RUNWAY& operator=(const RUNWAY& other)
{
swap(*this, other);
return *this;
}
RUNWAY* clone()
{
return new RUNWAY(*this);
}
double getLength() //In meter
{
return length;
}
double getWidth() { return width; }
CPoint getMiddlePoint()
{
if(type == WAYPOINT::TYPE_RUNWAY) return (start + (end - start)*0.5);
return start;
}
int getType(){ return type; }
~RUNWAY()
{
}
};
volatile int XFMS_DATA::__DATA_LOADED[8] = {0, 0, 0, 0, 0, 0, 0, 0};
volatile int XFMS_DATA::__CURRENT_LOADING = 0;
QStringList XFMS_DATA::__SL_FINISHED;
QString XFMS_DATA::__ERROR_LOADING;
int XFMS_DATA::dat; //Current date
std::multimap<QString, NVUPOINT*> XFMS_DATA::lWP;
std::multimap<QString, NVUPOINT*> XFMS_DATA::lWP2;
//VOR/DME loaded from X-Plane earth earth_nav.dat, as it includes VOR angle deviation data.
std::vector<NVUPOINT*> XFMS_DATA::lXVORDME;
//Loaded from rsbn.dat
std::vector<NVUPOINT*> XFMS_DATA::lRSBN;
//Loaded from xnvu.dat
std::vector<NVUPOINT*> XFMS_DATA::lXNVU;
XFMS_DATA::XFMS_DATA()
{
}
XFMS_DATA::~XFMS_DATA()
{
clear();
}
void XFMS_DATA::clear()
{
lXVORDME.clear();
lRSBN.clear();
lXNVU.clear();
lWP2.clear();
std::multimap<QString, NVUPOINT*>::iterator it = lWP.begin();
for(; it!=lWP.end(); it++)
{
delete it->second;
}
lWP.clear();
}
void XFMS_DATA::setDate(int _dat)
{
dat = _dat;
std::multimap<QString, NVUPOINT*>::iterator it = lWP.begin();
for(; it!=lWP.end(); it++)
{
NVUPOINT* cP = (*it).second;
double elev = 0;
/*
if(cP->wpOrigin == WAYPOINT::ORIGIN_AIRAC_AIRPORTS ||
cP->wpOrigin == WAYPOINT::ORIGIN_AIRAC_NAVAIDS ||
cP->wpOrigin == WAYPOINT::ORIGIN_EARTHNAV ||
cP->wpOrigin == WAYPOINT::ORIGIN_XNVU)
{
*/
elev = (double(LMATH::feetToMeter(cP->elev))/1000.0);
//}
cP->MD = calc_magvar(cP->latlon.x, cP->latlon.y, dat, elev);
}
}
//ICAO_name2 defines if user want to search in both ICAO name and full name of waypoint, or either:
//0: Search both
//1: Only ICAO
//2: Only full name
std::vector<NVUPOINT*> XFMS_DATA::search(const QString& _name, int _type, int _ICAO_name2, const QString& _country)
{
std::vector<NVUPOINT*> rWP;
std::pair<std::multimap<QString, NVUPOINT*>::iterator, std::multimap<QString, NVUPOINT*>::iterator> ret;
if(_name.isEmpty()) return rWP;
if(_ICAO_name2>0 && _ICAO_name2 != 1) goto _name2; //I hate GOTO:s, but they are effective.
ret = lWP.equal_range(_name);
for(std::multimap<QString, NVUPOINT*>::iterator it=ret.first; it!=ret.second; it++)
{
NVUPOINT* wp = it->second;
if(_type>0 && wp->type != _type) continue;
if(_country!=NULL && !_country.isEmpty()) if(_country.compare(wp->country) !=0) continue;
rWP.push_back(wp);
}
_name2:
if(_ICAO_name2>0 && _ICAO_name2 != 2) return rWP;
ret = lWP2.equal_range(_name);
for(std::multimap<QString, NVUPOINT*>::iterator it=ret.first; it!=ret.second; it++)
{
NVUPOINT* wp = it->second;
if(_type>0 && wp->type != _type) continue;
if(_country!=NULL && !_country.isEmpty()) if(_country.compare(wp->country) !=0) continue;
rWP.push_back(wp);
}
return rWP;
}
QString XFMS_DATA::getAirwayWaypointsBetween(QString& airway, NVUPOINT* wpA, NVUPOINT* wpB, std::vector<NVUPOINT*>& lA, NVUPOINT* &_wpAirway, bool allowOpposite)
{
std::vector<NVUPOINT*> llA = XFMS_DATA::search(airway); //Get a list of all airways with the same identifier (hence the airways are directional, and should logically share the same waypoints)
QString sError = "";
bool kAIsFound = false;
bool kBIsFound = false;
_wpAirway = NULL;
lA.clear();
for(unsigned int i=0; i<llA.size(); i++)
{
NVUPOINT* wp = llA[i];
if(wp->type != WAYPOINT::TYPE_AIRWAY) continue;
AIRWAY* aw = (AIRWAY*) wp->data;
if(!aw) continue;
int kA = -1;
int kB = -1;
for(unsigned int k=0; k<aw->lATS.size(); k++) //Search for the identifers in the airway (where the airway should start and end).
{
NVUPOINT* ap = (NVUPOINT*) aw->lATS[k];
if(ap->name.compare(wpA->name)==0) kA = k;
if(ap->name.compare(wpB->name)==0) kB = k;
}
if(kA>=0) kAIsFound = true;
if(kB>=0) kBIsFound = true;
if(kA<0 || kB<0) continue; //If identifiers is not found, continue to next airway in list.
else if(kB<kA) //If wpA is after wpB, and allowing the opposite direction of a directional airway
{
if(lA.size()==0 && allowOpposite)
{
for(int k=kA; k>=kB; k--)
{
lA.push_back((NVUPOINT*) aw->lATS[k]);
}
_wpAirway = wp;
sError = "";
}//if
else sError = "Route: Note, airway [" + airway + "] is directional and opposite to waypoints [" + wpA->name + "] -> [" + wpB->name + "].";
continue;
}
else if(kA<kB) //If wpB is after wpA
{
lA.clear();
for(int k=kA; k<=kB; k++)
{
lA.push_back((NVUPOINT*) aw->lATS[k]);
}
_wpAirway = wp;
return "";
}
}//for
if(llA.size()==0)
{
_wpAirway = NULL;
return "Route: Airway [" + airway + "] is not found.";
}
//If nothing is added something is wrong. Set error returns here.
if(sError.isEmpty() && lA.size()==0)
{
_wpAirway = NULL;
if(kAIsFound && kBIsFound)
{
return "Route: Cannot connect waypoints [" + wpA->name + "] and [" + wpB->name + "].";
}
if(!kAIsFound && !kBIsFound) return "Route: Waypoint [" + wpA->name + "] and [" + wpB->name + "] is not found in airway [" + airway + "].";
if(!kAIsFound) return "Route: Waypoint [" + wpA->name + "] is not found in airway [" + airway + "].";
else if(!kBIsFound) return "Route: Waypoint [" + wpB->name + "] is not found in airway [" + airway + "].";
return "Route: Major programming error, should not be here, contact confused developer.";
}
if(!sError.isEmpty()) _wpAirway = NULL;
return sError;
}
//Allocates and returns a list of waypoints. Caller needs to delete the waypoints when not used anymore.
//TODO: Make a smart algorithm for deciding which single waypoints should be added, hence there are different
//waypoints with the same identifer, and it is not enough to check the neareast waypoint
//(see airway 'R487' and waypoint 'OK').
QString XFMS_DATA::getRoute(const QString& _qstr, std::vector<NVUPOINT*>& _route, NVUPOINT* wpRef)
{
QStringList record = _qstr.split(' ', QString::SkipEmptyParts);
std::vector<NVUPOINT*> sWaypoint;
std::vector<NVUPOINT*> cRoute;
std::vector<NVUPOINT*> route;
std::vector<NVUPOINT*> pBuffer; //Buffer of custom created waypoints to delete if error.
QString qstr;
QString sError;
NVUPOINT* cwp;
double dCurrent, dMin;
AIRWAY* awyA = NULL;
AIRWAY* awyB = NULL;
int pBetween = 0;
route.clear();
for(unsigned int i=0; i<record.size(); i++)
{
qstr = record[i];
//Search for identifier in database, if not found, add if this is a custom waypoint.
sWaypoint = XFMS_DATA::search(qstr, 0, 1);
if(sWaypoint.size() == 0)
{
//TODO If there are more records, and this is the first one, set wpRef to next waypoint, and calculate later.
if(cRoute.size() == 0) cwp = wpRef;
else cwp = cRoute[cRoute.size() - 1];
NVUPOINT* wp = NULL;
sError = validate_custom_point(cwp, wp, qstr);
if(wp)
{
pBuffer.push_back(wp);
cRoute.push_back(wp);
continue;
}
else
{
sError = "Route: " + sError;
goto rError;
}
}//if
//If more than 1 waypoint is found, choose the waypoint that is closest to the last on in current route.
dMin = std::numeric_limits<double>::max();
dCurrent = 0;
for(unsigned int k=0; k<sWaypoint.size(); k++)
{
if(cRoute.size()>0) dCurrent = LMATH::calc_distance(sWaypoint[k]->latlon, cRoute[cRoute.size()-1]->latlon);
else if(wpRef) dCurrent = LMATH::calc_distance(sWaypoint[k]->latlon, wpRef->latlon);
if(dCurrent<dMin)
{
dMin = dCurrent;
cwp = sWaypoint[k];
}
}
cRoute.push_back(cwp);
}//for
sWaypoint.clear();
for(int i=0; i<cRoute.size(); i++)
{
NVUPOINT* wp = cRoute[i];
//If waypoint is an airway, check if previous and next waypoint in route exist in airway,
if(wp->type == WAYPOINT::TYPE_AIRWAY)
{
AIRWAY* ats = (AIRWAY*) wp->data;
if(!ats) continue;
//It is not valid if an airway starts or ends the route
if(i==0)
{
sError = "Route: First point in route should not be an airway [" + ats->name + "].";
goto rError;
}
if(i==(int(cRoute.size())-1))
{
sError = "Route: Last point in route should not be an airway [" + ats->name + "].";
goto rError;
}//if
std::vector<NVUPOINT*> lA;
NVUPOINT* rPrev = cRoute[i-1];
NVUPOINT* rNext = cRoute[i+1];
NVUPOINT* wpAirway = NULL;
sError = XFMS_DATA::getAirwayWaypointsBetween(ats->name, rPrev, rNext, lA, wpAirway, true);
//Return error
if(!sError.isEmpty())
{
goto rError;
}
//Set the correct airway in cRoute for comparsion algorithm in the for loop below
if(wpAirway) cRoute[i] = wpAirway;
//Set the correct waypoints before and after airway (for comparsion algorithm in the for loop below).
if(lA.size()>0)
{
cRoute[i-1] = lA[0];
cRoute[i+1] = lA[lA.size()-1];
}//if
route.pop_back(); //Remove the last waypoint, as a new and more correct one is added from the airway.
for(unsigned int k=0; k<lA.size(); k++)
{
route.push_back(lA[k]);
}//for
i++; //Step forward as we already added the next waypoint
continue; //Do not push the airway itself to the list
}//if
/*
else if(wp->type==WAYPOINT::ORIGIN_FLIGHTPLAN)
{
route.push_back(wp);
continue;
}
wp->wpOrigin = WAYPOINT::ORIGIN_FLIGHTPLAN;
*/
route.push_back(wp);
}//for
//Now the route is finished. First though we need to check if the start and end points
//of connecting airways are the same, if not there is an error in route.
//I.e. route R487 and W234 may share the same waypoint identifer between, but the waypoint
//itself may be duplicate and placed on several places in world.
for(unsigned int i=0; i<cRoute.size(); i++)
{
if(cRoute[i]->type == WAYPOINT::TYPE_AIRWAY)
{
if(awyA && pBetween == 1)
{
awyB = (AIRWAY*) cRoute[i]->data;
NVUPOINT* wp = cRoute[i-1];
//Check if waypoint is the same waypoint in both airways
bool okA = false;
bool okB = false;
for(unsigned int j=0; j<awyA->lATS.size(); j++)
{
if(wp == awyA->lATS[j])
{
okA = true;
break;
}
}//for
for(unsigned int j=0; j<awyB->lATS.size(); j++)
{
if(wp == awyB->lATS[j])
{
okB = true;
break;
}
}//for
if(okA == false || okB == false)
{
sError = "Route: Waypoint [" + wp->name + "] cannot join airway [" + awyA->name + "] and [" + awyB->name + "].";
goto rError;
}
awyA = NULL;
awyB = NULL;
pBetween = 0;
i--;
continue;
}//if
else
{
awyA = (AIRWAY*) cRoute[i]->data;
pBetween = 0;
}//else
}//if
else
{
pBetween++;
}//eöse
}//for
//Allocate and copy the route to flightplan
for(unsigned int i=0; i<route.size(); i++)
{
_route.push_back(new NVUPOINT(*route[i]));
}
return "";
//Deallocate custom waypoints and return error.
rError:
for(unsigned int i=0; i<pBuffer.size(); i++) delete pBuffer[i];
cRoute.clear();
route.clear();
return sError;
}
//Get closest waypoints of same type and name. Returns a list of found waypoints and their distances.
std::vector< std::pair<NVUPOINT*, double> > XFMS_DATA::getClosestWaypointType(const CPoint& _latlon, const QString& _name, int _type)//, double &distance)
{
std::vector< std::pair<NVUPOINT*, double> > lWP;
double dMin = std::numeric_limits<double>::max();
std::vector<NVUPOINT*> lS = search(_name, _type);
for(unsigned int i=0; i<lS.size(); i++)
{
double d = LMATH::calc_distance(_latlon, lS[i]->latlon);
if(d<=dMin)
{
lWP.push_back(std::make_pair(lS[i], d));
}
}
//Sorting the distance of found waypoints
sort(lWP.begin(), lWP.end(),
[](const std::pair<NVUPOINT*, double>& lhs, const std::pair<NVUPOINT*, double>& rhs) -> bool
{
//if(lhs.second == 0) return true;
return lhs.second < rhs.second;
});
return lWP;
}
//Closest to waypoint, N closest RSBN to return, D max distance of RSBN, includeVOR to true to include VORDME
//If N<1, return all the RSBN:s
std::vector< std::pair<NVUPOINT*, double> > XFMS_DATA::getClosestRSBN(const NVUPOINT* wp, int n, double d, bool includeVOR)
{
if(n<0) n = lRSBN.size() + ((includeVOR==true) ? lXVORDME.size() : 0);
if(d<0) d = std::numeric_limits<double>::max();
std::vector< std::pair<NVUPOINT*, double> > rCP;
for(unsigned int i=0; i<lRSBN.size(); i++)
{
double cD = LMATH::calc_distance(wp->latlon.x, wp->latlon.y, lRSBN[i]->latlon.x, lRSBN[i]->latlon.y);
if(cD>d) continue;
rCP.push_back(std::make_pair(lRSBN[i], cD));
}
if(includeVOR) for(unsigned int i=0; i<lXVORDME.size(); i++)
{
double cD = LMATH::calc_distance(wp->latlon.x, wp->latlon.y, lXVORDME[i]->latlon.x, lXVORDME[i]->latlon.y);
if(cD>d) continue;
rCP.push_back(std::make_pair(lXVORDME[i], cD));
}
sort(rCP.begin(), rCP.end(),
[](const std::pair<NVUPOINT*, double>& lhs, const std::pair<NVUPOINT*, double>& rhs) -> bool
{
//if(lhs.second == 0) return true;
return lhs.second < rhs.second;
});
while(rCP.size()>n) rCP.pop_back();
return rCP;
}
int XFMS_DATA::_loadXP10(const QString& file, int type)
{
QFile infile(file);
if(!infile.open(QIODevice::ReadOnly | QIODevice::Text)) return 0;
if(type == 6)
{
validate_airways_XP10(infile);
infile.close();
return 1;
}
while(!infile.atEnd())
{
QString line = infile.readLine();
QStringList list;
if(type == 3 || type == 5) list = line.split('|');//, QString::SkipEmptyParts); //RSBN data (rsbn.dat) and xnvu_wps.txt
else if(type == 4) list = line.split(' ', QString::SkipEmptyParts); //X-Plane navdata (earth_nav.dat) and all XP11 files
else list = line.split(',', QString::SkipEmptyParts); //XP10 other files
switch(type) //else
{
case 0: validate_airport_XP10(list); break;
case 1: validate_navaid_XP10(list); break;
case 2: validate_waypoint_XP10(list); break;
case 3: validate_RSBN(list); break;
case 4: validate_earthnav_XP10(list); break;
case 5: validate_xnvu(list); break;
}//switch, else
}//while
infile.close();
return 1;
}//void load
int XFMS_DATA::_loadAirportsXP11(QString& sError)
{
//1. Default apt.dat: "Resources\default scenery\default apt dat\Earth nav data\apt.dat"
//2. Gateway apt.dat (overrides default apt.dat): "Custom Scenery\Global Airports\Earth nav data\apt.dat"
//3. Custom apt.dat (overrides both above): "Custom Scenery/XXXXX/Earth nav data\apt.dat"
//We should load in above steps reversed, i.e. 3, 2 then 1 and ignoring previously added airports, using ICAO as identifier.
sError = "";
//1. Load custom airports first
//TODO: Define DialogSettings::customAirportsDir
//QDir dir(DialogSettings::customAirportsDir);
QDir dir(DialogSettings::xDir + "/Custom Scenery");
if(DialogSettings::XP11_includeCustomAirports)
{
QFileInfoList files = dir.entryInfoList();
foreach(const QFileInfo &fi, files)
{
QString path = fi.absoluteFilePath();
if(fi.baseName().compare("Global Airports") == 0) continue;
if(fi.isDir())
{
path = path + "/Earth nav data/apt.dat";
QFile inFile(path);
if(!inFile.open(QIODevice::ReadOnly | QIODevice::Text)) continue; //apt.dat is not readable or found in custom airport folder
validate_airports_XP11(inFile, WAYPOINT::ORIGIN_X11_CUSTOM_AIRPORTS);
inFile.close();
}//if
}//foreach
}//if
//2. Load gateway apt.dat
QString path = DialogSettings::xDir + "/Custom Scenery/Global Airports/Earth nav data/apt.dat";
QFile inFile(path);
if(inFile.open(QIODevice::ReadOnly | QIODevice::Text)) //If apt.dat is readable and found
{
validate_airports_XP11(inFile, WAYPOINT::ORIGIN_X11_GATEWAY_AIRPORTS);
}
else sError = "[GATEWAY apt.dat]";
inFile.close();
//3. Load default apt.dat
path = DialogSettings::xDir + "/Resources/default scenery/default apt dat/Earth nav data/apt.dat";
inFile.setFileName(path);
if(inFile.open(QIODevice::ReadOnly | QIODevice::Text)) //If apt.dat is readable and found
{
validate_airports_XP11(inFile, WAYPOINT::ORIGIN_X11_DEFAULT_AIRPORTS);
}
else
{
if(sError.length()>0) sError = "[DEFAULT & GATEWAY apt.dat]";
else sError = "[DEFAULT apt.dat]";
}
inFile.close();
return 1;
}
int XFMS_DATA::_loadWaypointsXP11(QString& sError)
{
sError = "";
//Read custom earth_fix.dat
QString path = DialogSettings::xDir + "/Custom Data/earth_fix.dat";
QFile inFile(path);
if(inFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
while(!inFile.atEnd())
{
QString line = inFile.readLine();
QStringList list;
list = line.split(' ', QString::SkipEmptyParts);
validate_waypoint_XP11(list, WAYPOINT::ORIGIN_X11_CUSTOM_FIXES);
}//while
inFile.close();
return 1;
}
inFile.close();
//If custom earth_fix.dat is not found, read default earth_fix.dat
path = DialogSettings::xDir + "/Resources/default data/earth_fix.dat";
inFile.setFileName(path);
if(inFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
while(!inFile.atEnd())
{
QString line = inFile.readLine();
QStringList list;
list = line.split(' ', QString::SkipEmptyParts);
validate_waypoint_XP11(list, WAYPOINT::ORIGIN_X11_DEFAULT_FIXES);
}//while
inFile.close();
return 1;
}
inFile.close();
sError = "[earth_fix.dat]";
return 0;
}
int XFMS_DATA::_loadNavDataXP11(QString& sError)
{
sError = "";
QString path = DialogSettings::xDir + "/Custom Data/earth_nav.dat";
QFile inFile(path);
if(inFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
while(!inFile.atEnd())
{
QString line = inFile.readLine();
QStringList list;
list = line.split(' ', QString::SkipEmptyParts);
validate_earthnav_XP11(list, WAYPOINT::ORIGIN_X11_CUSTOM_EARTHNAV);
}//while
inFile.close();
return 1;
}
inFile.close();
path = DialogSettings::xDir + "/Resources/default data/earth_nav.dat";
inFile.setFileName(path);
if(inFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
while(!inFile.atEnd())
{
QString line = inFile.readLine();
QStringList list;
list = line.split(' ', QString::SkipEmptyParts);
validate_earthnav_XP11(list, WAYPOINT::ORIGIN_X11_DEFAULT_EARTHNAV);
}//while
inFile.close();
return 1;
}
inFile.close();
sError = "[earth_nav.dat]";
return 0;
}
int XFMS_DATA::_loadAirwaysXP11(QString& sError)
{
sError = "";
QString path = DialogSettings::xDir + "/Custom Data/earth_awy.dat";
QFile inFile(path);
if(inFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
validate_airways_XP11(inFile, WAYPOINT::ORIGIN_X11_CUSTOM_ATS);
inFile.close();
return 1;
}
inFile.close();
path = DialogSettings::xDir + "/Resources/default data/earth_awy.dat";
inFile.setFileName(path);
if(inFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
validate_airways_XP11(inFile, WAYPOINT::ORIGIN_X11_DEFAULT_ATS);
inFile.close();
return 1;
}
inFile.close();
sError = "[earth_awy.dat]";
return 0;
}
void XFMS_DATA::load(int _dat)
{
QString sError;
QString rError;
dat = _dat;
__SL_FINISHED.clear();
__CURRENT_LOADING = 0;
for(int i=0; i<8; i++) __DATA_LOADED[i] = 0;
__ERROR_LOADING = "";
if(DialogSettings::XP11)
{
if(!_loadAirportsXP11(sError))
{
//1. Default apt.dat: "Resources\default scenery\default apt dat\Earth nav data\apt.dat"
//2. Gateway apt.dat (overrides default apt.dat): "Custom Scenery\Global Airports\Earth nav data\apt.dat"
//3. Custom apt.dat (overrides both above): "Custom Scenery/XXXXX/Earth nav data\apt.dat"
//We should load in above steps reversed, i.e. 3, 2 then 1 and ignoring previously added airports, using ICAO as identifier.
}
__CURRENT_LOADING++;
if(!_loadWaypointsXP11(rError))
{
//1. "Resources/default data/earth_fix.dat"
//2. "Custom Data/earth_fix.dat"
//If number 2 exist, we ignore number 1.
sError = sError + " " + rError;
}
__CURRENT_LOADING++;
if(!_loadNavDataXP11(rError))
{
//1. "Resources/default data/earth_nav.dat"
//2. "Custom Data/earth_nav.dat"
//3. "Custom Scenery/Global Airports/Earth nav data/earth_nav.dat"
//We do load the ILS beacons from 3 first. If number 2 exist, we ignore number 1. Then we load 1 or 2 and ignore ILS beacons which we already have added from 3.
//EDIT: Change, we skip loading ILS beacons (step 3) for now.
sError = sError + " " + rError;
}
__CURRENT_LOADING++;
if(!_loadAirwaysXP11(rError))
{
//1. "Resources/default data/earth_awy.dat"
//2. "Custom Data/earth_awy.dat"
//If number 2 exist, we ignore number 1.
sError = sError + " " + rError;
}
}
else
{
if(!_loadXP10(DialogSettings::fileAirports, 0))
{
sError = " [Airports.txt]";
}
__CURRENT_LOADING++;
if(!_loadXP10(DialogSettings::fileNavaids, 1))
{
sError = sError + " [Navaids.txt]";
}
__CURRENT_LOADING++;
if(!_loadXP10(DialogSettings::fileWaypoints, 2))
{
sError = sError + " [Waypoints.txt]";
}
__CURRENT_LOADING++;
if(!_loadXP10(DialogSettings::fileNavdata, 4))
{
sError = sError + " [earth_nav.txt]";
}
__CURRENT_LOADING++;
if(!_loadXP10(DialogSettings::fileAirways, 6))
{
sError = sError + " [ats.txt]";
}
}//else
__CURRENT_LOADING++;
if(!_loadXP10(DialogSettings::fileRSBN, 3))
{
sError = sError + " [rsbn.dat]";
}
__CURRENT_LOADING++;
if(!_loadXP10(XNVU_WPS_FILENAME, 5))
{
sError = sError + " [xnvu_wps.txt]";
}
__CURRENT_LOADING++;
__ERROR_LOADING = sError;
}
void XFMS_DATA::addXNVUData(std::vector<NVUPOINT*> lP)
{
for(unsigned int i=0; i<lP.size(); i++)
{
addXNVUWaypoint(lP[i]);
}
}
void XFMS_DATA::addXNVUWaypoint(NVUPOINT* lP)
{
lXNVU.push_back(lP);
lWP.insert(std::make_pair(lP->name, lP));
lWP2.insert(std::make_pair(lP->name2, lP));
}
void XFMS_DATA::removeXNVUWaypoint(NVUPOINT* lP)
{
std::vector<NVUPOINT*>::iterator iL;
std::multimap<QString, NVUPOINT*>::iterator iM;
for(iL = lXNVU.begin(); iL!=lXNVU.end(); iL++)
{
if(*iL == lP)
{
lXNVU.erase(iL);
break;
}
}
//Remove points from lWP and lWP2
for(iM = lWP.begin(); iM!=lWP.end(); iM++)
{
if((*iM).second == lP)
{
lWP.erase(iM);
break;
}//if
}
for(iM = lWP2.begin(); iM!=lWP2.end(); iM++)
{
if((*iM).second == lP)
{
lWP2.erase(iM);
break;
}//if
}
}
void XFMS_DATA::validate_airports_XP11(QFile& infile, int wpOrigin)
{
/* The new apt.dat format gives elevation and region data with these tags:
* 1302 datum_lat
* 1302 datum_lon
* 1302 icao_code
* 1302 faa_code
* 1302 region_code
* If the datum do not exist, take middle on the first runway (100/101), or the first helipad of no runway exist (102).
*/
QString line;
QStringList record;
/* currentState values:
* 0: Searching for airport header
* 1: Airport header
* 2: Searching for 1302 datum_xx or 100-102
* 1302: 1302 datum_xx header
* 100: 1st 100 header
* 102: 1st 101 header
* 103: 1st 102 header
*/
int currentState = 0;
QString qstr;
int cmpData;
bool lat_set = false;
bool lon_set = false;
CPoint latA, latB;
int surfaceType = 15; //Transparent as default, hence unknown.
QString rwyIdentifierA = "";
QString rwyIdentifierB = "";
double rwyLength = -1;
double rwyWidth = -1;
int lightA = 0;
int lightB = 0;
QString city = "";
std::vector<RUNWAY*> lRunways;
std::vector<std::pair<int, int> > lFreq;
NVUPOINT wp;
wp.type = WAYPOINT::TYPE_AIRPORT;
while(!infile.atEnd())
{
line = infile.readLine();
record = line.split(' ', QString::SkipEmptyParts);
for(int i=0; i<record.size(); i++)
{
qstr = record[i].simplified();
switch(i)
{
case 0: //Row type
cmpData = qstr.toInt();
if(cmpData == 1 || cmpData == 16 || cmpData == 17)
{
//If first item in row is 1, 16 or 17, it tells us this is the start of an airport data.
//We set the currentState to 1 to show we begin with a new initialization of an airport.
//Note that similar code is also placed in the end of this function, to grab the last found airport.
wp.wpOrigin = wpOrigin;
wp.type = WAYPOINT::TYPE_AIRPORT;
wp.data = NULL;