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 bool FGAirport::isAirportType(FGPositioned* pos)
105 return (pos->type() >= AIRPORT) && (pos->type() <= SEAPORT);
108 FGAirportDynamics * FGAirport::getDynamics()
114 _dynamics = new FGAirportDynamics(this);
115 XMLLoader::load(_dynamics);
117 FGRunwayPreference rwyPrefs(this);
118 XMLLoader::load(&rwyPrefs);
119 _dynamics->setRwyUse(rwyPrefs);
120 XMLLoader::load(_dynamics->getSIDs());
125 unsigned int FGAirport::numRunways() const
128 return mRunways.size();
131 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
135 assert(aIndex >= 0 && aIndex < mRunways.size());
136 return mRunways[aIndex];
139 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
141 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
144 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
146 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
147 if (it == mRunways.end()) {
148 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
149 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
155 FGAirport::Runway_iterator
156 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
159 return mRunways.end();
163 string ident(aIdent);
164 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
165 ident = "0" + aIdent;
168 Runway_iterator it = mRunways.begin();
169 for (; it != mRunways.end(); ++it) {
170 if ((*it)->ident() == ident) {
178 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
182 Runway_iterator it = mRunways.begin();
183 FGRunway* result = NULL;
184 double currentBestQuality = 0.0;
186 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
187 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
188 double widthWeight = param->getDoubleValue("width-weight", 0.01);
189 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
190 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
192 for (; it != mRunways.end(); ++it) {
193 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
195 double dev = aHeading - (*it)->headingDeg();
196 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
197 double bad = fabs(deviationWeight * dev) + 1e-20;
198 double quality = good / bad;
200 if (quality > currentBestQuality) {
201 currentBestQuality = quality;
209 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
213 Runway_iterator it = mRunways.begin();
214 FGRunway* result = NULL;
215 double currentLowestDev = 180.0;
217 for (; it != mRunways.end(); ++it) {
218 double inboundCourse = SGGeodesy::courseDeg(aPos, (*it)->end());
219 double dev = inboundCourse - (*it)->headingDeg();
220 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
223 if (dev < currentLowestDev) { // new best match
224 currentLowestDev = dev;
227 } // of runway iteration
233 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
237 unsigned int numRunways(mRunways.size());
238 for (unsigned int r=0; r<numRunways; ++r) {
239 FGRunway* rwy = mRunways[r];
240 if (rwy->isReciprocal()) {
241 continue; // we only care about lengths, so don't do work twice
244 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
245 return true; // we're done!
247 } // of runways iteration
252 unsigned int FGAirport::numTaxiways() const
255 return mTaxiways.size();
258 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
261 assert(aIndex >= 0 && aIndex < mTaxiways.size());
262 return mTaxiways[aIndex];
265 unsigned int FGAirport::numPavements() const
268 return mPavements.size();
271 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
274 assert(aIndex >= 0 && aIndex < mPavements.size());
275 return mPavements[aIndex];
278 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
279 vector<FGTaxiwayPtr>& txwys,
280 vector<FGPavementPtr>& pvts)
283 Runway_iterator it = mRunways.begin();
284 for (; it != mRunways.end(); ++it) {
285 (*it)->setAirport(this);
288 mTaxiways.swap(txwys);
289 mPavements.swap(pvts);
292 FGRunway* FGAirport::getActiveRunwayForUsage() const
294 static FGEnvironmentMgr* envMgr = NULL;
296 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
299 // This forces West-facing rwys to be used in no-wind situations
300 // which is consistent with Flightgear's initial setup.
304 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
306 double windSpeed = stationWeather.get_wind_speed_kt();
307 if (windSpeed > 0.0) {
308 hdg = stationWeather.get_wind_from_heading_deg();
312 return findBestRunwayForHeading(hdg);
315 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
317 AirportFilter aptFilter;
318 if (filter == NULL) {
322 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
327 return static_cast<FGAirport*>(r.ptr());
330 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
331 mMinLengthFt(minLengthFt)
335 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
337 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
340 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
344 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
346 return NULL; // we don't warn here, let the caller do that
348 return static_cast<FGAirport*>(r.ptr());
351 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
355 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
357 throw sg_range_exception("No such airport with ident: " + aIdent);
359 return static_cast<FGAirport*>(r.ptr());
362 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
364 // we delegate all the work to a horrible helper in FGPositioned, which can
365 // access the (private) index data.
366 return searchAirportNamesAndIdents(aFilter);
369 // find basic airport location info from airport database
370 const FGAirport *fgFindAirportID( const string& id)
376 return FGAirport::findByIdent(id);
379 void FGAirport::loadRunways() const
381 if (mRunwaysLoaded) {
382 return; // already loaded, great
385 mRunwaysLoaded = true;
386 loadSceneryDefinitions();
389 void FGAirport::loadTaxiways() const
391 if (mTaxiwaysLoaded) {
392 return; // already loaded, great
396 void FGAirport::loadProcedures() const
398 if (mProceduresLoaded) {
402 mProceduresLoaded = true;
404 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
405 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
409 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
410 Route::loadAirportProcedures(path, const_cast<FGAirport*>(this));
413 void FGAirport::loadSceneryDefinitions() const
416 SGPropertyNode_ptr rootNode = new SGPropertyNode;
417 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
418 readProperties(path.str(), rootNode);
419 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
422 // repeat for the tower data
423 rootNode = new SGPropertyNode;
424 if (XMLLoader::findAirportData(ident(), "twr", path)) {
425 readProperties(path.str(), rootNode);
426 const_cast<FGAirport*>(this)->readTowerData(rootNode);
430 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
432 SGPropertyNode* runway;
434 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
435 SGPropertyNode* t0 = runway->getChild("threshold", 0),
436 *t1 = runway->getChild("threshold", 1);
438 assert(t1); // too strict? mayeb we should finally allow single-ended runways
440 processThreshold(t0);
441 processThreshold(t1);
442 } // of runways iteration
445 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
447 // first, let's identify the current runway
448 string id(aThreshold->getStringValue("rwy"));
449 if (!hasRunwayWithIdent(id)) {
450 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
451 "found runway not defined in the global data:" << ident() << "/" << id);
455 FGRunway* rwy = getRunwayByIdent(id);
456 rwy->processThreshold(aThreshold);
459 void FGAirport::readTowerData(SGPropertyNode* aRoot)
461 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
462 double lat = twrNode->getDoubleValue("lat"),
463 lon = twrNode->getDoubleValue("lon"),
464 elevM = twrNode->getDoubleValue("elev-m");
465 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
466 // scenery for a precise terrain elevation, we use the field elevation
467 // (this is also what the apt.dat code does)
468 double fieldElevationM = geod().getElevationM();
470 _tower_location = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
473 bool FGAirport::buildApproach(Waypt* aEnroute, STAR* aSTAR, FGRunway* aRwy, WayptVec& aRoute)
477 if ((aRwy && (aRwy->airport() != this))) {
478 throw sg_exception("invalid parameters", "FGAirport::buildApproach");
482 bool ok = aSTAR->route(aRwy, aEnroute, aRoute);
484 SG_LOG(SG_GENERAL, SG_WARN, ident() << ": build approach, STAR " << aSTAR->ident()
485 << " failed to route from transition " << aEnroute->ident());
488 } else if (aEnroute) {
489 // no a STAR specified, just use enroute point directly
490 aRoute.push_back(aEnroute);
494 // no runway selected yet, but we loaded the STAR, so that's fine, we're done
498 // build the approach (possibly including transition), and including the missed segment
499 vector<Approach*> aps;
500 for (unsigned int j=0; j<mApproaches.size();++j) {
501 if (mApproaches[j]->runway() == aRwy) {
502 aps.push_back(mApproaches[j]);
504 } // of approach filter by runway
507 SG_LOG(SG_GENERAL, SG_INFO, ident() << "; no approaches defined for runway " << aRwy->ident());
508 // could build a fallback approach here
512 for (unsigned int k=0; k<aps.size(); ++k) {
513 if (aps[k]->route(aRoute.back(), aRoute)) {
516 } // of initial approach iteration
518 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": unable to find transition to runway "
519 << aRwy->ident() << ", assume vectors");
521 WayptRef v(new ATCVectors(NULL, this));
523 return aps.front()->routeFromVectors(aRoute);
526 pair<flightgear::SID*, WayptRef>
527 FGAirport::selectSID(const SGGeod& aDest, FGRunway* aRwy)
532 flightgear::SID* sid = NULL;
535 for (unsigned int i=0; i<mSIDs.size(); ++i) {
536 if (aRwy && !mSIDs[i]->isForRunway(aRwy)) {
540 WayptRef e = mSIDs[i]->findBestTransition(aDest);
542 continue; // strange, but let's not worry about it
545 // assert(e->isFixedPosition());
546 double ed = SGGeodesy::distanceM(aDest, e->position());
547 if (ed < d) { // new best match
552 } // of SID iteration
554 if (!mSIDs.empty() && !sid) {
555 SG_LOG(SG_GENERAL, SG_INFO, ident() << "selectSID, no SID found (runway="
556 << (aRwy ? aRwy->ident() : "no runway preference"));
559 return std::make_pair(sid, enroute);
562 pair<STAR*, WayptRef>
563 FGAirport::selectSTAR(const SGGeod& aOrigin, FGRunway* aRwy)
571 for (unsigned int i=0; i<mSTARs.size(); ++i) {
572 if (!mSTARs[i]->isForRunway(aRwy)) {
576 SG_LOG(SG_GENERAL, SG_INFO, "STAR " << mSTARs[i]->ident() << " is valid for runway");
577 WayptRef e = mSTARs[i]->findBestTransition(aOrigin);
579 continue; // strange, but let's not worry about it
582 // assert(e->isFixedPosition());
583 double ed = SGGeodesy::distanceM(aOrigin, e->position());
584 if (ed < d) { // new best match
589 } // of STAR iteration
591 return std::make_pair(star, enroute);
595 void FGAirport::addSID(flightgear::SID* aSid)
597 mSIDs.push_back(aSid);
600 void FGAirport::addSTAR(STAR* aStar)
602 mSTARs.push_back(aStar);
605 void FGAirport::addApproach(Approach* aApp)
607 mApproaches.push_back(aApp);
610 unsigned int FGAirport::numSIDs() const
616 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
619 return mSIDs[aIndex];
622 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
625 for (unsigned int i=0; i<mSIDs.size(); ++i) {
626 if (mSIDs[i]->ident() == aIdent) {
634 unsigned int FGAirport::numSTARs() const
637 return mSTARs.size();
640 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
643 return mSTARs[aIndex];
646 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
649 for (unsigned int i=0; i<mSTARs.size(); ++i) {
650 if (mSTARs[i]->ident() == aIdent) {
658 unsigned int FGAirport::numApproaches() const
661 return mApproaches.size();
664 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
667 return mApproaches[aIndex];
670 class AirportNodeListener : public SGPropertyChangeListener
673 AirportNodeListener()
675 SGPropertyNode* airports = fgGetNode("/sim/airport");
676 airports->addChangeListener(this, false);
679 virtual void valueChanged(SGPropertyNode*)
683 virtual void childAdded(SGPropertyNode* pr, SGPropertyNode* child)
685 FGAirport* apt = FGAirport::findByIdent(child->getName());
690 flightgear::PositionedBinding::bind(apt, child);
694 void FGAirport::installPropertyListener()
696 new AirportNodeListener;
699 flightgear::PositionedBinding*
700 FGAirport::createBinding(SGPropertyNode* nd) const
702 return new flightgear::AirportBinding(this, nd);
705 void FGAirport::setCommStations(CommStationList& comms)
707 mCommStations.swap(comms);
708 for (unsigned int c=0; c<mCommStations.size(); ++c) {
709 mCommStations[c]->setAirport(this);
714 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
716 CommStationList result;
717 for (unsigned int c=0; c<mCommStations.size(); ++c) {
718 if (mCommStations[c]->type() == aTy) {
719 result.push_back(mCommStations[c]);
725 // get airport elevation
726 double fgGetAirportElev( const string& id )
728 const FGAirport *a=fgFindAirportID( id);
730 return a->getElevation();
737 // get airport position
738 SGGeod fgGetAirportPos( const string& id )
740 const FGAirport *a = fgFindAirportID( id);
743 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
745 return SGGeod::fromDegM(0.0, 0.0, -9999.0);