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>
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");
84 cache = NavDataCache::instance();
87 void parseAPT(const SGPath &aptdb_file)
89 sg_gzifstream in( aptdb_file.str() );
91 if ( !in.is_open() ) {
92 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
100 unsigned int line_id = 0;
101 unsigned int line_num = 0;
103 while ( ! in.eof() ) {
104 in.getline(tmp, 2048);
105 line = tmp; // string copy, ack
108 if ( !line.size() || isspace(tmp[0])) {
112 if (line.size() >= 3) {
113 char *p = (char *)memchr(tmp, ' ', 3);
119 if ( tmp[0] == 'I' ) {
120 // First line, indicates IBM (i.e. DOS line endings I
123 // move past this line and read and discard the next line
124 // which is the version and copyright information
125 in.getline(tmp, 2048);
126 // vector<string> vers_token = simgear::strutils::split( tmp );
127 if ( strlen(tmp) > 4 ) {
128 char *p = (char *)memchr(tmp, ' ', 4);
132 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
133 } else if ( line_id == 1 /* Airport */ ||
134 line_id == 16 /* Seaplane base */ ||
135 line_id == 17 /* Heliport */ ) {
136 parseAirportLine(simgear::strutils::split(line));
137 } else if ( line_id == 10 ) { // Runway v810
138 parseRunwayLine810(simgear::strutils::split(line));
139 } else if ( line_id == 100 ) { // Runway v850
140 parseRunwayLine850(simgear::strutils::split(line));
141 } else if ( line_id == 101 ) { // Water Runway v850
142 parseWaterRunwayLine850(simgear::strutils::split(line));
143 } else if ( line_id == 102 ) { // Helipad v850
144 parseHelipadLine850(simgear::strutils::split(line));
145 } else if ( line_id == 18 ) {
146 // beacon entry (ignore)
147 } else if ( line_id == 14 ) {
148 // control tower entry
149 vector<string> token(simgear::strutils::split(line));
151 double lat = atof( token[1].c_str() );
152 double lon = atof( token[2].c_str() );
153 double elev = atof( token[3].c_str() );
154 tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
157 cache->insertTower(currentAirportID, tower);
158 } else if ( line_id == 19 ) {
159 // windsock entry (ignore)
160 } else if ( line_id == 20 ) {
161 // Taxiway sign (ignore)
162 } else if ( line_id == 21 ) {
163 // lighting objects (ignore)
164 } else if ( line_id == 15 ) {
165 // custom startup locations (ignore)
166 } else if ( line_id == 0 ) {
168 } else if ( line_id >= 50 && line_id <= 56) {
169 parseCommLine(line_id, simgear::strutils::split(line));
170 } else if ( line_id == 110 ) {
172 parsePavementLine850(simgear::strutils::split(line, 0, 4));
173 } else if ( line_id >= 111 && line_id <= 114 ) {
175 parsePavementNodeLine850(line_id, simgear::strutils::split(line));
176 } else if ( line_id >= 115 && line_id <= 116 ) {
177 // other pavement nodes (ignore)
178 } else if ( line_id == 120 ) {
180 } else if ( line_id == 130 ) {
182 } else if ( line_id >= 1000 ) {
183 // airport traffic flow (ignore)
184 } else if ( line_id == 99 ) {
185 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
187 SG_LOG( SG_GENERAL, SG_ALERT,
188 "Unknown line(#" << line_num << ") in apt.dat file: " << line );
197 vector<string> token;
198 double rwy_lat_accum;
199 double rwy_lon_accum;
200 double last_rwy_heading;
204 double last_apt_elev;
206 string last_apt_info;
207 string pavement_ident;
210 //vector<FGRunwayPtr> runways;
211 //vector<FGTaxiwayPtr> taxiways;
212 vector<FGPavementPtr> pavements;
215 PositionedID currentAirportID;
219 if (currentAirportID == 0) {
224 currentAirportID = 0;
225 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
230 double lat = rwy_lat_accum / (double)rwy_count;
231 double lon = rwy_lon_accum / (double)rwy_count;
234 // tower height hard coded for now...
235 const float tower_height = 50.0f;
236 // make a little off the heading for 1 runway airports...
237 float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
238 float fudge_lat = .003f - fudge_lon;
239 tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
241 cache->insertTower(currentAirportID, tower);
244 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
245 cache->updatePosition(currentAirportID, pos);
247 currentAirportID = 0;
250 void parseAirportLine(const vector<string>& token)
252 const string& id(token[4]);
253 double elev = atof( token[1].c_str() );
255 // finish the previous airport
258 last_apt_elev = elev;
263 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
264 name += token[i] + " ";
266 name += token[token.size() - 1];
268 // clear runway list for start of next airport
273 int robinType = atoi(token[0].c_str());
274 currentAirportID = cache->insertAirport(fptypeFromRobinType(robinType), id, name);
277 void parseRunwayLine810(const vector<string>& token)
279 double lat = atof( token[1].c_str() );
280 double lon = atof( token[2].c_str() );
281 rwy_lat_accum += lat;
282 rwy_lon_accum += lon;
285 const string& rwy_no(token[3]);
287 double heading = atof( token[4].c_str() );
288 double length = atoi( token[5].c_str() );
289 double width = atoi( token[8].c_str() );
291 last_rwy_heading = heading;
293 int surface_code = atoi( token[10].c_str() );
294 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
296 if (rwy_no[0] == 'x') {
297 cache->insertRunway(FGPositioned::TAXIWAY,rwy_no, pos, currentAirportID,
298 heading, length, width, 0, 0, surface_code);
301 string rwy_displ_threshold = token[6];
303 = simgear::strutils::split( rwy_displ_threshold, "." );
304 double displ_thresh1 = atof( displ[0].c_str() );
305 double displ_thresh2 = atof( displ[1].c_str() );
307 string rwy_stopway = token[7];
309 = simgear::strutils::split( rwy_stopway, "." );
310 double stopway1 = atof( stop[0].c_str() );
311 double stopway2 = atof( stop[1].c_str() );
313 PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos,
314 currentAirportID, heading, length,
315 width, displ_thresh1, stopway1,
318 PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
319 FGRunway::reverseIdent(rwy_no), pos,
320 currentAirportID, heading + 180.0, length,
321 width, displ_thresh2, stopway2,
324 cache->setRunwayReciprocal(rwy, reciprocal);
328 void parseRunwayLine850(const vector<string>& token)
330 double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
331 int surface_code = atoi( token[2].c_str() );
333 double lat_1 = atof( token[9].c_str() );
334 double lon_1 = atof( token[10].c_str() );
335 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
336 rwy_lat_accum += lat_1;
337 rwy_lon_accum += lon_1;
340 double lat_2 = atof( token[18].c_str() );
341 double lon_2 = atof( token[19].c_str() );
342 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
343 rwy_lat_accum += lat_2;
344 rwy_lon_accum += lon_2;
347 double length, heading_1, heading_2, dummy;
348 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
350 SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
351 length *= SG_METER_TO_FEET;
353 last_rwy_heading = heading_1;
355 const string& rwy_no_1(token[8]);
356 const string& rwy_no_2(token[17]);
357 if ( rwy_no_1.size() == 0 || rwy_no_2.size() == 0 )
360 double displ_thresh1 = atof( token[11].c_str() );
361 double displ_thresh2 = atof( token[20].c_str() );
363 double stopway1 = atof( token[12].c_str() );
364 double stopway2 = atof( token[21].c_str() );
366 PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos,
367 currentAirportID, heading_1, length,
368 width, displ_thresh1, stopway1,
371 PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
373 currentAirportID, heading_2, length,
374 width, displ_thresh2, stopway2,
377 cache->setRunwayReciprocal(rwy, reciprocal);
380 void parseWaterRunwayLine850(const vector<string>& token)
382 double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
384 double lat_1 = atof( token[4].c_str() );
385 double lon_1 = atof( token[5].c_str() );
386 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
387 rwy_lat_accum += lat_1;
388 rwy_lon_accum += lon_1;
391 double lat_2 = atof( token[7].c_str() );
392 double lon_2 = atof( token[8].c_str() );
393 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
394 rwy_lat_accum += lat_2;
395 rwy_lon_accum += lon_2;
398 double length, heading_1, heading_2, dummy;
399 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
401 SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
403 last_rwy_heading = heading_1;
405 const string& rwy_no_1(token[3]);
406 const string& rwy_no_2(token[6]);
408 PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos,
409 currentAirportID, heading_1, length,
410 width, 0.0, 0.0, 13);
412 PositionedID reciprocal = cache->insertRunway(FGPositioned::RUNWAY,
414 currentAirportID, heading_2, length,
415 width, 0.0, 0.0, 13);
417 cache->setRunwayReciprocal(rwy, reciprocal);
420 void parseHelipadLine850(const vector<string>& token)
422 double length = atof( token[5].c_str() ) * SG_METER_TO_FEET;
423 double width = atof( token[6].c_str() ) * SG_METER_TO_FEET;
425 double lat = atof( token[2].c_str() );
426 double lon = atof( token[3].c_str() );
427 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
428 rwy_lat_accum += lat;
429 rwy_lon_accum += lon;
432 double heading = atof( token[4].c_str() );
434 last_rwy_heading = heading;
436 const string& rwy_no(token[1]);
437 int surface_code = atoi( token[7].c_str() );
439 cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos,
440 currentAirportID, heading, length,
441 width, 0.0, 0.0, surface_code);
444 void parsePavementLine850(const vector<string>& token)
446 if ( token.size() >= 5 ) {
447 pavement_ident = token[4];
448 if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
449 pavement_ident.erase( pavement_ident.size()-1 );
451 pavement_ident = "xx";
455 void parsePavementNodeLine850(int num, const vector<string>& token)
457 double lat = atof( token[1].c_str() );
458 double lon = atof( token[2].c_str() );
459 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
462 if ( !pavement_ident.empty() ) {
463 pvt = new FGPavement( 0, pavement_ident, pos );
464 pavements.push_back( pvt );
467 pvt = pavements.back();
469 if ( num == 112 || num == 114 ) {
470 double lat_b = atof( token[3].c_str() );
471 double lon_b = atof( token[4].c_str() );
472 SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
473 pvt->addBezierNode(pos, pos_b, num == 114);
475 pvt->addNode(pos, num == 113);
479 void parseCommLine(int lineId, const vector<string>& token)
481 if ( rwy_count <= 0 ) {
482 SG_LOG( SG_GENERAL, SG_ALERT, "No runways; skipping comm for " + last_apt_id);
485 SGGeod pos = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count,
486 rwy_lat_accum / (double)rwy_count, last_apt_elev);
488 // short int representing tens of kHz:
489 int freqKhz = atoi(token[1].c_str()) * 10;
491 FGPositioned::Type ty;
492 // Make sure we only pass on stations with at least a name
493 if (token.size() >2){
497 ty = FGPositioned::FREQ_AWOS;
498 for( size_t i = 2; i < token.size(); ++i )
500 if( token[i] == "ATIS" )
502 ty = FGPositioned::FREQ_ATIS;
508 case 51: ty = FGPositioned::FREQ_UNICOM; break;
509 case 52: ty = FGPositioned::FREQ_CLEARANCE; break;
510 case 53: ty = FGPositioned::FREQ_GROUND; break;
511 case 54: ty = FGPositioned::FREQ_TOWER; break;
513 case 56: ty = FGPositioned::FREQ_APP_DEP; break;
515 throw sg_range_exception("unsupported apt.dat comm station type");
518 // Name can contain white spaces. All tokens after the second token are
520 std::string name = token[2];
521 for( size_t i = 3; i < token.size(); ++i )
522 name += ' ' + token[i];
524 cache->insertCommStation(ty, name, pos, freqKhz, rangeNm, currentAirportID);
526 else SG_LOG( SG_GENERAL, SG_DEBUG, "Found unnamed comm. Skipping: " << lineId);
531 // Load the airport data base from the specified aptdb file. The
532 // metar file is used to mark the airports as having metar available
534 bool airportDBLoad( const SGPath &aptdb_file )
537 ld.parseAPT(aptdb_file);
541 bool metarDataLoad(const SGPath& metar_file)
543 sg_gzifstream metar_in( metar_file.str() );
544 if ( !metar_in.is_open() ) {
545 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
549 NavDataCache* cache = NavDataCache::instance();
553 if ( ident == "#" || ident == "//" ) {
556 cache->setAirportMetar(ident, true);
563 } // of namespace flightgear