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>
39 #include <simgear/structure/exception.hxx>
44 #include "runways.hxx"
46 #include "apt_loader.hxx"
48 static FGPositioned::Type fptypeFromRobinType(int aType)
51 case 1: return FGPositioned::AIRPORT;
52 case 16: return FGPositioned::SEAPORT;
53 case 17: return FGPositioned::HELIPORT;
55 SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
56 throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
63 void parseAPT(const string &aptdb_file)
65 sg_gzifstream in( aptdb_file );
66 if ( !in.is_open() ) {
67 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
75 unsigned int line_id = 0;
76 unsigned int line_num = 0;
78 while ( ! in.eof() ) {
79 in.getline(tmp, 2048);
80 line = tmp; // string copy, ack
83 if ( !line.size() || isspace(tmp[0])) {
87 if (line.size() >= 3) {
88 char *p = (char *)memchr(tmp, ' ', 3);
94 if ( tmp[0] == 'I' ) {
95 // First line, indicates IBM (i.e. DOS line endings I
98 // move past this line and read and discard the next line
99 // which is the version and copyright information
100 in.getline(tmp, 2048);
101 // vector<string> vers_token = simgear::strutils::split( tmp );
102 if ( strlen(tmp) > 4 ) {
103 char *p = (char *)memchr(tmp, ' ', 4);
107 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
108 } else if ( line_id == 1 /* Airport */ ||
109 line_id == 16 /* Seaplane base */ ||
110 line_id == 17 /* Heliport */ ) {
111 parseAirportLine(simgear::strutils::split(line));
112 } else if ( line_id == 10 ) {
113 parseRunwayLine(simgear::strutils::split(line));
114 } else if ( line_id == 18 ) {
115 // beacon entry (ignore)
116 } else if ( line_id == 14 ) {
117 // control tower entry
118 vector<string> token(simgear::strutils::split(line));
120 double lat = atof( token[1].c_str() );
121 double lon = atof( token[2].c_str() );
122 double elev = atof( token[3].c_str() );
123 tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
125 } else if ( line_id == 19 ) {
126 // windsock entry (ignore)
127 } else if ( line_id == 15 ) {
128 // custom startup locations (ignore)
129 } else if ( line_id == 0 ) {
131 } else if ( line_id >= 50 && line_id <= 56 ) {
132 // frequency entries (ignore)
133 } else if ( line_id == 99 ) {
134 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
136 SG_LOG( SG_GENERAL, SG_ALERT,
137 "Unknown line(#" << line_num << ") in file: " << line );
146 double rwy_lat_accum;
147 double rwy_lon_accum;
148 double last_rwy_heading;
152 string last_apt_name;
153 double last_apt_elev;
157 vector<FGRunwayPtr> runways;
158 vector<FGTaxiwayPtr> taxiways;
162 if (last_apt_id.empty()) {
167 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
172 double lat = rwy_lat_accum / (double)rwy_count;
173 double lon = rwy_lon_accum / (double)rwy_count;
176 // tower height hard coded for now...
177 const float tower_height = 50.0f;
178 // make a little off the heading for 1 runway airports...
179 float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
180 float fudge_lat = .003f - fudge_lon;
181 tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
184 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
185 FGAirport* apt = new FGAirport(last_apt_id, pos, tower, last_apt_name, false,
186 fptypeFromRobinType(last_apt_type));
188 apt->setRunwaysAndTaxiways(runways, taxiways);
191 void parseAirportLine(const vector<string>& token)
193 const string& id(token[4]);
194 double elev = atof( token[1].c_str() );
199 last_apt_elev = elev;
204 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
205 last_apt_name += token[i];
206 last_apt_name += " ";
208 last_apt_name += token[token.size() - 1];
209 last_apt_type = atoi( token[0].c_str() );
211 // clear runway list for start of next airport
217 void parseRunwayLine(const vector<string>& token)
219 double lat = atof( token[1].c_str() );
220 double lon = atof( token[2].c_str() );
221 rwy_lat_accum += lat;
222 rwy_lon_accum += lon;
225 const string& rwy_no(token[3]);
227 double heading = atof( token[4].c_str() );
228 double length = atoi( token[5].c_str() );
229 double width = atoi( token[8].c_str() );
231 last_rwy_heading = heading;
233 int surface_code = atoi( token[10].c_str() );
234 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
236 if (rwy_no[0] == 'x') {
238 FGTaxiway* t = new FGTaxiway(rwy_no, pos, heading, length, width, surface_code);
239 taxiways.push_back(t);
242 string rwy_displ_threshold = token[6];
244 = simgear::strutils::split( rwy_displ_threshold, "." );
245 double displ_thresh1 = atof( displ[0].c_str() );
246 double displ_thresh2 = atof( displ[1].c_str() );
248 string rwy_stopway = token[7];
250 = simgear::strutils::split( rwy_stopway, "." );
251 double stopway1 = atof( stop[0].c_str() );
252 double stopway2 = atof( stop[1].c_str() );
254 FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
255 width, displ_thresh1, stopway1, surface_code, false);
256 runways.push_back(rwy);
258 FGRunway* reciprocal = new FGRunway(NULL, FGRunway::reverseIdent(rwy_no),
259 pos, heading + 180.0, length, width,
260 displ_thresh2, stopway2, surface_code, true);
262 runways.push_back(reciprocal);
267 // Load the airport data base from the specified aptdb file. The
268 // metar file is used to mark the airports as having metar available
270 bool fgAirportDBLoad( const string &aptdb_file, const string &metar_file )
273 ld.parseAPT(aptdb_file);
276 // Load the metar.dat file and update apt db with stations that
280 sg_gzifstream metar_in( metar_file );
281 if ( !metar_in.is_open() ) {
282 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
288 if ( ident == "#" || ident == "//" ) {
291 FGAirport* apt = FGAirport::findByIdent(ident);
298 SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");