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"
49 #include <ATCDCL/commlist.hxx>
55 static FGPositioned::Type fptypeFromRobinType(int aType)
58 case 1: return FGPositioned::AIRPORT;
59 case 16: return FGPositioned::SEAPORT;
60 case 17: return FGPositioned::HELIPORT;
62 SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
63 throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
81 void parseAPT(const string &aptdb_file, FGCommList *comm_list)
83 sg_gzifstream in( aptdb_file );
85 if ( !in.is_open() ) {
86 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
94 unsigned int line_id = 0;
95 unsigned int line_num = 0;
97 while ( ! in.eof() ) {
98 in.getline(tmp, 2048);
99 line = tmp; // string copy, ack
102 if ( !line.size() || isspace(tmp[0])) {
106 if (line.size() >= 3) {
107 char *p = (char *)memchr(tmp, ' ', 3);
113 if ( tmp[0] == 'I' ) {
114 // First line, indicates IBM (i.e. DOS line endings I
117 // move past this line and read and discard the next line
118 // which is the version and copyright information
119 in.getline(tmp, 2048);
120 // vector<string> vers_token = simgear::strutils::split( tmp );
121 if ( strlen(tmp) > 4 ) {
122 char *p = (char *)memchr(tmp, ' ', 4);
126 SG_LOG( SG_GENERAL, SG_INFO, "Data file version = " << tmp );
127 } else if ( line_id == 1 /* Airport */ ||
128 line_id == 16 /* Seaplane base */ ||
129 line_id == 17 /* Heliport */ ) {
130 parseAirportLine(simgear::strutils::split(line));
131 } else if ( line_id == 10 ) { // Runway v810
132 parseRunwayLine810(simgear::strutils::split(line));
133 } else if ( line_id == 100 ) { // Runway v850
134 parseRunwayLine850(simgear::strutils::split(line));
135 } else if ( line_id == 101 ) { // Water Runway v850
136 parseWaterRunwayLine850(simgear::strutils::split(line));
137 } else if ( line_id == 102 ) { // Helipad v850
138 parseHelipadLine850(simgear::strutils::split(line));
139 } else if ( line_id == 18 ) {
140 // beacon entry (ignore)
141 } else if ( line_id == 14 ) {
142 // control tower entry
143 vector<string> token(simgear::strutils::split(line));
145 double lat = atof( token[1].c_str() );
146 double lon = atof( token[2].c_str() );
147 double elev = atof( token[3].c_str() );
148 tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
150 } else if ( line_id == 19 ) {
151 // windsock entry (ignore)
152 } else if ( line_id == 20 ) {
153 // Taxiway sign (ignore)
154 } else if ( line_id == 21 ) {
155 // lighting objects (ignore)
156 } else if ( line_id == 15 ) {
157 // custom startup locations (ignore)
158 } else if ( line_id == 0 ) {
160 } else if ( line_id == 50 ) {
162 parseATISLine(comm_list, simgear::strutils::split(line));
164 } else if ( line_id >= 51 && line_id <= 56 ) {
165 // other frequency entries (ignore)
166 } else if ( line_id == 110 ) {
168 parsePavementLine850(simgear::strutils::split(line, 0, 4));
169 } else if ( line_id >= 111 && line_id <= 114 ) {
171 parsePavementNodeLine850(line_id, simgear::strutils::split(line));
172 } else if ( line_id >= 115 && line_id <= 116 ) {
173 // other pavement nodes (ignore)
174 } else if ( line_id == 120 ) {
176 } else if ( line_id == 130 ) {
178 } else if ( line_id == 99 ) {
179 SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
181 SG_LOG( SG_GENERAL, SG_ALERT,
182 "Unknown line(#" << line_num << ") in file: " << line );
191 vector<string> token;
192 double rwy_lat_accum;
193 double rwy_lon_accum;
194 double last_rwy_heading;
198 string last_apt_name;
199 double last_apt_elev;
201 string last_apt_info;
202 string last_apt_type;
203 string pavement_ident;
206 vector<FGRunwayPtr> runways;
207 vector<FGTaxiwayPtr> taxiways;
208 vector<FGPavementPtr> pavements;
212 if (last_apt_id.empty()) {
217 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << last_apt_id
222 double lat = rwy_lat_accum / (double)rwy_count;
223 double lon = rwy_lon_accum / (double)rwy_count;
226 // tower height hard coded for now...
227 const float tower_height = 50.0f;
228 // make a little off the heading for 1 runway airports...
229 float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
230 float fudge_lat = .003f - fudge_lon;
231 tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, last_apt_elev + tower_height);
234 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
235 FGAirport* apt = new FGAirport(last_apt_id, pos, tower, last_apt_name, false,
236 fptypeFromRobinType(atoi(last_apt_type.c_str())));
237 apt->setRunwaysAndTaxiways(runways, taxiways, pavements);
240 void parseAirportLine(const vector<string>& token)
242 const string& id(token[4]);
243 double elev = atof( token[1].c_str() );
248 last_apt_elev = elev;
253 for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
254 last_apt_name += token[i];
255 last_apt_name += " ";
257 last_apt_name += token[token.size() - 1];
258 last_apt_type = token[0];
260 // clear runway list for start of next airport
266 void parseRunwayLine810(const vector<string>& token)
268 double lat = atof( token[1].c_str() );
269 double lon = atof( token[2].c_str() );
270 rwy_lat_accum += lat;
271 rwy_lon_accum += lon;
274 const string& rwy_no(token[3]);
276 double heading = atof( token[4].c_str() );
277 double length = atoi( token[5].c_str() );
278 double width = atoi( token[8].c_str() );
280 last_rwy_heading = heading;
282 int surface_code = atoi( token[10].c_str() );
283 SGGeod pos(SGGeod::fromDegFt(lon, lat, last_apt_elev));
285 if (rwy_no[0] == 'x') {
287 FGTaxiway* t = new FGTaxiway(rwy_no, pos, heading, length, width, surface_code);
288 taxiways.push_back(t);
291 string rwy_displ_threshold = token[6];
293 = simgear::strutils::split( rwy_displ_threshold, "." );
294 double displ_thresh1 = atof( displ[0].c_str() );
295 double displ_thresh2 = atof( displ[1].c_str() );
297 string rwy_stopway = token[7];
299 = simgear::strutils::split( rwy_stopway, "." );
300 double stopway1 = atof( stop[0].c_str() );
301 double stopway2 = atof( stop[1].c_str() );
303 FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
304 width, displ_thresh1, stopway1, surface_code, false);
305 runways.push_back(rwy);
307 FGRunway* reciprocal = new FGRunway(NULL, FGRunway::reverseIdent(rwy_no),
308 pos, heading + 180.0, length, width,
309 displ_thresh2, stopway2, surface_code, true);
311 runways.push_back(reciprocal);
313 rwy->setReciprocalRunway(reciprocal);
314 reciprocal->setReciprocalRunway(rwy);
318 void parseRunwayLine850(const vector<string>& token)
320 double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
321 int surface_code = atoi( token[2].c_str() );
323 double lat_1 = atof( token[9].c_str() );
324 double lon_1 = atof( token[10].c_str() );
325 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
326 rwy_lat_accum += lat_1;
327 rwy_lon_accum += lon_1;
330 double lat_2 = atof( token[18].c_str() );
331 double lon_2 = atof( token[19].c_str() );
332 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
333 rwy_lat_accum += lat_2;
334 rwy_lon_accum += lon_2;
337 double length, heading_1, heading_2, dummy;
338 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
340 SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
341 length *= SG_METER_TO_FEET;
343 last_rwy_heading = heading_1;
345 const string& rwy_no_1(token[8]);
346 const string& rwy_no_2(token[17]);
347 if ( rwy_no_1.size() == 0 || rwy_no_2.size() == 0 )
350 double displ_thresh1 = atof( token[11].c_str() );
351 double displ_thresh2 = atof( token[20].c_str() );
353 double stopway1 = atof( token[12].c_str() );
354 double stopway2 = atof( token[21].c_str() );
356 FGRunway* rwy = new FGRunway(NULL, rwy_no_1, pos, heading_1, length,
357 width, displ_thresh1, stopway1, surface_code, false);
358 runways.push_back(rwy);
360 FGRunway* reciprocal = new FGRunway(NULL, rwy_no_2,
361 pos, heading_2, length, width,
362 displ_thresh2, stopway2, surface_code, true);
363 runways.push_back(reciprocal);
365 rwy->setReciprocalRunway(reciprocal);
366 reciprocal->setReciprocalRunway(rwy);
369 void parseWaterRunwayLine850(const vector<string>& token)
371 double width = atof( token[1].c_str() ) * SG_METER_TO_FEET;
373 double lat_1 = atof( token[4].c_str() );
374 double lon_1 = atof( token[5].c_str() );
375 SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
376 rwy_lat_accum += lat_1;
377 rwy_lon_accum += lon_1;
380 double lat_2 = atof( token[7].c_str() );
381 double lon_2 = atof( token[8].c_str() );
382 SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
383 rwy_lat_accum += lat_2;
384 rwy_lon_accum += lon_2;
387 double length, heading_1, heading_2, dummy;
388 SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length );
390 SGGeodesy::direct( pos_1, heading_1, length / 2.0, pos, dummy );
392 last_rwy_heading = heading_1;
394 const string& rwy_no_1(token[3]);
395 const string& rwy_no_2(token[6]);
397 FGRunway* rwy = new FGRunway(NULL, rwy_no_1, pos, heading_1, length,
398 width, 0.0, 0.0, 13, false);
399 runways.push_back(rwy);
401 FGRunway* reciprocal = new FGRunway(NULL, rwy_no_2,
402 pos, heading_2, length, width,
404 runways.push_back(reciprocal);
406 rwy->setReciprocalRunway(reciprocal);
407 reciprocal->setReciprocalRunway(rwy);
410 void parseHelipadLine850(const vector<string>& token)
412 double length = atof( token[5].c_str() ) * SG_METER_TO_FEET;
413 double width = atof( token[6].c_str() ) * SG_METER_TO_FEET;
415 double lat = atof( token[2].c_str() );
416 double lon = atof( token[3].c_str() );
417 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
418 rwy_lat_accum += lat;
419 rwy_lon_accum += lon;
422 double heading = atof( token[4].c_str() );
424 last_rwy_heading = heading;
426 const string& rwy_no(token[1]);
427 int surface_code = atoi( token[7].c_str() );
429 FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
430 width, 0.0, 0.0, surface_code, false);
431 runways.push_back(rwy);
434 void parsePavementLine850(const vector<string>& token)
436 if ( token.size() >= 5 ) {
437 pavement_ident = token[4];
438 if ( !pavement_ident.empty() && pavement_ident[pavement_ident.size()-1] == '\r' )
439 pavement_ident.erase( pavement_ident.size()-1 );
441 pavement_ident = "xx";
445 void parsePavementNodeLine850(int num, const vector<string>& token)
447 double lat = atof( token[1].c_str() );
448 double lon = atof( token[2].c_str() );
449 SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
452 if ( !pavement_ident.empty() ) {
453 pvt = new FGPavement( pavement_ident, pos );
454 pavements.push_back( pvt );
457 pvt = pavements.back();
459 if ( num == 112 || num == 114 ) {
460 double lat_b = atof( token[3].c_str() );
461 double lon_b = atof( token[4].c_str() );
462 SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
463 pvt->addBezierNode(pos, pos_b, num == 114);
465 pvt->addNode(pos, num == 113);
469 void parseATISLine(FGCommList *comm_list, const vector<string>& token)
471 if ( rwy_count <= 0 ) {
472 SG_LOG( SG_GENERAL, SG_ALERT,
473 "No runways; skipping AWOS for " + last_apt_id);
476 // This assumes/requires that any code-50 line (ATIS or AWOS)
477 // applies to the preceding code-1 line (airport ID and name)
478 // and that a full set of code-10 lines (runway descriptors)
479 // has come between the code-1 and code-50 lines.
480 // typical code-50 lines:
483 // This code parallels code found in "operator>>" in ATC.hxx;
484 // FIXME: unify the code.
486 a.geod = SGGeod::fromDegFt(rwy_lon_accum / (double)rwy_count,
487 rwy_lat_accum / (double)rwy_count, last_apt_elev);
488 a.range = 50; // give all ATISs small range
489 a.ident = last_apt_id;
490 a.name = last_apt_name;
491 // short int representing tens of kHz:
492 a.freq = atoi(token[1].c_str());
493 if (token[2] == "ATIS") a.type = ATIS;
494 else a.type = AWOS; // ASOS same as AWOS
496 // generate cartesian coordinates
497 a.cart = SGVec3d::fromGeod(a.geod);
498 comm_list->commlist_freq[a.freq].push_back(a);
500 SGBucket bucket(a.geod);
501 int bucknum = bucket.gen_index();
502 comm_list->commlist_bck[bucknum].push_back(a);
504 SG_LOG( SG_GENERAL, SG_ALERT,
505 "Loaded ATIS/AWOS for airport: " << a.ident
506 << " lat: " << a.geod.getLatitudeDeg()
507 << " lon: " << a.geod.getLongitudeDeg()
508 << " freq: " << a.freq
509 << " type: " << a.type );
516 // Load the airport data base from the specified aptdb file. The
517 // metar file is used to mark the airports as having metar available
519 bool fgAirportDBLoad( const string &aptdb_file,
520 FGCommList *comm_list, const std::string &metar_file )
524 ld.parseAPT(aptdb_file, comm_list);
526 // Load the metar.dat file and update apt db with stations that
530 sg_gzifstream metar_in( metar_file );
531 if ( !metar_in.is_open() ) {
532 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
538 if ( ident == "#" || ident == "//" ) {
541 FGAirport* apt = FGAirport::findByIdent(ident);
548 SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");