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