2 // simple.cxx -- a really simplistic class to manage airport ID,
3 // lat, lon of the center of one of it's runways, and
6 // Written by Curtis Olson, started April 1998.
7 // Updated by Durk Talsma, started December, 2004.
9 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of the
14 // License, or (at your option) any later version.
16 // This program is distributed in the hope that it will be useful, but
17 // WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/props/props.hxx>
37 #include <simgear/props/props_io.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/sg_inlines.h>
41 #include <Environment/environment_mgr.hxx>
42 #include <Environment/environment.hxx>
43 #include <Main/fg_props.hxx>
44 #include <Airports/runways.hxx>
45 #include <Airports/pavement.hxx>
46 #include <Airports/dynamics.hxx>
47 #include <Airports/xmlloader.hxx>
48 #include <Navaids/procedure.hxx>
49 #include <Navaids/waypoint.hxx>
50 #include <Navaids/PositionedBinding.hxx>
51 #include <ATC/CommStation.hxx>
56 using namespace flightgear;
58 // magic import of a helper which uses FGPositioned internals
59 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
61 /***************************************************************************
63 ***************************************************************************/
65 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
66 const string &name, bool has_metar, Type aType) :
67 FGPositioned(aType, id, location),
68 _tower_location(tower_location),
70 _has_metar(has_metar),
72 mRunwaysLoaded(false),
75 init(true); // init FGPositioned
79 FGAirport::~FGAirport()
84 bool FGAirport::isAirport() const
86 return type() == AIRPORT;
89 bool FGAirport::isSeaport() const
91 return type() == SEAPORT;
94 bool FGAirport::isHeliport() const
96 return type() == HELIPORT;
99 FGAirportDynamics * FGAirport::getDynamics()
105 _dynamics = new FGAirportDynamics(this);
106 XMLLoader::load(_dynamics);
108 FGRunwayPreference rwyPrefs(this);
109 XMLLoader::load(&rwyPrefs);
110 _dynamics->setRwyUse(rwyPrefs);
111 XMLLoader::load(_dynamics->getSIDs());
116 unsigned int FGAirport::numRunways() const
119 return mRunways.size();
122 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
126 assert(aIndex >= 0 && aIndex < mRunways.size());
127 return mRunways[aIndex];
130 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
132 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
135 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
137 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
138 if (it == mRunways.end()) {
139 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
140 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
146 FGAirport::Runway_iterator
147 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
150 return mRunways.end();
154 string ident(aIdent);
155 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
156 ident = "0" + aIdent;
159 Runway_iterator it = mRunways.begin();
160 for (; it != mRunways.end(); ++it) {
161 if ((*it)->ident() == ident) {
169 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
173 Runway_iterator it = mRunways.begin();
174 FGRunway* result = NULL;
175 double currentBestQuality = 0.0;
177 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
178 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
179 double widthWeight = param->getDoubleValue("width-weight", 0.01);
180 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
181 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
183 for (; it != mRunways.end(); ++it) {
184 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
186 double dev = aHeading - (*it)->headingDeg();
187 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
188 double bad = fabs(deviationWeight * dev) + 1e-20;
189 double quality = good / bad;
191 if (quality > currentBestQuality) {
192 currentBestQuality = quality;
200 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
204 Runway_iterator it = mRunways.begin();
205 FGRunway* result = NULL;
206 double currentLowestDev = 180.0;
208 for (; it != mRunways.end(); ++it) {
209 double inboundCourse = SGGeodesy::courseDeg(aPos, (*it)->end());
210 double dev = inboundCourse - (*it)->headingDeg();
211 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
214 if (dev < currentLowestDev) { // new best match
215 currentLowestDev = dev;
218 } // of runway iteration
224 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
228 unsigned int numRunways(mRunways.size());
229 for (unsigned int r=0; r<numRunways; ++r) {
230 FGRunway* rwy = mRunways[r];
231 if (rwy->isReciprocal()) {
232 continue; // we only care about lengths, so don't do work twice
235 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
236 return true; // we're done!
238 } // of runways iteration
243 unsigned int FGAirport::numTaxiways() const
246 return mTaxiways.size();
249 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
252 assert(aIndex >= 0 && aIndex < mTaxiways.size());
253 return mTaxiways[aIndex];
256 unsigned int FGAirport::numPavements() const
259 return mPavements.size();
262 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
265 assert(aIndex >= 0 && aIndex < mPavements.size());
266 return mPavements[aIndex];
269 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
270 vector<FGTaxiwayPtr>& txwys,
271 vector<FGPavementPtr>& pvts)
274 Runway_iterator it = mRunways.begin();
275 for (; it != mRunways.end(); ++it) {
276 (*it)->setAirport(this);
279 mTaxiways.swap(txwys);
280 mPavements.swap(pvts);
283 FGRunway* FGAirport::getActiveRunwayForUsage() const
285 static FGEnvironmentMgr* envMgr = NULL;
287 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
290 // This forces West-facing rwys to be used in no-wind situations
291 // which is consistent with Flightgear's initial setup.
295 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
297 double windSpeed = stationWeather.get_wind_speed_kt();
298 if (windSpeed > 0.0) {
299 hdg = stationWeather.get_wind_from_heading_deg();
303 return findBestRunwayForHeading(hdg);
306 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
308 AirportFilter aptFilter;
309 if (filter == NULL) {
313 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
318 return static_cast<FGAirport*>(r.ptr());
321 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
322 mMinLengthFt(minLengthFt)
326 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
328 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
331 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
335 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
337 return NULL; // we don't warn here, let the caller do that
339 return static_cast<FGAirport*>(r.ptr());
342 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
346 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
348 throw sg_range_exception("No such airport with ident: " + aIdent);
350 return static_cast<FGAirport*>(r.ptr());
353 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
355 // we delegate all the work to a horrible helper in FGPositioned, which can
356 // access the (private) index data.
357 return searchAirportNamesAndIdents(aFilter);
360 // find basic airport location info from airport database
361 const FGAirport *fgFindAirportID( const string& id)
367 return FGAirport::findByIdent(id);
370 void FGAirport::loadRunways() const
372 if (mRunwaysLoaded) {
373 return; // already loaded, great
376 mRunwaysLoaded = true;
377 loadSceneryDefinitions();
380 void FGAirport::loadTaxiways() const
382 if (mTaxiwaysLoaded) {
383 return; // already loaded, great
387 void FGAirport::loadProcedures() const
389 if (mProceduresLoaded) {
393 mProceduresLoaded = true;
395 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
396 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
400 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
401 Route::loadAirportProcedures(path, const_cast<FGAirport*>(this));
404 void FGAirport::loadSceneryDefinitions() const
407 SGPropertyNode_ptr rootNode = new SGPropertyNode;
408 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
409 readProperties(path.str(), rootNode);
410 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
413 // repeat for the tower data
414 rootNode = new SGPropertyNode;
415 if (XMLLoader::findAirportData(ident(), "twr", path)) {
416 readProperties(path.str(), rootNode);
417 const_cast<FGAirport*>(this)->readTowerData(rootNode);
421 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
423 SGPropertyNode* runway;
425 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
426 SGPropertyNode* t0 = runway->getChild("threshold", 0),
427 *t1 = runway->getChild("threshold", 1);
429 assert(t1); // too strict? mayeb we should finally allow single-ended runways
431 processThreshold(t0);
432 processThreshold(t1);
433 } // of runways iteration
436 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
438 // first, let's identify the current runway
439 string id(aThreshold->getStringValue("rwy"));
440 if (!hasRunwayWithIdent(id)) {
441 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
442 "found runway not defined in the global data:" << ident() << "/" << id);
446 FGRunway* rwy = getRunwayByIdent(id);
447 rwy->processThreshold(aThreshold);
450 void FGAirport::readTowerData(SGPropertyNode* aRoot)
452 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
453 double lat = twrNode->getDoubleValue("lat"),
454 lon = twrNode->getDoubleValue("lon"),
455 elevM = twrNode->getDoubleValue("elev-m");
456 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
457 // scenery for a precise terrain elevation, we use the field elevation
458 // (this is also what the apt.dat code does)
459 double fieldElevationM = geod().getElevationM();
461 _tower_location = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
464 bool FGAirport::buildApproach(Waypt* aEnroute, STAR* aSTAR, FGRunway* aRwy, WayptVec& aRoute)
468 if ((aRwy && (aRwy->airport() != this))) {
469 throw sg_exception("invalid parameters", "FGAirport::buildApproach");
473 bool ok = aSTAR->route(aRwy, aEnroute, aRoute);
475 SG_LOG(SG_GENERAL, SG_WARN, ident() << ": build approach, STAR " << aSTAR->ident()
476 << " failed to route from transition " << aEnroute->ident());
479 } else if (aEnroute) {
480 // no a STAR specified, just use enroute point directly
481 aRoute.push_back(aEnroute);
485 // no runway selected yet, but we loaded the STAR, so that's fine, we're done
489 // build the approach (possibly including transition), and including the missed segment
490 vector<Approach*> aps;
491 for (unsigned int j=0; j<mApproaches.size();++j) {
492 if (mApproaches[j]->runway() == aRwy) {
493 aps.push_back(mApproaches[j]);
495 } // of approach filter by runway
498 SG_LOG(SG_GENERAL, SG_INFO, ident() << "; no approaches defined for runway " << aRwy->ident());
499 // could build a fallback approach here
503 for (unsigned int k=0; k<aps.size(); ++k) {
504 if (aps[k]->route(aRoute.back(), aRoute)) {
507 } // of initial approach iteration
509 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": unable to find transition to runway "
510 << aRwy->ident() << ", assume vectors");
512 WayptRef v(new ATCVectors(NULL, this));
514 return aps.front()->routeFromVectors(aRoute);
517 pair<flightgear::SID*, WayptRef>
518 FGAirport::selectSID(const SGGeod& aDest, FGRunway* aRwy)
523 flightgear::SID* sid = NULL;
526 for (unsigned int i=0; i<mSIDs.size(); ++i) {
527 if (aRwy && !mSIDs[i]->isForRunway(aRwy)) {
531 WayptRef e = mSIDs[i]->findBestTransition(aDest);
533 continue; // strange, but let's not worry about it
536 // assert(e->isFixedPosition());
537 double ed = SGGeodesy::distanceM(aDest, e->position());
538 if (ed < d) { // new best match
543 } // of SID iteration
545 if (!mSIDs.empty() && !sid) {
546 SG_LOG(SG_GENERAL, SG_INFO, ident() << "selectSID, no SID found (runway="
547 << (aRwy ? aRwy->ident() : "no runway preference"));
550 return std::make_pair(sid, enroute);
553 pair<STAR*, WayptRef>
554 FGAirport::selectSTAR(const SGGeod& aOrigin, FGRunway* aRwy)
562 for (unsigned int i=0; i<mSTARs.size(); ++i) {
563 if (!mSTARs[i]->isForRunway(aRwy)) {
567 SG_LOG(SG_GENERAL, SG_INFO, "STAR " << mSTARs[i]->ident() << " is valid for runway");
568 WayptRef e = mSTARs[i]->findBestTransition(aOrigin);
570 continue; // strange, but let's not worry about it
573 // assert(e->isFixedPosition());
574 double ed = SGGeodesy::distanceM(aOrigin, e->position());
575 if (ed < d) { // new best match
580 } // of STAR iteration
582 return std::make_pair(star, enroute);
586 void FGAirport::addSID(flightgear::SID* aSid)
588 mSIDs.push_back(aSid);
591 void FGAirport::addSTAR(STAR* aStar)
593 mSTARs.push_back(aStar);
596 void FGAirport::addApproach(Approach* aApp)
598 mApproaches.push_back(aApp);
601 unsigned int FGAirport::numSIDs() const
607 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
610 return mSIDs[aIndex];
613 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
616 for (unsigned int i=0; i<mSIDs.size(); ++i) {
617 if (mSIDs[i]->ident() == aIdent) {
625 unsigned int FGAirport::numSTARs() const
628 return mSTARs.size();
631 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
634 return mSTARs[aIndex];
637 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
640 for (unsigned int i=0; i<mSTARs.size(); ++i) {
641 if (mSTARs[i]->ident() == aIdent) {
649 unsigned int FGAirport::numApproaches() const
652 return mApproaches.size();
655 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
658 return mApproaches[aIndex];
661 class AirportNodeListener : public SGPropertyChangeListener
664 AirportNodeListener()
666 SGPropertyNode* airports = fgGetNode("/sim/airport");
667 airports->addChangeListener(this, false);
670 virtual void valueChanged(SGPropertyNode*)
674 virtual void childAdded(SGPropertyNode* pr, SGPropertyNode* child)
676 FGAirport* apt = FGAirport::findByIdent(child->getName());
681 flightgear::PositionedBinding::bind(apt, child);
685 void FGAirport::installPropertyListener()
687 new AirportNodeListener;
690 flightgear::PositionedBinding*
691 FGAirport::createBinding(SGPropertyNode* nd) const
693 return new flightgear::AirportBinding(this, nd);
696 void FGAirport::setCommStations(CommStationList& comms)
698 mCommStations.swap(comms);
699 for (unsigned int c=0; c<mCommStations.size(); ++c) {
700 mCommStations[c]->setAirport(this);
705 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
707 CommStationList result;
708 for (unsigned int c=0; c<mCommStations.size(); ++c) {
709 if (mCommStations[c]->type() == aTy) {
710 result.push_back(mCommStations[c]);
716 // get airport elevation
717 double fgGetAirportElev( const string& id )
719 const FGAirport *a=fgFindAirportID( id);
721 return a->getElevation();
728 // get airport position
729 SGGeod fgGetAirportPos( const string& id )
731 const FGAirport *a = fgFindAirportID( id);
734 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
736 return SGGeod::fromDegM(0.0, 0.0, -9999.0);