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()
79 cerr << "Deleting Airport" << endl;
83 bool FGAirport::isAirport() const
85 return type() == AIRPORT;
88 bool FGAirport::isSeaport() const
90 return type() == SEAPORT;
93 bool FGAirport::isHeliport() const
95 return type() == HELIPORT;
98 FGAirportDynamics * FGAirport::getDynamics()
104 _dynamics = new FGAirportDynamics(this);
105 XMLLoader::load(_dynamics);
107 FGRunwayPreference rwyPrefs(this);
108 XMLLoader::load(&rwyPrefs);
109 _dynamics->setRwyUse(rwyPrefs);
110 XMLLoader::load(_dynamics->getSIDs());
115 unsigned int FGAirport::numRunways() const
118 return mRunways.size();
121 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
125 assert(aIndex >= 0 && aIndex < mRunways.size());
126 return mRunways[aIndex];
129 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
131 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
134 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
136 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
137 if (it == mRunways.end()) {
138 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
139 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
145 FGAirport::Runway_iterator
146 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
149 return mRunways.end();
153 string ident(aIdent);
154 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
155 ident = "0" + aIdent;
158 Runway_iterator it = mRunways.begin();
159 for (; it != mRunways.end(); ++it) {
160 if ((*it)->ident() == ident) {
168 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
172 Runway_iterator it = mRunways.begin();
173 FGRunway* result = NULL;
174 double currentBestQuality = 0.0;
176 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
177 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
178 double widthWeight = param->getDoubleValue("width-weight", 0.01);
179 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
180 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
182 for (; it != mRunways.end(); ++it) {
183 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
185 double dev = aHeading - (*it)->headingDeg();
186 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
187 double bad = fabs(deviationWeight * dev) + 1e-20;
188 double quality = good / bad;
190 if (quality > currentBestQuality) {
191 currentBestQuality = quality;
199 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
203 Runway_iterator it = mRunways.begin();
204 FGRunway* result = NULL;
205 double currentLowestDev = 180.0;
207 for (; it != mRunways.end(); ++it) {
208 double inboundCourse = SGGeodesy::courseDeg(aPos, (*it)->end());
209 double dev = inboundCourse - (*it)->headingDeg();
210 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
213 if (dev < currentLowestDev) { // new best match
214 currentLowestDev = dev;
217 } // of runway iteration
223 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
227 unsigned int numRunways(mRunways.size());
228 for (unsigned int r=0; r<numRunways; ++r) {
229 FGRunway* rwy = mRunways[r];
230 if (rwy->isReciprocal()) {
231 continue; // we only care about lengths, so don't do work twice
234 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
235 return true; // we're done!
237 } // of runways iteration
242 unsigned int FGAirport::numTaxiways() const
245 return mTaxiways.size();
248 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
251 assert(aIndex >= 0 && aIndex < mTaxiways.size());
252 return mTaxiways[aIndex];
255 unsigned int FGAirport::numPavements() const
258 return mPavements.size();
261 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
264 assert(aIndex >= 0 && aIndex < mPavements.size());
265 return mPavements[aIndex];
268 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
269 vector<FGTaxiwayPtr>& txwys,
270 vector<FGPavementPtr>& pvts)
273 Runway_iterator it = mRunways.begin();
274 for (; it != mRunways.end(); ++it) {
275 (*it)->setAirport(this);
278 mTaxiways.swap(txwys);
279 mPavements.swap(pvts);
282 FGRunway* FGAirport::getActiveRunwayForUsage() const
284 static FGEnvironmentMgr* envMgr = NULL;
286 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
289 // This forces West-facing rwys to be used in no-wind situations
290 // which is consistent with Flightgear's initial setup.
294 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
296 double windSpeed = stationWeather.get_wind_speed_kt();
297 if (windSpeed > 0.0) {
298 hdg = stationWeather.get_wind_from_heading_deg();
302 return findBestRunwayForHeading(hdg);
305 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
307 AirportFilter aptFilter;
308 if (filter == NULL) {
312 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
317 return static_cast<FGAirport*>(r.ptr());
320 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
321 mMinLengthFt(minLengthFt)
325 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
327 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
330 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
334 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
336 return NULL; // we don't warn here, let the caller do that
338 return static_cast<FGAirport*>(r.ptr());
341 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
345 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
347 throw sg_range_exception("No such airport with ident: " + aIdent);
349 return static_cast<FGAirport*>(r.ptr());
352 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
354 // we delegate all the work to a horrible helper in FGPositioned, which can
355 // access the (private) index data.
356 return searchAirportNamesAndIdents(aFilter);
359 // find basic airport location info from airport database
360 const FGAirport *fgFindAirportID( const string& id)
366 return FGAirport::findByIdent(id);
369 void FGAirport::loadRunways() const
371 if (mRunwaysLoaded) {
372 return; // already loaded, great
375 mRunwaysLoaded = true;
376 loadSceneryDefinitions();
379 void FGAirport::loadTaxiways() const
381 if (mTaxiwaysLoaded) {
382 return; // already loaded, great
386 void FGAirport::loadProcedures() const
388 if (mProceduresLoaded) {
392 mProceduresLoaded = true;
394 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
395 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
399 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
400 Route::loadAirportProcedures(path, const_cast<FGAirport*>(this));
403 void FGAirport::loadSceneryDefinitions() const
405 // allow users to disable the scenery data in the short-term
406 // longer term, this option can probably disappear
407 if (!fgGetBool("/sim/paths/use-custom-scenery-data")) {
412 SGPropertyNode_ptr rootNode = new SGPropertyNode;
413 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
414 readProperties(path.str(), rootNode);
415 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
418 // repeat for the tower data
419 rootNode = new SGPropertyNode;
420 if (XMLLoader::findAirportData(ident(), "twr", path)) {
421 readProperties(path.str(), rootNode);
422 const_cast<FGAirport*>(this)->readTowerData(rootNode);
426 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
428 SGPropertyNode* runway;
430 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
431 SGPropertyNode* t0 = runway->getChild("threshold", 0),
432 *t1 = runway->getChild("threshold", 1);
434 assert(t1); // too strict? mayeb we should finally allow single-ended runways
436 processThreshold(t0);
437 processThreshold(t1);
438 } // of runways iteration
441 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
443 // first, let's identify the current runway
444 string id(aThreshold->getStringValue("rwy"));
445 if (!hasRunwayWithIdent(id)) {
446 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
447 "found runway not defined in the global data:" << ident() << "/" << id);
451 FGRunway* rwy = getRunwayByIdent(id);
452 rwy->processThreshold(aThreshold);
455 void FGAirport::readTowerData(SGPropertyNode* aRoot)
457 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
458 double lat = twrNode->getDoubleValue("lat"),
459 lon = twrNode->getDoubleValue("lon"),
460 elevM = twrNode->getDoubleValue("elev-m");
461 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
462 // scenery for a precise terrain elevation, we use the field elevation
463 // (this is also what the apt.dat code does)
464 double fieldElevationM = geod().getElevationM();
466 _tower_location = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
469 bool FGAirport::buildApproach(Waypt* aEnroute, STAR* aSTAR, FGRunway* aRwy, WayptVec& aRoute)
473 if ((aRwy && (aRwy->airport() != this))) {
474 throw sg_exception("invalid parameters", "FGAirport::buildApproach");
478 bool ok = aSTAR->route(aRwy, aEnroute, aRoute);
480 SG_LOG(SG_GENERAL, SG_WARN, ident() << ": build approach, STAR " << aSTAR->ident()
481 << " failed to route from transition " << aEnroute->ident());
484 } else if (aEnroute) {
485 // no a STAR specified, just use enroute point directly
486 aRoute.push_back(aEnroute);
490 // no runway selected yet, but we loaded the STAR, so that's fine, we're done
494 // build the approach (possibly including transition), and including the missed segment
495 vector<Approach*> aps;
496 for (unsigned int j=0; j<mApproaches.size();++j) {
497 if (mApproaches[j]->runway() == aRwy) {
498 aps.push_back(mApproaches[j]);
500 } // of approach filter by runway
503 SG_LOG(SG_GENERAL, SG_INFO, ident() << "; no approaches defined for runway " << aRwy->ident());
504 // could build a fallback approach here
508 for (unsigned int k=0; k<aps.size(); ++k) {
509 if (aps[k]->route(aRoute.back(), aRoute)) {
512 } // of initial approach iteration
514 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": unable to find transition to runway "
515 << aRwy->ident() << ", assume vectors");
517 WayptRef v(new ATCVectors(NULL, this));
519 return aps.front()->routeFromVectors(aRoute);
522 pair<flightgear::SID*, WayptRef>
523 FGAirport::selectSID(const SGGeod& aDest, FGRunway* aRwy)
528 flightgear::SID* sid = NULL;
531 for (unsigned int i=0; i<mSIDs.size(); ++i) {
532 if (aRwy && !mSIDs[i]->isForRunway(aRwy)) {
536 WayptRef e = mSIDs[i]->findBestTransition(aDest);
538 continue; // strange, but let's not worry about it
541 // assert(e->isFixedPosition());
542 double ed = SGGeodesy::distanceM(aDest, e->position());
543 if (ed < d) { // new best match
548 } // of SID iteration
550 if (!mSIDs.empty() && !sid) {
551 SG_LOG(SG_GENERAL, SG_INFO, ident() << "selectSID, no SID found (runway="
552 << (aRwy ? aRwy->ident() : "no runway preference"));
555 return make_pair(sid, enroute);
558 pair<STAR*, WayptRef>
559 FGAirport::selectSTAR(const SGGeod& aOrigin, FGRunway* aRwy)
567 for (unsigned int i=0; i<mSTARs.size(); ++i) {
568 if (!mSTARs[i]->isForRunway(aRwy)) {
572 SG_LOG(SG_GENERAL, SG_INFO, "STAR " << mSTARs[i]->ident() << " is valid for runway");
573 WayptRef e = mSTARs[i]->findBestTransition(aOrigin);
575 continue; // strange, but let's not worry about it
578 // assert(e->isFixedPosition());
579 double ed = SGGeodesy::distanceM(aOrigin, e->position());
580 if (ed < d) { // new best match
585 } // of STAR iteration
587 return make_pair(star, enroute);
591 void FGAirport::addSID(flightgear::SID* aSid)
593 mSIDs.push_back(aSid);
596 void FGAirport::addSTAR(STAR* aStar)
598 mSTARs.push_back(aStar);
601 void FGAirport::addApproach(Approach* aApp)
603 mApproaches.push_back(aApp);
606 unsigned int FGAirport::numSIDs() const
612 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
615 return mSIDs[aIndex];
618 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
621 for (unsigned int i=0; i<mSIDs.size(); ++i) {
622 if (mSIDs[i]->ident() == aIdent) {
630 unsigned int FGAirport::numSTARs() const
633 return mSTARs.size();
636 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
639 return mSTARs[aIndex];
642 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
645 for (unsigned int i=0; i<mSTARs.size(); ++i) {
646 if (mSTARs[i]->ident() == aIdent) {
654 unsigned int FGAirport::numApproaches() const
657 return mApproaches.size();
660 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
663 return mApproaches[aIndex];
666 class AirportNodeListener : public SGPropertyChangeListener
669 AirportNodeListener()
671 SGPropertyNode* airports = fgGetNode("/sim/airport");
672 airports->addChangeListener(this, false);
675 virtual void valueChanged(SGPropertyNode*)
679 virtual void childAdded(SGPropertyNode* pr, SGPropertyNode* child)
681 FGAirport* apt = FGAirport::findByIdent(child->getName());
686 flightgear::PositionedBinding::bind(apt, child);
690 void FGAirport::installPropertyListener()
692 new AirportNodeListener;
695 flightgear::PositionedBinding*
696 FGAirport::createBinding(SGPropertyNode* nd) const
698 return new flightgear::AirportBinding(this, nd);
701 void FGAirport::setCommStations(CommStationList& comms)
703 mCommStations.swap(comms);
704 for (unsigned int c=0; c<mCommStations.size(); ++c) {
705 mCommStations[c]->setAirport(this);
710 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
712 CommStationList result;
713 for (unsigned int c=0; c<mCommStations.size(); ++c) {
714 if (mCommStations[c]->type() == aTy) {
715 result.push_back(mCommStations[c]);
721 // get airport elevation
722 double fgGetAirportElev( const string& id )
724 const FGAirport *a=fgFindAirportID( id);
726 return a->getElevation();
733 // get airport position
734 SGGeod fgGetAirportPos( const string& id )
736 const FGAirport *a = fgFindAirportID( id);
739 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
741 return SGGeod::fromDegM(0.0, 0.0, -9999.0);