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