]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
Percentage feedback during nav-cache build.
[flightgear.git] / src / Airports / apt_loader.cxx
1 // apt_loader.cxx -- a front end loader of the apt.dat file.  This loader
2 //                   populates the runway and basic classes.
3 //
4 // Written by Curtis Olson, started August 2000.
5 //
6 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include "apt_loader.hxx"
30
31 #include <simgear/compiler.h>
32
33 #include <stdlib.h> // atof(), atoi()
34 #include <string.h> // memchr()
35 #include <ctype.h> // isspace()
36
37 #include <simgear/constants.h>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sgstream.hxx>
40 #include <simgear/misc/strutils.hxx>
41 #include <simgear/structure/exception.hxx>
42 #include <simgear/misc/sg_path.hxx>
43
44 #include <string>
45
46 #include "airport.hxx"
47 #include "runways.hxx"
48 #include "pavement.hxx"
49 #include <Navaids/NavDataCache.hxx>
50 #include <ATC/CommStation.hxx>
51
52 #include <iostream>
53
54 using namespace std;
55
56 typedef SGSharedPtr<FGPavement> FGPavementPtr;
57
58 static FGPositioned::Type fptypeFromRobinType(int aType)
59 {
60   switch (aType) {
61   case 1: return FGPositioned::AIRPORT;
62   case 16: return FGPositioned::SEAPORT;
63   case 17: return FGPositioned::HELIPORT;
64   default:
65     SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
66     throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
67   }
68 }
69
70 // generated by 'wc -l' on the uncompressed file
71 const unsigned int LINES_IN_APT_DAT = 2465648;
72
73 namespace flightgear
74 {
75   
76 class APTLoader
77 {
78 public:
79
80   APTLoader()
81   :  last_apt_id(""),
82      last_apt_elev(0.0),
83      last_apt_info("")
84   {
85     currentAirportID = 0;
86     cache = NavDataCache::instance();
87   }
88
89   void parseAPT(const SGPath &aptdb_file)
90   {
91     sg_gzifstream in( aptdb_file.str() );
92
93     if ( !in.is_open() ) {
94         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
95         throw sg_io_exception("cannot open APT file", aptdb_file);
96     }
97
98     string line;
99     char tmp[2049];
100     tmp[2048] = 0;
101     
102     unsigned int line_id = 0;
103     unsigned int line_num = 0;
104
105     while ( ! in.eof() ) {
106       in.getline(tmp, 2048);
107       line = tmp; // string copy, ack
108       line_num++;
109
110       if ( line.empty() || isspace(tmp[0]) || tmp[0] == '#' ) {
111         continue;
112       }
113
114         if ((line_num % 100) == 0) {
115             // every 100 lines
116             unsigned int percent = (line_num * 100) / LINES_IN_APT_DAT;
117             cache->setRebuildPhaseProgress(NavDataCache::REBUILD_AIRPORTS, percent);
118         }
119
120       if (line.size() >= 3) {
121           char *p = (char *)memchr(tmp, ' ', 3);
122           if ( p )
123               *p = 0;
124       }
125
126       line_id = atoi(tmp);
127       if ( tmp[0] == 'I' || tmp[0] == 'A' ) {
128         // First line, indicates IBM ("I") or Macintosh ("A")
129         // line endings.
130
131         // move past this line and read and discard the next line
132         // which is the version and copyright information
133         in.getline(tmp, 2048);
134
135         if ( strlen(tmp) > 5 ) {
136            char *p = (char *)memchr(tmp, ' ', 5);
137            if ( p )
138               *p = 0;
139         }
140         SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
141       } else if ( line_id == 1 /* Airport */ ||
142                     line_id == 16 /* Seaplane base */ ||
143                     line_id == 17 /* Heliport */ ) {
144         parseAirportLine(simgear::strutils::split(line));
145       } else if ( line_id == 10 ) { // Runway v810
146         parseRunwayLine810(simgear::strutils::split(line));
147       } else if ( line_id == 100 ) { // Runway v850
148         parseRunwayLine850(simgear::strutils::split(line));
149       } else if ( line_id == 101 ) { // Water Runway v850
150         parseWaterRunwayLine850(simgear::strutils::split(line));
151       } else if ( line_id == 102 ) { // Helipad v850
152         parseHelipadLine850(simgear::strutils::split(line));
153       } else if ( line_id == 18 ) {
154             // beacon entry (ignore)
155       } else if ( line_id == 14 ) {
156         // control tower entry
157         vector<string> token(simgear::strutils::split(line));
158         
159         double lat = atof( token[1].c_str() );
160         double lon = atof( token[2].c_str() );
161         double elev = atof( token[3].c_str() );
162         tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
163         got_tower = true;
164         
165         cache->insertTower(currentAirportID, tower);
166       } else if ( line_id == 19 ) {
167           // windsock entry (ignore)
168       } else if ( line_id == 20 ) {
169           // Taxiway sign (ignore)
170       } else if ( line_id == 21 ) {
171           // lighting objects (ignore)
172       } else if ( line_id == 15 ) {
173           // custom startup locations (ignore)
174       } else if ( line_id == 0 ) {
175           // ??
176       } else if ( line_id >= 50 && line_id <= 56) {
177         parseCommLine(line_id, simgear::strutils::split(line));
178       } else if ( line_id == 110 ) {
179         pavement = true;
180         parsePavementLine850(simgear::strutils::split(line, 0, 4));
181       } else if ( line_id >= 111 && line_id <= 114 ) {
182         if ( pavement )
183           parsePavementNodeLine850(line_id, simgear::strutils::split(line));
184       } else if ( line_id >= 115 && line_id <= 116 ) {
185           // other pavement nodes (ignore)
186       } else if ( line_id == 120 ) {
187         pavement = false;
188       } else if ( line_id == 130 ) {
189         pavement = false;
190       } else if ( line_id >= 1000 ) {
191           // airport traffic flow (ignore)
192       } else if ( line_id == 99 ) {
193           SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
194       } else {
195           SG_LOG( SG_GENERAL, SG_ALERT, 
196                   "Unknown line(#" << line_num << ") in apt.dat file: " << line );
197           throw sg_format_exception("malformed line in apt.dat:", line);
198       }
199     }
200
201     finishAirport();
202   }
203   
204 private:
205   vector<string> token;
206   double rwy_lat_accum;
207   double rwy_lon_accum;
208   double last_rwy_heading;
209   int rwy_count;
210   bool got_tower;
211   string last_apt_id;
212   double last_apt_elev;
213   SGGeod tower;
214   string last_apt_info;
215   string pavement_ident;
216   bool pavement;
217   
218   //vector<FGRunwayPtr> runways;
219   //vector<FGTaxiwayPtr> taxiways;
220   vector<FGPavementPtr> pavements;
221   
222   NavDataCache* cache;
223   PositionedID currentAirportID;
224   
225   void finishAirport()
226   {
227     if (currentAirportID == 0) {
228       return;
229     }
230     
231     if (!rwy_count) {
232       currentAirportID = 0;
233       SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
234               << ", skipping." );
235       return;
236     }
237
238     double lat = rwy_lat_accum / (double)rwy_count;
239     double lon = rwy_lon_accum / (double)rwy_count;
240
241     if (!got_tower) {
242       // tower height hard coded for now...
243       const float tower_height = 50.0f;
244       // make a little off the heading for 1 runway airports...
245       float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
246       float fudge_lat = .003f - fudge_lon;
247       tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
248       
249       cache->insertTower(currentAirportID, tower);
250     }
251
252     SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
253     cache->updatePosition(currentAirportID, pos);
254     
255     currentAirportID = 0;
256   }
257   
258   void parseAirportLine(const vector<string>& token)
259   {
260     const string& id(token[4]);
261     double elev = atof( token[1].c_str() );
262
263   // finish the previous airport
264     finishAirport();
265             
266     last_apt_elev = elev;
267     got_tower = false;
268
269     string name;
270     // build the name
271     for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
272         name += token[i] + " ";
273     }
274     name += token[token.size() - 1];
275
276     // clear runway list for start of next airport
277     rwy_lon_accum = 0.0;
278     rwy_lat_accum = 0.0;
279     rwy_count = 0;
280     
281     int robinType = atoi(token[0].c_str());
282     currentAirportID = cache->insertAirport(fptypeFromRobinType(robinType), id, name);
283   }
284   
285   void parseRunwayLine810(const vector<string>& token)
286   {
287     double lat = atof( token[1].c_str() );
288     double lon = atof( token[2].c_str() );
289     rwy_lat_accum += lat;
290     rwy_lon_accum += lon;
291     rwy_count++;
292
293     const string& rwy_no(token[3]);
294
295     double heading = atof( token[4].c_str() );
296     double length = atoi( token[5].c_str() );
297     double width = atoi( token[8].c_str() );
298     length *= SG_FEET_TO_METER;
299     width *= SG_FEET_TO_METER;
300
301     // adjust lat / lon to the start of the runway/taxiway, not the middle
302     SGGeod pos_1 = SGGeodesy::direct( SGGeod::fromDegFt(lon, lat, last_apt_elev), heading, -length/2 );
303
304     last_rwy_heading = heading;
305
306     int surface_code = atoi( token[10].c_str() );
307
308     if (rwy_no[0] == 'x') {  // Taxiway
309       cache->insertRunway(FGPositioned::TAXIWAY, rwy_no, pos_1, currentAirportID,
310                           heading, length, width, 0.0, 0.0, surface_code);
311     } else if (rwy_no[0] == 'H') {  // Helipad
312       SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
313       cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos, currentAirportID,
314                           heading, length, width, 0.0, 0.0, surface_code);
315     } else {
316       // (pair of) runways
317       string rwy_displ_threshold = token[6];
318       vector<string> displ
319           = simgear::strutils::split( rwy_displ_threshold, "." );
320       double displ_thresh1 = atof( displ[0].c_str() );
321       double displ_thresh2 = atof( displ[1].c_str() );
322       displ_thresh1 *= SG_FEET_TO_METER;
323       displ_thresh2 *= SG_FEET_TO_METER;
324
325       string rwy_stopway = token[7];
326       vector<string> stop
327           = simgear::strutils::split( rwy_stopway, "." );
328       double stopway1 = atof( stop[0].c_str() );
329       double stopway2 = atof( stop[1].c_str() );
330       stopway1 *= SG_FEET_TO_METER;
331       stopway2 *= SG_FEET_TO_METER;
332
333       SGGeod pos_2 = SGGeodesy::direct( pos_1, heading, length );
334
335       PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos_1,
336                                              currentAirportID, heading, length,
337                                              width, displ_thresh1, stopway1,
338                                              surface_code);
339       
340       PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
341                                              FGRunway::reverseIdent(rwy_no), pos_2,
342                                              currentAirportID,
343                                              SGMiscd::normalizePeriodic(0, 360, heading + 180.0),
344                                              length, width, displ_thresh2, stopway2,
345                                              surface_code);
346
347       cache->setRunwayReciprocal(rwy, reciprocal);
348     }
349   }
350
351   void parseRunwayLine850(const vector<string>& token)
352   {
353     double width = atof( token[1].c_str() );
354     int surface_code = atoi( token[2].c_str() );
355
356     double lat_1 = atof( token[9].c_str() );
357     double lon_1 = atof( token[10].c_str() );
358     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
359     rwy_lat_accum += lat_1;
360     rwy_lon_accum += lon_1;
361     rwy_count++;
362
363     double lat_2 = atof( token[18].c_str() );
364     double lon_2 = atof( token[19].c_str() );
365     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
366     rwy_lat_accum += lat_2;
367     rwy_lon_accum += lon_2;
368     rwy_count++;
369
370     double length, heading_1, heading_2;
371     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
372
373     last_rwy_heading = heading_1;
374
375     const string& rwy_no_1(token[8]);
376     const string& rwy_no_2(token[17]);
377     if ( rwy_no_1.empty() || rwy_no_2.empty() )
378         return;
379
380     double displ_thresh1 = atof( token[11].c_str() );
381     double displ_thresh2 = atof( token[20].c_str() );
382
383     double stopway1 = atof( token[12].c_str() );
384     double stopway2 = atof( token[21].c_str() );
385
386     PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
387                                            currentAirportID, heading_1, length,
388                                            width, displ_thresh1, stopway1,
389                                            surface_code);
390     
391     PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
392                                                   rwy_no_2, pos_2,
393                                                   currentAirportID, heading_2, length,
394                                                   width, displ_thresh2, stopway2,
395                                                   surface_code);
396     
397     cache->setRunwayReciprocal(rwy, reciprocal);
398   }
399
400   void parseWaterRunwayLine850(const vector<string>& token)
401   {
402     double width = atof( token[1].c_str() );
403
404     double lat_1 = atof( token[4].c_str() );
405     double lon_1 = atof( token[5].c_str() );
406     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
407     rwy_lat_accum += lat_1;
408     rwy_lon_accum += lon_1;
409     rwy_count++;
410
411     double lat_2 = atof( token[7].c_str() );
412     double lon_2 = atof( token[8].c_str() );
413     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
414     rwy_lat_accum += lat_2;
415     rwy_lon_accum += lon_2;
416     rwy_count++;
417
418     double length, heading_1, heading_2;
419     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
420
421     last_rwy_heading = heading_1;
422
423     const string& rwy_no_1(token[3]);
424     const string& rwy_no_2(token[6]);
425
426     PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
427                                            currentAirportID, heading_1, length,
428                                            width, 0.0, 0.0, 13);
429     
430     PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
431                                                   rwy_no_2, pos_2,
432                                                   currentAirportID, heading_2, length,
433                                                   width, 0.0, 0.0, 13);
434     
435     cache->setRunwayReciprocal(rwy, reciprocal);
436   }
437
438   void parseHelipadLine850(const vector<string>& token)
439   {
440     double length = atof( token[5].c_str() );
441     double width = atof( token[6].c_str() );
442
443     double lat = atof( token[2].c_str() );
444     double lon = atof( token[3].c_str() );
445     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
446     rwy_lat_accum += lat;
447     rwy_lon_accum += lon;
448     rwy_count++;
449
450     double heading = atof( token[4].c_str() );
451
452     last_rwy_heading = heading;
453
454     const string& rwy_no(token[1]);
455     int surface_code = atoi( token[7].c_str() );
456
457     cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos,
458                         currentAirportID, heading, length,
459                         width, 0.0, 0.0, surface_code);
460   }
461
462   void parsePavementLine850(const vector<string>& token)
463   {
464     if ( token.size() >= 5 ) {
465       pavement_ident = token[4];
466       if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
467         pavement_ident.erase( pavement_ident.size()-1 );
468     } else {
469       pavement_ident = "xx";
470     }
471   }
472
473   void parsePavementNodeLine850(int num, const vector<string>& token)
474   {
475     double lat = atof( token[1].c_str() );
476     double lon = atof( token[2].c_str() );
477     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
478
479     FGPavement* pvt = 0;
480     if ( !pavement_ident.empty() ) {
481       pvt = new FGPavement( 0, pavement_ident, pos );
482       pavements.push_back( pvt );
483       pavement_ident = "";
484     } else {
485       pvt = pavements.back();
486     }
487     if ( num == 112 || num == 114 ) {
488       double lat_b = atof( token[3].c_str() );
489       double lon_b = atof( token[4].c_str() );
490       SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
491       pvt->addBezierNode(pos, pos_b, num == 114);
492     } else {
493       pvt->addNode(pos, num == 113);
494     }
495   }
496
497   void parseCommLine(int lineId, const vector<string>& token) 
498   {
499     if ( rwy_count <= 0 ) {
500       SG_LOG( SG_GENERAL, SG_ALERT, "No runways; skipping comm for " + last_apt_id);
501     }
502  
503     SGGeod pos = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count, 
504         rwy_lat_accum / (double)rwy_count, last_apt_elev);
505     
506     // short int representing tens of kHz:
507     int freqKhz = atoi(token[1].c_str()) * 10;
508     int rangeNm = 50;
509     FGPositioned::Type ty;
510     // Make sure we only pass on stations with at least a name
511     if (token.size() >2){
512
513         switch (lineId) {
514             case 50:
515                 ty = FGPositioned::FREQ_AWOS;
516                 for( size_t i = 2; i < token.size(); ++i )
517                 {
518                   if( token[i] == "ATIS" )
519                   {
520                     ty = FGPositioned::FREQ_ATIS;
521                     break;
522                   }
523                 }
524                 break;
525
526             case 51:    ty = FGPositioned::FREQ_UNICOM; break;
527             case 52:    ty = FGPositioned::FREQ_CLEARANCE; break;
528             case 53:    ty = FGPositioned::FREQ_GROUND; break;
529             case 54:    ty = FGPositioned::FREQ_TOWER; break;
530             case 55:
531             case 56:    ty = FGPositioned::FREQ_APP_DEP; break;
532             default:
533                 throw sg_range_exception("unsupported apt.dat comm station type");
534         }
535
536       // Name can contain white spaces. All tokens after the second token are
537       // part of the name.
538       std::string name = token[2];
539       for( size_t i = 3; i < token.size(); ++i )
540         name += ' ' + token[i];
541
542       cache->insertCommStation(ty, name, pos, freqKhz, rangeNm, currentAirportID);
543     }
544     else SG_LOG( SG_GENERAL, SG_DEBUG, "Found unnamed comm. Skipping: " << lineId);
545   }
546
547 };
548   
549 // Load the airport data base from the specified aptdb file.  The
550 // metar file is used to mark the airports as having metar available
551 // or not.
552 bool airportDBLoad( const SGPath &aptdb_file )
553 {
554   APTLoader ld;
555   ld.parseAPT(aptdb_file);
556   return true;
557 }
558   
559 bool metarDataLoad(const SGPath& metar_file)
560 {
561   sg_gzifstream metar_in( metar_file.str() );
562   if ( !metar_in.is_open() ) {
563     SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
564     return false;
565   }
566   
567   NavDataCache* cache = NavDataCache::instance();
568
569   string ident;
570   while ( metar_in ) {
571     metar_in >> ident;
572     if ( ident == "#" || ident == "//" ) {
573       metar_in >> skipeol;
574     } else {
575       cache->setAirportMetar(ident, true);
576     }
577   }
578   
579   return true;
580 }
581
582 } // of namespace flightgear