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