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