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 FGAirport* 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 return 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,
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;
103 vector<FGRunway> runways;
105 unsigned int line_id = 0;
106 unsigned int line_num = 0;
107 double rwy_lon_accum = 0.0;
108 double rwy_lat_accum = 0.0;
110 double last_rwy_heading = 0.0;
112 while ( ! in.eof() ) {
113 in.getline(tmp, 2048);
117 SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
118 if ( !line.size() || isspace(tmp[0]))
121 if (line.size() >= 3) {
122 char *p = (char *)memchr(tmp, ' ', 3);
128 if ( tmp[0] == 'I' ) {
129 // First line, indicates IBM (i.e. DOS line endings I
132 // move past this line and read and discard the next line
133 // which is the version and copyright information
134 in.getline(tmp, 2048);
135 // vector<string> vers_token = simgear::strutils::split( tmp );
136 if ( strlen(tmp) > 4 ) {
137 char *p = (char *)memchr(tmp, ' ', 4);
141 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
143 } else if ( line_id == 1 /* Airport */ ||
144 line_id == 16 /* Seaplane base */ ||
145 line_id == 17 /* Heliport */ ) {
148 token = simgear::strutils::split(line);
149 string id = token[4];
150 double elev = atof( token[1].c_str() );
151 SG_LOG( SG_GENERAL, SG_BULK, "Next airport = " << id << " "
154 FGAirport* apt = addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
155 last_rwy_heading, last_apt_elev, last_tower, got_tower, last_apt_type);
157 for (unsigned int r=0; r< runways.size(); ++r) {
158 apt->addRunway(runways[r]);
164 last_apt_elev = elev;
169 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
170 last_apt_name += token[i];
171 last_apt_name += " ";
173 last_apt_name += token[token.size() - 1];
175 last_apt_info = line;
176 last_apt_type = atoi( token[0].c_str() );
178 // clear runway list for start of next airport
182 } else if ( line_id == 10 ) {
184 token = simgear::strutils::split(line);
187 double lat = atof( token[1].c_str() );
188 double lon = atof( token[2].c_str() );
189 rwy_lat_accum += lat;
190 rwy_lon_accum += lon;
193 string rwy_no = token[3];
195 double heading = atof( token[4].c_str() );
196 double length = atoi( token[5].c_str() );
197 double width = atoi( token[8].c_str() );
199 last_rwy_heading = heading;
201 string rwy_displ_threshold = token[6];
203 = simgear::strutils::split( rwy_displ_threshold, "." );
204 double displ_thresh1 = atof( displ[0].c_str() );
205 double displ_thresh2 = atof( displ[1].c_str() );
207 string rwy_stopway = token[7];
209 = simgear::strutils::split( rwy_stopway, "." );
210 double stopway1 = atof( stop[0].c_str() );
211 double stopway2 = atof( stop[1].c_str() );
213 int surface_code = atoi( token[10].c_str() );
215 FGRunway rwy(rwy_no, lon, lat, heading, length,
216 width, displ_thresh1, displ_thresh2,
217 stopway1, stopway2, surface_code);
218 runways.push_back(rwy);
219 } else if ( line_id == 18 ) {
220 // beacon entry (ignore)
221 } else if ( line_id == 14 ) {
222 // control tower entry
224 token = simgear::strutils::split(line);
226 double lat = atof( token[1].c_str() );
227 double lon = atof( token[2].c_str() );
228 double elev = atof( token[3].c_str() );
229 last_tower = SGGeod::fromDegFt(lon, lat, elev);
231 } else if ( line_id == 19 ) {
232 // windsock entry (ignore)
233 } else if ( line_id == 15 ) {
234 // custom startup locations (ignore)
235 } else if ( line_id == 0 ) {
237 } else if ( line_id >= 50 && line_id <= 56 ) {
238 // frequency entries (ignore)
239 } else if ( line_id == 99 ) {
240 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
242 SG_LOG( SG_GENERAL, SG_ALERT,
243 "Unknown line(#" << line_num << ") in file: " << line );
248 // add the last airport being processed if any
249 addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
250 last_rwy_heading, last_apt_elev, last_tower, got_tower, 0);
254 // Load the metar.dat file and update apt db with stations that
258 sg_gzifstream metar_in( metar_file );
259 if ( !metar_in.is_open() ) {
260 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
266 if ( ident == "#" || ident == "//" ) {
269 const FGAirport* a = airports->search( ident );
270 if ( a ) airports->has_metar( ident );
274 SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");