2003-03-05 18:02:24 +00:00
|
|
|
// runways.cxx -- a simple class to manage airport runway info
|
2000-08-16 01:28:33 +00:00
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started August 2000.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-11-08 16:52:51 +00:00
|
|
|
#include <math.h> // fabs()
|
2001-11-07 17:55:28 +00:00
|
|
|
#include <stdio.h> // sprintf()
|
2000-08-16 01:28:33 +00:00
|
|
|
|
|
|
|
#include <simgear/compiler.h>
|
|
|
|
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
2001-03-25 14:20:12 +00:00
|
|
|
#include <simgear/misc/sgstream.hxx>
|
2000-08-16 01:28:33 +00:00
|
|
|
|
|
|
|
#include STL_STRING
|
2003-08-29 04:11:23 +00:00
|
|
|
#include STL_IOSTREAM
|
|
|
|
#include <map>
|
2000-08-16 01:28:33 +00:00
|
|
|
|
|
|
|
#include "runways.hxx"
|
|
|
|
|
2001-03-23 22:59:18 +00:00
|
|
|
SG_USING_NAMESPACE(std);
|
2002-04-04 06:05:51 +00:00
|
|
|
SG_USING_STD(istream);
|
2003-08-29 04:11:23 +00:00
|
|
|
SG_USING_STD(multimap);
|
2000-08-16 01:28:33 +00:00
|
|
|
|
2002-04-04 06:05:51 +00:00
|
|
|
inline istream&
|
|
|
|
operator >> ( istream& in, FGRunway& a )
|
|
|
|
{
|
2003-08-29 04:11:23 +00:00
|
|
|
string type;
|
2002-04-04 06:05:51 +00:00
|
|
|
int tmp;
|
2000-08-16 01:28:33 +00:00
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
in >> a.type;
|
|
|
|
if ( a.type == "R" ) {
|
|
|
|
in >> a.id >> a.rwy_no >> a.lat >> a.lon >> a.heading
|
|
|
|
>> a.length >> a.width >> a.surface_flags >> a.end1_flags
|
|
|
|
>> tmp >> tmp >> a.end2_flags >> tmp >> tmp;
|
|
|
|
} else {
|
|
|
|
in >> a.id >> a.rwy_no >> a.lat >> a.lon >> a.heading
|
|
|
|
>> a.length >> a.width >> a.surface_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
2000-08-16 01:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
FGRunwayList::FGRunwayList( const string& file ) {
|
|
|
|
SG_LOG( SG_GENERAL, SG_DEBUG, "Reading runway list: " << file );
|
2000-08-16 01:28:33 +00:00
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
// open the specified file for reading
|
|
|
|
sg_gzifstream in( file );
|
|
|
|
if ( !in.is_open() ) {
|
|
|
|
SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
|
2000-08-16 01:28:33 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
// skip header line
|
|
|
|
in >> skipeol;
|
2000-08-16 01:28:33 +00:00
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
FGRunway rwy;
|
|
|
|
while ( in ) {
|
|
|
|
in >> rwy;
|
|
|
|
runways.insert(pair<const string, FGRunway>(rwy.id, rwy));
|
|
|
|
}
|
2000-08-16 01:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-20 09:38:06 +00:00
|
|
|
// Return reverse rwy number
|
|
|
|
// eg 01 -> 19
|
|
|
|
// 03L -> 21R
|
|
|
|
static string GetReverseRunwayNo(string rwyno) {
|
|
|
|
// cout << "Original rwyno = " << rwyNo << '\n';
|
|
|
|
|
|
|
|
// standardize input number
|
|
|
|
string tmp = rwyno.substr(1, 1);
|
|
|
|
if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
|
|
|
|
tmp = rwyno;
|
|
|
|
rwyno = "0" + tmp;
|
2003-03-20 10:43:02 +00:00
|
|
|
SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from " << tmp
|
|
|
|
<< " to " << rwyno );
|
2003-03-20 09:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char buf[4];
|
|
|
|
int rn = atoi(rwyno.substr(0,2).c_str());
|
|
|
|
rn += 18;
|
|
|
|
while(rn > 36) {
|
|
|
|
rn -= 36;
|
|
|
|
}
|
|
|
|
sprintf(buf, "%02i", rn);
|
|
|
|
if(rwyno.size() == 3) {
|
|
|
|
if(rwyno.substr(2,1) == "L") {
|
|
|
|
buf[2] = 'R';
|
|
|
|
buf[3] = '\0';
|
|
|
|
} else if (rwyno.substr(2,1) == "R") {
|
|
|
|
buf[2] = 'L';
|
|
|
|
buf[3] = '\0';
|
|
|
|
} else if (rwyno.substr(2,1) == "C") {
|
|
|
|
buf[2] = 'C';
|
|
|
|
buf[3] = '\0';
|
|
|
|
} else {
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
|
|
|
|
<< rwyno << " passed to GetReverseRunwayNo(...)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return((string)buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
// search for the specified apt id (wierd!)
|
|
|
|
bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
|
|
|
|
runway_map_iterator pos;
|
2000-08-16 01:28:33 +00:00
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
pos = runways.lower_bound(aptid);
|
|
|
|
if ( pos != runways.end() ) {
|
|
|
|
current = pos;
|
|
|
|
*r = pos->second;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2000-08-16 01:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-15 00:00:08 +00:00
|
|
|
// search for the specified apt id and runway no
|
2003-08-29 04:11:23 +00:00
|
|
|
bool FGRunwayList::search( const string& aptid, const string& rwyno,
|
|
|
|
FGRunway *r )
|
2001-05-15 00:00:08 +00:00
|
|
|
{
|
2003-03-20 09:38:06 +00:00
|
|
|
// standardize input number
|
2003-08-29 04:11:23 +00:00
|
|
|
string runwayno = rwyno;
|
2003-03-20 09:38:06 +00:00
|
|
|
string tmp = runwayno.substr(1, 1);
|
|
|
|
if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (runwayno.size() == 1)) {
|
|
|
|
tmp = runwayno;
|
|
|
|
runwayno = "0" + tmp;
|
2003-08-29 04:11:23 +00:00
|
|
|
SG_LOG( SG_GENERAL, SG_INFO,
|
|
|
|
"Standardising rwy number from " << tmp << " to " << runwayno );
|
2003-03-20 09:38:06 +00:00
|
|
|
}
|
2003-08-29 04:11:23 +00:00
|
|
|
string revrwyno = GetReverseRunwayNo(runwayno);
|
2001-05-15 00:00:08 +00:00
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
runway_map_iterator pos;
|
|
|
|
for ( pos = runways.lower_bound( aptid );
|
|
|
|
pos != runways.upper_bound( aptid ); ++pos)
|
|
|
|
{
|
|
|
|
if ( pos->second.rwy_no == runwayno ) {
|
|
|
|
current = pos;
|
|
|
|
*r = pos->second;
|
|
|
|
return true;
|
|
|
|
} else if ( pos->second.rwy_no == revrwyno ) {
|
|
|
|
// Search again with the other-end runway number.
|
|
|
|
// Remember we have to munge the heading and rwy_no
|
|
|
|
// results if this one matches
|
|
|
|
current = pos;
|
|
|
|
*r = pos->second;
|
|
|
|
r->rwy_no = revrwyno;
|
|
|
|
r->heading += 180.0;
|
|
|
|
string tmp = r->end1_flags;
|
|
|
|
r->end1_flags = r->end2_flags;
|
|
|
|
r->end2_flags = tmp;
|
2001-05-15 00:00:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
// (wierd!)
|
|
|
|
FGRunway FGRunwayList::search( const string& aptid ) {
|
2000-08-16 01:28:33 +00:00
|
|
|
FGRunway a;
|
2001-05-15 00:00:08 +00:00
|
|
|
search( aptid, &a );
|
2000-08-16 01:28:33 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-07 17:55:28 +00:00
|
|
|
// Return the runway closest to a given heading
|
2003-08-29 04:11:23 +00:00
|
|
|
bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
|
|
|
|
FGRunway *runway )
|
2001-11-07 17:55:28 +00:00
|
|
|
{
|
2003-03-05 18:02:24 +00:00
|
|
|
string rwyNo = search(aptid, tgt_hdg);
|
|
|
|
return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
|
2001-11-07 17:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return the runway number of the runway closest to a given heading
|
2003-08-29 04:11:23 +00:00
|
|
|
string FGRunwayList::search( const string& aptid, const int tgt_hdg ) {
|
2001-11-07 17:55:28 +00:00
|
|
|
FGRunway r;
|
2003-03-20 09:38:06 +00:00
|
|
|
FGRunway tmp_r;
|
2001-11-07 17:55:28 +00:00
|
|
|
string rn;
|
|
|
|
double found_dir = 0.0;
|
|
|
|
|
2003-03-20 09:38:06 +00:00
|
|
|
if ( !search( aptid, &tmp_r ) ) {
|
2001-11-07 17:55:28 +00:00
|
|
|
SG_LOG( SG_GENERAL, SG_ALERT,
|
|
|
|
"Failed to find " << aptid << " in database." );
|
|
|
|
return "NN";
|
|
|
|
}
|
|
|
|
|
|
|
|
double diff;
|
|
|
|
double min_diff = 360.0;
|
|
|
|
|
2003-03-20 09:38:06 +00:00
|
|
|
while ( tmp_r.id == aptid ) {
|
|
|
|
r = tmp_r;
|
|
|
|
|
2001-11-07 17:55:28 +00:00
|
|
|
// forward direction
|
|
|
|
diff = tgt_hdg - r.heading;
|
|
|
|
while ( diff < -180.0 ) { diff += 360.0; }
|
|
|
|
while ( diff > 180.0 ) { diff -= 360.0; }
|
|
|
|
diff = fabs(diff);
|
|
|
|
// SG_LOG( SG_GENERAL, SG_INFO,
|
|
|
|
// "Runway " << r.rwy_no << " heading = " << r.heading <<
|
|
|
|
// " diff = " << diff );
|
|
|
|
if ( diff < min_diff ) {
|
|
|
|
min_diff = diff;
|
|
|
|
rn = r.rwy_no;
|
|
|
|
found_dir = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse direction
|
|
|
|
diff = tgt_hdg - r.heading - 180.0;
|
|
|
|
while ( diff < -180.0 ) { diff += 360.0; }
|
|
|
|
while ( diff > 180.0 ) { diff -= 360.0; }
|
|
|
|
diff = fabs(diff);
|
|
|
|
// SG_LOG( SG_GENERAL, SG_INFO,
|
|
|
|
// "Runway -" << r.rwy_no << " heading = " <<
|
|
|
|
// r.heading + 180.0 <<
|
|
|
|
// " diff = " << diff );
|
|
|
|
if ( diff < min_diff ) {
|
|
|
|
min_diff = diff;
|
|
|
|
rn = r.rwy_no;
|
|
|
|
found_dir = 180.0;
|
|
|
|
}
|
|
|
|
|
2003-03-20 09:38:06 +00:00
|
|
|
next( &tmp_r );
|
2001-11-07 17:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
|
|
|
|
// << " + " << found_dir );
|
2003-03-20 09:38:06 +00:00
|
|
|
rn = r.rwy_no;
|
2001-11-07 17:55:28 +00:00
|
|
|
// cout << "In search, rn = " << rn << endl;
|
|
|
|
if ( found_dir == 180 ) {
|
2003-03-20 09:38:06 +00:00
|
|
|
rn = GetReverseRunwayNo(rn);
|
|
|
|
//cout << "New rn = " << rn << '\n';
|
2001-11-07 17:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
bool FGRunwayList::next( FGRunway* runway ) {
|
|
|
|
++current;
|
|
|
|
if ( current != runways.end() ) {
|
|
|
|
*runway = current->second;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2000-08-16 01:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
FGRunway FGRunwayList::next() {
|
|
|
|
FGRunway result;
|
2000-08-16 01:28:33 +00:00
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
++current;
|
|
|
|
if ( current != runways.end() ) {
|
|
|
|
result = current->second;
|
2000-08-16 01:28:33 +00:00
|
|
|
}
|
|
|
|
|
2003-08-29 04:11:23 +00:00
|
|
|
return result;
|
2000-08-16 01:28:33 +00:00
|
|
|
}
|
2003-08-29 04:11:23 +00:00
|
|
|
|
2000-08-16 01:28:33 +00:00
|
|
|
|
|
|
|
// Destructor
|
2003-08-29 04:11:23 +00:00
|
|
|
FGRunwayList::~FGRunwayList( void ) {
|
2000-08-16 01:28:33 +00:00
|
|
|
}
|