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