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>
54 using namespace flightgear;
56 // magic import of a helper which uses FGPositioned internals
57 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
59 /***************************************************************************
61 ***************************************************************************/
63 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
64 const string &name, bool has_metar, Type aType) :
65 FGPositioned(aType, id, location),
66 _tower_location(tower_location),
68 _has_metar(has_metar),
70 mRunwaysLoaded(false),
73 init(true); // init FGPositioned
77 FGAirport::~FGAirport()
82 bool FGAirport::isAirport() const
84 return type() == AIRPORT;
87 bool FGAirport::isSeaport() const
89 return type() == SEAPORT;
92 bool FGAirport::isHeliport() const
94 return type() == HELIPORT;
97 FGAirportDynamics * FGAirport::getDynamics()
103 _dynamics = new FGAirportDynamics(this);
104 XMLLoader::load(_dynamics);
106 FGRunwayPreference rwyPrefs(this);
107 XMLLoader::load(&rwyPrefs);
108 _dynamics->setRwyUse(rwyPrefs);
109 XMLLoader::load(_dynamics->getSIDs());
114 unsigned int FGAirport::numRunways() const
117 return mRunways.size();
120 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
124 assert(aIndex >= 0 && aIndex < mRunways.size());
125 return mRunways[aIndex];
128 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
130 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
133 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
135 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
136 if (it == mRunways.end()) {
137 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
138 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
144 FGAirport::Runway_iterator
145 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
148 return mRunways.end();
152 string ident(aIdent);
153 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
154 ident = "0" + aIdent;
157 Runway_iterator it = mRunways.begin();
158 for (; it != mRunways.end(); ++it) {
159 if ((*it)->ident() == ident) {
167 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
171 Runway_iterator it = mRunways.begin();
172 FGRunway* result = NULL;
173 double currentBestQuality = 0.0;
175 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
176 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
177 double widthWeight = param->getDoubleValue("width-weight", 0.01);
178 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
179 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
181 for (; it != mRunways.end(); ++it) {
182 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
184 double dev = aHeading - (*it)->headingDeg();
185 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
186 double bad = fabs(deviationWeight * dev) + 1e-20;
187 double quality = good / bad;
189 if (quality > currentBestQuality) {
190 currentBestQuality = quality;
198 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
202 Runway_iterator it = mRunways.begin();
203 FGRunway* result = NULL;
204 double currentLowestDev = 180.0;
206 for (; it != mRunways.end(); ++it) {
207 double inboundCourse = SGGeodesy::courseDeg(aPos, (*it)->end());
208 double dev = inboundCourse - (*it)->headingDeg();
209 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
212 if (dev < currentLowestDev) { // new best match
213 currentLowestDev = dev;
216 } // of runway iteration
222 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
226 unsigned int numRunways(mRunways.size());
227 for (unsigned int r=0; r<numRunways; ++r) {
228 FGRunway* rwy = mRunways[r];
229 if (rwy->isReciprocal()) {
230 continue; // we only care about lengths, so don't do work twice
233 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
234 return true; // we're done!
236 } // of runways iteration
241 unsigned int FGAirport::numTaxiways() const
244 return mTaxiways.size();
247 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
250 assert(aIndex >= 0 && aIndex < mTaxiways.size());
251 return mTaxiways[aIndex];
254 unsigned int FGAirport::numPavements() const
257 return mPavements.size();
260 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
263 assert(aIndex >= 0 && aIndex < mPavements.size());
264 return mPavements[aIndex];
267 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
268 vector<FGTaxiwayPtr>& txwys,
269 vector<FGPavementPtr>& pvts)
272 Runway_iterator it = mRunways.begin();
273 for (; it != mRunways.end(); ++it) {
274 (*it)->setAirport(this);
277 mTaxiways.swap(txwys);
278 mPavements.swap(pvts);
281 FGRunway* FGAirport::getActiveRunwayForUsage() const
283 static FGEnvironmentMgr* envMgr = NULL;
285 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
288 // This forces West-facing rwys to be used in no-wind situations
289 // which is consistent with Flightgear's initial setup.
293 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
295 double windSpeed = stationWeather.get_wind_speed_kt();
296 if (windSpeed > 0.0) {
297 hdg = stationWeather.get_wind_from_heading_deg();
301 return findBestRunwayForHeading(hdg);
304 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
306 AirportFilter aptFilter;
307 if (filter == NULL) {
311 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
316 return static_cast<FGAirport*>(r.ptr());
319 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
320 mMinLengthFt(minLengthFt)
324 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
326 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
329 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
333 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
335 return NULL; // we don't warn here, let the caller do that
337 return static_cast<FGAirport*>(r.ptr());
340 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
344 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
346 throw sg_range_exception("No such airport with ident: " + aIdent);
348 return static_cast<FGAirport*>(r.ptr());
351 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
353 // we delegate all the work to a horrible helper in FGPositioned, which can
354 // access the (private) index data.
355 return searchAirportNamesAndIdents(aFilter);
358 // find basic airport location info from airport database
359 const FGAirport *fgFindAirportID( const string& id)
365 return FGAirport::findByIdent(id);
368 void FGAirport::loadRunways() const
370 if (mRunwaysLoaded) {
371 return; // already loaded, great
374 mRunwaysLoaded = true;
375 loadSceneryDefinitions();
378 void FGAirport::loadTaxiways() const
380 if (mTaxiwaysLoaded) {
381 return; // already loaded, great
385 void FGAirport::loadProcedures() const
387 if (mProceduresLoaded) {
391 mProceduresLoaded = true;
393 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
394 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
398 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
399 Route::loadAirportProcedures(path, const_cast<FGAirport*>(this));
402 void FGAirport::loadSceneryDefinitions() const
404 // allow users to disable the scenery data in the short-term
405 // longer term, this option can probably disappear
406 if (!fgGetBool("/sim/paths/use-custom-scenery-data")) {
411 SGPropertyNode_ptr rootNode = new SGPropertyNode;
412 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
413 readProperties(path.str(), rootNode);
414 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
417 // repeat for the tower data
418 rootNode = new SGPropertyNode;
419 if (XMLLoader::findAirportData(ident(), "twr", path)) {
420 readProperties(path.str(), rootNode);
421 const_cast<FGAirport*>(this)->readTowerData(rootNode);
425 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
427 SGPropertyNode* runway;
429 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
430 SGPropertyNode* t0 = runway->getChild("threshold", 0),
431 *t1 = runway->getChild("threshold", 1);
433 assert(t1); // too strict? mayeb we should finally allow single-ended runways
435 processThreshold(t0);
436 processThreshold(t1);
437 } // of runways iteration
440 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
442 // first, let's identify the current runway
443 string id(aThreshold->getStringValue("rwy"));
444 if (!hasRunwayWithIdent(id)) {
445 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
446 "found runway not defined in the global data:" << ident() << "/" << id);
450 FGRunway* rwy = getRunwayByIdent(id);
451 rwy->processThreshold(aThreshold);
454 void FGAirport::readTowerData(SGPropertyNode* aRoot)
456 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
457 double lat = twrNode->getDoubleValue("lat"),
458 lon = twrNode->getDoubleValue("lon"),
459 elevM = twrNode->getDoubleValue("elev-m");
460 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
461 // scenery for a precise terrain elevation, we use the field elevation
462 // (this is also what the apt.dat code does)
463 double fieldElevationM = geod().getElevationM();
465 _tower_location = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
468 bool FGAirport::buildApproach(Waypt* aEnroute, STAR* aSTAR, FGRunway* aRwy, WayptVec& aRoute)
472 if ((aRwy && (aRwy->airport() != this))) {
473 throw sg_exception("invalid parameters", "FGAirport::buildApproach");
477 bool ok = aSTAR->route(aRwy, aEnroute, aRoute);
479 SG_LOG(SG_GENERAL, SG_WARN, ident() << ": build approach, STAR " << aSTAR->ident()
480 << " failed to route from transition " << aEnroute->ident());
483 } else if (aEnroute) {
484 // no a STAR specified, just use enroute point directly
485 aRoute.push_back(aEnroute);
489 // no runway selected yet, but we loaded the STAR, so that's fine, we're done
493 // build the approach (possibly including transition), and including the missed segment
494 vector<Approach*> aps;
495 for (unsigned int j=0; j<mApproaches.size();++j) {
496 if (mApproaches[j]->runway() == aRwy) {
497 aps.push_back(mApproaches[j]);
499 } // of approach filter by runway
502 SG_LOG(SG_GENERAL, SG_INFO, ident() << "; no approaches defined for runway " << aRwy->ident());
503 // could build a fallback approach here
507 for (unsigned int k=0; k<aps.size(); ++k) {
508 if (aps[k]->route(aRoute.back(), aRoute)) {
511 } // of initial approach iteration
513 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": unable to find transition to runway "
514 << aRwy->ident() << ", assume vectors");
516 WayptRef v(new ATCVectors(NULL, this));
518 return aps.front()->routeFromVectors(aRoute);
521 pair<flightgear::SID*, WayptRef>
522 FGAirport::selectSID(const SGGeod& aDest, FGRunway* aRwy)
527 flightgear::SID* sid = NULL;
530 for (unsigned int i=0; i<mSIDs.size(); ++i) {
531 if (aRwy && !mSIDs[i]->isForRunway(aRwy)) {
535 WayptRef e = mSIDs[i]->findBestTransition(aDest);
537 continue; // strange, but let's not worry about it
540 // assert(e->isFixedPosition());
541 double ed = SGGeodesy::distanceM(aDest, e->position());
542 if (ed < d) { // new best match
547 } // of SID iteration
549 if (!mSIDs.empty() && !sid) {
550 SG_LOG(SG_GENERAL, SG_INFO, ident() << "selectSID, no SID found (runway="
551 << (aRwy ? aRwy->ident() : "no runway preference"));
554 return make_pair(sid, enroute);
557 pair<STAR*, WayptRef>
558 FGAirport::selectSTAR(const SGGeod& aOrigin, FGRunway* aRwy)
566 for (unsigned int i=0; i<mSTARs.size(); ++i) {
567 if (!mSTARs[i]->isForRunway(aRwy)) {
571 SG_LOG(SG_GENERAL, SG_INFO, "STAR " << mSTARs[i]->ident() << " is valid for runway");
572 WayptRef e = mSTARs[i]->findBestTransition(aOrigin);
574 continue; // strange, but let's not worry about it
577 // assert(e->isFixedPosition());
578 double ed = SGGeodesy::distanceM(aOrigin, e->position());
579 if (ed < d) { // new best match
584 } // of STAR iteration
586 return make_pair(star, enroute);
590 void FGAirport::addSID(flightgear::SID* aSid)
592 mSIDs.push_back(aSid);
595 void FGAirport::addSTAR(STAR* aStar)
597 mSTARs.push_back(aStar);
600 void FGAirport::addApproach(Approach* aApp)
602 mApproaches.push_back(aApp);
605 unsigned int FGAirport::numSIDs() const
611 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
614 return mSIDs[aIndex];
617 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
620 for (unsigned int i=0; i<mSIDs.size(); ++i) {
621 if (mSIDs[i]->ident() == aIdent) {
629 unsigned int FGAirport::numSTARs() const
632 return mSTARs.size();
635 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
638 return mSTARs[aIndex];
641 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
644 for (unsigned int i=0; i<mSTARs.size(); ++i) {
645 if (mSTARs[i]->ident() == aIdent) {
653 unsigned int FGAirport::numApproaches() const
656 return mApproaches.size();
659 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
662 return mApproaches[aIndex];
665 class AirportNodeListener : public SGPropertyChangeListener
668 AirportNodeListener()
670 SGPropertyNode* airports = fgGetNode("/sim/airport");
671 airports->addChangeListener(this, false);
674 virtual void valueChanged(SGPropertyNode*)
678 virtual void childAdded(SGPropertyNode* pr, SGPropertyNode* child)
680 FGAirport* apt = FGAirport::findByIdent(child->getName());
685 flightgear::PositionedBinding::bind(apt, child);
689 void FGAirport::installPropertyListener()
691 new AirportNodeListener;
694 flightgear::PositionedBinding*
695 FGAirport::createBinding(SGPropertyNode* nd) const
697 return new flightgear::AirportBinding(this, nd);
700 void FGAirport::setCommStations(CommStationList& comms)
702 mCommStations.swap(comms);
703 for (unsigned int c=0; c<mCommStations.size(); ++c) {
704 mCommStations[c]->setAirport(this);
709 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
711 CommStationList result;
712 for (unsigned int c=0; c<mCommStations.size(); ++c) {
713 if (mCommStations[c]->type() == aTy) {
714 result.push_back(mCommStations[c]);
720 // get airport elevation
721 double fgGetAirportElev( const string& id )
723 const FGAirport *a=fgFindAirportID( id);
725 return a->getElevation();
732 // get airport position
733 SGGeod fgGetAirportPos( const string& id )
735 const FGAirport *a = fgFindAirportID( id);
738 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
740 return SGGeod::fromDegM(0.0, 0.0, -9999.0);