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);
163 cache->insertTower(currentAirportID, tower);
164 } else if ( line_id == 19 ) {
165 // windsock entry (ignore)
166 } else if ( line_id == 20 ) {
167 // Taxiway sign (ignore)
168 } else if ( line_id == 21 ) {
169 // lighting objects (ignore)
170 } else if ( line_id == 15 ) {
171 // custom startup locations (ignore)
172 } else if ( line_id == 0 ) {
174 } else if ( line_id >= 50 && line_id <= 56) {
175 parseCommLine(line_id, simgear::strutils::split(line));
176 } else if ( line_id == 110 ) {
178 parsePavementLine850(simgear::strutils::split(line, 0, 4));
179 } else if ( line_id >= 111 && line_id <= 114 ) {
181 parsePavementNodeLine850(line_id, simgear::strutils::split(line));
182 } else if ( line_id >= 115 && line_id <= 116 ) {
183 // other pavement nodes (ignore)
184 } else if ( line_id == 120 ) {
186 } else if ( line_id == 130 ) {
188 } else if ( line_id >= 1000 ) {
189 // airport traffic flow (ignore)
190 } else if ( line_id == 99 ) {
191 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
193 SG_LOG( SG_GENERAL, SG_ALERT,
194 "Unknown line(#" << line_num << ") in apt.dat file: " << line );
195 throw sg_format_exception("malformed line in apt.dat:", line);
203 vector<string> token;
204 double rwy_lat_accum;
205 double rwy_lon_accum;
206 double last_rwy_heading;
209 double last_apt_elev;
211 string last_apt_info;
212 string pavement_ident;
215 //vector<FGRunwayPtr> runways;
216 //vector<FGTaxiwayPtr> taxiways;
217 vector<FGPavementPtr> pavements;
220 PositionedID currentAirportID;
224 if (currentAirportID == 0) {
229 currentAirportID = 0;
230 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
235 double lat = rwy_lat_accum / (double)rwy_count;
236 double lon = rwy_lon_accum / (double)rwy_count;
238 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
239 cache->updatePosition(currentAirportID, pos);
241 currentAirportID = 0;
244 void parseAirportLine(const vector<string>& token)
246 const string& id(token[4]);
247 double elev = atof( token[1].c_str() );
249 // finish the previous airport
252 last_apt_elev = elev;
256 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
257 name += token[i] + " ";
259 name += token[token.size() - 1];
261 // clear runway list for start of next airport
266 int robinType = atoi(token[0].c_str());
267 currentAirportID = cache->insertAirport(fptypeFromRobinType(robinType), id, name);
270 void parseRunwayLine810(const vector<string>& token)
272 double lat = atof( token[1].c_str() );
273 double lon = atof( token[2].c_str() );
274 rwy_lat_accum += lat;
275 rwy_lon_accum += lon;
278 const string& rwy_no(token[3]);
280 double heading = atof( token[4].c_str() );
281 double length = atoi( token[5].c_str() );
282 double width = atoi( token[8].c_str() );
283 length *= SG_FEET_TO_METER;
284 width *= SG_FEET_TO_METER;
286 // adjust lat / lon to the start of the runway/taxiway, not the middle
287 SGGeod pos_1 = SGGeodesy::direct( SGGeod::fromDegFt(lon, lat, last_apt_elev), heading, -length/2 );
289 last_rwy_heading = heading;
291 int surface_code = atoi( token[10].c_str() );
293 if (rwy_no[0] == 'x') { // Taxiway
294 cache->insertRunway(FGPositioned::TAXIWAY, rwy_no, pos_1, currentAirportID,
295 heading, length, width, 0.0, 0.0, surface_code);
296 } else if (rwy_no[0] == 'H') { // Helipad
297 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
298 cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos, currentAirportID,
299 heading, length, width, 0.0, 0.0, surface_code);
302 string rwy_displ_threshold = token[6];
304 = simgear::strutils::split( rwy_displ_threshold, "." );
305 double displ_thresh1 = atof( displ[0].c_str() );
306 double displ_thresh2 = atof( displ[1].c_str() );
307 displ_thresh1 *= SG_FEET_TO_METER;
308 displ_thresh2 *= SG_FEET_TO_METER;
310 string rwy_stopway = token[7];
312 = simgear::strutils::split( rwy_stopway, "." );
313 double stopway1 = atof( stop[0].c_str() );
314 double stopway2 = atof( stop[1].c_str() );
315 stopway1 *= SG_FEET_TO_METER;
316 stopway2 *= SG_FEET_TO_METER;
318 SGGeod pos_2 = SGGeodesy::direct( pos_1, heading, length );
320 PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos_1,
321 currentAirportID, heading, length,
322 width, displ_thresh1, stopway1,
325 PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
326 FGRunway::reverseIdent(rwy_no), pos_2,
328 SGMiscd::normalizePeriodic(0, 360, heading + 180.0),
329 length, width, displ_thresh2, stopway2,
332 cache->setRunwayReciprocal(rwy, reciprocal);
336 void parseRunwayLine850(const vector<string>& token)
338 double width = atof( token[1].c_str() );
339 int surface_code = atoi( token[2].c_str() );
341 double lat_1 = atof( token[9].c_str() );
342 double lon_1 = atof( token[10].c_str() );
343 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
344 rwy_lat_accum += lat_1;
345 rwy_lon_accum += lon_1;
348 double lat_2 = atof( token[18].c_str() );
349 double lon_2 = atof( token[19].c_str() );
350 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
351 rwy_lat_accum += lat_2;
352 rwy_lon_accum += lon_2;
355 double length, heading_1, heading_2;
356 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
358 last_rwy_heading = heading_1;
360 const string& rwy_no_1(token[8]);
361 const string& rwy_no_2(token[17]);
362 if ( rwy_no_1.empty() || rwy_no_2.empty() )
365 double displ_thresh1 = atof( token[11].c_str() );
366 double displ_thresh2 = atof( token[20].c_str() );
368 double stopway1 = atof( token[12].c_str() );
369 double stopway2 = atof( token[21].c_str() );
371 PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
372 currentAirportID, heading_1, length,
373 width, displ_thresh1, stopway1,
376 PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
378 currentAirportID, heading_2, length,
379 width, displ_thresh2, stopway2,
382 cache->setRunwayReciprocal(rwy, reciprocal);
385 void parseWaterRunwayLine850(const vector<string>& token)
387 double width = atof( token[1].c_str() );
389 double lat_1 = atof( token[4].c_str() );
390 double lon_1 = atof( token[5].c_str() );
391 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
392 rwy_lat_accum += lat_1;
393 rwy_lon_accum += lon_1;
396 double lat_2 = atof( token[7].c_str() );
397 double lon_2 = atof( token[8].c_str() );
398 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
399 rwy_lat_accum += lat_2;
400 rwy_lon_accum += lon_2;
403 double length, heading_1, heading_2;
404 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
406 last_rwy_heading = heading_1;
408 const string& rwy_no_1(token[3]);
409 const string& rwy_no_2(token[6]);
411 PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
412 currentAirportID, heading_1, length,
413 width, 0.0, 0.0, 13);
415 PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
417 currentAirportID, heading_2, length,
418 width, 0.0, 0.0, 13);
420 cache->setRunwayReciprocal(rwy, reciprocal);
423 void parseHelipadLine850(const vector<string>& token)
425 double length = atof( token[5].c_str() );
426 double width = atof( token[6].c_str() );
428 double lat = atof( token[2].c_str() );
429 double lon = atof( token[3].c_str() );
430 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
431 rwy_lat_accum += lat;
432 rwy_lon_accum += lon;
435 double heading = atof( token[4].c_str() );
437 last_rwy_heading = heading;
439 const string& rwy_no(token[1]);
440 int surface_code = atoi( token[7].c_str() );
442 cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos,
443 currentAirportID, heading, length,
444 width, 0.0, 0.0, surface_code);
447 void parsePavementLine850(const vector<string>& token)
449 if ( token.size() >= 5 ) {
450 pavement_ident = token[4];
451 if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
452 pavement_ident.erase( pavement_ident.size()-1 );
454 pavement_ident = "xx";
458 void parsePavementNodeLine850(int num, const vector<string>& token)
460 double lat = atof( token[1].c_str() );
461 double lon = atof( token[2].c_str() );
462 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
465 if ( !pavement_ident.empty() ) {
466 pvt = new FGPavement( 0, pavement_ident, pos );
467 pavements.push_back( pvt );
470 pvt = pavements.back();
472 if ( num == 112 || num == 114 ) {
473 double lat_b = atof( token[3].c_str() );
474 double lon_b = atof( token[4].c_str() );
475 SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
476 pvt->addBezierNode(pos, pos_b, num == 114);
478 pvt->addNode(pos, num == 113);
482 void parseCommLine(int lineId, const vector<string>& token)
484 if ( rwy_count <= 0 ) {
485 SG_LOG( SG_GENERAL, SG_ALERT, "No runways; skipping comm for " + last_apt_id);
488 SGGeod pos = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count,
489 rwy_lat_accum / (double)rwy_count, last_apt_elev);
491 // short int representing tens of kHz:
492 int freqKhz = atoi(token[1].c_str()) * 10;
494 FGPositioned::Type ty;
495 // Make sure we only pass on stations with at least a name
496 if (token.size() >2){
500 ty = FGPositioned::FREQ_AWOS;
501 for( size_t i = 2; i < token.size(); ++i )
503 if( token[i] == "ATIS" )
505 ty = FGPositioned::FREQ_ATIS;
511 case 51: ty = FGPositioned::FREQ_UNICOM; break;
512 case 52: ty = FGPositioned::FREQ_CLEARANCE; break;
513 case 53: ty = FGPositioned::FREQ_GROUND; break;
514 case 54: ty = FGPositioned::FREQ_TOWER; break;
516 case 56: ty = FGPositioned::FREQ_APP_DEP; break;
518 throw sg_range_exception("unsupported apt.dat comm station type");
521 // Name can contain white spaces. All tokens after the second token are
523 std::string name = token[2];
524 for( size_t i = 3; i < token.size(); ++i )
525 name += ' ' + token[i];
527 cache->insertCommStation(ty, name, pos, freqKhz, rangeNm, currentAirportID);
529 else SG_LOG( SG_GENERAL, SG_DEBUG, "Found unnamed comm. Skipping: " << lineId);
534 // Load the airport data base from the specified aptdb file. The
535 // metar file is used to mark the airports as having metar available
537 bool airportDBLoad( const SGPath &aptdb_file )
540 ld.parseAPT(aptdb_file);
544 bool metarDataLoad(const SGPath& metar_file)
546 sg_gzifstream metar_in( metar_file.str() );
547 if ( !metar_in.is_open() ) {
548 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
552 NavDataCache* cache = NavDataCache::instance();
557 if ( ident == "#" || ident == "//" ) {
560 cache->setAirportMetar(ident, true);
567 } // of namespace flightgear