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., 675 Mass Ave, Cambridge, MA 02139, 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"
48 // Load the airport data base from the specified aptdb file. The
49 // metar file is used to mark the airports as having metar available
51 bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
52 const string &aptdb_file, const string &metar_file )
55 // Load the apt.dat file
58 sg_gzifstream in( aptdb_file );
59 if ( !in.is_open() ) {
60 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
65 string last_apt_id = "";
66 double last_apt_elev = 0.0;
67 string last_apt_name = "";
68 string last_apt_info = "";
69 string last_apt_type = "";
74 unsigned int line_id = 0;
75 unsigned int line_num = 0;
76 double rwy_lon_accum = 0.0;
77 double rwy_lat_accum = 0.0;
80 while ( ! in.eof() ) {
81 in.getline(tmp, 2048);
85 SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
86 if ( !line.size() || isspace(tmp[0]))
89 if (line.size() >= 3) {
90 char *p = (char *)memchr(tmp, ' ', 3);
96 if ( tmp[0] == 'I' ) {
97 // First line, indicates IBM (i.e. DOS line endings I
100 // move past this line and read and discard the next line
101 // which is the version and copyright information
102 in.getline(tmp, 2048);
103 // vector<string> vers_token = simgear::strutils::split( tmp );
104 if ( strlen(tmp) > 4 ) {
105 char *p = (char *)memchr(tmp, ' ', 4);
109 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
111 } else if ( line_id == 1 /* Airport */ ||
112 line_id == 16 /* Seaplane base */ ||
113 line_id == 17 /* Heliport */ ) {
116 token = simgear::strutils::split(line);
117 string id = token[4];
118 double elev = atof( token[1].c_str() );
119 SG_LOG( SG_GENERAL, SG_BULK, "Next airport = " << id << " "
122 if ( !last_apt_id.empty()) {
123 if ( rwy_count > 0 ) {
124 double lat = rwy_lat_accum / (double)rwy_count;
125 double lon = rwy_lon_accum / (double)rwy_count;
126 airports->add( last_apt_id, lon, lat, last_apt_elev,
127 last_apt_name, false );
129 if ( !last_apt_id.length() ) {
130 SG_LOG(SG_GENERAL, SG_ALERT,
131 "ERROR: No runways for " << last_apt_id
138 last_apt_elev = atof( token[1].c_str() );
142 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
143 last_apt_name += token[i];
144 last_apt_name += " ";
146 last_apt_name += token[token.size() - 1];
148 last_apt_info = line;
149 last_apt_type = token[0];
151 // clear runway list for start of next airport
155 } else if ( line_id == 10 ) {
157 token = simgear::strutils::split(line);
160 double lat = atof( token[1].c_str() );
161 double lon = atof( token[2].c_str() );
162 rwy_lat_accum += lat;
163 rwy_lon_accum += lon;
166 string rwy_no = token[3];
168 double heading = atof( token[4].c_str() );
169 double length = atoi( token[5].c_str() );
170 double width = atoi( token[8].c_str() );
172 string rwy_displ_threshold = token[6];
174 = simgear::strutils::split( rwy_displ_threshold, "." );
175 double displ_thresh1 = atof( displ[0].c_str() );
176 double displ_thresh2 = atof( displ[1].c_str() );
178 string rwy_stopway = token[7];
180 = simgear::strutils::split( rwy_stopway, "." );
181 double stopway1 = atof( stop[0].c_str() );
182 double stopway2 = atof( stop[1].c_str() );
184 string lighting_flags = token[9];
185 int surface_code = atoi( token[10].c_str() );
186 string shoulder_code = token[11];
187 int marking_code = atoi( token[12].c_str() );
188 double smoothness = atof( token[13].c_str() );
189 bool dist_remaining = (atoi( token[14].c_str() ) == 1 );
191 runways->add( last_apt_id, rwy_no, lon, lat, heading, length,
192 width, displ_thresh1, displ_thresh2,
193 stopway1, stopway2, lighting_flags, surface_code,
194 shoulder_code, marking_code, smoothness,
196 } else if ( line_id == 18 ) {
197 // beacon entry (ignore)
198 } else if ( line_id == 14 ) {
199 // control tower entry (ignore)
200 } else if ( line_id == 19 ) {
201 // windsock entry (ignore)
202 } else if ( line_id == 15 ) {
203 // custom startup locations (ignore)
204 } else if ( line_id >= 50 && line_id <= 56 ) {
205 // frequency entries (ignore)
206 } else if ( line_id == 99 ) {
207 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
209 SG_LOG( SG_GENERAL, SG_ALERT,
210 "Unknown line(#" << line_num << ") in file: " << line );
215 if ( !last_apt_id.empty()) {
216 if ( rwy_count > 0 ) {
217 double lat = rwy_lat_accum / (double)rwy_count;
218 double lon = rwy_lon_accum / (double)rwy_count;
219 airports->add( last_apt_id, lon, lat, last_apt_elev,
220 last_apt_name, false );
222 if ( !last_apt_id.length() ) {
223 SG_LOG(SG_GENERAL, SG_ALERT,
224 "ERROR: No runways for " << last_apt_id
231 // Load the metar.dat file and update apt db with stations that
235 sg_gzifstream metar_in( metar_file );
236 if ( !metar_in.is_open() ) {
237 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
243 if ( ident == "#" || ident == "//" ) {
246 const FGAirport* a = airports->search( ident );
247 if ( a ) airports->has_metar( ident );
251 SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");