]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
Document that property write-protection is not a security measure
[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/misc/sg_path.hxx>
43
44 #include <string>
45
46 #include "airport.hxx"
47 #include "runways.hxx"
48 #include "pavement.hxx"
49 #include <Navaids/NavDataCache.hxx>
50 #include <ATC/CommStation.hxx>
51
52 #include <iostream>
53
54 using namespace std;
55
56 typedef SGSharedPtr<FGPavement> FGPavementPtr;
57
58 static FGPositioned::Type fptypeFromRobinType(int aType)
59 {
60   switch (aType) {
61   case 1: return FGPositioned::AIRPORT;
62   case 16: return FGPositioned::SEAPORT;
63   case 17: return FGPositioned::HELIPORT;
64   default:
65     SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
66     throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
67   }
68 }
69
70
71 namespace flightgear
72 {
73   
74 class APTLoader
75 {
76 public:
77
78   APTLoader()
79   :  last_apt_id(""),
80      last_apt_elev(0.0),
81      last_apt_info("")
82   {
83     currentAirportID = 0;
84     cache = NavDataCache::instance();
85   }
86
87   void parseAPT(const SGPath &aptdb_file)
88   {
89     sg_gzifstream in( aptdb_file.str() );
90
91     if ( !in.is_open() ) {
92         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
93         throw sg_io_exception("cannot open APT file", aptdb_file);
94     }
95
96     string line;
97     char tmp[2049];
98     tmp[2048] = 0;
99     
100     unsigned int line_id = 0;
101     unsigned int line_num = 0;
102
103     while ( ! in.eof() ) {
104       in.getline(tmp, 2048);
105       line = tmp; // string copy, ack
106       line_num++;
107
108       if ( line.empty() || isspace(tmp[0]) || tmp[0] == '#' ) {
109         continue;
110       }
111
112       if (line.size() >= 3) {
113           char *p = (char *)memchr(tmp, ' ', 3);
114           if ( p )
115               *p = 0;
116       }
117
118       line_id = atoi(tmp);
119       if ( tmp[0] == 'I' || tmp[0] == 'A' ) {
120         // First line, indicates IBM ("I") or Macintosh ("A")
121         // line endings.
122
123         // move past this line and read and discard the next line
124         // which is the version and copyright information
125         in.getline(tmp, 2048);
126
127         if ( strlen(tmp) > 5 ) {
128            char *p = (char *)memchr(tmp, ' ', 5);
129            if ( p )
130               *p = 0;
131         }
132         SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
133       } else if ( line_id == 1 /* Airport */ ||
134                     line_id == 16 /* Seaplane base */ ||
135                     line_id == 17 /* Heliport */ ) {
136         parseAirportLine(simgear::strutils::split(line));
137       } else if ( line_id == 10 ) { // Runway v810
138         parseRunwayLine810(simgear::strutils::split(line));
139       } else if ( line_id == 100 ) { // Runway v850
140         parseRunwayLine850(simgear::strutils::split(line));
141       } else if ( line_id == 101 ) { // Water Runway v850
142         parseWaterRunwayLine850(simgear::strutils::split(line));
143       } else if ( line_id == 102 ) { // Helipad v850
144         parseHelipadLine850(simgear::strutils::split(line));
145       } else if ( line_id == 18 ) {
146             // beacon entry (ignore)
147       } else if ( line_id == 14 ) {
148         // control tower entry
149         vector<string> token(simgear::strutils::split(line));
150         
151         double lat = atof( token[1].c_str() );
152         double lon = atof( token[2].c_str() );
153         double elev = atof( token[3].c_str() );
154         tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
155         got_tower = true;
156         
157         cache->insertTower(currentAirportID, tower);
158       } else if ( line_id == 19 ) {
159           // windsock entry (ignore)
160       } else if ( line_id == 20 ) {
161           // Taxiway sign (ignore)
162       } else if ( line_id == 21 ) {
163           // lighting objects (ignore)
164       } else if ( line_id == 15 ) {
165           // custom startup locations (ignore)
166       } else if ( line_id == 0 ) {
167           // ??
168       } else if ( line_id >= 50 && line_id <= 56) {
169         parseCommLine(line_id, simgear::strutils::split(line));
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 >= 1000 ) {
183           // airport traffic flow (ignore)
184       } else if ( line_id == 99 ) {
185           SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
186       } else {
187           SG_LOG( SG_GENERAL, SG_ALERT, 
188                   "Unknown line(#" << line_num << ") in apt.dat file: " << line );
189           throw sg_format_exception("malformed line in apt.dat:", line);
190       }
191     }
192
193     finishAirport();
194   }
195   
196 private:
197   vector<string> token;
198   double rwy_lat_accum;
199   double rwy_lon_accum;
200   double last_rwy_heading;
201   int rwy_count;
202   bool got_tower;
203   string last_apt_id;
204   double last_apt_elev;
205   SGGeod tower;
206   string last_apt_info;
207   string pavement_ident;
208   bool pavement;
209   
210   //vector<FGRunwayPtr> runways;
211   //vector<FGTaxiwayPtr> taxiways;
212   vector<FGPavementPtr> pavements;
213   
214   NavDataCache* cache;
215   PositionedID currentAirportID;
216   
217   void finishAirport()
218   {
219     if (currentAirportID == 0) {
220       return;
221     }
222     
223     if (!rwy_count) {
224       currentAirportID = 0;
225       SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
226               << ", skipping." );
227       return;
228     }
229
230     double lat = rwy_lat_accum / (double)rwy_count;
231     double lon = rwy_lon_accum / (double)rwy_count;
232
233     if (!got_tower) {
234       // tower height hard coded for now...
235       const float tower_height = 50.0f;
236       // make a little off the heading for 1 runway airports...
237       float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
238       float fudge_lat = .003f - fudge_lon;
239       tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
240       
241       cache->insertTower(currentAirportID, tower);
242     }
243
244     SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
245     cache->updatePosition(currentAirportID, pos);
246     
247     currentAirportID = 0;
248   }
249   
250   void parseAirportLine(const vector<string>& token)
251   {
252     const string& id(token[4]);
253     double elev = atof( token[1].c_str() );
254
255   // finish the previous airport
256     finishAirport();
257             
258     last_apt_elev = elev;
259     got_tower = false;
260
261     string name;
262     // build the name
263     for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
264         name += token[i] + " ";
265     }
266     name += token[token.size() - 1];
267
268     // clear runway list for start of next airport
269     rwy_lon_accum = 0.0;
270     rwy_lat_accum = 0.0;
271     rwy_count = 0;
272     
273     int robinType = atoi(token[0].c_str());
274     currentAirportID = cache->insertAirport(fptypeFromRobinType(robinType), id, name);
275   }
276   
277   void parseRunwayLine810(const vector<string>& token)
278   {
279     double lat = atof( token[1].c_str() );
280     double lon = atof( token[2].c_str() );
281     rwy_lat_accum += lat;
282     rwy_lon_accum += lon;
283     rwy_count++;
284
285     const string& rwy_no(token[3]);
286
287     double heading = atof( token[4].c_str() );
288     double length = atoi( token[5].c_str() );
289     double width = atoi( token[8].c_str() );
290     length *= SG_FEET_TO_METER;
291     width *= SG_FEET_TO_METER;
292
293     // adjust lat / lon to the start of the runway/taxiway, not the middle
294     SGGeod pos_1 = SGGeodesy::direct( SGGeod::fromDegFt(lon, lat, last_apt_elev), heading, -length/2 );
295
296     last_rwy_heading = heading;
297
298     int surface_code = atoi( token[10].c_str() );
299
300     if (rwy_no[0] == 'x') {  // Taxiway
301       cache->insertRunway(FGPositioned::TAXIWAY, rwy_no, pos_1, currentAirportID,
302                           heading, length, width, 0.0, 0.0, surface_code);
303     } else if (rwy_no[0] == 'H') {  // Helipad
304       SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
305       cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos, currentAirportID,
306                           heading, length, width, 0.0, 0.0, surface_code);
307     } else {
308       // (pair of) runways
309       string rwy_displ_threshold = token[6];
310       vector<string> displ
311           = simgear::strutils::split( rwy_displ_threshold, "." );
312       double displ_thresh1 = atof( displ[0].c_str() );
313       double displ_thresh2 = atof( displ[1].c_str() );
314       displ_thresh1 *= SG_FEET_TO_METER;
315       displ_thresh2 *= SG_FEET_TO_METER;
316
317       string rwy_stopway = token[7];
318       vector<string> stop
319           = simgear::strutils::split( rwy_stopway, "." );
320       double stopway1 = atof( stop[0].c_str() );
321       double stopway2 = atof( stop[1].c_str() );
322       stopway1 *= SG_FEET_TO_METER;
323       stopway2 *= SG_FEET_TO_METER;
324
325       SGGeod pos_2 = SGGeodesy::direct( pos_1, heading, length );
326
327       PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos_1,
328                                              currentAirportID, heading, length,
329                                              width, displ_thresh1, stopway1,
330                                              surface_code);
331       
332       PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
333                                              FGRunway::reverseIdent(rwy_no), pos_2,
334                                              currentAirportID,
335                                              SGMiscd::normalizePeriodic(0, 360, heading + 180.0),
336                                              length, width, displ_thresh2, stopway2,
337                                              surface_code);
338
339       cache->setRunwayReciprocal(rwy, reciprocal);
340     }
341   }
342
343   void parseRunwayLine850(const vector<string>& token)
344   {
345     double width = atof( token[1].c_str() );
346     int surface_code = atoi( token[2].c_str() );
347
348     double lat_1 = atof( token[9].c_str() );
349     double lon_1 = atof( token[10].c_str() );
350     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
351     rwy_lat_accum += lat_1;
352     rwy_lon_accum += lon_1;
353     rwy_count++;
354
355     double lat_2 = atof( token[18].c_str() );
356     double lon_2 = atof( token[19].c_str() );
357     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
358     rwy_lat_accum += lat_2;
359     rwy_lon_accum += lon_2;
360     rwy_count++;
361
362     double length, heading_1, heading_2;
363     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
364
365     last_rwy_heading = heading_1;
366
367     const string& rwy_no_1(token[8]);
368     const string& rwy_no_2(token[17]);
369     if ( rwy_no_1.empty() || rwy_no_2.empty() )
370         return;
371
372     double displ_thresh1 = atof( token[11].c_str() );
373     double displ_thresh2 = atof( token[20].c_str() );
374
375     double stopway1 = atof( token[12].c_str() );
376     double stopway2 = atof( token[21].c_str() );
377
378     PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
379                                            currentAirportID, heading_1, length,
380                                            width, displ_thresh1, stopway1,
381                                            surface_code);
382     
383     PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
384                                                   rwy_no_2, pos_2,
385                                                   currentAirportID, heading_2, length,
386                                                   width, displ_thresh2, stopway2,
387                                                   surface_code);
388     
389     cache->setRunwayReciprocal(rwy, reciprocal);
390   }
391
392   void parseWaterRunwayLine850(const vector<string>& token)
393   {
394     double width = atof( token[1].c_str() );
395
396     double lat_1 = atof( token[4].c_str() );
397     double lon_1 = atof( token[5].c_str() );
398     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
399     rwy_lat_accum += lat_1;
400     rwy_lon_accum += lon_1;
401     rwy_count++;
402
403     double lat_2 = atof( token[7].c_str() );
404     double lon_2 = atof( token[8].c_str() );
405     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
406     rwy_lat_accum += lat_2;
407     rwy_lon_accum += lon_2;
408     rwy_count++;
409
410     double length, heading_1, heading_2;
411     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
412
413     last_rwy_heading = heading_1;
414
415     const string& rwy_no_1(token[3]);
416     const string& rwy_no_2(token[6]);
417
418     PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
419                                            currentAirportID, heading_1, length,
420                                            width, 0.0, 0.0, 13);
421     
422     PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
423                                                   rwy_no_2, pos_2,
424                                                   currentAirportID, heading_2, length,
425                                                   width, 0.0, 0.0, 13);
426     
427     cache->setRunwayReciprocal(rwy, reciprocal);
428   }
429
430   void parseHelipadLine850(const vector<string>& token)
431   {
432     double length = atof( token[5].c_str() );
433     double width = atof( token[6].c_str() );
434
435     double lat = atof( token[2].c_str() );
436     double lon = atof( token[3].c_str() );
437     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
438     rwy_lat_accum += lat;
439     rwy_lon_accum += lon;
440     rwy_count++;
441
442     double heading = atof( token[4].c_str() );
443
444     last_rwy_heading = heading;
445
446     const string& rwy_no(token[1]);
447     int surface_code = atoi( token[7].c_str() );
448
449     cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos,
450                         currentAirportID, heading, length,
451                         width, 0.0, 0.0, surface_code);
452   }
453
454   void parsePavementLine850(const vector<string>& token)
455   {
456     if ( token.size() >= 5 ) {
457       pavement_ident = token[4];
458       if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
459         pavement_ident.erase( pavement_ident.size()-1 );
460     } else {
461       pavement_ident = "xx";
462     }
463   }
464
465   void parsePavementNodeLine850(int num, const vector<string>& token)
466   {
467     double lat = atof( token[1].c_str() );
468     double lon = atof( token[2].c_str() );
469     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
470
471     FGPavement* pvt = 0;
472     if ( !pavement_ident.empty() ) {
473       pvt = new FGPavement( 0, pavement_ident, pos );
474       pavements.push_back( pvt );
475       pavement_ident = "";
476     } else {
477       pvt = pavements.back();
478     }
479     if ( num == 112 || num == 114 ) {
480       double lat_b = atof( token[3].c_str() );
481       double lon_b = atof( token[4].c_str() );
482       SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
483       pvt->addBezierNode(pos, pos_b, num == 114);
484     } else {
485       pvt->addNode(pos, num == 113);
486     }
487   }
488
489   void parseCommLine(int lineId, const vector<string>& token) 
490   {
491     if ( rwy_count <= 0 ) {
492       SG_LOG( SG_GENERAL, SG_ALERT, "No runways; skipping comm for " + last_apt_id);
493     }
494  
495     SGGeod pos = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count, 
496         rwy_lat_accum / (double)rwy_count, last_apt_elev);
497     
498     // short int representing tens of kHz:
499     int freqKhz = atoi(token[1].c_str()) * 10;
500     int rangeNm = 50;
501     FGPositioned::Type ty;
502     // Make sure we only pass on stations with at least a name
503     if (token.size() >2){
504
505         switch (lineId) {
506             case 50:
507                 ty = FGPositioned::FREQ_AWOS;
508                 for( size_t i = 2; i < token.size(); ++i )
509                 {
510                   if( token[i] == "ATIS" )
511                   {
512                     ty = FGPositioned::FREQ_ATIS;
513                     break;
514                   }
515                 }
516                 break;
517
518             case 51:    ty = FGPositioned::FREQ_UNICOM; break;
519             case 52:    ty = FGPositioned::FREQ_CLEARANCE; break;
520             case 53:    ty = FGPositioned::FREQ_GROUND; break;
521             case 54:    ty = FGPositioned::FREQ_TOWER; break;
522             case 55:
523             case 56:    ty = FGPositioned::FREQ_APP_DEP; break;
524             default:
525                 throw sg_range_exception("unsupported apt.dat comm station type");
526         }
527
528       // Name can contain white spaces. All tokens after the second token are
529       // part of the name.
530       std::string name = token[2];
531       for( size_t i = 3; i < token.size(); ++i )
532         name += ' ' + token[i];
533
534       cache->insertCommStation(ty, name, pos, freqKhz, rangeNm, currentAirportID);
535     }
536     else SG_LOG( SG_GENERAL, SG_DEBUG, "Found unnamed comm. Skipping: " << lineId);
537   }
538
539 };
540   
541 // Load the airport data base from the specified aptdb file.  The
542 // metar file is used to mark the airports as having metar available
543 // or not.
544 bool airportDBLoad( const SGPath &aptdb_file )
545 {
546   APTLoader ld;
547   ld.parseAPT(aptdb_file);
548   return true;
549 }
550   
551 bool metarDataLoad(const SGPath& metar_file)
552 {
553   sg_gzifstream metar_in( metar_file.str() );
554   if ( !metar_in.is_open() ) {
555     SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
556     return false;
557   }
558   
559   NavDataCache* cache = NavDataCache::instance();
560   string ident;
561   while ( metar_in ) {
562     metar_in >> ident;
563     if ( ident == "#" || ident == "//" ) {
564       metar_in >> skipeol;
565     } else {
566       cache->setAirportMetar(ident, true);
567     }
568   }
569   
570   return true;
571 }
572
573 } // of namespace flightgear