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
406 // allow users to disable the scenery data in the short-term
407 // longer term, this option can probably disappear
408 if (!fgGetBool("/sim/paths/use-custom-scenery-data")) {
413 SGPropertyNode_ptr rootNode = new SGPropertyNode;
414 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
415 readProperties(path.str(), rootNode);
416 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
419 // repeat for the tower data
420 rootNode = new SGPropertyNode;
421 if (XMLLoader::findAirportData(ident(), "twr", path)) {
422 readProperties(path.str(), rootNode);
423 const_cast<FGAirport*>(this)->readTowerData(rootNode);
427 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
429 SGPropertyNode* runway;
431 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
432 SGPropertyNode* t0 = runway->getChild("threshold", 0),
433 *t1 = runway->getChild("threshold", 1);
435 assert(t1); // too strict? mayeb we should finally allow single-ended runways
437 processThreshold(t0);
438 processThreshold(t1);
439 } // of runways iteration
442 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
444 // first, let's identify the current runway
445 string id(aThreshold->getStringValue("rwy"));
446 if (!hasRunwayWithIdent(id)) {
447 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
448 "found runway not defined in the global data:" << ident() << "/" << id);
452 FGRunway* rwy = getRunwayByIdent(id);
453 rwy->processThreshold(aThreshold);
456 void FGAirport::readTowerData(SGPropertyNode* aRoot)
458 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
459 double lat = twrNode->getDoubleValue("lat"),
460 lon = twrNode->getDoubleValue("lon"),
461 elevM = twrNode->getDoubleValue("elev-m");
462 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
463 // scenery for a precise terrain elevation, we use the field elevation
464 // (this is also what the apt.dat code does)
465 double fieldElevationM = geod().getElevationM();
467 _tower_location = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
470 bool FGAirport::buildApproach(Waypt* aEnroute, STAR* aSTAR, FGRunway* aRwy, WayptVec& aRoute)
474 if ((aRwy && (aRwy->airport() != this))) {
475 throw sg_exception("invalid parameters", "FGAirport::buildApproach");
479 bool ok = aSTAR->route(aRwy, aEnroute, aRoute);
481 SG_LOG(SG_GENERAL, SG_WARN, ident() << ": build approach, STAR " << aSTAR->ident()
482 << " failed to route from transition " << aEnroute->ident());
485 } else if (aEnroute) {
486 // no a STAR specified, just use enroute point directly
487 aRoute.push_back(aEnroute);
491 // no runway selected yet, but we loaded the STAR, so that's fine, we're done
495 // build the approach (possibly including transition), and including the missed segment
496 vector<Approach*> aps;
497 for (unsigned int j=0; j<mApproaches.size();++j) {
498 if (mApproaches[j]->runway() == aRwy) {
499 aps.push_back(mApproaches[j]);
501 } // of approach filter by runway
504 SG_LOG(SG_GENERAL, SG_INFO, ident() << "; no approaches defined for runway " << aRwy->ident());
505 // could build a fallback approach here
509 for (unsigned int k=0; k<aps.size(); ++k) {
510 if (aps[k]->route(aRoute.back(), aRoute)) {
513 } // of initial approach iteration
515 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": unable to find transition to runway "
516 << aRwy->ident() << ", assume vectors");
518 WayptRef v(new ATCVectors(NULL, this));
520 return aps.front()->routeFromVectors(aRoute);
523 pair<flightgear::SID*, WayptRef>
524 FGAirport::selectSID(const SGGeod& aDest, FGRunway* aRwy)
529 flightgear::SID* sid = NULL;
532 for (unsigned int i=0; i<mSIDs.size(); ++i) {
533 if (aRwy && !mSIDs[i]->isForRunway(aRwy)) {
537 WayptRef e = mSIDs[i]->findBestTransition(aDest);
539 continue; // strange, but let's not worry about it
542 // assert(e->isFixedPosition());
543 double ed = SGGeodesy::distanceM(aDest, e->position());
544 if (ed < d) { // new best match
549 } // of SID iteration
551 if (!mSIDs.empty() && !sid) {
552 SG_LOG(SG_GENERAL, SG_INFO, ident() << "selectSID, no SID found (runway="
553 << (aRwy ? aRwy->ident() : "no runway preference"));
556 return std::make_pair(sid, enroute);
559 pair<STAR*, WayptRef>
560 FGAirport::selectSTAR(const SGGeod& aOrigin, FGRunway* aRwy)
568 for (unsigned int i=0; i<mSTARs.size(); ++i) {
569 if (!mSTARs[i]->isForRunway(aRwy)) {
573 SG_LOG(SG_GENERAL, SG_INFO, "STAR " << mSTARs[i]->ident() << " is valid for runway");
574 WayptRef e = mSTARs[i]->findBestTransition(aOrigin);
576 continue; // strange, but let's not worry about it
579 // assert(e->isFixedPosition());
580 double ed = SGGeodesy::distanceM(aOrigin, e->position());
581 if (ed < d) { // new best match
586 } // of STAR iteration
588 return std::make_pair(star, enroute);
592 void FGAirport::addSID(flightgear::SID* aSid)
594 mSIDs.push_back(aSid);
597 void FGAirport::addSTAR(STAR* aStar)
599 mSTARs.push_back(aStar);
602 void FGAirport::addApproach(Approach* aApp)
604 mApproaches.push_back(aApp);
607 unsigned int FGAirport::numSIDs() const
613 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
616 return mSIDs[aIndex];
619 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
622 for (unsigned int i=0; i<mSIDs.size(); ++i) {
623 if (mSIDs[i]->ident() == aIdent) {
631 unsigned int FGAirport::numSTARs() const
634 return mSTARs.size();
637 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
640 return mSTARs[aIndex];
643 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
646 for (unsigned int i=0; i<mSTARs.size(); ++i) {
647 if (mSTARs[i]->ident() == aIdent) {
655 unsigned int FGAirport::numApproaches() const
658 return mApproaches.size();
661 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
664 return mApproaches[aIndex];
667 class AirportNodeListener : public SGPropertyChangeListener
670 AirportNodeListener()
672 SGPropertyNode* airports = fgGetNode("/sim/airport");
673 airports->addChangeListener(this, false);
676 virtual void valueChanged(SGPropertyNode*)
680 virtual void childAdded(SGPropertyNode* pr, SGPropertyNode* child)
682 FGAirport* apt = FGAirport::findByIdent(child->getName());
687 flightgear::PositionedBinding::bind(apt, child);
691 void FGAirport::installPropertyListener()
693 new AirportNodeListener;
696 flightgear::PositionedBinding*
697 FGAirport::createBinding(SGPropertyNode* nd) const
699 return new flightgear::AirportBinding(this, nd);
702 void FGAirport::setCommStations(CommStationList& comms)
704 mCommStations.swap(comms);
705 for (unsigned int c=0; c<mCommStations.size(); ++c) {
706 mCommStations[c]->setAirport(this);
711 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
713 CommStationList result;
714 for (unsigned int c=0; c<mCommStations.size(); ++c) {
715 if (mCommStations[c]->type() == aTy) {
716 result.push_back(mCommStations[c]);
722 // get airport elevation
723 double fgGetAirportElev( const string& id )
725 const FGAirport *a=fgFindAirportID( id);
727 return a->getElevation();
734 // get airport position
735 SGGeod fgGetAirportPos( const string& id )
737 const FGAirport *a = fgFindAirportID( id);
740 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
742 return SGGeod::fromDegM(0.0, 0.0, -9999.0);