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