]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
Merge branch 'maint' into next
[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 <simgear/compiler.h>
30
31 #include <stdlib.h> // atof(), atoi()
32 #include <string.h> // memchr()
33 #include <ctype.h> // isspace()
34
35 #include <simgear/constants.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/misc/sgstream.hxx>
38 #include <simgear/misc/strutils.hxx>
39 #include <simgear/structure/exception.hxx>
40
41 #include <string>
42
43 #include "simple.hxx"
44 #include "runways.hxx"
45
46 #include "apt_loader.hxx"
47
48 static FGPositioned::Type fptypeFromRobinType(int aType)
49 {
50   switch (aType) {
51   case 1: return FGPositioned::AIRPORT;
52   case 16: return FGPositioned::SEAPORT;
53   case 17: return FGPositioned::HELIPORT;
54   default:
55     SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
56     throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
57   }
58 }
59
60 class APTLoader
61 {
62 public:
63   void parseAPT(const string &aptdb_file)
64   {
65     sg_gzifstream in( aptdb_file );
66     if ( !in.is_open() ) {
67         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
68         exit(-1);
69     }
70
71     string line;
72     char tmp[2049];
73     tmp[2048] = 0;
74     
75     unsigned int line_id = 0;
76     unsigned int line_num = 0;
77
78     while ( ! in.eof() ) {
79       in.getline(tmp, 2048);
80       line = tmp; // string copy, ack
81       line_num++;
82
83       if ( !line.size() || isspace(tmp[0])) {
84         continue;
85       }
86       
87       if (line.size() >= 3) {
88           char *p = (char *)memchr(tmp, ' ', 3);
89           if ( p )
90               *p = 0;
91       }
92
93       line_id = atoi(tmp);
94       if ( tmp[0] == 'I' ) {
95         // First line, indicates IBM (i.e. DOS line endings I
96         // believe.)
97
98         // move past this line and read and discard the next line
99         // which is the version and copyright information
100         in.getline(tmp, 2048);
101         // vector<string> vers_token = simgear::strutils::split( tmp );
102         if ( strlen(tmp) > 4 ) {
103            char *p = (char *)memchr(tmp, ' ', 4);
104            if ( p )
105               *p = 0;
106         }
107         SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
108             } else if ( line_id == 1 /* Airport */ ||
109                     line_id == 16 /* Seaplane base */ ||
110                     line_id == 17 /* Heliport */ ) {
111         parseAirportLine(simgear::strutils::split(line));
112       } else if ( line_id == 10 ) {
113         parseRunwayLine(simgear::strutils::split(line));
114       } else if ( line_id == 18 ) {
115             // beacon entry (ignore)
116       } else if ( line_id == 14 ) {
117         // control tower entry
118         vector<string> token(simgear::strutils::split(line));
119         
120         double lat = atof( token[1].c_str() );
121         double lon = atof( token[2].c_str() );
122         double elev = atof( token[3].c_str() );
123         tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
124         got_tower = true;
125       } else if ( line_id == 19 ) {
126           // windsock entry (ignore)
127       } else if ( line_id == 15 ) {
128           // custom startup locations (ignore)
129       } else if ( line_id == 0 ) {
130           // ??
131       } else if ( line_id >= 50 && line_id <= 56 ) {
132           // frequency entries (ignore)
133       } else if ( line_id == 99 ) {
134           SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
135       } else {
136           SG_LOG( SG_GENERAL, SG_ALERT, 
137                   "Unknown line(#" << line_num << ") in file: " << line );
138           exit(-1);
139       }
140     }
141
142     addAirport();
143   }
144   
145 private:
146   double rwy_lat_accum;
147   double rwy_lon_accum;
148   double last_rwy_heading;
149   int rwy_count;
150   bool got_tower;
151   string last_apt_id;
152   string last_apt_name;
153   double last_apt_elev;
154   SGGeod tower;
155   int last_apt_type;
156   
157   vector<FGRunwayPtr> runways;
158   vector<FGTaxiwayPtr> taxiways;
159   
160   void addAirport()
161   {  
162     if (last_apt_id.empty()) {
163       return;
164     }
165
166     if (!rwy_count) {
167         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
168                 << ", skipping." );
169         return;
170     }
171
172     double lat = rwy_lat_accum / (double)rwy_count;
173     double lon = rwy_lon_accum / (double)rwy_count;
174
175     if (!got_tower) {
176         // tower height hard coded for now...
177         const float tower_height = 50.0f;
178         // make a little off the heading for 1 runway airports...
179         float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
180         float fudge_lat = .003f - fudge_lon;
181         tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
182     }
183
184     SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
185     FGAirport* apt = new FGAirport(last_apt_id, pos, tower, last_apt_name, false,
186         fptypeFromRobinType(last_apt_type));
187         
188     apt->setRunwaysAndTaxiways(runways, taxiways);
189   }
190   
191   void parseAirportLine(const vector<string>& token)
192   {
193     const string& id(token[4]);
194     double elev = atof( token[1].c_str() );
195
196     addAirport();
197             
198     last_apt_id = id;
199     last_apt_elev = elev;
200     last_apt_name = "";
201     got_tower = false;
202
203     // build the name
204     for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
205         last_apt_name += token[i];
206         last_apt_name += " ";
207     }
208     last_apt_name += token[token.size() - 1];
209     last_apt_type = atoi( token[0].c_str() );
210
211     // clear runway list for start of next airport
212     rwy_lon_accum = 0.0;
213     rwy_lat_accum = 0.0;
214     rwy_count = 0;
215   }
216   
217   void parseRunwayLine(const vector<string>& token)
218   {
219     double lat = atof( token[1].c_str() );
220     double lon = atof( token[2].c_str() );
221     rwy_lat_accum += lat;
222     rwy_lon_accum += lon;
223     rwy_count++;
224
225     const string& rwy_no(token[3]);
226
227     double heading = atof( token[4].c_str() );
228     double length = atoi( token[5].c_str() );
229     double width = atoi( token[8].c_str() );
230
231     last_rwy_heading = heading;
232
233     int surface_code = atoi( token[10].c_str() );
234     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
235     
236     if (rwy_no[0] == 'x') {
237       // taxiway
238       FGTaxiway* t = new FGTaxiway(rwy_no, pos, heading, length, width, surface_code);
239       taxiways.push_back(t);
240     } else {
241       // (pair of) runways
242       string rwy_displ_threshold = token[6];
243       vector<string> displ
244           = simgear::strutils::split( rwy_displ_threshold, "." );
245       double displ_thresh1 = atof( displ[0].c_str() );
246       double displ_thresh2 = atof( displ[1].c_str() );
247
248       string rwy_stopway = token[7];
249       vector<string> stop
250           = simgear::strutils::split( rwy_stopway, "." );
251       double stopway1 = atof( stop[0].c_str() );
252       double stopway2 = atof( stop[1].c_str() );
253
254       FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
255                             width, displ_thresh1, stopway1, surface_code, false);
256       runways.push_back(rwy);
257       
258       FGRunway* reciprocal = new FGRunway(NULL, FGRunway::reverseIdent(rwy_no), 
259                 pos, heading + 180.0, length, width, 
260                 displ_thresh2, stopway2, surface_code, true);
261               
262       runways.push_back(reciprocal);
263     }
264   }
265 };
266
267 // Load the airport data base from the specified aptdb file.  The
268 // metar file is used to mark the airports as having metar available
269 // or not.
270 bool fgAirportDBLoad( const string &aptdb_file, const string &metar_file )
271 {
272    APTLoader ld;
273    ld.parseAPT(aptdb_file);
274
275     //
276     // Load the metar.dat file and update apt db with stations that
277     // have metar data.
278     //
279
280     sg_gzifstream metar_in( metar_file );
281     if ( !metar_in.is_open() ) {
282         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
283     }
284
285     string ident;
286     while ( metar_in ) {
287         metar_in >> ident;
288         if ( ident == "#" || ident == "//" ) {
289             metar_in >> skipeol;
290         } else {
291             FGAirport* apt = FGAirport::findByIdent(ident);
292             if (apt) {
293                 apt->setMetar(true);
294             }
295         }
296     }
297
298     SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");
299
300     return true;
301 }
302