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