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/misc/sg_path.hxx>
46 #include "airport.hxx"
47 #include "runways.hxx"
48 #include "pavement.hxx"
49 #include <Navaids/NavDataCache.hxx>
50 #include <ATC/CommStation.hxx>
56 typedef SGSharedPtr<FGPavement> FGPavementPtr;
58 static FGPositioned::Type fptypeFromRobinType(int aType)
61 case 1: return FGPositioned::AIRPORT;
62 case 16: return FGPositioned::SEAPORT;
63 case 17: return FGPositioned::HELIPORT;
65 SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
66 throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
70 // generated by 'wc -l' on the uncompressed file
71 const unsigned int LINES_IN_APT_DAT = 2465648;
86 cache = NavDataCache::instance();
89 void parseAPT(const SGPath &aptdb_file)
91 sg_gzifstream in( aptdb_file.str() );
93 if ( !in.is_open() ) {
94 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
95 throw sg_io_exception("cannot open APT file", aptdb_file);
102 unsigned int line_id = 0;
103 unsigned int line_num = 0;
105 while ( ! in.eof() ) {
106 in.getline(tmp, 2048);
107 line = tmp; // string copy, ack
110 if ( line.empty() || isspace(tmp[0]) || tmp[0] == '#' ) {
114 if ((line_num % 100) == 0) {
116 unsigned int percent = (line_num * 100) / LINES_IN_APT_DAT;
117 cache->setRebuildPhaseProgress(NavDataCache::REBUILD_AIRPORTS, percent);
120 if (line.size() >= 3) {
121 char *p = (char *)memchr(tmp, ' ', 3);
127 if ( tmp[0] == 'I' || tmp[0] == 'A' ) {
128 // First line, indicates IBM ("I") or Macintosh ("A")
131 // move past this line and read and discard the next line
132 // which is the version and copyright information
133 in.getline(tmp, 2048);
135 if ( strlen(tmp) > 5 ) {
136 char *p = (char *)memchr(tmp, ' ', 5);
140 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
141 } else if ( line_id == 1 /* Airport */ ||
142 line_id == 16 /* Seaplane base */ ||
143 line_id == 17 /* Heliport */ ) {
144 parseAirportLine(simgear::strutils::split(line));
145 } else if ( line_id == 10 ) { // Runway v810
146 parseRunwayLine810(simgear::strutils::split(line));
147 } else if ( line_id == 100 ) { // Runway v850
148 parseRunwayLine850(simgear::strutils::split(line));
149 } else if ( line_id == 101 ) { // Water Runway v850
150 parseWaterRunwayLine850(simgear::strutils::split(line));
151 } else if ( line_id == 102 ) { // Helipad v850
152 parseHelipadLine850(simgear::strutils::split(line));
153 } else if ( line_id == 18 ) {
154 // beacon entry (ignore)
155 } else if ( line_id == 14 ) {
156 // control tower entry
157 vector<string> token(simgear::strutils::split(line));
159 double lat = atof( token[1].c_str() );
160 double lon = atof( token[2].c_str() );
161 double elev = atof( token[3].c_str() );
162 tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
165 cache->insertTower(currentAirportID, tower);
166 } else if ( line_id == 19 ) {
167 // windsock entry (ignore)
168 } else if ( line_id == 20 ) {
169 // Taxiway sign (ignore)
170 } else if ( line_id == 21 ) {
171 // lighting objects (ignore)
172 } else if ( line_id == 15 ) {
173 // custom startup locations (ignore)
174 } else if ( line_id == 0 ) {
176 } else if ( line_id >= 50 && line_id <= 56) {
177 parseCommLine(line_id, simgear::strutils::split(line));
178 } else if ( line_id == 110 ) {
180 parsePavementLine850(simgear::strutils::split(line, 0, 4));
181 } else if ( line_id >= 111 && line_id <= 114 ) {
183 parsePavementNodeLine850(line_id, simgear::strutils::split(line));
184 } else if ( line_id >= 115 && line_id <= 116 ) {
185 // other pavement nodes (ignore)
186 } else if ( line_id == 120 ) {
188 } else if ( line_id == 130 ) {
190 } else if ( line_id >= 1000 ) {
191 // airport traffic flow (ignore)
192 } else if ( line_id == 99 ) {
193 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
195 SG_LOG( SG_GENERAL, SG_ALERT,
196 "Unknown line(#" << line_num << ") in apt.dat file: " << line );
197 throw sg_format_exception("malformed line in apt.dat:", line);
205 vector<string> token;
206 double rwy_lat_accum;
207 double rwy_lon_accum;
208 double last_rwy_heading;
212 double last_apt_elev;
214 string last_apt_info;
215 string pavement_ident;
218 //vector<FGRunwayPtr> runways;
219 //vector<FGTaxiwayPtr> taxiways;
220 vector<FGPavementPtr> pavements;
223 PositionedID currentAirportID;
227 if (currentAirportID == 0) {
232 currentAirportID = 0;
233 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
238 double lat = rwy_lat_accum / (double)rwy_count;
239 double lon = rwy_lon_accum / (double)rwy_count;
242 // tower height hard coded for now...
243 const float tower_height = 50.0f;
244 // make a little off the heading for 1 runway airports...
245 float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
246 float fudge_lat = .003f - fudge_lon;
247 tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
249 cache->insertTower(currentAirportID, tower);
252 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
253 cache->updatePosition(currentAirportID, pos);
255 currentAirportID = 0;
258 void parseAirportLine(const vector<string>& token)
260 const string& id(token[4]);
261 double elev = atof( token[1].c_str() );
263 // finish the previous airport
266 last_apt_elev = elev;
271 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
272 name += token[i] + " ";
274 name += token[token.size() - 1];
276 // clear runway list for start of next airport
281 int robinType = atoi(token[0].c_str());
282 currentAirportID = cache->insertAirport(fptypeFromRobinType(robinType), id, name);
285 void parseRunwayLine810(const vector<string>& token)
287 double lat = atof( token[1].c_str() );
288 double lon = atof( token[2].c_str() );
289 rwy_lat_accum += lat;
290 rwy_lon_accum += lon;
293 const string& rwy_no(token[3]);
295 double heading = atof( token[4].c_str() );
296 double length = atoi( token[5].c_str() );
297 double width = atoi( token[8].c_str() );
298 length *= SG_FEET_TO_METER;
299 width *= SG_FEET_TO_METER;
301 // adjust lat / lon to the start of the runway/taxiway, not the middle
302 SGGeod pos_1 = SGGeodesy::direct( SGGeod::fromDegFt(lon, lat, last_apt_elev), heading, -length/2 );
304 last_rwy_heading = heading;
306 int surface_code = atoi( token[10].c_str() );
308 if (rwy_no[0] == 'x') { // Taxiway
309 cache->insertRunway(FGPositioned::TAXIWAY, rwy_no, pos_1, currentAirportID,
310 heading, length, width, 0.0, 0.0, surface_code);
311 } else if (rwy_no[0] == 'H') { // Helipad
312 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
313 cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos, currentAirportID,
314 heading, length, width, 0.0, 0.0, surface_code);
317 string rwy_displ_threshold = token[6];
319 = simgear::strutils::split( rwy_displ_threshold, "." );
320 double displ_thresh1 = atof( displ[0].c_str() );
321 double displ_thresh2 = atof( displ[1].c_str() );
322 displ_thresh1 *= SG_FEET_TO_METER;
323 displ_thresh2 *= SG_FEET_TO_METER;
325 string rwy_stopway = token[7];
327 = simgear::strutils::split( rwy_stopway, "." );
328 double stopway1 = atof( stop[0].c_str() );
329 double stopway2 = atof( stop[1].c_str() );
330 stopway1 *= SG_FEET_TO_METER;
331 stopway2 *= SG_FEET_TO_METER;
333 SGGeod pos_2 = SGGeodesy::direct( pos_1, heading, length );
335 PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos_1,
336 currentAirportID, heading, length,
337 width, displ_thresh1, stopway1,
340 PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
341 FGRunway::reverseIdent(rwy_no), pos_2,
343 SGMiscd::normalizePeriodic(0, 360, heading + 180.0),
344 length, width, displ_thresh2, stopway2,
347 cache->setRunwayReciprocal(rwy, reciprocal);
351 void parseRunwayLine850(const vector<string>& token)
353 double width = atof( token[1].c_str() );
354 int surface_code = atoi( token[2].c_str() );
356 double lat_1 = atof( token[9].c_str() );
357 double lon_1 = atof( token[10].c_str() );
358 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
359 rwy_lat_accum += lat_1;
360 rwy_lon_accum += lon_1;
363 double lat_2 = atof( token[18].c_str() );
364 double lon_2 = atof( token[19].c_str() );
365 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
366 rwy_lat_accum += lat_2;
367 rwy_lon_accum += lon_2;
370 double length, heading_1, heading_2;
371 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
373 last_rwy_heading = heading_1;
375 const string& rwy_no_1(token[8]);
376 const string& rwy_no_2(token[17]);
377 if ( rwy_no_1.empty() || rwy_no_2.empty() )
380 double displ_thresh1 = atof( token[11].c_str() );
381 double displ_thresh2 = atof( token[20].c_str() );
383 double stopway1 = atof( token[12].c_str() );
384 double stopway2 = atof( token[21].c_str() );
386 PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
387 currentAirportID, heading_1, length,
388 width, displ_thresh1, stopway1,
391 PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
393 currentAirportID, heading_2, length,
394 width, displ_thresh2, stopway2,
397 cache->setRunwayReciprocal(rwy, reciprocal);
400 void parseWaterRunwayLine850(const vector<string>& token)
402 double width = atof( token[1].c_str() );
404 double lat_1 = atof( token[4].c_str() );
405 double lon_1 = atof( token[5].c_str() );
406 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
407 rwy_lat_accum += lat_1;
408 rwy_lon_accum += lon_1;
411 double lat_2 = atof( token[7].c_str() );
412 double lon_2 = atof( token[8].c_str() );
413 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
414 rwy_lat_accum += lat_2;
415 rwy_lon_accum += lon_2;
418 double length, heading_1, heading_2;
419 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
421 last_rwy_heading = heading_1;
423 const string& rwy_no_1(token[3]);
424 const string& rwy_no_2(token[6]);
426 PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
427 currentAirportID, heading_1, length,
428 width, 0.0, 0.0, 13);
430 PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
432 currentAirportID, heading_2, length,
433 width, 0.0, 0.0, 13);
435 cache->setRunwayReciprocal(rwy, reciprocal);
438 void parseHelipadLine850(const vector<string>& token)
440 double length = atof( token[5].c_str() );
441 double width = atof( token[6].c_str() );
443 double lat = atof( token[2].c_str() );
444 double lon = atof( token[3].c_str() );
445 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
446 rwy_lat_accum += lat;
447 rwy_lon_accum += lon;
450 double heading = atof( token[4].c_str() );
452 last_rwy_heading = heading;
454 const string& rwy_no(token[1]);
455 int surface_code = atoi( token[7].c_str() );
457 cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos,
458 currentAirportID, heading, length,
459 width, 0.0, 0.0, surface_code);
462 void parsePavementLine850(const vector<string>& token)
464 if ( token.size() >= 5 ) {
465 pavement_ident = token[4];
466 if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
467 pavement_ident.erase( pavement_ident.size()-1 );
469 pavement_ident = "xx";
473 void parsePavementNodeLine850(int num, const vector<string>& token)
475 double lat = atof( token[1].c_str() );
476 double lon = atof( token[2].c_str() );
477 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
480 if ( !pavement_ident.empty() ) {
481 pvt = new FGPavement( 0, pavement_ident, pos );
482 pavements.push_back( pvt );
485 pvt = pavements.back();
487 if ( num == 112 || num == 114 ) {
488 double lat_b = atof( token[3].c_str() );
489 double lon_b = atof( token[4].c_str() );
490 SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
491 pvt->addBezierNode(pos, pos_b, num == 114);
493 pvt->addNode(pos, num == 113);
497 void parseCommLine(int lineId, const vector<string>& token)
499 if ( rwy_count <= 0 ) {
500 SG_LOG( SG_GENERAL, SG_ALERT, "No runways; skipping comm for " + last_apt_id);
503 SGGeod pos = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count,
504 rwy_lat_accum / (double)rwy_count, last_apt_elev);
506 // short int representing tens of kHz:
507 int freqKhz = atoi(token[1].c_str()) * 10;
509 FGPositioned::Type ty;
510 // Make sure we only pass on stations with at least a name
511 if (token.size() >2){
515 ty = FGPositioned::FREQ_AWOS;
516 for( size_t i = 2; i < token.size(); ++i )
518 if( token[i] == "ATIS" )
520 ty = FGPositioned::FREQ_ATIS;
526 case 51: ty = FGPositioned::FREQ_UNICOM; break;
527 case 52: ty = FGPositioned::FREQ_CLEARANCE; break;
528 case 53: ty = FGPositioned::FREQ_GROUND; break;
529 case 54: ty = FGPositioned::FREQ_TOWER; break;
531 case 56: ty = FGPositioned::FREQ_APP_DEP; break;
533 throw sg_range_exception("unsupported apt.dat comm station type");
536 // Name can contain white spaces. All tokens after the second token are
538 std::string name = token[2];
539 for( size_t i = 3; i < token.size(); ++i )
540 name += ' ' + token[i];
542 cache->insertCommStation(ty, name, pos, freqKhz, rangeNm, currentAirportID);
544 else SG_LOG( SG_GENERAL, SG_DEBUG, "Found unnamed comm. Skipping: " << lineId);
549 // Load the airport data base from the specified aptdb file. The
550 // metar file is used to mark the airports as having metar available
552 bool airportDBLoad( const SGPath &aptdb_file )
555 ld.parseAPT(aptdb_file);
559 bool metarDataLoad(const SGPath& metar_file)
561 sg_gzifstream metar_in( metar_file.str() );
562 if ( !metar_in.is_open() ) {
563 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
567 NavDataCache* cache = NavDataCache::instance();
572 if ( ident == "#" || ident == "//" ) {
575 cache->setAirportMetar(ident, true);
582 } // of namespace flightgear