]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
Conditional compilation of ATCDCL module. Use --disable-atcdcl to try building flight...
[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
43 #include <string>
44
45 #include "simple.hxx"
46 #include "runways.hxx"
47 #include "pavement.hxx"
48 #if ENABLE_ATCDCL
49 #    include <ATCDCL/commlist.hxx>
50 #endif
51
52 #include <iostream>
53
54 using namespace std;
55
56 static FGPositioned::Type fptypeFromRobinType(int aType)
57 {
58   switch (aType) {
59   case 1: return FGPositioned::AIRPORT;
60   case 16: return FGPositioned::SEAPORT;
61   case 17: return FGPositioned::HELIPORT;
62   default:
63     SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
64     throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
65   }
66 }
67
68 class APTLoader
69 {
70 public:
71
72   APTLoader()
73   :  last_apt_id(""),
74      last_apt_name(""),
75      last_apt_elev(0.0),
76      last_apt_info(""),
77      last_apt_type("")
78   {}
79
80
81 #ifdef ENABLE_ATCDCL
82   void parseAPT(const string &aptdb_file, FGCommList *comm_list)
83 #else
84   void parseAPT(const string &aptdb_file)
85 #endif
86   {
87     sg_gzifstream in( aptdb_file );
88
89     if ( !in.is_open() ) {
90         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
91         exit(-1);
92     }
93
94     string line;
95     char tmp[2049];
96     tmp[2048] = 0;
97     
98     unsigned int line_id = 0;
99     unsigned int line_num = 0;
100
101     while ( ! in.eof() ) {
102       in.getline(tmp, 2048);
103       line = tmp; // string copy, ack
104       line_num++;
105
106       if ( !line.size() || isspace(tmp[0])) {
107         continue;
108       }
109       
110       if (line.size() >= 3) {
111           char *p = (char *)memchr(tmp, ' ', 3);
112           if ( p )
113               *p = 0;
114       }
115
116       line_id = atoi(tmp);
117       if ( tmp[0] == 'I' ) {
118         // First line, indicates IBM (i.e. DOS line endings I
119         // believe.)
120
121         // move past this line and read and discard the next line
122         // which is the version and copyright information
123         in.getline(tmp, 2048);
124         // vector<string> vers_token = simgear::strutils::split( tmp );
125         if ( strlen(tmp) > 4 ) {
126            char *p = (char *)memchr(tmp, ' ', 4);
127            if ( p )
128               *p = 0;
129         }
130         SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
131       } else if ( line_id == 1 /* Airport */ ||
132                     line_id == 16 /* Seaplane base */ ||
133                     line_id == 17 /* Heliport */ ) {
134         parseAirportLine(simgear::strutils::split(line));
135       } else if ( line_id == 10 ) { // Runway v810
136         parseRunwayLine810(simgear::strutils::split(line));
137       } else if ( line_id == 100 ) { // Runway v850
138         parseRunwayLine850(simgear::strutils::split(line));
139       } else if ( line_id == 101 ) { // Water Runway v850
140         parseWaterRunwayLine850(simgear::strutils::split(line));
141       } else if ( line_id == 102 ) { // Helipad v850
142         parseHelipadLine850(simgear::strutils::split(line));
143       } else if ( line_id == 18 ) {
144             // beacon entry (ignore)
145       } else if ( line_id == 14 ) {
146         // control tower entry
147         vector<string> token(simgear::strutils::split(line));
148         
149         double lat = atof( token[1].c_str() );
150         double lon = atof( token[2].c_str() );
151         double elev = atof( token[3].c_str() );
152         tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
153         got_tower = true;
154       } else if ( line_id == 19 ) {
155           // windsock entry (ignore)
156       } else if ( line_id == 20 ) {
157           // Taxiway sign (ignore)
158       } else if ( line_id == 21 ) {
159           // lighting objects (ignore)
160       } else if ( line_id == 15 ) {
161           // custom startup locations (ignore)
162       } else if ( line_id == 0 ) {
163           // ??
164       } else if ( line_id == 50 ) {
165         parseATISLine(comm_list, simgear::strutils::split(line));
166       } else if ( line_id >= 51 && line_id <= 56 ) {
167         // other frequency entries (ignore)
168       } else if ( line_id == 110 ) {
169         pavement = true;
170         parsePavementLine850(simgear::strutils::split(line, 0, 4));
171       } else if ( line_id >= 111 && line_id <= 114 ) {
172         if ( pavement )
173           parsePavementNodeLine850(line_id, simgear::strutils::split(line));
174       } else if ( line_id >= 115 && line_id <= 116 ) {
175           // other pavement nodes (ignore)
176       } else if ( line_id == 120 ) {
177         pavement = false;
178       } else if ( line_id == 130 ) {
179         pavement = false;
180       } else if ( line_id == 99 ) {
181           SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
182       } else {
183           SG_LOG( SG_GENERAL, SG_ALERT, 
184                   "Unknown line(#" << line_num << ") in file: " << line );
185           exit( -1 );
186       }
187     }
188
189     addAirport();
190   }
191   
192 private:
193   vector<string> token;
194   double rwy_lat_accum;
195   double rwy_lon_accum;
196   double last_rwy_heading;
197   int rwy_count;
198   bool got_tower;
199   string last_apt_id;
200   string last_apt_name;
201   double last_apt_elev;
202   SGGeod tower;
203   string last_apt_info;
204   string last_apt_type;
205   string pavement_ident;
206   bool pavement;
207   
208   vector<FGRunwayPtr> runways;
209   vector<FGTaxiwayPtr> taxiways;
210   vector<FGPavementPtr> pavements;
211   
212   void addAirport()
213   {  
214     if (last_apt_id.empty()) {
215       return;
216     }
217
218     if (!rwy_count) {
219         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
220                 << ", skipping." );
221         return;
222     }
223
224     double lat = rwy_lat_accum / (double)rwy_count;
225     double lon = rwy_lon_accum / (double)rwy_count;
226
227     if (!got_tower) {
228         // tower height hard coded for now...
229         const float tower_height = 50.0f;
230         // make a little off the heading for 1 runway airports...
231         float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
232         float fudge_lat = .003f - fudge_lon;
233         tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
234     }
235
236     SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
237     FGAirport* apt = new FGAirport(last_apt_id, pos, tower, last_apt_name, false,
238         fptypeFromRobinType(atoi(last_apt_type.c_str())));
239         
240     apt->setRunwaysAndTaxiways(runways, taxiways, pavements);
241   }
242   
243   void parseAirportLine(const vector<string>& token)
244   {
245     const string& id(token[4]);
246     double elev = atof( token[1].c_str() );
247
248     addAirport();
249             
250     last_apt_id = id;
251     last_apt_elev = elev;
252     last_apt_name = "";
253     got_tower = false;
254
255     // build the name
256     for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
257         last_apt_name += token[i];
258         last_apt_name += " ";
259     }
260     last_apt_name += token[token.size() - 1];
261     last_apt_type = token[0];
262
263     // clear runway list for start of next airport
264     rwy_lon_accum = 0.0;
265     rwy_lat_accum = 0.0;
266     rwy_count = 0;
267   }
268   
269   void parseRunwayLine810(const vector<string>& token)
270   {
271     double lat = atof( token[1].c_str() );
272     double lon = atof( token[2].c_str() );
273     rwy_lat_accum += lat;
274     rwy_lon_accum += lon;
275     rwy_count++;
276
277     const string& rwy_no(token[3]);
278
279     double heading = atof( token[4].c_str() );
280     double length = atoi( token[5].c_str() );
281     double width = atoi( token[8].c_str() );
282
283     last_rwy_heading = heading;
284
285     int surface_code = atoi( token[10].c_str() );
286     SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
287     
288     if (rwy_no[0] == 'x') {
289       // taxiway
290       FGTaxiway* t = new FGTaxiway(rwy_no, pos, heading, length, width, surface_code);
291       taxiways.push_back(t);
292     } else {
293       // (pair of) runways
294       string rwy_displ_threshold = token[6];
295       vector<string> displ
296           = simgear::strutils::split( rwy_displ_threshold, "." );
297       double displ_thresh1 = atof( displ[0].c_str() );
298       double displ_thresh2 = atof( displ[1].c_str() );
299
300       string rwy_stopway = token[7];
301       vector<string> stop
302           = simgear::strutils::split( rwy_stopway, "." );
303       double stopway1 = atof( stop[0].c_str() );
304       double stopway2 = atof( stop[1].c_str() );
305
306       FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
307                             width, displ_thresh1, stopway1, surface_code, false);
308       runways.push_back(rwy);
309
310       FGRunway* reciprocal = new FGRunway(NULL, FGRunway::reverseIdent(rwy_no), 
311                 pos, heading + 180.0, length, width, 
312                 displ_thresh2, stopway2, surface_code, true);
313
314       runways.push_back(reciprocal);
315       
316       rwy->setReciprocalRunway(reciprocal);
317       reciprocal->setReciprocalRunway(rwy);
318     }
319   }
320
321   void parseRunwayLine850(const vector<string>& token)
322   {
323     double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
324     int surface_code = atoi( token[2].c_str() );
325
326     double lat_1 = atof( token[9].c_str() );
327     double lon_1 = atof( token[10].c_str() );
328     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
329     rwy_lat_accum += lat_1;
330     rwy_lon_accum += lon_1;
331     rwy_count++;
332
333     double lat_2 = atof( token[18].c_str() );
334     double lon_2 = atof( token[19].c_str() );
335     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
336     rwy_lat_accum += lat_2;
337     rwy_lon_accum += lon_2;
338     rwy_count++;
339
340     double length, heading_1, heading_2, dummy;
341     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
342     SGGeod pos;
343     SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
344     length *= SG_METER_TO_FEET;
345
346     last_rwy_heading = heading_1;
347
348     const string& rwy_no_1(token[8]);
349     const string& rwy_no_2(token[17]);
350     if ( rwy_no_1.size() == 0 || rwy_no_2.size() == 0 )
351         return;
352
353     double displ_thresh1 = atof( token[11].c_str() );
354     double displ_thresh2 = atof( token[20].c_str() );
355
356     double stopway1 = atof( token[12].c_str() );
357     double stopway2 = atof( token[21].c_str() );
358
359     FGRunway* rwy = new FGRunway(NULL, rwy_no_1, pos, heading_1, length,
360                           width, displ_thresh1, stopway1, surface_code, false);
361     runways.push_back(rwy);
362
363     FGRunway* reciprocal = new FGRunway(NULL, rwy_no_2, 
364               pos, heading_2, length, width, 
365               displ_thresh2, stopway2, surface_code, true);
366     runways.push_back(reciprocal);
367     
368     rwy->setReciprocalRunway(reciprocal);
369     reciprocal->setReciprocalRunway(rwy);
370   }
371
372   void parseWaterRunwayLine850(const vector<string>& token)
373   {
374     double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
375
376     double lat_1 = atof( token[4].c_str() );
377     double lon_1 = atof( token[5].c_str() );
378     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
379     rwy_lat_accum += lat_1;
380     rwy_lon_accum += lon_1;
381     rwy_count++;
382
383     double lat_2 = atof( token[7].c_str() );
384     double lon_2 = atof( token[8].c_str() );
385     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
386     rwy_lat_accum += lat_2;
387     rwy_lon_accum += lon_2;
388     rwy_count++;
389
390     double length, heading_1, heading_2, dummy;
391     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
392     SGGeod pos;
393     SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
394
395     last_rwy_heading = heading_1;
396
397     const string& rwy_no_1(token[3]);
398     const string& rwy_no_2(token[6]);
399
400     FGRunway* rwy = new FGRunway(NULL, rwy_no_1, pos, heading_1, length,
401                           width, 0.0, 0.0, 13, false);
402     runways.push_back(rwy);
403
404     FGRunway* reciprocal = new FGRunway(NULL, rwy_no_2, 
405               pos, heading_2, length, width, 
406               0.0, 0.0, 13, true);
407     runways.push_back(reciprocal);
408     
409     rwy->setReciprocalRunway(reciprocal);
410     reciprocal->setReciprocalRunway(rwy);
411   }
412
413   void parseHelipadLine850(const vector<string>& token)
414   {
415     double length = atof( token[5].c_str() ) * SG_METER_TO_FEET;
416     double width = atof( token[6].c_str() ) * SG_METER_TO_FEET;
417
418     double lat = atof( token[2].c_str() );
419     double lon = atof( token[3].c_str() );
420     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
421     rwy_lat_accum += lat;
422     rwy_lon_accum += lon;
423     rwy_count++;
424
425     double heading = atof( token[4].c_str() );
426
427     last_rwy_heading = heading;
428
429     const string& rwy_no(token[1]);
430     int surface_code = atoi( token[7].c_str() );
431
432     FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
433                           width, 0.0, 0.0, surface_code, false);
434     runways.push_back(rwy);
435   }
436
437   void parsePavementLine850(const vector<string>& token)
438   {
439     if ( token.size() >= 5 ) {
440       pavement_ident = token[4];
441       if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
442         pavement_ident.erase( pavement_ident.size()-1 );
443     } else {
444       pavement_ident = "xx";
445     }
446   }
447
448   void parsePavementNodeLine850(int num, const vector<string>& token)
449   {
450     double lat = atof( token[1].c_str() );
451     double lon = atof( token[2].c_str() );
452     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
453
454     FGPavement* pvt = 0;
455     if ( !pavement_ident.empty() ) {
456       pvt = new FGPavement( pavement_ident, pos );
457       pavements.push_back( pvt );
458       pavement_ident = "";
459     } else {
460       pvt = pavements.back();
461     }
462     if ( num == 112 || num == 114 ) {
463       double lat_b = atof( token[3].c_str() );
464       double lon_b = atof( token[4].c_str() );
465       SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
466       pvt->addBezierNode(pos, pos_b, num == 114);
467     } else {
468       pvt->addNode(pos, num == 113);
469     }
470   }
471 #if ENABLE_ATCDCL  
472   void parseATISLine(FGCommList *comm_list, const vector<string>& token) 
473   {
474     if ( rwy_count <= 0 ) {
475       SG_LOG( SG_GENERAL, SG_ALERT, 
476         "No runways; skipping AWOS for " + last_apt_id);
477     }
478      
479 // This assumes/requires that any code-50 line (ATIS or AWOS)
480  // applies to the preceding code-1 line (airport ID and name)
481  // and that a full set of code-10 lines (runway descriptors)
482  // has come between the code-1 and code-50 lines.
483             // typical code-50 lines:
484             // 50 11770 ATIS
485             // 50 11770 AWOS 3
486  // This code parallels code found in "operator>>" in ATC.hxx;
487  // FIXME: unify the code.      
488     ATCData a;
489     a.geod = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count, 
490       rwy_lat_accum / (double)rwy_count, last_apt_elev);
491     a.range = 50;       // give all ATISs small range
492     a.ident = last_apt_id;
493     a.name = last_apt_name;
494     // short int representing tens of kHz:
495     a.freq = atoi(token[1].c_str());
496     if (token[2] == "ATIS") a.type = ATIS;
497     else a.type = AWOS;         // ASOS same as AWOS
498
499     // generate cartesian coordinates
500     a.cart = SGVec3d::fromGeod(a.geod);
501     comm_list->commlist_freq[a.freq].push_back(a);
502
503     SGBucket bucket(a.geod);
504     int bucknum = bucket.gen_index();
505     comm_list->commlist_bck[bucknum].push_back(a);
506 #if 0
507    SG_LOG( SG_GENERAL, SG_ALERT, 
508      "Loaded ATIS/AWOS for airport: " << a.ident
509     << "  lat: "  << a.geod.getLatitudeDeg()
510     << "  lon: "  << a.geod.getLongitudeDeg()
511     << "  freq: " << a.freq
512     << "  type: " << a.type );
513 #endif
514   }
515 #endif
516 };
517
518
519 // Load the airport data base from the specified aptdb file.  The
520 // metar file is used to mark the airports as having metar available
521 // or not.
522 bool fgAirportDBLoad( const string &aptdb_file, 
523 #if ENABLE_ATCDCL
524         FGCommList *comm_list, const std::string &metar_file )
525 #else
526         const std::string &metar_file )
527 #endif
528 {
529
530    APTLoader ld;
531 #if ENABLE_ATCDCL
532    ld.parseAPT(aptdb_file, comm_list);
533 #else
534    ld.parseAPT(aptdb_file);
535 #endif
536     //
537     // Load the metar.dat file and update apt db with stations that
538     // have metar data.
539     //
540
541     sg_gzifstream metar_in( metar_file );
542     if ( !metar_in.is_open() ) {
543         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
544     }
545
546     string ident;
547     while ( metar_in ) {
548         metar_in >> ident;
549         if ( ident == "#" || ident == "//" ) {
550             metar_in >> skipeol;
551         } else {
552             FGAirport* apt = FGAirport::findByIdent(ident);
553             if (apt) {
554                 apt->setMetar(true);
555             }
556         }
557     }
558
559     SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");
560
561     return true;
562 }
563