-// runways.hxx -- a simple class to manage airport runway info
+// runways.cxx -- a simple class to manage airport runway info
//
// Written by Curtis Olson, started August 2000.
//
-// Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
#include <simgear/compiler.h>
#include <simgear/debug/logstream.hxx>
-#include <simgear/misc/sgstream.hxx>
#include STL_STRING
-#include STL_FUNCTIONAL
-#include STL_ALGORITHM
+#include <map>
#include "runways.hxx"
SG_USING_NAMESPACE(std);
-
-
-FGRunway::FGRunway() {
-}
-
-
-FGRunway::~FGRunway() {
-}
-
-
-FGRunways::FGRunways( const string& file ) {
- // open the specified database readonly
- storage = new c4_Storage( file.c_str(), false );
-
- if ( !storage->Strategy().IsValid() ) {
- SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
- exit(-1);
+SG_USING_STD(istream);
+SG_USING_STD(multimap);
+
+
+// add an entry to the list
+void FGRunwayList::add( const string& id, const string& rwy_no,
+ const double longitude, const double latitude,
+ const double heading, const double length,
+ const double width,
+ const double displ_thresh1, const double displ_thresh2,
+ const double stopway1, const double stopway2,
+ const string& lighting_flags, const int surface_code,
+ const string& shoulder_code, const int marking_code,
+ const double smoothness, const bool dist_remaining )
+{
+ FGRunway rwy;
+
+ rwy._id = id;
+ rwy._rwy_no = rwy_no;
+ // strip trailing "x" if it exists in runway number
+ string tmp = rwy._rwy_no.substr(2, 1);
+ if ( tmp == "x" ) {
+ rwy._rwy_no = rwy._rwy_no.substr(0, 2);
}
- vRunway = new c4_View;
- *vRunway =
- storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
-
- next_index = 0;
+ rwy._lon = longitude;
+ rwy._lat = latitude;
+ rwy._heading = heading;
+ rwy._length = length;
+ rwy._width = width;
+ rwy._displ_thresh1 = displ_thresh1;
+ rwy._displ_thresh2 = displ_thresh2;
+ rwy._stopway1 = stopway1;
+ rwy._stopway2 = stopway2;
+
+ rwy._lighting_flags = lighting_flags;
+ rwy._surface_code = surface_code;
+ rwy._shoulder_code = shoulder_code;
+ rwy._marking_code = marking_code;
+ rwy._smoothness = smoothness;
+ rwy._dist_remaining = dist_remaining;
+
+ if ( rwy_no == "xxx" ) {
+ rwy._type = "taxiway";
+ // don't insert taxiways into the DB for now
+ } else {
+ rwy._type = "runway";
+ runways.insert(pair<const string, FGRunway>(rwy._id, rwy));
+ }
}
-// search for the specified apt id
-bool FGRunways::search( const string& aptid, FGRunway* r ) {
- c4_StringProp pID ("ID");
- c4_StringProp pRwy ("Rwy");
- c4_FloatProp pLon ("Longitude");
- c4_FloatProp pLat ("Latitude");
- c4_FloatProp pHdg ("Heading");
- c4_FloatProp pLen ("Length");
- c4_FloatProp pWid ("Width");
- c4_StringProp pSurf ("SurfaceFlags");
- c4_StringProp pEnd1 ("End1Flags");
- c4_StringProp pEnd2 ("End2Flags");
-
- int index = vRunway->Find(pID[aptid.c_str()]);
- // cout << "index = " << index << endl;
-
- if ( index == -1 ) {
- return false;
+// 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;
+ SG_LOG( SG_GENERAL, SG_INFO,
+ "Standardising rwy number from " << tmp << " to " << rwyno );
}
+
+ 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 if (rwyno.substr(2,1) == "T") {
+ buf[2] = 'T';
+ buf[3] = '\0';
+ } else {
+ SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
+ << rwyno << " passed to GetReverseRunwayNo(...)");
+ }
+ }
+ return(buf);
+}
- next_index = index + 1;
-
- c4_RowRef row = vRunway->GetAt(index);
- r->id = (const char *) pID(row);
- r->rwy_no = (const char *) pRwy(row);
- r->lon = (double) pLon(row);
- r->lat = (double) pLat(row);
- r->heading = (double) pHdg(row);
- r->length = (double) pLen(row);
- r->width = (double) pWid(row);
- r->surface_flags = (const char *) pSurf(row);
- r->end1_flags = (const char *) pEnd1(row);
- r->end2_flags = (const char *) pEnd2(row);
+// search for the specified apt id (wierd!)
+bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
+ runway_map_iterator pos;
- return true;
+ pos = runways.lower_bound(aptid);
+ if ( pos != runways.end() ) {
+ current = pos;
+ *r = pos->second;
+ return true;
+ } else {
+ return false;
+ }
}
// search for the specified apt id and runway no
-bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
+bool FGRunwayList::search( const string& aptid, const string& rwyno,
+ FGRunway *r )
{
- c4_StringProp pID ("ID");
- c4_StringProp pRwy ("Rwy");
- c4_FloatProp pLon ("Longitude");
- c4_FloatProp pLat ("Latitude");
- c4_FloatProp pHdg ("Heading");
- c4_FloatProp pLen ("Length");
- c4_FloatProp pWid ("Width");
- c4_StringProp pSurf ("SurfaceFlags");
- c4_StringProp pEnd1 ("End1Flags");
- c4_StringProp pEnd2 ("End2Flags");
-
- int index = vRunway->Find(pID[aptid.c_str()]);
- // cout << "index = " << index << endl;
-
- if ( index == -1 ) {
- return false;
+ string revrwyno = "";
+ string runwayno = rwyno;
+ if ( runwayno.length() ) {
+ // standardize input number
+ string tmp = runwayno.substr(1, 1);
+ if (( tmp == "L" || tmp == "R" || tmp == "C" )
+ || (runwayno.size() == 1))
+ {
+ tmp = runwayno;
+ runwayno = "0" + tmp;
+ SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from "
+ << tmp << " to " << runwayno );
+ }
+ revrwyno = GetReverseRunwayNo(runwayno);
}
-
- c4_RowRef row = vRunway->GetAt(index);
- string rowid = (const char *) pID(row);
- string rowrwyno = (const char *) pRwy(row);
- while ( rowid == aptid ) {
- next_index = index + 1;
-
- if ( rowrwyno == rwyno ) {
- r->id = (const char *) pID(row);
- r->rwy_no = (const char *) pRwy(row);
- r->lon = (double) pLon(row);
- r->lat = (double) pLat(row);
- r->heading = (double) pHdg(row);
- r->length = (double) pLen(row);
- r->width = (double) pWid(row);
- r->surface_flags = (const char *) pSurf(row);
- r->end1_flags = (const char *) pEnd1(row);
- r->end2_flags = (const char *) pEnd2(row);
-
+ 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;
+ // NOTE - matching revrwyno implies that runwayno was
+ // actually correct.
+ r->_rwy_no = runwayno;
+ r->_heading += 180.0;
return true;
}
-
- index++;
- c4_RowRef row = vRunway->GetAt(index);
- string rowid = (const char *) pID(row);
- string rowrwyno = (const char *) pRwy(row);
}
return false;
}
-FGRunway FGRunways::search( const string& aptid ) {
+// (wierd!)
+FGRunway FGRunwayList::search( const string& aptid ) {
FGRunway a;
search( aptid, &a );
return a;
}
-// search for the specified id
-bool FGRunways::next( FGRunway* r ) {
- c4_StringProp pID ("ID");
- c4_StringProp pRwy ("Rwy");
- c4_FloatProp pLon ("Longitude");
- c4_FloatProp pLat ("Latitude");
- c4_FloatProp pHdg ("Heading");
- c4_FloatProp pLen ("Length");
- c4_FloatProp pWid ("Width");
- c4_StringProp pSurf ("SurfaceFlags");
- c4_StringProp pEnd1 ("End1Flags");
- c4_StringProp pEnd2 ("End2Flags");
-
- int size = vRunway->GetSize();
- // cout << "total records = " << size << endl;
-
- int index = next_index;
- // cout << "index = " << index << endl;
-
- if ( index == -1 || index >= size ) {
- return false;
- }
-
- next_index = index + 1;
-
- c4_RowRef row = vRunway->GetAt(index);
-
- r->id = (const char *) pID(row);
- r->rwy_no = (const char *) pRwy(row);
- r->lon = (double) pLon(row);
- r->lat = (double) pLat(row);
- r->heading = (double) pHdg(row);
- r->length = (double) pLen(row);
- r->width = (double) pWid(row);
- r->surface_flags = (const char *) pSurf(row);
- r->end1_flags = (const char *) pEnd1(row);
- r->end2_flags = (const char *) pEnd2(row);
-
- return true;
-}
-
-
// Return the runway closest to a given heading
-bool FGRunways::search( const string& aptid, const int tgt_hdg,
- FGRunway* runway )
+bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
+ FGRunway *runway )
{
- FGRunway r;
- double found_dir = 0.0;
-
- if ( !search( aptid, &r ) ) {
- SG_LOG( SG_GENERAL, SG_ALERT,
- "Failed to find " << aptid << " in database." );
- return false;
- }
-
- double diff;
- double min_diff = 360.0;
-
- while ( r.id == aptid ) {
- // 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;
- runway = &r;
- 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;
- runway = &r;
- found_dir = 180.0;
- }
-
- next( &r );
- }
-
- // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << runway->rwy_no
- // << " + " << found_dir );
-
- double heading = runway->heading + found_dir;
- while ( heading >= 360.0 ) { heading -= 360.0; }
- runway->heading = heading;
-
- return true;
+ string rwyNo = search(aptid, tgt_hdg);
+ return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
}
// Return the runway number of the runway closest to a given heading
-string FGRunways::search( const string& aptid, const int tgt_hdg ) {
+string FGRunwayList::search( const string& aptid, const int tgt_hdg ) {
FGRunway r;
+ FGRunway tmp_r;
string rn;
- double found_dir = 0.0;
-
- if ( !search( aptid, &r ) ) {
+ double found_dir = 0.0;
+
+ if ( !search( aptid, &tmp_r ) ) {
SG_LOG( SG_GENERAL, SG_ALERT,
"Failed to find " << aptid << " in database." );
return "NN";
double diff;
double min_diff = 360.0;
- while ( r.id == aptid ) {
+ while ( tmp_r._id == aptid ) {
// forward direction
- diff = tgt_hdg - r.heading;
+ diff = tgt_hdg - tmp_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 );
+ // "Runway " << tmp_r._rwy_no << " heading = "
+ // << tmp_r._heading << " diff = " << diff );
if ( diff < min_diff ) {
min_diff = diff;
- rn = r.rwy_no;
+ r = tmp_r;
found_dir = 0;
}
// reverse direction
- diff = tgt_hdg - r.heading - 180.0;
+ diff = tgt_hdg - tmp_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 <<
+ // "Runway -" << tmp_r._rwy_no << " heading = " <<
+ // tmp_r._heading + 180.0 <<
// " diff = " << diff );
if ( diff < min_diff ) {
min_diff = diff;
- rn = r.rwy_no;
+ r = tmp_r;
found_dir = 180.0;
}
- next( &r );
+ next( &tmp_r );
}
- // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
+ // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r._rwy_no
// << " + " << found_dir );
- // rn = r.rwy_no;
- // cout << "In search, rn = " << rn << endl;
+ rn = r._rwy_no;
if ( found_dir == 180 ) {
- int irn = atoi(rn.c_str());
- irn += 18;
- if ( irn > 36 ) {
- irn -= 36;
- }
- char buf[4]; // 2 chars + string terminator + 1 for safety
- sprintf(buf, "%i", irn);
- rn = buf;
+ rn = GetReverseRunwayNo(rn);
}
return rn;
}
-// Destructor
-FGRunways::~FGRunways( void ) {
- delete storage;
-}
-
-
-// Constructor
-FGRunwaysUtil::FGRunwaysUtil() {
-}
-
-
-// load the data
-int FGRunwaysUtil::load( const string& file ) {
- FGRunway r;
- string apt_id;
-
- runways.erase( runways.begin(), runways.end() );
-
- sg_gzifstream in( file );
- if ( !in.is_open() ) {
- SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
- exit(-1);
+bool FGRunwayList::next( FGRunway* runway ) {
+ ++current;
+ if ( current != runways.end() ) {
+ *runway = current->second;
+ return true;
+ } else {
+ return false;
}
-
- // skip first line of file
- char tmp[2048];
- in.getline( tmp, 2048 );
-
- // read in each line of the file
-
-#ifdef __MWERKS__
-
- in >> ::skipws;
- char c = 0;
- while ( in.get(c) && c != '\0' ) {
- if ( c == 'A' ) {
- in >> apt_id;
- in >> skipeol;
- } else if ( c == 'R' ) {
- in >> r;
- r.id = apt_id;
- runways.push_back(r);
- } else {
- in >> skipeol;
- }
- in >> ::skipws;
- }
-
-#else
-
- in >> ::skipws;
- while ( ! in.eof() ) {
- char c = 0;
- in.get(c);
- if ( c == 'A' ) {
- in >> apt_id;
- in >> skipeol;
- } else if ( c == 'R' ) {
- in >> r;
- r.id = apt_id;
- // cout << apt_id << " " << r.rwy_no << endl;
- runways.push_back(r);
- } else {
- in >> skipeol;
- }
- in >> ::skipws;
- }
-
-#endif
-
- return 1;
}
-// save the data in gdbm format
-bool FGRunwaysUtil::dump_mk4( const string& file ) {
- // open database for writing
- c4_Storage storage( file.c_str(), true );
-
- // need to do something about error handling here!
-
- // define the properties
- c4_StringProp pID ("ID");
- c4_StringProp pRwy ("Rwy");
- c4_FloatProp pLon ("Longitude");
- c4_FloatProp pLat ("Latitude");
- c4_FloatProp pHdg ("Heading");
- c4_FloatProp pLen ("Length");
- c4_FloatProp pWid ("Width");
- c4_StringProp pSurf ("SurfaceFlags");
- c4_StringProp pEnd1 ("End1Flags");
- c4_StringProp pEnd2 ("End2Flags");
-
- // Start with an empty view of the proper structure.
- c4_View vRunway =
- storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
-
- c4_Row row;
-
- iterator current = runways.begin();
- const_iterator end = runways.end();
- while ( current != end ) {
- // add each runway record
- pID (row) = current->id.c_str();
- pRwy (row) = current->rwy_no.c_str();
- pLon (row) = current->lon;
- pLat (row) = current->lat;
- pHdg (row) = current->heading;
- pLen (row) = current->length;
- pWid (row) = current->width;
- pSurf (row) = current->surface_flags.c_str();
- pEnd1 (row) = current->end1_flags.c_str();
- pEnd2 (row) = current->end2_flags.c_str();
- vRunway.Add(row);
-
- ++current;
- }
-
- // commit our changes
- storage.Commit();
-
- return true;
-}
+FGRunway FGRunwayList::next() {
+ FGRunway result;
-
-#if 0
-// search for the specified id
-bool
-FGRunwaysUtil::search( const string& id, FGRunway* a ) const
-{
- const_iterator it = runways.find( FGRunway(id) );
- if ( it != runways.end() )
- {
- *a = *it;
- return true;
+ ++current;
+ if ( current != runways.end() ) {
+ result = current->second;
}
- else
- {
- return false;
- }
-}
-
-FGRunway
-FGRunwaysUtil::search( const string& id ) const
-{
- FGRunway a;
- this->search( id, &a );
- return a;
+ return result;
}
-#endif
+
// Destructor
-FGRunwaysUtil::~FGRunwaysUtil( void ) {
+FGRunwayList::~FGRunwayList( void ) {
}
-
-