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>
41 #include <simgear/structure/exception.hxx>
43 #include <Environment/environment_mgr.hxx>
44 #include <Environment/environment.hxx>
45 #include <Main/fg_props.hxx>
46 #include <Airports/runways.hxx>
47 #include <Airports/pavement.hxx>
48 #include <Airports/dynamics.hxx>
49 #include <Airports/xmlloader.hxx>
50 #include <Navaids/procedure.hxx>
51 #include <Navaids/waypoint.hxx>
52 #include <ATC/CommStation.hxx>
53 #include <Navaids/NavDataCache.hxx>
58 using namespace flightgear;
61 /***************************************************************************
63 ***************************************************************************/
65 FGAirport::FGAirport(PositionedID aGuid, const string &id, const SGGeod& location,
66 const string &name, bool has_metar, Type aType) :
67 FGPositioned(aGuid, aType, id, location),
69 _has_metar(has_metar),
71 mTowerDataLoaded(false),
72 mRunwaysLoaded(false),
73 mTaxiwaysLoaded(false)
78 FGAirport::~FGAirport()
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 bool FGAirport::isAirportType(FGPositioned* pos)
104 return (pos->type() >= AIRPORT) && (pos->type() <= SEAPORT);
107 FGAirportDynamics * FGAirport::getDynamics()
113 _dynamics = new FGAirportDynamics(this);
114 XMLLoader::load(_dynamics);
116 FGRunwayPreference rwyPrefs(this);
117 XMLLoader::load(&rwyPrefs);
118 _dynamics->setRwyUse(rwyPrefs);
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
238 assert(aIndex >= 0 && aIndex < mTaxiways.size());
239 return (FGTaxiway*) flightgear::NavDataCache::instance()->loadById(mTaxiways[aIndex]);
242 unsigned int FGAirport::numPavements() const
245 return mPavements.size();
248 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
251 assert(aIndex >= 0 && aIndex < mPavements.size());
252 return (FGPavement*) flightgear::NavDataCache::instance()->loadById(mPavements[aIndex]);
255 FGRunway* FGAirport::getActiveRunwayForUsage() const
257 static FGEnvironmentMgr* envMgr = NULL;
259 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
262 // This forces West-facing rwys to be used in no-wind situations
263 // which is consistent with Flightgear's initial setup.
267 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
269 double windSpeed = stationWeather.get_wind_speed_kt();
270 if (windSpeed > 0.0) {
271 hdg = stationWeather.get_wind_from_heading_deg();
275 return findBestRunwayForHeading(hdg);
278 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
280 AirportFilter aptFilter;
281 if (filter == NULL) {
285 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
290 return static_cast<FGAirport*>(r.ptr());
293 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
294 mMinLengthFt(minLengthFt)
296 if (minLengthFt < 0.0) {
297 mMinLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
301 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
303 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
306 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
309 FGPositionedRef r = FGPositioned::findFirstWithIdent(aIdent, &filter);
311 return NULL; // we don't warn here, let the caller do that
313 return static_cast<FGAirport*>(r.ptr());
316 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
320 r = FGPositioned::findFirstWithIdent(aIdent, &filter);
322 throw sg_range_exception("No such airport with ident: " + aIdent);
324 return static_cast<FGAirport*>(r.ptr());
327 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
329 return NavDataCache::instance()->searchAirportNamesAndIdents(aFilter);
332 // find basic airport location info from airport database
333 const FGAirport *fgFindAirportID( const string& id)
339 return FGAirport::findByIdent(id);
342 void FGAirport::loadRunways() const
344 if (mRunwaysLoaded) {
345 return; // already loaded, great
348 loadSceneryDefinitions();
350 mRunwaysLoaded = true;
351 mRunways = flightgear::NavDataCache::instance()->airportItemsOfType(guid(), FGPositioned::RUNWAY);
354 void FGAirport::loadTaxiways() const
356 if (mTaxiwaysLoaded) {
357 return; // already loaded, great
360 mTaxiwaysLoaded = true;
361 mTaxiways = flightgear::NavDataCache::instance()->airportItemsOfType(guid(), FGPositioned::TAXIWAY);
364 void FGAirport::loadProcedures() const
366 if (mProceduresLoaded) {
370 mProceduresLoaded = true;
372 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
373 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
377 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
378 RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
381 void FGAirport::loadSceneryDefinitions() const
383 NavDataCache* cache = NavDataCache::instance();
385 if (!XMLLoader::findAirportData(ident(), "threshold", path)) {
386 return; // no XML threshold data
389 if (!cache->isCachedFileModified(path)) {
390 // cached values are correct, we're all done
394 SGPropertyNode_ptr rootNode = new SGPropertyNode;
395 readProperties(path.str(), rootNode);
396 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
397 cache->stampCacheFile(path);
401 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
403 SGPropertyNode* runway;
405 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
406 SGPropertyNode* t0 = runway->getChild("threshold", 0),
407 *t1 = runway->getChild("threshold", 1);
409 assert(t1); // too strict? mayeb we should finally allow single-ended runways
411 processThreshold(t0);
412 processThreshold(t1);
413 } // of runways iteration
416 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
418 // first, let's identify the current runway
419 string rwyIdent(aThreshold->getStringValue("rwy"));
420 NavDataCache* cache = NavDataCache::instance();
421 PositionedID id = cache->airportItemWithIdent(guid(), FGPositioned::RUNWAY, rwyIdent);
423 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
424 "found runway not defined in the global data:" << ident() << "/" << rwyIdent);
428 double lon = aThreshold->getDoubleValue("lon"),
429 lat = aThreshold->getDoubleValue("lat");
430 SGGeod newThreshold(SGGeod::fromDegM(lon, lat, mPosition.getElevationM()));
432 double newHeading = aThreshold->getDoubleValue("hdg-deg");
433 double newDisplacedThreshold = aThreshold->getDoubleValue("displ-m") * SG_METER_TO_FEET;
434 double newStopway = aThreshold->getDoubleValue("stopw-m") * SG_METER_TO_FEET;
436 cache->updateRunwayThreshold(id, newThreshold,
437 newHeading, newDisplacedThreshold, newStopway);
440 SGGeod FGAirport::getTowerLocation() const
444 NavDataCache* cache = NavDataCache::instance();
445 PositionedIDVec towers = cache->airportItemsOfType(guid(), FGPositioned::TOWER);
446 if (towers.empty()) {
447 SG_LOG(SG_GENERAL, SG_ALERT, "No towers defined for:" <<ident());
451 FGPositionedRef tower = cache->loadById(towers.front());
452 return tower->geod();
455 void FGAirport::validateTowerData() const
457 if (mTowerDataLoaded) {
461 mTowerDataLoaded = true;
462 NavDataCache* cache = NavDataCache::instance();
464 if (!XMLLoader::findAirportData(ident(), "twr", path)) {
465 return; // no XML tower data
468 if (!cache->isCachedFileModified(path)) {
469 // cached values are correct, we're all done
473 SGPropertyNode_ptr rootNode = new SGPropertyNode;
474 readProperties(path.str(), rootNode);
475 const_cast<FGAirport*>(this)->readTowerData(rootNode);
476 cache->stampCacheFile(path);
479 void FGAirport::readTowerData(SGPropertyNode* aRoot)
481 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
482 double lat = twrNode->getDoubleValue("lat"),
483 lon = twrNode->getDoubleValue("lon"),
484 elevM = twrNode->getDoubleValue("elev-m");
485 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
486 // scenery for a precise terrain elevation, we use the field elevation
487 // (this is also what the apt.dat code does)
488 double fieldElevationM = geod().getElevationM();
489 SGGeod towerLocation(SGGeod::fromDegM(lon, lat, fieldElevationM + elevM));
491 NavDataCache* cache = NavDataCache::instance();
492 PositionedIDVec towers = cache->airportItemsOfType(guid(), FGPositioned::TOWER);
493 if (towers.empty()) {
494 cache->insertTower(guid(), towerLocation);
496 // update the position
497 cache->updatePosition(towers.front(), towerLocation);
501 bool FGAirport::validateILSData()
503 if (mILSDataLoaded) {
507 mILSDataLoaded = true;
508 NavDataCache* cache = NavDataCache::instance();
510 if (!XMLLoader::findAirportData(ident(), "ils", path)) {
511 return false; // no XML tower data
514 if (!cache->isCachedFileModified(path)) {
515 // cached values are correct, we're all done
519 SGPropertyNode_ptr rootNode = new SGPropertyNode;
520 readProperties(path.str(), rootNode);
521 readILSData(rootNode);
522 cache->stampCacheFile(path);
524 // we loaded data, tell the caller it might need to reload things
528 void FGAirport::readILSData(SGPropertyNode* aRoot)
530 NavDataCache* cache = NavDataCache::instance();
532 // find the entry matching the runway
533 SGPropertyNode* runwayNode, *ilsNode;
534 for (int i=0; (runwayNode = aRoot->getChild("runway", i)) != NULL; ++i) {
535 for (int j=0; (ilsNode = runwayNode->getChild("ils", j)) != NULL; ++j) {
536 // must match on both nav-ident and runway ident, to support the following:
537 // - runways with multiple distinct ILS installations (KEWD, for example)
538 // - runways where both ends share the same nav ident (LFAT, for example)
539 PositionedID ils = cache->findILS(guid(), ilsNode->getStringValue("rwy"),
540 ilsNode->getStringValue("nav-id"));
542 SG_LOG(SG_GENERAL, SG_INFO, "reading ILS data for " << ident() <<
543 ", couldn;t find runway/navaid for:" <<
544 ilsNode->getStringValue("rwy") << "/" <<
545 ilsNode->getStringValue("nav-id"));
549 double hdgDeg = ilsNode->getDoubleValue("hdg-deg"),
550 lon = ilsNode->getDoubleValue("lon"),
551 lat = ilsNode->getDoubleValue("lat"),
552 elevM = ilsNode->getDoubleValue("elev-m");
554 cache->updateILS(ils, SGGeod::fromDegM(lon, lat, elevM), hdgDeg);
555 } // of ILS iteration
556 } // of runway iteration
559 void FGAirport::addSID(flightgear::SID* aSid)
561 mSIDs.push_back(aSid);
564 void FGAirport::addSTAR(STAR* aStar)
566 mSTARs.push_back(aStar);
569 void FGAirport::addApproach(Approach* aApp)
571 mApproaches.push_back(aApp);
574 unsigned int FGAirport::numSIDs() const
580 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
583 return mSIDs[aIndex];
586 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
589 for (unsigned int i=0; i<mSIDs.size(); ++i) {
590 if (mSIDs[i]->ident() == aIdent) {
598 unsigned int FGAirport::numSTARs() const
601 return mSTARs.size();
604 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
607 return mSTARs[aIndex];
610 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
613 for (unsigned int i=0; i<mSTARs.size(); ++i) {
614 if (mSTARs[i]->ident() == aIdent) {
622 unsigned int FGAirport::numApproaches() const
625 return mApproaches.size();
628 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
631 return mApproaches[aIndex];
634 Approach* FGAirport::findApproachWithIdent(const std::string& aIdent) const
637 for (unsigned int i=0; i<mApproaches.size(); ++i) {
638 if (mApproaches[i]->ident() == aIdent) {
639 return mApproaches[i];
647 FGAirport::commStations() const
649 NavDataCache* cache = NavDataCache::instance();
650 CommStationList result;
651 BOOST_FOREACH(PositionedID pos, cache->airportItemsOfType(guid(),
652 FGPositioned::FREQ_GROUND,
653 FGPositioned::FREQ_UNICOM))
655 result.push_back((CommStation*) cache->loadById(pos));
662 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
664 NavDataCache* cache = NavDataCache::instance();
665 CommStationList result;
666 BOOST_FOREACH(PositionedID pos, cache->airportItemsOfType(guid(), aTy)) {
667 result.push_back((CommStation*) cache->loadById(pos));
673 // get airport elevation
674 double fgGetAirportElev( const string& id )
676 const FGAirport *a=fgFindAirportID( id);
678 return a->getElevation();
685 // get airport position
686 SGGeod fgGetAirportPos( const string& id )
688 const FGAirport *a = fgFindAirportID( id);
691 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
693 return SGGeod::fromDegM(0.0, 0.0, -9999.0);