1 // apt_loader.cxx -- a front end loader of the apt.dat file. This loader
2 // populates the runway and basic classes.
4 // Written by Curtis Olson, started August 2000.
6 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
29 #include <simgear/compiler.h>
31 #include <stdlib.h> // atof(), atoi()
32 #include <string.h> // memchr()
33 #include <ctype.h> // isspace()
35 #include <simgear/constants.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/misc/sgstream.hxx>
38 #include <simgear/misc/strutils.hxx>
43 #include "runways.hxx"
45 #include "apt_loader.hxx"
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)
55 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << apt_id
60 double lat = rwy_lat_accum / (double)rwy_count;
61 double lon = rwy_lon_accum / (double)rwy_count;
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);
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*/);
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
79 bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
80 const string &aptdb_file, const string &metar_file )
83 // Load the apt.dat file
86 sg_gzifstream in( aptdb_file );
87 if ( !in.is_open() ) {
88 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
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;
99 bool got_tower = false;
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;
109 double last_rwy_heading = 0.0;
111 while ( ! in.eof() ) {
112 in.getline(tmp, 2048);
116 SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
117 if ( !line.size() || isspace(tmp[0]))
120 if (line.size() >= 3) {
121 char *p = (char *)memchr(tmp, ' ', 3);
127 if ( tmp[0] == 'I' ) {
128 // First line, indicates IBM (i.e. DOS line endings I
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);
140 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
142 } else if ( line_id == 1 /* Airport */ ||
143 line_id == 16 /* Seaplane base */ ||
144 line_id == 17 /* Heliport */ ) {
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 << " "
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);
157 last_apt_elev = elev;
162 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
163 last_apt_name += token[i];
164 last_apt_name += " ";
166 last_apt_name += token[token.size() - 1];
168 last_apt_info = line;
169 last_apt_type = atoi( token[0].c_str() );
171 // clear runway list for start of next airport
175 } else if ( line_id == 10 ) {
177 token = simgear::strutils::split(line);
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;
186 string rwy_no = token[3];
188 double heading = atof( token[4].c_str() );
189 double length = atoi( token[5].c_str() );
190 double width = atoi( token[8].c_str() );
192 last_rwy_heading = heading;
194 string rwy_displ_threshold = token[6];
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() );
200 string rwy_stopway = token[7];
202 = simgear::strutils::split( rwy_stopway, "." );
203 double stopway1 = atof( stop[0].c_str() );
204 double stopway2 = atof( stop[1].c_str() );
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 );
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,
218 } else if ( line_id == 18 ) {
219 // beacon entry (ignore)
220 } else if ( line_id == 14 ) {
221 // control tower entry
223 token = simgear::strutils::split(line);
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);
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 ) {
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" );
241 SG_LOG( SG_GENERAL, SG_ALERT,
242 "Unknown line(#" << line_num << ") in file: " << line );
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);
253 // Load the metar.dat file and update apt db with stations that
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 );
265 if ( ident == "#" || ident == "//" ) {
268 const FGAirport* a = airports->search( ident );
269 if ( a ) airports->has_metar( ident );
273 SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");