]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
Canvas: Proper fix for OpenVG init handling.
[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 "simple.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         exit(-1);
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.size() || isspace(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' ) {
120         // First line, indicates IBM (i.e. DOS line endings I
121         // believe.)
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         // vector<string> vers_token = simgear::strutils::split( tmp );
127         if ( strlen(tmp) > 4 ) {
128            char *p = (char *)memchr(tmp, ' ', 4);
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 == 99 ) {
183           SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
184       } else {
185           SG_LOG( SG_GENERAL, SG_ALERT, 
186                   "Unknown line(#" << line_num << ") in file: " << line );
187           exit( -1 );
188       }
189     }
190
191     finishAirport();
192   }
193   
194 private:
195   vector<string> token;
196   double rwy_lat_accum;
197   double rwy_lon_accum;
198   double last_rwy_heading;
199   int rwy_count;
200   bool got_tower;
201   string last_apt_id;
202   double last_apt_elev;
203   SGGeod tower;
204   string last_apt_info;
205   string pavement_ident;
206   bool pavement;
207   
208   //vector<FGRunwayPtr> runways;
209   //vector<FGTaxiwayPtr> taxiways;
210   vector<FGPavementPtr> pavements;
211   
212   NavDataCache* cache;
213   PositionedID currentAirportID;
214   
215   void finishAirport()
216   {
217     if (currentAirportID == 0) {
218       return;
219     }
220     
221     if (!rwy_count) {
222       currentAirportID = 0;
223       SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
224               << ", skipping." );
225       return;
226     }
227
228     double lat = rwy_lat_accum / (double)rwy_count;
229     double lon = rwy_lon_accum / (double)rwy_count;
230
231     if (!got_tower) {
232       // tower height hard coded for now...
233       const float tower_height = 50.0f;
234       // make a little off the heading for 1 runway airports...
235       float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
236       float fudge_lat = .003f - fudge_lon;
237       tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
238       
239       cache->insertTower(currentAirportID, tower);
240     }
241
242     SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
243     cache->updatePosition(currentAirportID, pos);
244     
245     currentAirportID = 0;
246   }
247   
248   void parseAirportLine(const vector<string>& token)
249   {
250     const string& id(token[4]);
251     double elev = atof( token[1].c_str() );
252
253   // finish the previous airport
254     finishAirport();
255             
256     last_apt_elev = elev;
257     got_tower = false;
258
259     string name;
260     // build the name
261     for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
262         name += token[i] + " ";
263     }
264     name += token[token.size() - 1];
265
266     // clear runway list for start of next airport
267     rwy_lon_accum = 0.0;
268     rwy_lat_accum = 0.0;
269     rwy_count = 0;
270     
271     int robinType = atoi(token[0].c_str());
272     currentAirportID = cache->insertAirport(fptypeFromRobinType(robinType), id, name);
273   }
274   
275   void parseRunwayLine810(const vector<string>& token)
276   {
277     double lat = atof( token[1].c_str() );
278     double lon = atof( token[2].c_str() );
279     rwy_lat_accum += lat;
280     rwy_lon_accum += lon;
281     rwy_count++;
282
283     const string& rwy_no(token[3]);
284
285     double heading = atof( token[4].c_str() );
286     double length = atoi( token[5].c_str() );
287     double width = atoi( token[8].c_str() );
288
289     last_rwy_heading = heading;
290
291     int surface_code = atoi( token[10].c_str() );
292     SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
293     
294     if (rwy_no[0] == 'x') {
295       cache->insertRunway(FGPositioned::TAXIWAY,rwy_no, pos, currentAirportID,
296                           heading, length, width, 0, 0, surface_code);
297     } else {
298       // (pair of) runways
299       string rwy_displ_threshold = token[6];
300       vector<string> displ
301           = simgear::strutils::split( rwy_displ_threshold, "." );
302       double displ_thresh1 = atof( displ[0].c_str() );
303       double displ_thresh2 = atof( displ[1].c_str() );
304
305       string rwy_stopway = token[7];
306       vector<string> stop
307           = simgear::strutils::split( rwy_stopway, "." );
308       double stopway1 = atof( stop[0].c_str() );
309       double stopway2 = atof( stop[1].c_str() );
310
311       PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos,
312                                              currentAirportID, heading, length,
313                                              width, displ_thresh1, stopway1,
314                                              surface_code);
315       
316       PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
317                                               FGRunway::reverseIdent(rwy_no), pos,
318                                              currentAirportID, heading + 180.0, length,
319                                              width, displ_thresh2, stopway2,
320                                              surface_code);
321
322       cache->setRunwayReciprocal(rwy, reciprocal);
323     }
324   }
325
326   void parseRunwayLine850(const vector<string>& token)
327   {
328     double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
329     int surface_code = atoi( token[2].c_str() );
330
331     double lat_1 = atof( token[9].c_str() );
332     double lon_1 = atof( token[10].c_str() );
333     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
334     rwy_lat_accum += lat_1;
335     rwy_lon_accum += lon_1;
336     rwy_count++;
337
338     double lat_2 = atof( token[18].c_str() );
339     double lon_2 = atof( token[19].c_str() );
340     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
341     rwy_lat_accum += lat_2;
342     rwy_lon_accum += lon_2;
343     rwy_count++;
344
345     double length, heading_1, heading_2, dummy;
346     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
347     SGGeod pos;
348     SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
349     length *= SG_METER_TO_FEET;
350
351     last_rwy_heading = heading_1;
352
353     const string& rwy_no_1(token[8]);
354     const string& rwy_no_2(token[17]);
355     if ( rwy_no_1.size() == 0 || rwy_no_2.size() == 0 )
356         return;
357
358     double displ_thresh1 = atof( token[11].c_str() );
359     double displ_thresh2 = atof( token[20].c_str() );
360
361     double stopway1 = atof( token[12].c_str() );
362     double stopway2 = atof( token[21].c_str() );
363
364     PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos,
365                                            currentAirportID, heading_1, length,
366                                            width, displ_thresh1, stopway1,
367                                            surface_code);
368     
369     PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
370                                                   rwy_no_2, pos,
371                                                   currentAirportID, heading_2, length,
372                                                   width, displ_thresh2, stopway2,
373                                                   surface_code);
374     
375     cache->setRunwayReciprocal(rwy, reciprocal);
376   }
377
378   void parseWaterRunwayLine850(const vector<string>& token)
379   {
380     double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
381
382     double lat_1 = atof( token[4].c_str() );
383     double lon_1 = atof( token[5].c_str() );
384     SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
385     rwy_lat_accum += lat_1;
386     rwy_lon_accum += lon_1;
387     rwy_count++;
388
389     double lat_2 = atof( token[7].c_str() );
390     double lon_2 = atof( token[8].c_str() );
391     SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
392     rwy_lat_accum += lat_2;
393     rwy_lon_accum += lon_2;
394     rwy_count++;
395
396     double length, heading_1, heading_2, dummy;
397     SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
398     SGGeod pos;
399     SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
400
401     last_rwy_heading = heading_1;
402
403     const string& rwy_no_1(token[3]);
404     const string& rwy_no_2(token[6]);
405
406     PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos,
407                                            currentAirportID, heading_1, length,
408                                            width, 0.0, 0.0, 13);
409     
410     PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
411                                                   rwy_no_2, pos,
412                                                   currentAirportID, heading_2, length,
413                                                   width, 0.0, 0.0, 13);
414     
415     cache->setRunwayReciprocal(rwy, reciprocal);
416   }
417
418   void parseHelipadLine850(const vector<string>& token)
419   {
420     double length = atof( token[5].c_str() ) * SG_METER_TO_FEET;
421     double width = atof( token[6].c_str() ) * SG_METER_TO_FEET;
422
423     double lat = atof( token[2].c_str() );
424     double lon = atof( token[3].c_str() );
425     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
426     rwy_lat_accum += lat;
427     rwy_lon_accum += lon;
428     rwy_count++;
429
430     double heading = atof( token[4].c_str() );
431
432     last_rwy_heading = heading;
433
434     const string& rwy_no(token[1]);
435     int surface_code = atoi( token[7].c_str() );
436
437     cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos,
438                         currentAirportID, heading, length,
439                         width, 0.0, 0.0, surface_code);
440   }
441
442   void parsePavementLine850(const vector<string>& token)
443   {
444     if ( token.size() >= 5 ) {
445       pavement_ident = token[4];
446       if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
447         pavement_ident.erase( pavement_ident.size()-1 );
448     } else {
449       pavement_ident = "xx";
450     }
451   }
452
453   void parsePavementNodeLine850(int num, const vector<string>& token)
454   {
455     double lat = atof( token[1].c_str() );
456     double lon = atof( token[2].c_str() );
457     SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
458
459     FGPavement* pvt = 0;
460     if ( !pavement_ident.empty() ) {
461       pvt = new FGPavement( 0, pavement_ident, pos );
462       pavements.push_back( pvt );
463       pavement_ident = "";
464     } else {
465       pvt = pavements.back();
466     }
467     if ( num == 112 || num == 114 ) {
468       double lat_b = atof( token[3].c_str() );
469       double lon_b = atof( token[4].c_str() );
470       SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
471       pvt->addBezierNode(pos, pos_b, num == 114);
472     } else {
473       pvt->addNode(pos, num == 113);
474     }
475   }
476
477   void parseCommLine(int lineId, const vector<string>& token) 
478   {
479     if ( rwy_count <= 0 ) {
480       SG_LOG( SG_GENERAL, SG_ALERT, "No runways; skipping comm for " + last_apt_id);
481     }
482  
483     SGGeod pos = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count, 
484         rwy_lat_accum / (double)rwy_count, last_apt_elev);
485     
486     // short int representing tens of kHz:
487     int freqKhz = atoi(token[1].c_str()) * 10;
488     int rangeNm = 50;
489     FGPositioned::Type ty;
490     // Make sure we only pass on stations with at least a name
491     if (token.size() >2){
492
493         switch (lineId) {
494             case 50:
495                 ty = FGPositioned::FREQ_AWOS;
496                 if (token[2] == "ATIS") {
497                     ty = FGPositioned::FREQ_ATIS;
498                 }
499                 break;
500
501             case 51:    ty = FGPositioned::FREQ_UNICOM; break;
502             case 52:    ty = FGPositioned::FREQ_CLEARANCE; break;
503             case 53:    ty = FGPositioned::FREQ_GROUND; break;
504             case 54:    ty = FGPositioned::FREQ_TOWER; break;
505             case 55:
506             case 56:    ty = FGPositioned::FREQ_APP_DEP; break;
507             default:
508                 throw sg_range_exception("unupported apt.dat comm station type");
509         }
510
511       cache->insertCommStation(ty, token[2], pos, freqKhz, rangeNm, currentAirportID);
512     }
513     else SG_LOG( SG_GENERAL, SG_DEBUG, "Found unnamed comm. Skipping: " << lineId);
514   }
515
516 };
517   
518 // Load the airport data base from the specified aptdb file.  The
519 // metar file is used to mark the airports as having metar available
520 // or not.
521 bool airportDBLoad( const SGPath &aptdb_file )
522 {
523   APTLoader ld;
524   ld.parseAPT(aptdb_file);
525   return true;
526 }
527   
528 bool metarDataLoad(const SGPath& metar_file)
529 {
530   sg_gzifstream metar_in( metar_file.str() );
531   if ( !metar_in.is_open() ) {
532     SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
533     return false;
534   }
535   
536   NavDataCache* cache = NavDataCache::instance();
537   string ident;
538   while ( metar_in ) {
539     metar_in >> ident;
540     if ( ident == "#" || ident == "//" ) {
541       metar_in >> skipeol;
542     } else {
543       cache->setAirportMetar(ident, true);
544     }
545   }
546   
547   return true;
548 }
549
550 } // of namespace flightgear