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");
60 FGAirport* addAirport(FGAirportList *airports, const string& apt_id, const string& apt_name,
61 int rwy_count, double rwy_lat_accum, double rwy_lon_accum, double last_rwy_heading,
62 double apt_elev, SGGeod& tower, bool got_tower, int type)
68 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << apt_id
73 double lat = rwy_lat_accum / (double)rwy_count;
74 double lon = rwy_lon_accum / (double)rwy_count;
77 // tower height hard coded for now...
78 const float tower_height = 50.0f;
79 // make a little off the heading for 1 runway airports...
80 float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
81 float fudge_lat = .003f - fudge_lon;
82 tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, apt_elev + tower_height);
87 return airports->add(apt_id, SGGeod::fromDegFt(lon, lat, apt_elev), tower, apt_name, false,
88 fptypeFromRobinType(type));
91 // Load the airport data base from the specified aptdb file. The
92 // metar file is used to mark the airports as having metar available
94 bool fgAirportDBLoad( FGAirportList *airports,
95 const string &aptdb_file, const string &metar_file )
98 // Load the apt.dat file
101 sg_gzifstream in( aptdb_file );
102 if ( !in.is_open() ) {
103 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
107 vector<string> token;
108 string last_apt_id = "";
109 double last_apt_elev = 0.0;
110 string last_apt_name = "";
111 string last_apt_info = "";
112 int last_apt_type = 0;
114 bool got_tower = false;
118 vector<FGRunwayPtr> runways;
120 unsigned int line_id = 0;
121 unsigned int line_num = 0;
122 double rwy_lon_accum = 0.0;
123 double rwy_lat_accum = 0.0;
125 double last_rwy_heading = 0.0;
127 while ( ! in.eof() ) {
128 in.getline(tmp, 2048);
132 SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
133 if ( !line.size() || isspace(tmp[0]))
136 if (line.size() >= 3) {
137 char *p = (char *)memchr(tmp, ' ', 3);
143 if ( tmp[0] == 'I' ) {
144 // First line, indicates IBM (i.e. DOS line endings I
147 // move past this line and read and discard the next line
148 // which is the version and copyright information
149 in.getline(tmp, 2048);
150 // vector<string> vers_token = simgear::strutils::split( tmp );
151 if ( strlen(tmp) > 4 ) {
152 char *p = (char *)memchr(tmp, ' ', 4);
156 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
158 } else if ( line_id == 1 /* Airport */ ||
159 line_id == 16 /* Seaplane base */ ||
160 line_id == 17 /* Heliport */ ) {
163 token = simgear::strutils::split(line);
164 string id = token[4];
165 double elev = atof( token[1].c_str() );
166 SG_LOG( SG_GENERAL, SG_BULK, "Next airport = " << id << " "
169 FGAirport* apt = addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
170 last_rwy_heading, last_apt_elev, last_tower, got_tower, last_apt_type);
172 for (unsigned int r=0; r< runways.size(); ++r) {
173 apt->addRunway(runways[r]);
179 last_apt_elev = elev;
184 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
185 last_apt_name += token[i];
186 last_apt_name += " ";
188 last_apt_name += token[token.size() - 1];
190 last_apt_info = line;
191 last_apt_type = atoi( token[0].c_str() );
193 // clear runway list for start of next airport
197 } else if ( line_id == 10 ) {
199 token = simgear::strutils::split(line);
202 double lat = atof( token[1].c_str() );
203 double lon = atof( token[2].c_str() );
204 rwy_lat_accum += lat;
205 rwy_lon_accum += lon;
208 string rwy_no = token[3];
210 double heading = atof( token[4].c_str() );
211 double length = atoi( token[5].c_str() );
212 double width = atoi( token[8].c_str() );
214 last_rwy_heading = heading;
216 string rwy_displ_threshold = token[6];
218 = simgear::strutils::split( rwy_displ_threshold, "." );
219 double displ_thresh1 = atof( displ[0].c_str() );
220 double displ_thresh2 = atof( displ[1].c_str() );
222 string rwy_stopway = token[7];
224 = simgear::strutils::split( rwy_stopway, "." );
225 double stopway1 = atof( stop[0].c_str() );
226 double stopway2 = atof( stop[1].c_str() );
228 int surface_code = atoi( token[10].c_str() );
229 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
230 FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
231 width, displ_thresh1, stopway1, surface_code, false);
232 runways.push_back(rwy);
234 if (rwy_no[0] != 'x') {
235 // runways need a reciprocal, taxiways do not
236 FGRunway* reciprocal = new FGRunway(NULL, FGRunway::reverseIdent(rwy_no),
237 pos, heading + 180.0, length, width,
238 displ_thresh2, stopway2, surface_code, true);
240 runways.push_back(reciprocal);
242 } else if ( line_id == 18 ) {
243 // beacon entry (ignore)
244 } else if ( line_id == 14 ) {
245 // control tower entry
247 token = simgear::strutils::split(line);
249 double lat = atof( token[1].c_str() );
250 double lon = atof( token[2].c_str() );
251 double elev = atof( token[3].c_str() );
252 last_tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
254 } else if ( line_id == 19 ) {
255 // windsock entry (ignore)
256 } else if ( line_id == 15 ) {
257 // custom startup locations (ignore)
258 } else if ( line_id == 0 ) {
260 } else if ( line_id >= 50 && line_id <= 56 ) {
261 // frequency entries (ignore)
262 } else if ( line_id == 99 ) {
263 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
265 SG_LOG( SG_GENERAL, SG_ALERT,
266 "Unknown line(#" << line_num << ") in file: " << line );
271 // add the last airport being processed if any
272 addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
273 last_rwy_heading, last_apt_elev, last_tower, got_tower, last_apt_type);
277 // Load the metar.dat file and update apt db with stations that
281 sg_gzifstream metar_in( metar_file );
282 if ( !metar_in.is_open() ) {
283 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
289 if ( ident == "#" || ident == "//" ) {
292 const FGAirport* a = airports->search( ident );
293 if ( a ) const_cast<FGAirport*>(a)->setMetar(true);
297 SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");