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.
34 #include <boost/foreach.hpp>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/props/props.hxx>
38 #include <simgear/props/props_io.hxx>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/sg_inlines.h>
42 #include <Environment/environment_mgr.hxx>
43 #include <Environment/environment.hxx>
44 #include <Main/fg_props.hxx>
45 #include <Airports/runways.hxx>
46 #include <Airports/pavement.hxx>
47 #include <Airports/dynamics.hxx>
48 #include <Airports/xmlloader.hxx>
49 #include <Navaids/procedure.hxx>
50 #include <Navaids/waypoint.hxx>
51 #include <ATC/CommStation.hxx>
52 #include <Navaids/NavDataCache.hxx>
57 using namespace flightgear;
60 /***************************************************************************
62 ***************************************************************************/
64 FGAirport::FGAirport(PositionedID aGuid, const string &id, const SGGeod& location,
65 const string &name, bool has_metar, Type aType) :
66 FGPositioned(aGuid, aType, id, location),
68 _has_metar(has_metar),
70 mTowerDataLoaded(false),
71 mRunwaysLoaded(false),
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 bool FGAirport::isAirportType(FGPositioned* pos)
103 return (pos->type() >= AIRPORT) && (pos->type() <= SEAPORT);
106 FGAirportDynamics * FGAirport::getDynamics()
112 _dynamics = new FGAirportDynamics(this);
113 XMLLoader::load(_dynamics);
115 FGRunwayPreference rwyPrefs(this);
116 XMLLoader::load(&rwyPrefs);
117 _dynamics->setRwyUse(rwyPrefs);
118 XMLLoader::load(_dynamics->getSIDs());
123 unsigned int FGAirport::numRunways() const
126 return mRunways.size();
129 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
133 assert(aIndex >= 0 && aIndex < mRunways.size());
134 return (FGRunway*) flightgear::NavDataCache::instance()->loadById(mRunways[aIndex]);
137 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
139 return flightgear::NavDataCache::instance()->airportItemWithIdent(guid(), FGPositioned::RUNWAY, aIdent) != 0;
142 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
144 PositionedID id = flightgear::NavDataCache::instance()->airportItemWithIdent(guid(), FGPositioned::RUNWAY, aIdent);
146 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
147 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
150 return (FGRunway*) flightgear::NavDataCache::instance()->loadById(id);
154 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
158 FGRunway* result = NULL;
159 double currentBestQuality = 0.0;
161 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
162 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
163 double widthWeight = param->getDoubleValue("width-weight", 0.01);
164 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
165 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
167 BOOST_FOREACH(PositionedID id, mRunways) {
168 FGRunway* rwy = (FGRunway*) flightgear::NavDataCache::instance()->loadById(id);
169 double good = rwy->score(lengthWeight, widthWeight, surfaceWeight);
170 double dev = aHeading - rwy->headingDeg();
171 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
172 double bad = fabs(deviationWeight * dev) + 1e-20;
173 double quality = good / bad;
175 if (quality > currentBestQuality) {
176 currentBestQuality = quality;
184 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
188 FGRunway* result = NULL;
189 double currentLowestDev = 180.0;
191 BOOST_FOREACH(PositionedID id, mRunways) {
192 FGRunway* rwy = (FGRunway*) flightgear::NavDataCache::instance()->loadById(id);
194 double inboundCourse = SGGeodesy::courseDeg(aPos, rwy->end());
195 double dev = inboundCourse - rwy->headingDeg();
196 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
199 if (dev < currentLowestDev) { // new best match
200 currentLowestDev = dev;
203 } // of runway iteration
209 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
213 BOOST_FOREACH(PositionedID id, mRunways) {
214 FGRunway* rwy = (FGRunway*) flightgear::NavDataCache::instance()->loadById(id);
216 if (rwy->isReciprocal()) {
217 continue; // we only care about lengths, so don't do work twice
220 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
221 return true; // we're done!
223 } // of runways iteration
228 unsigned int FGAirport::numTaxiways() const
231 return mTaxiways.size();
234 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
237 assert(aIndex >= 0 && aIndex < mTaxiways.size());
238 return (FGTaxiway*) flightgear::NavDataCache::instance()->loadById(mTaxiways[aIndex]);
241 unsigned int FGAirport::numPavements() const
244 return mPavements.size();
247 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
250 assert(aIndex >= 0 && aIndex < mPavements.size());
251 return (FGPavement*) flightgear::NavDataCache::instance()->loadById(mPavements[aIndex]);
254 FGRunway* FGAirport::getActiveRunwayForUsage() const
256 static FGEnvironmentMgr* envMgr = NULL;
258 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
261 // This forces West-facing rwys to be used in no-wind situations
262 // which is consistent with Flightgear's initial setup.
266 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
268 double windSpeed = stationWeather.get_wind_speed_kt();
269 if (windSpeed > 0.0) {
270 hdg = stationWeather.get_wind_from_heading_deg();
274 return findBestRunwayForHeading(hdg);
277 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
279 AirportFilter aptFilter;
280 if (filter == NULL) {
284 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
289 return static_cast<FGAirport*>(r.ptr());
292 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
293 mMinLengthFt(minLengthFt)
295 if (minLengthFt < 0.0) {
296 mMinLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
300 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
302 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
305 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
308 FGPositionedRef r = FGPositioned::findFirstWithIdent(aIdent, &filter);
310 return NULL; // we don't warn here, let the caller do that
312 return static_cast<FGAirport*>(r.ptr());
315 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
319 r = FGPositioned::findFirstWithIdent(aIdent, &filter);
321 throw sg_range_exception("No such airport with ident: " + aIdent);
323 return static_cast<FGAirport*>(r.ptr());
326 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
328 return NavDataCache::instance()->searchAirportNamesAndIdents(aFilter);
331 // find basic airport location info from airport database
332 const FGAirport *fgFindAirportID( const string& id)
338 return FGAirport::findByIdent(id);
341 void FGAirport::loadRunways() const
343 if (mRunwaysLoaded) {
344 return; // already loaded, great
347 loadSceneryDefinitions();
349 mRunwaysLoaded = true;
350 mRunways = flightgear::NavDataCache::instance()->airportItemsOfType(guid(), FGPositioned::RUNWAY);
353 void FGAirport::loadTaxiways() const
355 if (mTaxiwaysLoaded) {
356 return; // already loaded, great
359 mTaxiwaysLoaded = true;
360 mTaxiways = flightgear::NavDataCache::instance()->airportItemsOfType(guid(), FGPositioned::TAXIWAY);
363 void FGAirport::loadProcedures() const
365 if (mProceduresLoaded) {
369 mProceduresLoaded = true;
371 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
372 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
376 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
377 RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
380 void FGAirport::loadSceneryDefinitions() const
382 NavDataCache* cache = NavDataCache::instance();
384 if (!XMLLoader::findAirportData(ident(), "threshold", path)) {
385 return; // no XML threshold data
388 if (!cache->isCachedFileModified(path)) {
389 // cached values are correct, we're all done
393 SGPropertyNode_ptr rootNode = new SGPropertyNode;
394 readProperties(path.str(), rootNode);
395 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
396 cache->stampCacheFile(path);
400 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
402 SGPropertyNode* runway;
404 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
405 SGPropertyNode* t0 = runway->getChild("threshold", 0),
406 *t1 = runway->getChild("threshold", 1);
408 assert(t1); // too strict? mayeb we should finally allow single-ended runways
410 processThreshold(t0);
411 processThreshold(t1);
412 } // of runways iteration
415 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
417 // first, let's identify the current runway
418 string rwyIdent(aThreshold->getStringValue("rwy"));
419 NavDataCache* cache = NavDataCache::instance();
420 PositionedID id = cache->airportItemWithIdent(guid(), FGPositioned::RUNWAY, rwyIdent);
422 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
423 "found runway not defined in the global data:" << ident() << "/" << rwyIdent);
427 double lon = aThreshold->getDoubleValue("lon"),
428 lat = aThreshold->getDoubleValue("lat");
429 SGGeod newThreshold(SGGeod::fromDegM(lon, lat, mPosition.getElevationM()));
431 double newHeading = aThreshold->getDoubleValue("hdg-deg");
432 double newDisplacedThreshold = aThreshold->getDoubleValue("displ-m") * SG_METER_TO_FEET;
433 double newStopway = aThreshold->getDoubleValue("stopw-m") * SG_METER_TO_FEET;
435 cache->updateRunwayThreshold(id, newThreshold,
436 newHeading, newDisplacedThreshold, newStopway);
439 SGGeod FGAirport::getTowerLocation() const
443 NavDataCache* cache = NavDataCache::instance();
444 PositionedIDVec towers = cache->airportItemsOfType(guid(), FGPositioned::TOWER);
445 if (towers.empty()) {
446 SG_LOG(SG_GENERAL, SG_ALERT, "No towers defined for:" <<ident());
450 FGPositionedRef tower = cache->loadById(towers.front());
451 return tower->geod();
454 void FGAirport::validateTowerData() const
456 if (mTowerDataLoaded) {
460 mTowerDataLoaded = true;
461 NavDataCache* cache = NavDataCache::instance();
463 if (!XMLLoader::findAirportData(ident(), "twr", path)) {
464 return; // no XML tower data
467 if (!cache->isCachedFileModified(path)) {
468 // cached values are correct, we're all done
472 SGPropertyNode_ptr rootNode = new SGPropertyNode;
473 readProperties(path.str(), rootNode);
474 const_cast<FGAirport*>(this)->readTowerData(rootNode);
475 cache->stampCacheFile(path);
478 void FGAirport::readTowerData(SGPropertyNode* aRoot)
480 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
481 double lat = twrNode->getDoubleValue("lat"),
482 lon = twrNode->getDoubleValue("lon"),
483 elevM = twrNode->getDoubleValue("elev-m");
484 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
485 // scenery for a precise terrain elevation, we use the field elevation
486 // (this is also what the apt.dat code does)
487 double fieldElevationM = geod().getElevationM();
488 SGGeod towerLocation(SGGeod::fromDegM(lon, lat, fieldElevationM + elevM));
490 NavDataCache* cache = NavDataCache::instance();
491 PositionedIDVec towers = cache->airportItemsOfType(guid(), FGPositioned::TOWER);
492 if (towers.empty()) {
493 cache->insertTower(guid(), towerLocation);
495 // update the position
496 cache->updatePosition(towers.front(), towerLocation);
500 bool FGAirport::validateILSData()
502 if (mILSDataLoaded) {
506 mILSDataLoaded = true;
507 NavDataCache* cache = NavDataCache::instance();
509 if (!XMLLoader::findAirportData(ident(), "ils", path)) {
510 return false; // no XML tower data
513 if (!cache->isCachedFileModified(path)) {
514 // cached values are correct, we're all done
518 SGPropertyNode_ptr rootNode = new SGPropertyNode;
519 readProperties(path.str(), rootNode);
520 readILSData(rootNode);
521 cache->stampCacheFile(path);
523 // we loaded data, tell the caller it might need to reload things
527 void FGAirport::readILSData(SGPropertyNode* aRoot)
529 NavDataCache* cache = NavDataCache::instance();
531 // find the entry matching the runway
532 SGPropertyNode* runwayNode, *ilsNode;
533 for (int i=0; (runwayNode = aRoot->getChild("runway", i)) != NULL; ++i) {
534 for (int j=0; (ilsNode = runwayNode->getChild("ils", j)) != NULL; ++j) {
535 // must match on both nav-ident and runway ident, to support the following:
536 // - runways with multiple distinct ILS installations (KEWD, for example)
537 // - runways where both ends share the same nav ident (LFAT, for example)
538 PositionedID ils = cache->findILS(guid(), ilsNode->getStringValue("rwy"),
539 ilsNode->getStringValue("nav-id"));
541 SG_LOG(SG_GENERAL, SG_INFO, "reading ILS data for " << ident() <<
542 ", couldn;t find runway/navaid for:" <<
543 ilsNode->getStringValue("rwy") << "/" <<
544 ilsNode->getStringValue("nav-id"));
548 double hdgDeg = ilsNode->getDoubleValue("hdg-deg"),
549 lon = ilsNode->getDoubleValue("lon"),
550 lat = ilsNode->getDoubleValue("lat"),
551 elevM = ilsNode->getDoubleValue("elev-m");
553 cache->updateILS(ils, SGGeod::fromDegM(lon, lat, elevM), hdgDeg);
554 } // of ILS iteration
555 } // of runway iteration
558 void FGAirport::addSID(flightgear::SID* aSid)
560 mSIDs.push_back(aSid);
563 void FGAirport::addSTAR(STAR* aStar)
565 mSTARs.push_back(aStar);
568 void FGAirport::addApproach(Approach* aApp)
570 mApproaches.push_back(aApp);
573 unsigned int FGAirport::numSIDs() const
579 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
582 return mSIDs[aIndex];
585 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
588 for (unsigned int i=0; i<mSIDs.size(); ++i) {
589 if (mSIDs[i]->ident() == aIdent) {
597 unsigned int FGAirport::numSTARs() const
600 return mSTARs.size();
603 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
606 return mSTARs[aIndex];
609 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
612 for (unsigned int i=0; i<mSTARs.size(); ++i) {
613 if (mSTARs[i]->ident() == aIdent) {
621 unsigned int FGAirport::numApproaches() const
624 return mApproaches.size();
627 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
630 return mApproaches[aIndex];
633 Approach* FGAirport::findApproachWithIdent(const std::string& aIdent) const
636 for (unsigned int i=0; i<mApproaches.size(); ++i) {
637 if (mApproaches[i]->ident() == aIdent) {
638 return mApproaches[i];
646 FGAirport::commStations() const
648 NavDataCache* cache = NavDataCache::instance();
649 CommStationList result;
650 BOOST_FOREACH(PositionedID pos, cache->airportItemsOfType(guid(),
651 FGPositioned::FREQ_GROUND,
652 FGPositioned::FREQ_UNICOM))
654 result.push_back((CommStation*) cache->loadById(pos));
661 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
663 NavDataCache* cache = NavDataCache::instance();
664 CommStationList result;
665 BOOST_FOREACH(PositionedID pos, cache->airportItemsOfType(guid(), aTy)) {
666 result.push_back((CommStation*) cache->loadById(pos));
672 // get airport elevation
673 double fgGetAirportElev( const string& id )
675 const FGAirport *a=fgFindAirportID( id);
677 return a->getElevation();
684 // get airport position
685 SGGeod fgGetAirportPos( const string& id )
687 const FGAirport *a = fgFindAirportID( id);
690 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
692 return SGGeod::fromDegM(0.0, 0.0, -9999.0);