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>
46 #include "runways.hxx"
47 #include "pavement.hxx"
48 #include <ATCDCL/commlist.hxx>
54 static FGPositioned::Type fptypeFromRobinType(int aType)
57 case 1: return FGPositioned::AIRPORT;
58 case 16: return FGPositioned::SEAPORT;
59 case 17: return FGPositioned::HELIPORT;
61 SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
62 throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
78 void parseAPT(const string &aptdb_file, FGCommList *comm_list)
80 sg_gzifstream in( aptdb_file );
82 if ( !in.is_open() ) {
83 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
91 unsigned int line_id = 0;
92 unsigned int line_num = 0;
94 while ( ! in.eof() ) {
95 in.getline(tmp, 2048);
96 line = tmp; // string copy, ack
99 if ( !line.size() || isspace(tmp[0])) {
103 if (line.size() >= 3) {
104 char *p = (char *)memchr(tmp, ' ', 3);
110 if ( tmp[0] == 'I' ) {
111 // First line, indicates IBM (i.e. DOS line endings I
114 // move past this line and read and discard the next line
115 // which is the version and copyright information
116 in.getline(tmp, 2048);
117 // vector<string> vers_token = simgear::strutils::split( tmp );
118 if ( strlen(tmp) > 4 ) {
119 char *p = (char *)memchr(tmp, ' ', 4);
123 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
124 } else if ( line_id == 1 /* Airport */ ||
125 line_id == 16 /* Seaplane base */ ||
126 line_id == 17 /* Heliport */ ) {
127 parseAirportLine(simgear::strutils::split(line));
128 } else if ( line_id == 10 ) { // Runway v810
129 parseRunwayLine810(simgear::strutils::split(line));
130 } else if ( line_id == 100 ) { // Runway v850
131 parseRunwayLine850(simgear::strutils::split(line));
132 } else if ( line_id == 101 ) { // Water Runway v850
133 parseWaterRunwayLine850(simgear::strutils::split(line));
134 } else if ( line_id == 102 ) { // Helipad v850
135 parseHelipadLine850(simgear::strutils::split(line));
136 } else if ( line_id == 18 ) {
137 // beacon entry (ignore)
138 } else if ( line_id == 14 ) {
139 // control tower entry
140 vector<string> token(simgear::strutils::split(line));
142 double lat = atof( token[1].c_str() );
143 double lon = atof( token[2].c_str() );
144 double elev = atof( token[3].c_str() );
145 tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
147 } else if ( line_id == 19 ) {
148 // windsock entry (ignore)
149 } else if ( line_id == 20 ) {
150 // Taxiway sign (ignore)
151 } else if ( line_id == 21 ) {
152 // lighting objects (ignore)
153 } else if ( line_id == 15 ) {
154 // custom startup locations (ignore)
155 } else if ( line_id == 0 ) {
157 } else if ( line_id == 50 ) {
158 parseATISLine(comm_list, simgear::strutils::split(line));
159 } else if ( line_id >= 51 && line_id <= 56 ) {
160 // other frequency entries (ignore)
161 } else if ( line_id == 110 ) {
163 parsePavementLine850(simgear::strutils::split(line, 0, 4));
164 } else if ( line_id >= 111 && line_id <= 114 ) {
166 parsePavementNodeLine850(line_id, simgear::strutils::split(line));
167 } else if ( line_id >= 115 && line_id <= 116 ) {
168 // other pavement nodes (ignore)
169 } else if ( line_id == 120 ) {
171 } else if ( line_id == 130 ) {
173 } else if ( line_id == 99 ) {
174 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
176 SG_LOG( SG_GENERAL, SG_ALERT,
177 "Unknown line(#" << line_num << ") in file: " << line );
186 vector<string> token;
187 double rwy_lat_accum;
188 double rwy_lon_accum;
189 double last_rwy_heading;
193 string last_apt_name;
194 double last_apt_elev;
196 string last_apt_info;
197 string last_apt_type;
198 string pavement_ident;
201 vector<FGRunwayPtr> runways;
202 vector<FGTaxiwayPtr> taxiways;
203 vector<FGPavementPtr> pavements;
207 if (last_apt_id.empty()) {
212 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
217 double lat = rwy_lat_accum / (double)rwy_count;
218 double lon = rwy_lon_accum / (double)rwy_count;
221 // tower height hard coded for now...
222 const float tower_height = 50.0f;
223 // make a little off the heading for 1 runway airports...
224 float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
225 float fudge_lat = .003f - fudge_lon;
226 tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
229 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
230 FGAirport* apt = new FGAirport(last_apt_id, pos, tower, last_apt_name, false,
231 fptypeFromRobinType(atoi(last_apt_type.c_str())));
233 apt->setRunwaysAndTaxiways(runways, taxiways, pavements);
236 void parseAirportLine(const vector<string>& token)
238 const string& id(token[4]);
239 double elev = atof( token[1].c_str() );
244 last_apt_elev = elev;
249 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
250 last_apt_name += token[i];
251 last_apt_name += " ";
253 last_apt_name += token[token.size() - 1];
254 last_apt_type = token[0];
256 // clear runway list for start of next airport
262 void parseRunwayLine810(const vector<string>& token)
264 double lat = atof( token[1].c_str() );
265 double lon = atof( token[2].c_str() );
266 rwy_lat_accum += lat;
267 rwy_lon_accum += lon;
270 const string& rwy_no(token[3]);
272 double heading = atof( token[4].c_str() );
273 double length = atoi( token[5].c_str() );
274 double width = atoi( token[8].c_str() );
276 last_rwy_heading = heading;
278 int surface_code = atoi( token[10].c_str() );
279 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
281 if (rwy_no[0] == 'x') {
283 FGTaxiway* t = new FGTaxiway(rwy_no, pos, heading, length, width, surface_code);
284 taxiways.push_back(t);
287 string rwy_displ_threshold = token[6];
289 = simgear::strutils::split( rwy_displ_threshold, "." );
290 double displ_thresh1 = atof( displ[0].c_str() );
291 double displ_thresh2 = atof( displ[1].c_str() );
293 string rwy_stopway = token[7];
295 = simgear::strutils::split( rwy_stopway, "." );
296 double stopway1 = atof( stop[0].c_str() );
297 double stopway2 = atof( stop[1].c_str() );
299 FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
300 width, displ_thresh1, stopway1, surface_code, false);
301 runways.push_back(rwy);
303 FGRunway* reciprocal = new FGRunway(NULL, FGRunway::reverseIdent(rwy_no),
304 pos, heading + 180.0, length, width,
305 displ_thresh2, stopway2, surface_code, true);
307 runways.push_back(reciprocal);
309 rwy->setReciprocalRunway(reciprocal);
310 reciprocal->setReciprocalRunway(rwy);
314 void parseRunwayLine850(const vector<string>& token)
316 double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
317 int surface_code = atoi( token[2].c_str() );
319 double lat_1 = atof( token[9].c_str() );
320 double lon_1 = atof( token[10].c_str() );
321 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
322 rwy_lat_accum += lat_1;
323 rwy_lon_accum += lon_1;
326 double lat_2 = atof( token[18].c_str() );
327 double lon_2 = atof( token[19].c_str() );
328 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
329 rwy_lat_accum += lat_2;
330 rwy_lon_accum += lon_2;
333 double length, heading_1, heading_2, dummy;
334 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
336 SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
337 length *= SG_METER_TO_FEET;
339 last_rwy_heading = heading_1;
341 const string& rwy_no_1(token[8]);
342 const string& rwy_no_2(token[17]);
343 if ( rwy_no_1.size() == 0 || rwy_no_2.size() == 0 )
346 double displ_thresh1 = atof( token[11].c_str() );
347 double displ_thresh2 = atof( token[20].c_str() );
349 double stopway1 = atof( token[12].c_str() );
350 double stopway2 = atof( token[21].c_str() );
352 FGRunway* rwy = new FGRunway(NULL, rwy_no_1, pos, heading_1, length,
353 width, displ_thresh1, stopway1, surface_code, false);
354 runways.push_back(rwy);
356 FGRunway* reciprocal = new FGRunway(NULL, rwy_no_2,
357 pos, heading_2, length, width,
358 displ_thresh2, stopway2, surface_code, true);
359 runways.push_back(reciprocal);
361 rwy->setReciprocalRunway(reciprocal);
362 reciprocal->setReciprocalRunway(rwy);
365 void parseWaterRunwayLine850(const vector<string>& token)
367 double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
369 double lat_1 = atof( token[4].c_str() );
370 double lon_1 = atof( token[5].c_str() );
371 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
372 rwy_lat_accum += lat_1;
373 rwy_lon_accum += lon_1;
376 double lat_2 = atof( token[7].c_str() );
377 double lon_2 = atof( token[8].c_str() );
378 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
379 rwy_lat_accum += lat_2;
380 rwy_lon_accum += lon_2;
383 double length, heading_1, heading_2, dummy;
384 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
386 SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
388 last_rwy_heading = heading_1;
390 const string& rwy_no_1(token[3]);
391 const string& rwy_no_2(token[6]);
393 FGRunway* rwy = new FGRunway(NULL, rwy_no_1, pos, heading_1, length,
394 width, 0.0, 0.0, 13, false);
395 runways.push_back(rwy);
397 FGRunway* reciprocal = new FGRunway(NULL, rwy_no_2,
398 pos, heading_2, length, width,
400 runways.push_back(reciprocal);
402 rwy->setReciprocalRunway(reciprocal);
403 reciprocal->setReciprocalRunway(rwy);
406 void parseHelipadLine850(const vector<string>& token)
408 double length = atof( token[5].c_str() ) * SG_METER_TO_FEET;
409 double width = atof( token[6].c_str() ) * SG_METER_TO_FEET;
411 double lat = atof( token[2].c_str() );
412 double lon = atof( token[3].c_str() );
413 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
414 rwy_lat_accum += lat;
415 rwy_lon_accum += lon;
418 double heading = atof( token[4].c_str() );
420 last_rwy_heading = heading;
422 const string& rwy_no(token[1]);
423 int surface_code = atoi( token[7].c_str() );
425 FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
426 width, 0.0, 0.0, surface_code, false);
427 runways.push_back(rwy);
430 void parsePavementLine850(const vector<string>& token)
432 if ( token.size() >= 5 ) {
433 pavement_ident = token[4];
434 if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
435 pavement_ident.erase( pavement_ident.size()-1 );
437 pavement_ident = "xx";
441 void parsePavementNodeLine850(int num, const vector<string>& token)
443 double lat = atof( token[1].c_str() );
444 double lon = atof( token[2].c_str() );
445 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
448 if ( !pavement_ident.empty() ) {
449 pvt = new FGPavement( pavement_ident, pos );
450 pavements.push_back( pvt );
453 pvt = pavements.back();
455 if ( num == 112 || num == 114 ) {
456 double lat_b = atof( token[3].c_str() );
457 double lon_b = atof( token[4].c_str() );
458 SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
459 pvt->addBezierNode(pos, pos_b, num == 114);
461 pvt->addNode(pos, num == 113);
465 void parseATISLine(FGCommList *comm_list, const vector<string>& token)
467 if ( rwy_count <= 0 ) {
468 SG_LOG( SG_GENERAL, SG_ALERT,
469 "No runways; skipping AWOS for " + last_apt_id);
472 // This assumes/requires that any code-50 line (ATIS or AWOS)
473 // applies to the preceding code-1 line (airport ID and name)
474 // and that a full set of code-10 lines (runway descriptors)
475 // has come between the code-1 and code-50 lines.
476 // typical code-50 lines:
479 // This code parallels code found in "operator>>" in ATC.hxx;
480 // FIXME: unify the code.
482 a.geod = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count,
483 rwy_lat_accum / (double)rwy_count, last_apt_elev);
484 a.range = 50; // give all ATISs small range
485 a.ident = last_apt_id;
486 a.name = last_apt_name;
487 // short int representing tens of kHz:
488 a.freq = atoi(token[1].c_str());
489 if (token[2] == "ATIS") a.type = ATIS;
490 else a.type = AWOS; // ASOS same as AWOS
492 // generate cartesian coordinates
493 a.cart = SGVec3d::fromGeod(a.geod);
494 comm_list->commlist_freq[a.freq].push_back(a);
496 SGBucket bucket(a.geod);
497 int bucknum = bucket.gen_index();
498 comm_list->commlist_bck[bucknum].push_back(a);
500 SG_LOG( SG_GENERAL, SG_ALERT,
501 "Loaded ATIS/AWOS for airport: " << a.ident
502 << " lat: " << a.geod.getLatitudeDeg()
503 << " lon: " << a.geod.getLongitudeDeg()
504 << " freq: " << a.freq
505 << " type: " << a.type );
510 // Load the airport data base from the specified aptdb file. The
511 // metar file is used to mark the airports as having metar available
513 bool fgAirportDBLoad( const string &aptdb_file,
514 FGCommList *comm_list, const std::string &metar_file )
519 ld.parseAPT(aptdb_file, comm_list);
522 // Load the metar.dat file and update apt db with stations that
526 sg_gzifstream metar_in( metar_file );
527 if ( !metar_in.is_open() ) {
528 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
534 if ( ident == "#" || ident == "//" ) {
537 FGAirport* apt = FGAirport::findByIdent(ident);
544 SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");