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);
117 FGRunwayPreference rwyPrefs(this);
118 XMLLoader::load(&rwyPrefs);
119 _dynamics->setRwyUse(rwyPrefs);
124 unsigned int FGAirport::numRunways() const
127 return mRunways.size();
130 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
134 assert(aIndex >= 0 && aIndex < mRunways.size());
135 return (FGRunway*) flightgear::NavDataCache::instance()->loadById(mRunways[aIndex]);
138 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
140 return flightgear::NavDataCache::instance()->airportItemWithIdent(guid(), FGPositioned::RUNWAY, aIdent) != 0;
143 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
145 PositionedID id = flightgear::NavDataCache::instance()->airportItemWithIdent(guid(), FGPositioned::RUNWAY, aIdent);
147 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
148 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
151 return (FGRunway*) flightgear::NavDataCache::instance()->loadById(id);
155 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
159 FGRunway* result = NULL;
160 double currentBestQuality = 0.0;
162 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
163 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
164 double widthWeight = param->getDoubleValue("width-weight", 0.01);
165 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
166 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
168 BOOST_FOREACH(PositionedID id, mRunways) {
169 FGRunway* rwy = (FGRunway*) flightgear::NavDataCache::instance()->loadById(id);
170 double good = rwy->score(lengthWeight, widthWeight, surfaceWeight);
171 double dev = aHeading - rwy->headingDeg();
172 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
173 double bad = fabs(deviationWeight * dev) + 1e-20;
174 double quality = good / bad;
176 if (quality > currentBestQuality) {
177 currentBestQuality = quality;
185 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
189 FGRunway* result = NULL;
190 double currentLowestDev = 180.0;
192 BOOST_FOREACH(PositionedID id, mRunways) {
193 FGRunway* rwy = (FGRunway*) flightgear::NavDataCache::instance()->loadById(id);
195 double inboundCourse = SGGeodesy::courseDeg(aPos, rwy->end());
196 double dev = inboundCourse - rwy->headingDeg();
197 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
200 if (dev < currentLowestDev) { // new best match
201 currentLowestDev = dev;
204 } // of runway iteration
210 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
214 BOOST_FOREACH(PositionedID id, mRunways) {
215 FGRunway* rwy = (FGRunway*) flightgear::NavDataCache::instance()->loadById(id);
217 if (rwy->isReciprocal()) {
218 continue; // we only care about lengths, so don't do work twice
221 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
222 return true; // we're done!
224 } // of runways iteration
229 unsigned int FGAirport::numTaxiways() const
232 return mTaxiways.size();
235 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
239 assert(aIndex >= 0 && aIndex < mTaxiways.size());
240 return (FGTaxiway*) flightgear::NavDataCache::instance()->loadById(mTaxiways[aIndex]);
243 unsigned int FGAirport::numPavements() const
246 return mPavements.size();
249 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
252 assert(aIndex >= 0 && aIndex < mPavements.size());
253 return (FGPavement*) flightgear::NavDataCache::instance()->loadById(mPavements[aIndex]);
256 FGRunway* FGAirport::getActiveRunwayForUsage() const
258 static FGEnvironmentMgr* envMgr = NULL;
260 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
263 // This forces West-facing rwys to be used in no-wind situations
264 // which is consistent with Flightgear's initial setup.
268 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
270 double windSpeed = stationWeather.get_wind_speed_kt();
271 if (windSpeed > 0.0) {
272 hdg = stationWeather.get_wind_from_heading_deg();
276 return findBestRunwayForHeading(hdg);
279 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
281 AirportFilter aptFilter;
282 if (filter == NULL) {
286 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
291 return static_cast<FGAirport*>(r.ptr());
294 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
295 mMinLengthFt(minLengthFt)
297 if (minLengthFt < 0.0) {
298 mMinLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
302 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
304 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
307 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
310 FGPositionedRef r = FGPositioned::findFirstWithIdent(aIdent, &filter);
312 return NULL; // we don't warn here, let the caller do that
314 return static_cast<FGAirport*>(r.ptr());
317 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
321 r = FGPositioned::findFirstWithIdent(aIdent, &filter);
323 throw sg_range_exception("No such airport with ident: " + aIdent);
325 return static_cast<FGAirport*>(r.ptr());
328 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
330 return NavDataCache::instance()->searchAirportNamesAndIdents(aFilter);
333 // find basic airport location info from airport database
334 const FGAirport *fgFindAirportID( const string& id)
340 return FGAirport::findByIdent(id);
343 void FGAirport::loadRunways() const
345 if (mRunwaysLoaded) {
346 return; // already loaded, great
349 loadSceneryDefinitions();
351 mRunwaysLoaded = true;
352 mRunways = flightgear::NavDataCache::instance()->airportItemsOfType(guid(), FGPositioned::RUNWAY);
355 void FGAirport::loadTaxiways() const
357 if (mTaxiwaysLoaded) {
358 return; // already loaded, great
361 mTaxiwaysLoaded = true;
362 mTaxiways = flightgear::NavDataCache::instance()->airportItemsOfType(guid(), FGPositioned::TAXIWAY);
365 void FGAirport::loadProcedures() const
367 if (mProceduresLoaded) {
371 mProceduresLoaded = true;
373 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
374 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
378 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
379 RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
382 void FGAirport::loadSceneryDefinitions() const
384 NavDataCache* cache = NavDataCache::instance();
386 if (!XMLLoader::findAirportData(ident(), "threshold", path)) {
387 return; // no XML threshold data
390 if (!cache->isCachedFileModified(path)) {
391 // cached values are correct, we're all done
395 SGPropertyNode_ptr rootNode = new SGPropertyNode;
396 readProperties(path.str(), rootNode);
397 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
398 cache->stampCacheFile(path);
402 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
404 SGPropertyNode* runway;
406 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
407 SGPropertyNode* t0 = runway->getChild("threshold", 0),
408 *t1 = runway->getChild("threshold", 1);
410 assert(t1); // too strict? mayeb we should finally allow single-ended runways
412 processThreshold(t0);
413 processThreshold(t1);
414 } // of runways iteration
417 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
419 // first, let's identify the current runway
420 string rwyIdent(aThreshold->getStringValue("rwy"));
421 NavDataCache* cache = NavDataCache::instance();
422 PositionedID id = cache->airportItemWithIdent(guid(), FGPositioned::RUNWAY, rwyIdent);
424 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
425 "found runway not defined in the global data:" << ident() << "/" << rwyIdent);
429 double lon = aThreshold->getDoubleValue("lon"),
430 lat = aThreshold->getDoubleValue("lat");
431 SGGeod newThreshold(SGGeod::fromDegM(lon, lat, mPosition.getElevationM()));
433 double newHeading = aThreshold->getDoubleValue("hdg-deg");
434 double newDisplacedThreshold = aThreshold->getDoubleValue("displ-m") * SG_METER_TO_FEET;
435 double newStopway = aThreshold->getDoubleValue("stopw-m") * SG_METER_TO_FEET;
437 cache->updateRunwayThreshold(id, newThreshold,
438 newHeading, newDisplacedThreshold, newStopway);
441 SGGeod FGAirport::getTowerLocation() const
445 NavDataCache* cache = NavDataCache::instance();
446 PositionedIDVec towers = cache->airportItemsOfType(guid(), FGPositioned::TOWER);
447 if (towers.empty()) {
448 SG_LOG(SG_GENERAL, SG_ALERT, "No towers defined for:" <<ident());
452 FGPositionedRef tower = cache->loadById(towers.front());
453 return tower->geod();
456 void FGAirport::validateTowerData() const
458 if (mTowerDataLoaded) {
462 mTowerDataLoaded = true;
463 NavDataCache* cache = NavDataCache::instance();
465 if (!XMLLoader::findAirportData(ident(), "twr", path)) {
466 return; // no XML tower data
469 if (!cache->isCachedFileModified(path)) {
470 // cached values are correct, we're all done
474 SGPropertyNode_ptr rootNode = new SGPropertyNode;
475 readProperties(path.str(), rootNode);
476 const_cast<FGAirport*>(this)->readTowerData(rootNode);
477 cache->stampCacheFile(path);
480 void FGAirport::readTowerData(SGPropertyNode* aRoot)
482 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
483 double lat = twrNode->getDoubleValue("lat"),
484 lon = twrNode->getDoubleValue("lon"),
485 elevM = twrNode->getDoubleValue("elev-m");
486 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
487 // scenery for a precise terrain elevation, we use the field elevation
488 // (this is also what the apt.dat code does)
489 double fieldElevationM = geod().getElevationM();
490 SGGeod towerLocation(SGGeod::fromDegM(lon, lat, fieldElevationM + elevM));
492 NavDataCache* cache = NavDataCache::instance();
493 PositionedIDVec towers = cache->airportItemsOfType(guid(), FGPositioned::TOWER);
494 if (towers.empty()) {
495 cache->insertTower(guid(), towerLocation);
497 // update the position
498 cache->updatePosition(towers.front(), towerLocation);
502 bool FGAirport::validateILSData()
504 if (mILSDataLoaded) {
508 mILSDataLoaded = true;
509 NavDataCache* cache = NavDataCache::instance();
511 if (!XMLLoader::findAirportData(ident(), "ils", path)) {
512 return false; // no XML tower data
515 if (!cache->isCachedFileModified(path)) {
516 // cached values are correct, we're all done
520 SGPropertyNode_ptr rootNode = new SGPropertyNode;
521 readProperties(path.str(), rootNode);
522 readILSData(rootNode);
523 cache->stampCacheFile(path);
525 // we loaded data, tell the caller it might need to reload things
529 void FGAirport::readILSData(SGPropertyNode* aRoot)
531 NavDataCache* cache = NavDataCache::instance();
533 // find the entry matching the runway
534 SGPropertyNode* runwayNode, *ilsNode;
535 for (int i=0; (runwayNode = aRoot->getChild("runway", i)) != NULL; ++i) {
536 for (int j=0; (ilsNode = runwayNode->getChild("ils", j)) != NULL; ++j) {
537 // must match on both nav-ident and runway ident, to support the following:
538 // - runways with multiple distinct ILS installations (KEWD, for example)
539 // - runways where both ends share the same nav ident (LFAT, for example)
540 PositionedID ils = cache->findILS(guid(), ilsNode->getStringValue("rwy"),
541 ilsNode->getStringValue("nav-id"));
543 SG_LOG(SG_GENERAL, SG_INFO, "reading ILS data for " << ident() <<
544 ", couldn;t find runway/navaid for:" <<
545 ilsNode->getStringValue("rwy") << "/" <<
546 ilsNode->getStringValue("nav-id"));
550 double hdgDeg = ilsNode->getDoubleValue("hdg-deg"),
551 lon = ilsNode->getDoubleValue("lon"),
552 lat = ilsNode->getDoubleValue("lat"),
553 elevM = ilsNode->getDoubleValue("elev-m");
555 cache->updateILS(ils, SGGeod::fromDegM(lon, lat, elevM), hdgDeg);
556 } // of ILS iteration
557 } // of runway iteration
560 void FGAirport::addSID(flightgear::SID* aSid)
562 mSIDs.push_back(aSid);
565 void FGAirport::addSTAR(STAR* aStar)
567 mSTARs.push_back(aStar);
570 void FGAirport::addApproach(Approach* aApp)
572 mApproaches.push_back(aApp);
575 unsigned int FGAirport::numSIDs() const
581 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
584 return mSIDs[aIndex];
587 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
590 for (unsigned int i=0; i<mSIDs.size(); ++i) {
591 if (mSIDs[i]->ident() == aIdent) {
599 unsigned int FGAirport::numSTARs() const
602 return mSTARs.size();
605 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
608 return mSTARs[aIndex];
611 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
614 for (unsigned int i=0; i<mSTARs.size(); ++i) {
615 if (mSTARs[i]->ident() == aIdent) {
623 unsigned int FGAirport::numApproaches() const
626 return mApproaches.size();
629 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
632 return mApproaches[aIndex];
635 Approach* FGAirport::findApproachWithIdent(const std::string& aIdent) const
638 for (unsigned int i=0; i<mApproaches.size(); ++i) {
639 if (mApproaches[i]->ident() == aIdent) {
640 return mApproaches[i];
648 FGAirport::commStations() const
650 NavDataCache* cache = NavDataCache::instance();
651 CommStationList result;
652 BOOST_FOREACH(PositionedID pos, cache->airportItemsOfType(guid(),
653 FGPositioned::FREQ_GROUND,
654 FGPositioned::FREQ_UNICOM))
656 result.push_back((CommStation*) cache->loadById(pos));
663 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
665 NavDataCache* cache = NavDataCache::instance();
666 CommStationList result;
667 BOOST_FOREACH(PositionedID pos, cache->airportItemsOfType(guid(), aTy)) {
668 result.push_back((CommStation*) cache->loadById(pos));
674 // get airport elevation
675 double fgGetAirportElev( const string& id )
677 const FGAirport *a=fgFindAirportID( id);
679 return a->getElevation();
686 // get airport position
687 SGGeod fgGetAirportPos( const string& id )
689 const FGAirport *a = fgFindAirportID( id);
692 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
694 return SGGeod::fromDegM(0.0, 0.0, -9999.0);