]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
initialize release_keys array
[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
40 #include STL_STRING
41
42 #include "simple.hxx"
43 #include "runways.hxx"
44
45 #include "apt_loader.hxx"
46
47 static void addAirport(FGAirportList *airports, const string& apt_id, const string& apt_name,
48     int rwy_count, double rwy_lat_accum, double rwy_lon_accum, double last_rwy_heading,
49     double apt_elev, SGGeod& tower, bool got_tower, int type)
50 {
51     if (apt_id.empty())
52         return;
53
54     if (!rwy_count) {
55         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << apt_id
56                 << ", skipping." );
57         return;
58     }
59
60     double lat = rwy_lat_accum / (double)rwy_count;
61     double lon = rwy_lon_accum / (double)rwy_count;
62
63     if (!got_tower) {
64         // tower height hard coded for now...
65         const float tower_height = 50.0f;
66         // make a little off the heading for 1 runway airports...
67         float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
68         float fudge_lat = .003f - fudge_lon;
69         tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, apt_elev + tower_height);
70     }
71
72     airports->add(apt_id, SGGeod::fromDegFt(lon, lat, apt_elev), tower, apt_name, false,
73             type == 1/*airport*/, type == 16/*seaport*/, type == 17/*heliport*/);
74 }
75
76 // Load the airport data base from the specified aptdb file.  The
77 // metar file is used to mark the airports as having metar available
78 // or not.
79 bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
80                       const string &aptdb_file, const string &metar_file )
81 {
82     //
83     // Load the apt.dat file
84     //
85
86     sg_gzifstream in( aptdb_file );
87     if ( !in.is_open() ) {
88         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
89         exit(-1);
90     }
91
92     vector<string> token;
93     string last_apt_id = "";
94     double last_apt_elev = 0.0;
95     string last_apt_name = "";
96     string last_apt_info = "";
97     int last_apt_type = 0;
98     SGGeod last_tower;
99     bool got_tower = false;
100     string line;
101     char tmp[2049];
102     tmp[2048] = 0;
103
104     unsigned int line_id = 0;
105     unsigned int line_num = 0;
106     double rwy_lon_accum = 0.0;
107     double rwy_lat_accum = 0.0;
108     int rwy_count = 0;
109     double last_rwy_heading = 0.0;
110
111     while ( ! in.eof() ) {
112         in.getline(tmp, 2048);
113         line = tmp;
114         line_num++;
115
116         SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
117         if ( !line.size() || isspace(tmp[0]))
118             continue;
119
120         if (line.size() >= 3) {
121             char *p = (char *)memchr(tmp, ' ', 3);
122             if ( p )
123                 *p = 0;
124         }
125
126         line_id = atoi(tmp);
127         if ( tmp[0] == 'I' ) {
128             // First line, indicates IBM (i.e. DOS line endings I
129             // believe.)
130
131             // move past this line and read and discard the next line
132             // which is the version and copyright information
133             in.getline(tmp, 2048);
134             // vector<string> vers_token = simgear::strutils::split( tmp );
135             if ( strlen(tmp) > 4 ) {
136                char *p = (char *)memchr(tmp, ' ', 4);
137                if ( p )
138                   *p = 0;
139             }
140             SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
141                     << tmp );
142         } else if ( line_id == 1 /* Airport */ ||
143                     line_id == 16 /* Seaplane base */ ||
144                     line_id == 17 /* Heliport */ ) {
145
146             token.clear();
147             token = simgear::strutils::split(line);
148             string id = token[4];
149             double elev = atof( token[1].c_str() );
150             SG_LOG( SG_GENERAL, SG_BULK, "Next airport = " << id << " "
151                     << elev );
152
153             addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
154                 last_rwy_heading, last_apt_elev, last_tower, got_tower, last_apt_type);
155
156             last_apt_id = id;
157             last_apt_elev = elev;
158             last_apt_name = "";
159             got_tower = false;
160
161             // build the name
162             for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
163                 last_apt_name += token[i];
164                 last_apt_name += " ";
165             }
166             last_apt_name += token[token.size() - 1];
167
168             last_apt_info = line;
169             last_apt_type = atoi( token[0].c_str() );
170
171             // clear runway list for start of next airport
172             rwy_lon_accum = 0.0;
173             rwy_lat_accum = 0.0;
174             rwy_count = 0;
175         } else if ( line_id == 10 ) {
176             token.clear();
177             token = simgear::strutils::split(line);
178
179             // runway entry
180             double lat = atof( token[1].c_str() );
181             double lon = atof( token[2].c_str() );
182             rwy_lat_accum += lat;
183             rwy_lon_accum += lon;
184             rwy_count++;
185
186             string rwy_no = token[3];
187
188             double heading = atof( token[4].c_str() );
189             double length = atoi( token[5].c_str() );
190             double width = atoi( token[8].c_str() );
191             
192             last_rwy_heading = heading;
193
194             string rwy_displ_threshold = token[6];
195             vector<string> displ
196                 = simgear::strutils::split( rwy_displ_threshold, "." );
197             double displ_thresh1 = atof( displ[0].c_str() );
198             double displ_thresh2 = atof( displ[1].c_str() );
199
200             string rwy_stopway = token[7];
201             vector<string> stop
202                 = simgear::strutils::split( rwy_stopway, "." );
203             double stopway1 = atof( stop[0].c_str() );
204             double stopway2 = atof( stop[1].c_str() );
205
206             string lighting_flags = token[9];
207             int surface_code = atoi( token[10].c_str() );
208             string shoulder_code = token[11];
209             int marking_code = atoi( token[12].c_str() );
210             double smoothness = atof( token[13].c_str() );
211             bool dist_remaining = (atoi( token[14].c_str() ) == 1 );
212
213             runways->add( last_apt_id, rwy_no, lon, lat, heading, length,
214                           width, displ_thresh1, displ_thresh2,
215                           stopway1, stopway2, lighting_flags, surface_code,
216                           shoulder_code, marking_code, smoothness,
217                           dist_remaining );
218         } else if ( line_id == 18 ) {
219             // beacon entry (ignore)
220         } else if ( line_id == 14 ) {
221             // control tower entry
222             token.clear();
223             token = simgear::strutils::split(line);
224
225             double lat = atof( token[1].c_str() );
226             double lon = atof( token[2].c_str() );
227             double elev = atof( token[3].c_str() );
228             last_tower = SGGeod::fromDegFt(lon, lat, elev);
229             got_tower = true;
230         } else if ( line_id == 19 ) {
231             // windsock entry (ignore)
232         } else if ( line_id == 15 ) {
233             // custom startup locations (ignore)
234         } else if ( line_id == 0 ) {
235             // ??
236         } else if ( line_id >= 50 && line_id <= 56 ) {
237             // frequency entries (ignore)
238         } else if ( line_id == 99 ) {
239             SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
240         } else {
241             SG_LOG( SG_GENERAL, SG_ALERT, 
242                     "Unknown line(#" << line_num << ") in file: " << line );
243             exit(-1);
244         }
245     }
246
247     // add the last airport being processed if any
248     addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
249         last_rwy_heading, last_apt_elev, last_tower, got_tower, 0);
250
251
252     //
253     // Load the metar.dat file and update apt db with stations that
254     // have metar data.
255     //
256
257     sg_gzifstream metar_in( metar_file );
258     if ( !metar_in.is_open() ) {
259         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
260     }
261
262     string ident;
263     while ( metar_in ) {
264         metar_in >> ident;
265         if ( ident == "#" || ident == "//" ) {
266             metar_in >> skipeol;
267         } else {
268             const FGAirport* a = airports->search( ident );
269             if ( a ) airports->has_metar( ident );
270         }
271     }
272
273     SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");
274
275     return true;
276 }