]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
apt.dat parser: clearer log and exception messages
[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     std::string apt_dat = aptdb_file.str();
92     sg_gzifstream in(apt_dat);
93
94     if ( !in.is_open() ) {
95       SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << apt_dat );
96       throw sg_io_exception("cannot open apt.dat file", apt_dat.c_str());
97     }
98
99     string line;
100     char tmp[2049];
101     tmp[2048] = 0;
102     
103     unsigned int line_id = 0;
104     unsigned int line_num = 0;
105
106     while ( ! in.eof() ) {
107       in.getline(tmp, 2048);
108       line = tmp; // string copy, ack
109       line_num++;
110
111       if ( line.empty() || isspace(tmp[0]) || tmp[0] == '#' ) {
112         continue;
113       }
114
115         if ((line_num % 100) == 0) {
116             // every 100 lines
117             unsigned int percent = (line_num * 100) / LINES_IN_APT_DAT;
118             cache->setRebuildPhaseProgress(NavDataCache::REBUILD_AIRPORTS, percent);
119         }
120
121       if (line.size() >= 3) {
122           char *p = (char *)memchr(tmp, ' ', 3);
123           if ( p )
124               *p = 0;
125       }
126
127       line_id = atoi(tmp);
128       if ( tmp[0] == 'I' || tmp[0] == 'A' ) {
129         // First line, indicates IBM ("I") or Macintosh ("A")
130         // line endings.
131
132         // move past this line and read and discard the next line
133         // which is the version and copyright information
134         in.getline(tmp, 2048);
135
136         if ( strlen(tmp) > 5 ) {
137            char *p = (char *)memchr(tmp, ' ', 5);
138            if ( p )
139               *p = 0;
140         }
141         SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
142       } else if ( line_id == 1 /* Airport */ ||
143                     line_id == 16 /* Seaplane base */ ||
144                     line_id == 17 /* Heliport */ ) {
145         parseAirportLine(simgear::strutils::split(line));
146       } else if ( line_id == 10 ) { // Runway v810
147         parseRunwayLine810(simgear::strutils::split(line));
148       } else if ( line_id == 100 ) { // Runway v850
149         parseRunwayLine850(simgear::strutils::split(line));
150       } else if ( line_id == 101 ) { // Water Runway v850
151         parseWaterRunwayLine850(simgear::strutils::split(line));
152       } else if ( line_id == 102 ) { // Helipad v850
153         parseHelipadLine850(simgear::strutils::split(line));
154       } else if ( line_id == 18 ) {
155             // beacon entry (ignore)
156       } else if ( line_id == 14 ) {
157         // control tower entry
158         vector<string> token(simgear::strutils::split(line));
159         
160         double lat = atof( token[1].c_str() );
161         double lon = atof( token[2].c_str() );
162         double elev = atof( token[3].c_str() );
163         tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);        
164         cache->insertTower(currentAirportID, tower);
165       } else if ( line_id == 19 ) {
166           // windsock entry (ignore)
167       } else if ( line_id == 20 ) {
168           // Taxiway sign (ignore)
169       } else if ( line_id == 21 ) {
170           // lighting objects (ignore)
171       } else if ( line_id == 15 ) {
172           // custom startup locations (ignore)
173       } else if ( line_id == 0 ) {
174           // ??
175       } else if ( line_id >= 50 && line_id <= 56) {
176         parseCommLine(line_id, simgear::strutils::split(line));
177       } else if ( line_id == 110 ) {
178         pavement = true;
179         parsePavementLine850(simgear::strutils::split(line, 0, 4));
180       } else if ( line_id >= 111 && line_id <= 114 ) {
181         if ( pavement )
182           parsePavementNodeLine850(line_id, simgear::strutils::split(line));
183       } else if ( line_id >= 115 && line_id <= 116 ) {
184           // other pavement nodes (ignore)
185       } else if ( line_id == 120 ) {
186         pavement = false;
187       } else if ( line_id == 130 ) {
188         pavement = false;
189       } else if ( line_id >= 1000 ) {
190           // airport traffic flow (ignore)
191       } else if ( line_id == 99 ) {
192           SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
193       } else {
194           SG_LOG( SG_GENERAL, SG_ALERT, 
195                   "Unknown line(#" << line_num << ") in apt.dat file: " << line );
196           throw sg_format_exception("malformed line in apt.dat:", line);
197       }
198     }
199
200     finishAirport();
201   }
202   
203 private:
204   vector<string> token;
205   double rwy_lat_accum;
206   double rwy_lon_accum;
207   double last_rwy_heading;
208   int rwy_count;
209   string last_apt_id;
210   double last_apt_elev;
211   SGGeod tower;
212   string last_apt_info;
213   string pavement_ident;
214   bool pavement;
215   
216   //vector<FGRunwayPtr> runways;
217   //vector<FGTaxiwayPtr> taxiways;
218   vector<FGPavementPtr> pavements;
219   
220   NavDataCache* cache;
221   PositionedID currentAirportID;
222   
223   void finishAirport()
224   {
225     if (currentAirportID == 0) {
226       return;
227     }
228     
229     if (!rwy_count) {
230       currentAirportID = 0;
231       SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
232               << ", skipping." );
233       return;
234     }
235
236     double lat = rwy_lat_accum / (double)rwy_count;
237     double lon = rwy_lon_accum / (double)rwy_count;
238
239     SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
240     cache->updatePosition(currentAirportID, pos);
241     
242     currentAirportID = 0;
243   }
244   
245   void parseAirportLine(const vector<string>& token)
246   {
247     const string& id(token[4]);
248     double elev = atof( token[1].c_str() );
249
250   // finish the previous airport
251     finishAirport();
252             
253     last_apt_elev = elev;
254
255     string name;
256     // build the name
257     for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
258         name += token[i] + " ";
259     }
260     name += token[token.size() - 1];
261
262     // clear runway list for start of next airport
263     rwy_lon_accum = 0.0;
264     rwy_lat_accum = 0.0;
265     rwy_count = 0;
266     
267     int robinType = atoi(token[0].c_str());
268     currentAirportID = cache->insertAirport(fptypeFromRobinType(robinType), id, name);
269   }
270   
271   void parseRunwayLine810(const vector<string>& token)
272   {
273     double lat = atof( token[1].c_str() );
274     double lon = atof( token[2].c_str() );
275     rwy_lat_accum += lat;
276     rwy_lon_accum += lon;
277     rwy_count++;
278
279     const string& rwy_no(token[3]);
280
281     double heading = atof( token[4].c_str() );
282     double length = atoi( token[5].c_str() );
283     double width = atoi( token[8].c_str() );
284     length *= SG_FEET_TO_METER;
285     width *= SG_FEET_TO_METER;
286
287     // adjust lat / lon to the start of the runway/taxiway, not the middle
288     SGGeod pos_1 = SGGeodesy::direct( SGGeod::fromDegFt(lon, lat, last_apt_elev), heading, -length/2 );
289
290     last_rwy_heading = heading;
291
292     int surface_code = atoi( token[10].c_str() );
293
294     if (rwy_no[0] == 'x') {  // Taxiway
295       cache->insertRunway(FGPositioned::TAXIWAY, rwy_no, pos_1, currentAirportID,
296                           heading, length, width, 0.0, 0.0, surface_code);
297     } else if (rwy_no[0] == 'H') {  // Helipad
298       SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
299       cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos, currentAirportID,
300                           heading, length, width, 0.0, 0.0, surface_code);
301     } else {
302       // (pair of) runways
303       string rwy_displ_threshold = token[6];
304       vector<string> displ
305           = simgear::strutils::split( rwy_displ_threshold, "." );
306       double displ_thresh1 = atof( displ[0].c_str() );
307       double displ_thresh2 = atof( displ[1].c_str() );
308       displ_thresh1 *= SG_FEET_TO_METER;
309       displ_thresh2 *= SG_FEET_TO_METER;
310
311       string rwy_stopway = token[7];
312       vector<string> stop
313           = simgear::strutils::split( rwy_stopway, "." );
314       double stopway1 = atof( stop[0].c_str() );
315       double stopway2 = atof( stop[1].c_str() );
316       stopway1 *= SG_FEET_TO_METER;
317       stopway2 *= SG_FEET_TO_METER;
318
319       SGGeod pos_2 = SGGeodesy::direct( pos_1, heading, length );
320
321       PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos_1,
322                                              currentAirportID, heading, length,
323                                              width, displ_thresh1, stopway1,
324                                              surface_code);
325       
326       PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
327                                              FGRunway::reverseIdent(rwy_no), pos_2,
328                                              currentAirportID,
329                                              SGMiscd::normalizePeriodic(0, 360, heading + 180.0),
330                                              length, width, displ_thresh2, stopway2,
331                                              surface_code);
332
333       cache->setRunwayReciprocal(rwy, reciprocal);
334     }
335   }
336
337   void parseRunwayLine850(const vector<string>& token)
338   {
339     double width = atof( token[1].c_str() );
340     int surface_code = atoi( token[2].c_str() );
341
342     double lat_1 = atof( token[9].c_str() );
343     double lon_1 = atof( token[10].c_str() );
344     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
345     rwy_lat_accum += lat_1;
346     rwy_lon_accum += lon_1;
347     rwy_count++;
348
349     double lat_2 = atof( token[18].c_str() );
350     double lon_2 = atof( token[19].c_str() );
351     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
352     rwy_lat_accum += lat_2;
353     rwy_lon_accum += lon_2;
354     rwy_count++;
355
356     double length, heading_1, heading_2;
357     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
358
359     last_rwy_heading = heading_1;
360
361     const string& rwy_no_1(token[8]);
362     const string& rwy_no_2(token[17]);
363     if ( rwy_no_1.empty() || rwy_no_2.empty() )
364         return;
365
366     double displ_thresh1 = atof( token[11].c_str() );
367     double displ_thresh2 = atof( token[20].c_str() );
368
369     double stopway1 = atof( token[12].c_str() );
370     double stopway2 = atof( token[21].c_str() );
371
372     PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
373                                            currentAirportID, heading_1, length,
374                                            width, displ_thresh1, stopway1,
375                                            surface_code);
376     
377     PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
378                                                   rwy_no_2, pos_2,
379                                                   currentAirportID, heading_2, length,
380                                                   width, displ_thresh2, stopway2,
381                                                   surface_code);
382     
383     cache->setRunwayReciprocal(rwy, reciprocal);
384   }
385
386   void parseWaterRunwayLine850(const vector<string>& token)
387   {
388     double width = atof( token[1].c_str() );
389
390     double lat_1 = atof( token[4].c_str() );
391     double lon_1 = atof( token[5].c_str() );
392     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
393     rwy_lat_accum += lat_1;
394     rwy_lon_accum += lon_1;
395     rwy_count++;
396
397     double lat_2 = atof( token[7].c_str() );
398     double lon_2 = atof( token[8].c_str() );
399     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
400     rwy_lat_accum += lat_2;
401     rwy_lon_accum += lon_2;
402     rwy_count++;
403
404     double length, heading_1, heading_2;
405     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
406
407     last_rwy_heading = heading_1;
408
409     const string& rwy_no_1(token[3]);
410     const string& rwy_no_2(token[6]);
411
412     PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
413                                            currentAirportID, heading_1, length,
414                                            width, 0.0, 0.0, 13);
415     
416     PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
417                                                   rwy_no_2, pos_2,
418                                                   currentAirportID, heading_2, length,
419                                                   width, 0.0, 0.0, 13);
420     
421     cache->setRunwayReciprocal(rwy, reciprocal);
422   }
423
424   void parseHelipadLine850(const vector<string>& token)
425   {
426     double length = atof( token[5].c_str() );
427     double width = atof( token[6].c_str() );
428
429     double lat = atof( token[2].c_str() );
430     double lon = atof( token[3].c_str() );
431     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
432     rwy_lat_accum += lat;
433     rwy_lon_accum += lon;
434     rwy_count++;
435
436     double heading = atof( token[4].c_str() );
437
438     last_rwy_heading = heading;
439
440     const string& rwy_no(token[1]);
441     int surface_code = atoi( token[7].c_str() );
442
443     cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos,
444                         currentAirportID, heading, length,
445                         width, 0.0, 0.0, surface_code);
446   }
447
448   void parsePavementLine850(const vector<string>& token)
449   {
450     if ( token.size() >= 5 ) {
451       pavement_ident = token[4];
452       if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
453         pavement_ident.erase( pavement_ident.size()-1 );
454     } else {
455       pavement_ident = "xx";
456     }
457   }
458
459   void parsePavementNodeLine850(int num, const vector<string>& token)
460   {
461     double lat = atof( token[1].c_str() );
462     double lon = atof( token[2].c_str() );
463     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
464
465     FGPavement* pvt = 0;
466     if ( !pavement_ident.empty() ) {
467       pvt = new FGPavement( 0, pavement_ident, pos );
468       pavements.push_back( pvt );
469       pavement_ident = "";
470     } else {
471       pvt = pavements.back();
472     }
473     if ( num == 112 || num == 114 ) {
474       double lat_b = atof( token[3].c_str() );
475       double lon_b = atof( token[4].c_str() );
476       SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
477       pvt->addBezierNode(pos, pos_b, num == 114);
478     } else {
479       pvt->addNode(pos, num == 113);
480     }
481   }
482
483   void parseCommLine(int lineId, const vector<string>& token) 
484   {
485     if ( rwy_count <= 0 ) {
486       SG_LOG( SG_GENERAL, SG_ALERT, "No runways; skipping comm for " + last_apt_id);
487     }
488  
489     SGGeod pos = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count, 
490         rwy_lat_accum / (double)rwy_count, last_apt_elev);
491     
492     // short int representing tens of kHz:
493     int freqKhz = atoi(token[1].c_str()) * 10;
494     int rangeNm = 50;
495     FGPositioned::Type ty;
496     // Make sure we only pass on stations with at least a name
497     if (token.size() >2){
498
499         switch (lineId) {
500             case 50:
501                 ty = FGPositioned::FREQ_AWOS;
502                 for( size_t i = 2; i < token.size(); ++i )
503                 {
504                   if( token[i] == "ATIS" )
505                   {
506                     ty = FGPositioned::FREQ_ATIS;
507                     break;
508                   }
509                 }
510                 break;
511
512             case 51:    ty = FGPositioned::FREQ_UNICOM; break;
513             case 52:    ty = FGPositioned::FREQ_CLEARANCE; break;
514             case 53:    ty = FGPositioned::FREQ_GROUND; break;
515             case 54:    ty = FGPositioned::FREQ_TOWER; break;
516             case 55:
517             case 56:    ty = FGPositioned::FREQ_APP_DEP; break;
518             default:
519                 throw sg_range_exception("unsupported apt.dat comm station type");
520         }
521
522       // Name can contain white spaces. All tokens after the second token are
523       // part of the name.
524       std::string name = token[2];
525       for( size_t i = 3; i < token.size(); ++i )
526         name += ' ' + token[i];
527
528       cache->insertCommStation(ty, name, pos, freqKhz, rangeNm, currentAirportID);
529     }
530     else SG_LOG( SG_GENERAL, SG_DEBUG, "Found unnamed comm. Skipping: " << lineId);
531   }
532
533 };
534   
535 // Load the airport data base from the specified aptdb file.  The
536 // metar file is used to mark the airports as having metar available
537 // or not.
538 bool airportDBLoad( const SGPath &aptdb_file )
539 {
540   APTLoader ld;
541   ld.parseAPT(aptdb_file);
542   return true;
543 }
544   
545 bool metarDataLoad(const SGPath& metar_file)
546 {
547   sg_gzifstream metar_in( metar_file.str() );
548   if ( !metar_in.is_open() ) {
549     SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
550     return false;
551   }
552   
553   NavDataCache* cache = NavDataCache::instance();
554
555   string ident;
556   while ( metar_in ) {
557     metar_in >> ident;
558     if ( ident == "#" || ident == "//" ) {
559       metar_in >> skipeol;
560     } else {
561       cache->setAirportMetar(ident, true);
562     }
563   }
564   
565   return true;
566 }
567
568 } // of namespace flightgear