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 "apt_loader.hxx"
31 #include <simgear/compiler.h>
33 #include <stdlib.h> // atof(), atoi()
34 #include <string.h> // memchr()
35 #include <ctype.h> // isspace()
37 #include <simgear/constants.h>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sgstream.hxx>
40 #include <simgear/misc/strutils.hxx>
41 #include <simgear/structure/exception.hxx>
42 #include <simgear/bucket/newbucket.hxx>
47 #include "runways.hxx"
48 #include "pavement.hxx"
50 #include <ATC/CommStation.hxx>
56 static FGPositioned::Type fptypeFromRobinType(int aType)
59 case 1: return FGPositioned::AIRPORT;
60 case 16: return FGPositioned::SEAPORT;
61 case 17: return FGPositioned::HELIPORT;
63 SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
64 throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
82 void parseAPT(const string &aptdb_file)
84 sg_gzifstream in( aptdb_file );
86 if ( !in.is_open() ) {
87 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
95 unsigned int line_id = 0;
96 unsigned int line_num = 0;
98 while ( ! in.eof() ) {
99 in.getline(tmp, 2048);
100 line = tmp; // string copy, ack
103 if ( !line.size() || isspace(tmp[0])) {
107 if (line.size() >= 3) {
108 char *p = (char *)memchr(tmp, ' ', 3);
114 if ( tmp[0] == 'I' ) {
115 // First line, indicates IBM (i.e. DOS line endings I
118 // move past this line and read and discard the next line
119 // which is the version and copyright information
120 in.getline(tmp, 2048);
121 // vector<string> vers_token = simgear::strutils::split( tmp );
122 if ( strlen(tmp) > 4 ) {
123 char *p = (char *)memchr(tmp, ' ', 4);
127 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
128 } else if ( line_id == 1 /* Airport */ ||
129 line_id == 16 /* Seaplane base */ ||
130 line_id == 17 /* Heliport */ ) {
131 parseAirportLine(simgear::strutils::split(line));
132 } else if ( line_id == 10 ) { // Runway v810
133 parseRunwayLine810(simgear::strutils::split(line));
134 } else if ( line_id == 100 ) { // Runway v850
135 parseRunwayLine850(simgear::strutils::split(line));
136 } else if ( line_id == 101 ) { // Water Runway v850
137 parseWaterRunwayLine850(simgear::strutils::split(line));
138 } else if ( line_id == 102 ) { // Helipad v850
139 parseHelipadLine850(simgear::strutils::split(line));
140 } else if ( line_id == 18 ) {
141 // beacon entry (ignore)
142 } else if ( line_id == 14 ) {
143 // control tower entry
144 vector<string> token(simgear::strutils::split(line));
146 double lat = atof( token[1].c_str() );
147 double lon = atof( token[2].c_str() );
148 double elev = atof( token[3].c_str() );
149 tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
151 } else if ( line_id == 19 ) {
152 // windsock entry (ignore)
153 } else if ( line_id == 20 ) {
154 // Taxiway sign (ignore)
155 } else if ( line_id == 21 ) {
156 // lighting objects (ignore)
157 } else if ( line_id == 15 ) {
158 // custom startup locations (ignore)
159 } else if ( line_id == 0 ) {
161 } else if ( line_id >= 50 && line_id <= 56) {
162 parseCommLine(line_id, simgear::strutils::split(line));
163 } else if ( line_id == 110 ) {
165 parsePavementLine850(simgear::strutils::split(line, 0, 4));
166 } else if ( line_id >= 111 && line_id <= 114 ) {
168 parsePavementNodeLine850(line_id, simgear::strutils::split(line));
169 } else if ( line_id >= 115 && line_id <= 116 ) {
170 // other pavement nodes (ignore)
171 } else if ( line_id == 120 ) {
173 } else if ( line_id == 130 ) {
175 } else if ( line_id == 99 ) {
176 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
178 SG_LOG( SG_GENERAL, SG_ALERT,
179 "Unknown line(#" << line_num << ") in file: " << line );
188 vector<string> token;
189 double rwy_lat_accum;
190 double rwy_lon_accum;
191 double last_rwy_heading;
195 string last_apt_name;
196 double last_apt_elev;
198 string last_apt_info;
199 string last_apt_type;
200 string pavement_ident;
203 vector<FGRunwayPtr> runways;
204 vector<FGTaxiwayPtr> taxiways;
205 vector<FGPavementPtr> pavements;
206 vector<flightgear::CommStation*> commStations;
210 if (last_apt_id.empty()) {
215 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
220 double lat = rwy_lat_accum / (double)rwy_count;
221 double lon = rwy_lon_accum / (double)rwy_count;
224 // tower height hard coded for now...
225 const float tower_height = 50.0f;
226 // make a little off the heading for 1 runway airports...
227 float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
228 float fudge_lat = .003f - fudge_lon;
229 tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
232 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
233 FGAirport* apt = new FGAirport(last_apt_id, pos, tower, last_apt_name, false,
234 fptypeFromRobinType(atoi(last_apt_type.c_str())));
235 apt->setRunwaysAndTaxiways(runways, taxiways, pavements);
236 apt->setCommStations(commStations);
239 void parseAirportLine(const vector<string>& token)
241 const string& id(token[4]);
242 double elev = atof( token[1].c_str() );
247 last_apt_elev = elev;
252 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
253 last_apt_name += token[i];
254 last_apt_name += " ";
256 last_apt_name += token[token.size() - 1];
257 last_apt_type = token[0];
259 // clear runway list for start of next airport
265 void parseRunwayLine810(const vector<string>& token)
267 double lat = atof( token[1].c_str() );
268 double lon = atof( token[2].c_str() );
269 rwy_lat_accum += lat;
270 rwy_lon_accum += lon;
273 const string& rwy_no(token[3]);
275 double heading = atof( token[4].c_str() );
276 double length = atoi( token[5].c_str() );
277 double width = atoi( token[8].c_str() );
279 last_rwy_heading = heading;
281 int surface_code = atoi( token[10].c_str() );
282 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
284 if (rwy_no[0] == 'x') {
286 FGTaxiway* t = new FGTaxiway(rwy_no, pos, heading, length, width, surface_code);
287 taxiways.push_back(t);
290 string rwy_displ_threshold = token[6];
292 = simgear::strutils::split( rwy_displ_threshold, "." );
293 double displ_thresh1 = atof( displ[0].c_str() );
294 double displ_thresh2 = atof( displ[1].c_str() );
296 string rwy_stopway = token[7];
298 = simgear::strutils::split( rwy_stopway, "." );
299 double stopway1 = atof( stop[0].c_str() );
300 double stopway2 = atof( stop[1].c_str() );
302 FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
303 width, displ_thresh1, stopway1, surface_code, false);
304 runways.push_back(rwy);
306 FGRunway* reciprocal = new FGRunway(NULL, FGRunway::reverseIdent(rwy_no),
307 pos, heading + 180.0, length, width,
308 displ_thresh2, stopway2, surface_code, true);
310 runways.push_back(reciprocal);
312 rwy->setReciprocalRunway(reciprocal);
313 reciprocal->setReciprocalRunway(rwy);
317 void parseRunwayLine850(const vector<string>& token)
319 double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
320 int surface_code = atoi( token[2].c_str() );
322 double lat_1 = atof( token[9].c_str() );
323 double lon_1 = atof( token[10].c_str() );
324 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
325 rwy_lat_accum += lat_1;
326 rwy_lon_accum += lon_1;
329 double lat_2 = atof( token[18].c_str() );
330 double lon_2 = atof( token[19].c_str() );
331 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
332 rwy_lat_accum += lat_2;
333 rwy_lon_accum += lon_2;
336 double length, heading_1, heading_2, dummy;
337 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
339 SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
340 length *= SG_METER_TO_FEET;
342 last_rwy_heading = heading_1;
344 const string& rwy_no_1(token[8]);
345 const string& rwy_no_2(token[17]);
346 if ( rwy_no_1.size() == 0 || rwy_no_2.size() == 0 )
349 double displ_thresh1 = atof( token[11].c_str() );
350 double displ_thresh2 = atof( token[20].c_str() );
352 double stopway1 = atof( token[12].c_str() );
353 double stopway2 = atof( token[21].c_str() );
355 FGRunway* rwy = new FGRunway(NULL, rwy_no_1, pos, heading_1, length,
356 width, displ_thresh1, stopway1, surface_code, false);
357 runways.push_back(rwy);
359 FGRunway* reciprocal = new FGRunway(NULL, rwy_no_2,
360 pos, heading_2, length, width,
361 displ_thresh2, stopway2, surface_code, true);
362 runways.push_back(reciprocal);
364 rwy->setReciprocalRunway(reciprocal);
365 reciprocal->setReciprocalRunway(rwy);
368 void parseWaterRunwayLine850(const vector<string>& token)
370 double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
372 double lat_1 = atof( token[4].c_str() );
373 double lon_1 = atof( token[5].c_str() );
374 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
375 rwy_lat_accum += lat_1;
376 rwy_lon_accum += lon_1;
379 double lat_2 = atof( token[7].c_str() );
380 double lon_2 = atof( token[8].c_str() );
381 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
382 rwy_lat_accum += lat_2;
383 rwy_lon_accum += lon_2;
386 double length, heading_1, heading_2, dummy;
387 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
389 SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
391 last_rwy_heading = heading_1;
393 const string& rwy_no_1(token[3]);
394 const string& rwy_no_2(token[6]);
396 FGRunway* rwy = new FGRunway(NULL, rwy_no_1, pos, heading_1, length,
397 width, 0.0, 0.0, 13, false);
398 runways.push_back(rwy);
400 FGRunway* reciprocal = new FGRunway(NULL, rwy_no_2,
401 pos, heading_2, length, width,
403 runways.push_back(reciprocal);
405 rwy->setReciprocalRunway(reciprocal);
406 reciprocal->setReciprocalRunway(rwy);
409 void parseHelipadLine850(const vector<string>& token)
411 double length = atof( token[5].c_str() ) * SG_METER_TO_FEET;
412 double width = atof( token[6].c_str() ) * SG_METER_TO_FEET;
414 double lat = atof( token[2].c_str() );
415 double lon = atof( token[3].c_str() );
416 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
417 rwy_lat_accum += lat;
418 rwy_lon_accum += lon;
421 double heading = atof( token[4].c_str() );
423 last_rwy_heading = heading;
425 const string& rwy_no(token[1]);
426 int surface_code = atoi( token[7].c_str() );
428 FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
429 width, 0.0, 0.0, surface_code, false);
430 runways.push_back(rwy);
433 void parsePavementLine850(const vector<string>& token)
435 if ( token.size() >= 5 ) {
436 pavement_ident = token[4];
437 if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
438 pavement_ident.erase( pavement_ident.size()-1 );
440 pavement_ident = "xx";
444 void parsePavementNodeLine850(int num, const vector<string>& token)
446 double lat = atof( token[1].c_str() );
447 double lon = atof( token[2].c_str() );
448 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
451 if ( !pavement_ident.empty() ) {
452 pvt = new FGPavement( pavement_ident, pos );
453 pavements.push_back( pvt );
456 pvt = pavements.back();
458 if ( num == 112 || num == 114 ) {
459 double lat_b = atof( token[3].c_str() );
460 double lon_b = atof( token[4].c_str() );
461 SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
462 pvt->addBezierNode(pos, pos_b, num == 114);
464 pvt->addNode(pos, num == 113);
468 void parseCommLine(int lineId, const vector<string>& token)
470 if ( rwy_count <= 0 ) {
471 SG_LOG( SG_GENERAL, SG_ALERT, "No runways; skipping comm for " + last_apt_id);
474 SGGeod pos = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count,
475 rwy_lat_accum / (double)rwy_count, last_apt_elev);
477 // short int representing tens of kHz:
478 int freqKhz = atoi(token[1].c_str());
480 FGPositioned::Type ty;
483 ty = FGPositioned::FREQ_AWOS;
484 if (token[2] == "ATIS") {
485 ty = FGPositioned::FREQ_ATIS;
489 case 51: ty = FGPositioned::FREQ_UNICOM; break;
490 case 52: ty = FGPositioned::FREQ_CLEARANCE; break;
491 case 53: ty = FGPositioned::FREQ_GROUND; break;
492 case 54: ty = FGPositioned::FREQ_TOWER; break;
494 case 56: ty = FGPositioned::FREQ_APP_DEP; break;
496 throw sg_range_exception("unupported apt.dat comm station type");
499 commStations.push_back(new flightgear::CommStation(token[2], ty, pos, rangeNm, freqKhz));
505 // Load the airport data base from the specified aptdb file. The
506 // metar file is used to mark the airports as having metar available
508 bool fgAirportDBLoad( const string &aptdb_file, const std::string &metar_file )
512 ld.parseAPT(aptdb_file);
514 // Load the metar.dat file and update apt db with stations that
518 sg_gzifstream metar_in( metar_file );
519 if ( !metar_in.is_open() ) {
520 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
526 if ( ident == "#" || ident == "//" ) {
529 FGAirport* apt = FGAirport::findByIdent(ident);
536 SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");