-// 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
//
// 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.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
# include <config.h>
#endif
-// #include <sys/types.h> // for gdbm open flags
-// #include <sys/stat.h> // for gdbm open flags
+#include <cstdio> // sprintf()
+#include <cstdlib> // atoi()
+#include <cassert>
#include <simgear/compiler.h>
-#include <simgear/debug/logstream.hxx>
-#include <simgear/misc/fgstream.hxx>
+#include <simgear/props/props.hxx>
-#include STL_STRING
-#include STL_FUNCTIONAL
-#include STL_ALGORITHM
+#include <string>
#include "runways.hxx"
-SG_USING_NAMESPACE(std);
-
-
-FGRunway::FGRunway() {
-}
-
-
-FGRunway::~FGRunway() {
+#include <Airports/airport.hxx>
+#include <Navaids/procedure.hxx>
+#include <Navaids/navrecord.hxx>
+#include <Navaids/NavDataCache.hxx>
+
+using std::string;
+
+FGRunway::FGRunway(PositionedID aGuid,
+ PositionedID aAirport, const string& aIdent,
+ const SGGeod& aGeod,
+ const double heading, const double length,
+ const double width,
+ const double displ_thresh,
+ const double stopway,
+ const int surface_code) :
+ FGRunwayBase(aGuid, RUNWAY, aIdent, aGeod,
+ heading, length, width, surface_code),
+ _airport(aAirport),
+ _reciprocal(0),
+ _displ_thresh(displ_thresh),
+ _stopway(stopway),
+ _ils(0)
+{
}
-
-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);
+string FGRunway::reverseIdent(const string& aRunwayIdent)
+{
+ // Helipads don't have a seperate number per end
+ if (aRunwayIdent.size() && (aRunwayIdent[0] == 'H' || aRunwayIdent[0] == 'h' || aRunwayIdent[0] == 'x')) {
+ return aRunwayIdent;
+ }
+
+ std::string ident(aRunwayIdent);
+
+ char buf[4];
+ int rn = atoi(ident.substr(0,2).c_str());
+ rn += 18;
+ while(rn > 36) {
+ rn -= 36;
+ }
+
+ sprintf(buf, "%02i", rn);
+ if(ident.size() == 3) {
+ char suffix = toupper(ident[2]);
+ if(suffix == 'L') {
+ buf[2] = 'R';
+ } else if (suffix == 'R') {
+ buf[2] = 'L';
+ } else {
+ // something else, just copy
+ buf[2] = suffix;
}
-
- 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:F,End2Flags:F]");
-
- next_index = 0;
+
+ buf[3] = 0;
+ }
+
+ return buf;
}
-
-// search for the specified id
-bool FGRunways::search( const string& id, 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[id.c_str()]);
- cout << "index = " << index << endl;
-
- if ( index == -1 ) {
- 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;
+double FGRunway::score(double aLengthWt, double aWidthWt, double aSurfaceWt, double aIlsWt) const
+{
+ int surface = 1;
+ if (_surface_code == 12 || _surface_code == 5) // dry lakebed & gravel
+ surface = 2;
+ else if (_surface_code == 1 || _surface_code == 2) // asphalt & concrete
+ surface = 3;
+
+ int ils = (_ils != 0);
+
+ return _length * aLengthWt + _width * aWidthWt + surface * aSurfaceWt + ils * aIlsWt + 1e-20;
}
-
-FGRunway FGRunways::search( const string& id ) {
- FGRunway a;
- search( id, &a );
- return a;
+SGGeod FGRunway::begin() const
+{
+ return pointOnCenterline(0.0);
}
-
-// 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;
+SGGeod FGRunway::end() const
+{
+ return pointOnCenterline(lengthM());
}
-
-// Destructor
-FGRunways::~FGRunways( void ) {
- delete storage;
+SGGeod FGRunway::threshold() const
+{
+ return pointOnCenterline(_displ_thresh);
}
-
-// Constructor
-FGRunwaysUtil::FGRunwaysUtil() {
+void FGRunway::setReciprocalRunway(PositionedID other)
+{
+ assert(_reciprocal==0);
+ _reciprocal = other;
}
+FGAirport* FGRunway::airport() const
+{
+ return loadById<FGAirport>(_airport);
+}
-// load the data
-int FGRunwaysUtil::load( const string& file ) {
- FGRunway r;
- string apt_id;
-
- runways.erase( runways.begin(), runways.end() );
-
- fg_gzifstream in( file );
- if ( !in.is_open() ) {
- SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
- exit(-1);
- }
-
- // skip first line of file
- char tmp[256];
- in.getline( tmp, 256 );
-
- // read in each line of the file
-
-#ifdef __MWERKS__
+FGRunway* FGRunway::reciprocalRunway() const
+{
+ return loadById<FGRunway>(_reciprocal);
+}
- 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;
- }
+FGNavRecord* FGRunway::ILS() const
+{
+ if (_ils == 0) {
+ return NULL;
+ }
+
+ return loadById<FGNavRecord>(_ils);
+}
-#else
+FGNavRecord* FGRunway::glideslope() const
+{
+ flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
+ PositionedID gsId = cache->findNavaidForRunway(guid(), FGPositioned::GS);
+ if (gsId == 0) {
+ return NULL;
+ }
+
+ return loadById<FGNavRecord>(gsId);
+}
- 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;
+flightgear::SIDList FGRunway::getSIDs() const
+{
+ FGAirport* apt = airport();
+ flightgear::SIDList result;
+ for (unsigned int i=0; i<apt->numSIDs(); ++i) {
+ flightgear::SID* s = apt->getSIDByIndex(i);
+ if (s->isForRunway(this)) {
+ result.push_back(s);
}
-
-#endif
-
- return 1;
+ } // of SIDs at the airport iteration
+
+ return result;
}
-
-// 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:F,End2Flags:F]");
-
- 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;
+flightgear::STARList FGRunway::getSTARs() const
+{
+ FGAirport* apt = airport();
+ flightgear::STARList result;
+ for (unsigned int i=0; i<apt->numSTARs(); ++i) {
+ flightgear::STAR* s = apt->getSTARByIndex(i);
+ if (s->isForRunway(this)) {
+ result.push_back(s);
}
-
- // commit our changes
- storage.Commit();
-
- return true;
+ } // of STARs at the airport iteration
+
+ return result;
}
-
-#if 0
-// search for the specified id
-bool
-FGRunwaysUtil::search( const string& id, FGRunway* a ) const
+flightgear::ApproachList
+FGRunway::getApproaches(flightgear::ProcedureType type) const
{
- const_iterator it = runways.find( FGRunway(id) );
- if ( it != runways.end() )
+ FGAirport* apt = airport();
+ flightgear::ApproachList result;
+ for (unsigned int i=0; i<apt->numApproaches(); ++i)
+ {
+ flightgear::Approach* s = apt->getApproachByIndex(i);
+ if( s->runway() == this
+ && (type == flightgear::PROCEDURE_INVALID || type == s->type()) )
{
- *a = *it;
- return true;
- }
- else
- {
- return false;
+ result.push_back(s);
}
+ } // of approaches at the airport iteration
+
+ return result;
}
-
-FGRunway
-FGRunwaysUtil::search( const string& id ) const
+void FGRunway::updateThreshold(const SGGeod& newThreshold, double newHeading,
+ double newDisplacedThreshold,
+ double newStopway)
{
- FGRunway a;
- this->search( id, &a );
- return a;
+ modifyPosition(newThreshold);
+ _heading = newHeading;
+ _stopway = newStopway;
+ _displ_thresh = newDisplacedThreshold;
}
-#endif
-// Destructor
-FGRunwaysUtil::~FGRunwaysUtil( void ) {
+FGHelipad::FGHelipad(PositionedID aGuid,
+ PositionedID aAirport, const string& aIdent,
+ const SGGeod& aGeod,
+ const double heading, const double length,
+ const double width,
+ const int surface_code) :
+ FGRunwayBase(aGuid, RUNWAY, aIdent, aGeod,
+ heading, length, width, surface_code)
+{
}
-
-