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>
52 using namespace flightgear;
54 // magic import of a helper which uses FGPositioned internals
55 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
57 /***************************************************************************
59 ***************************************************************************/
61 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
62 const string &name, bool has_metar, Type aType) :
63 FGPositioned(aType, id, location),
64 _tower_location(tower_location),
66 _has_metar(has_metar),
68 mRunwaysLoaded(false),
71 init(true); // init FGPositioned
75 FGAirport::~FGAirport()
80 bool FGAirport::isAirport() const
82 return type() == AIRPORT;
85 bool FGAirport::isSeaport() const
87 return type() == SEAPORT;
90 bool FGAirport::isHeliport() const
92 return type() == HELIPORT;
95 FGAirportDynamics * FGAirport::getDynamics()
100 //cerr << "Trying to load dynamics for " << _id << endl;
101 _dynamics = new FGAirportDynamics(this);
102 XMLLoader::load(_dynamics);
104 FGRunwayPreference rwyPrefs(this);
105 XMLLoader::load(&rwyPrefs);
106 _dynamics->setRwyUse(rwyPrefs);
108 //FGSidStar SIDs(this);
109 XMLLoader::load(_dynamics->getSIDs());
114 unsigned int FGAirport::numRunways() const
117 return mRunways.size();
120 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
124 assert(aIndex >= 0 && aIndex < mRunways.size());
125 return mRunways[aIndex];
128 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
130 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
133 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
135 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
136 if (it == mRunways.end()) {
137 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
138 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
144 FGAirport::Runway_iterator
145 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
149 string ident(aIdent);
150 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
151 ident = "0" + aIdent;
154 Runway_iterator it = mRunways.begin();
155 for (; it != mRunways.end(); ++it) {
156 if ((*it)->ident() == ident) {
164 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
168 Runway_iterator it = mRunways.begin();
169 FGRunway* result = NULL;
170 double currentBestQuality = 0.0;
172 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
173 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
174 double widthWeight = param->getDoubleValue("width-weight", 0.01);
175 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
176 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
178 for (; it != mRunways.end(); ++it) {
179 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
181 double dev = aHeading - (*it)->headingDeg();
182 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
183 double bad = fabs(deviationWeight * dev) + 1e-20;
184 double quality = good / bad;
186 if (quality > currentBestQuality) {
187 currentBestQuality = quality;
195 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
199 Runway_iterator it = mRunways.begin();
200 FGRunway* result = NULL;
201 double currentLowestDev = 180.0;
203 for (; it != mRunways.end(); ++it) {
204 double inboundCourse = SGGeodesy::courseDeg(aPos, (*it)->end());
205 double dev = inboundCourse - (*it)->headingDeg();
206 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
209 if (dev < currentLowestDev) { // new best match
210 currentLowestDev = dev;
213 } // of runway iteration
219 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
223 unsigned int numRunways(mRunways.size());
224 for (unsigned int r=0; r<numRunways; ++r) {
225 FGRunway* rwy = mRunways[r];
226 if (rwy->isReciprocal()) {
227 continue; // we only care about lengths, so don't do work twice
230 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
231 return true; // we're done!
233 } // of runways iteration
238 unsigned int FGAirport::numTaxiways() const
241 return mTaxiways.size();
244 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
247 assert(aIndex >= 0 && aIndex < mTaxiways.size());
248 return mTaxiways[aIndex];
251 unsigned int FGAirport::numPavements() const
254 return mPavements.size();
257 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
260 assert(aIndex >= 0 && aIndex < mPavements.size());
261 return mPavements[aIndex];
264 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
265 vector<FGTaxiwayPtr>& txwys,
266 vector<FGPavementPtr>& pvts)
269 Runway_iterator it = mRunways.begin();
270 for (; it != mRunways.end(); ++it) {
271 (*it)->setAirport(this);
274 mTaxiways.swap(txwys);
275 mPavements.swap(pvts);
278 FGRunway* FGAirport::getActiveRunwayForUsage() const
280 static FGEnvironmentMgr* envMgr = NULL;
282 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
285 // This forces West-facing rwys to be used in no-wind situations
286 // which is consistent with Flightgear's initial setup.
290 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
292 double windSpeed = stationWeather.get_wind_speed_kt();
293 if (windSpeed > 0.0) {
294 hdg = stationWeather.get_wind_from_heading_deg();
298 return findBestRunwayForHeading(hdg);
301 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
303 AirportFilter aptFilter;
304 if (filter == NULL) {
308 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
313 return static_cast<FGAirport*>(r.ptr());
316 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
317 mMinLengthFt(minLengthFt)
321 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
323 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
326 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
330 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
332 return NULL; // we don't warn here, let the caller do that
334 return static_cast<FGAirport*>(r.ptr());
337 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
341 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
343 throw sg_range_exception("No such airport with ident: " + aIdent);
345 return static_cast<FGAirport*>(r.ptr());
348 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
350 // we delegate all the work to a horrible helper in FGPositioned, which can
351 // access the (private) index data.
352 return searchAirportNamesAndIdents(aFilter);
355 // find basic airport location info from airport database
356 const FGAirport *fgFindAirportID( const string& id)
362 return FGAirport::findByIdent(id);
365 void FGAirport::loadRunways() const
367 if (mRunwaysLoaded) {
368 return; // already loaded, great
371 mRunwaysLoaded = true;
372 loadSceneryDefintions();
375 void FGAirport::loadTaxiways() const
377 if (mTaxiwaysLoaded) {
378 return; // already loaded, great
382 void FGAirport::loadProcedures() const
384 if (mProceduresLoaded) {
388 mProceduresLoaded = true;
390 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
391 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
395 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
396 Route::loadAirportProcedures(path, const_cast<FGAirport*>(this));
399 void FGAirport::loadSceneryDefintions() const
401 // allow users to disable the scenery data in the short-term
402 // longer term, this option can probably disappear
403 if (!fgGetBool("/sim/paths/use-custom-scenery-data")) {
408 SGPropertyNode_ptr rootNode = new SGPropertyNode;
409 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
410 readProperties(path.str(), rootNode);
411 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
414 // repeat for the tower data
415 rootNode = new SGPropertyNode;
416 if (XMLLoader::findAirportData(ident(), "twr", path)) {
417 readProperties(path.str(), rootNode);
418 const_cast<FGAirport*>(this)->readTowerData(rootNode);
422 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
424 SGPropertyNode* runway;
426 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
427 SGPropertyNode* t0 = runway->getChild("threshold", 0),
428 *t1 = runway->getChild("threshold", 1);
430 assert(t1); // too strict? mayeb we should finally allow single-ended runways
432 processThreshold(t0);
433 processThreshold(t1);
434 } // of runways iteration
437 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
439 // first, let's identify the current runway
440 string id(aThreshold->getStringValue("rwy"));
441 if (!hasRunwayWithIdent(id)) {
442 SG_LOG(SG_GENERAL, SG_WARN, "FGAirport::processThreshold: "
443 "found runway not defined in the global data:" << ident() << "/" << id);
447 FGRunway* rwy = getRunwayByIdent(id);
448 rwy->processThreshold(aThreshold);
451 void FGAirport::readTowerData(SGPropertyNode* aRoot)
453 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
454 double lat = twrNode->getDoubleValue("lat"),
455 lon = twrNode->getDoubleValue("lon"),
456 elevM = twrNode->getDoubleValue("elev-m");
458 _tower_location = SGGeod::fromDegM(lon, lat, elevM);
461 bool FGAirport::buildApproach(Waypt* aEnroute, STAR* aSTAR, FGRunway* aRwy, WayptVec& aRoute)
465 if ((aRwy && (aRwy->airport() != this))) {
466 throw sg_exception("invalid parameters", "FGAirport::buildApproach");
470 bool ok = aSTAR->route(aRwy, aEnroute, aRoute);
472 SG_LOG(SG_GENERAL, SG_WARN, ident() << ": build approach, STAR " << aSTAR->ident()
473 << " failed to route from transition " << aEnroute->ident());
476 } else if (aEnroute) {
477 // no a STAR specified, just use enroute point directly
478 aRoute.push_back(aEnroute);
482 // no runway selected yet, but we loaded the STAR, so that's fine, we're done
486 // build the approach (possibly including transition), and including the missed segment
487 vector<Approach*> aps;
488 for (unsigned int j=0; j<mApproaches.size();++j) {
489 if (mApproaches[j]->runway() == aRwy) {
490 aps.push_back(mApproaches[j]);
492 } // of approach filter by runway
495 SG_LOG(SG_GENERAL, SG_INFO, ident() << "; no approaches defined for runway " << aRwy->ident());
496 // could build a fallback approach here
500 for (unsigned int k=0; k<aps.size(); ++k) {
501 if (aps[k]->route(aRoute.back(), aRoute)) {
504 } // of initial approach iteration
506 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": unable to find transition to runway "
507 << aRwy->ident() << ", assume vectors");
509 WayptRef v(new ATCVectors(NULL, this));
511 return aps.front()->routeFromVectors(aRoute);
514 pair<flightgear::SID*, WayptRef>
515 FGAirport::selectSID(const SGGeod& aDest, FGRunway* aRwy)
520 flightgear::SID* sid = NULL;
523 for (unsigned int i=0; i<mSIDs.size(); ++i) {
524 if (aRwy && !mSIDs[i]->isForRunway(aRwy)) {
528 WayptRef e = mSIDs[i]->findBestTransition(aDest);
530 continue; // strange, but let's not worry about it
533 // assert(e->isFixedPosition());
534 double ed = SGGeodesy::distanceM(aDest, e->position());
535 if (ed < d) { // new best match
540 } // of SID iteration
542 if (!mSIDs.empty() && !sid) {
543 SG_LOG(SG_GENERAL, SG_INFO, ident() << "selectSID, no SID found (runway="
544 << (aRwy ? aRwy->ident() : "no runway preference"));
547 return make_pair(sid, enroute);
550 pair<STAR*, WayptRef>
551 FGAirport::selectSTAR(const SGGeod& aOrigin, FGRunway* aRwy)
559 for (unsigned int i=0; i<mSTARs.size(); ++i) {
560 if (!mSTARs[i]->isForRunway(aRwy)) {
564 SG_LOG(SG_GENERAL, SG_INFO, "STAR " << mSTARs[i]->ident() << " is valid for runway");
565 WayptRef e = mSTARs[i]->findBestTransition(aOrigin);
567 continue; // strange, but let's not worry about it
570 // assert(e->isFixedPosition());
571 double ed = SGGeodesy::distanceM(aOrigin, e->position());
572 if (ed < d) { // new best match
577 } // of STAR iteration
579 return make_pair(star, enroute);
583 void FGAirport::addSID(flightgear::SID* aSid)
585 mSIDs.push_back(aSid);
588 void FGAirport::addSTAR(STAR* aStar)
590 mSTARs.push_back(aStar);
593 void FGAirport::addApproach(Approach* aApp)
595 mApproaches.push_back(aApp);
598 unsigned int FGAirport::numSIDs() const
604 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
607 return mSIDs[aIndex];
610 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
613 for (unsigned int i=0; i<mSIDs.size(); ++i) {
614 if (mSIDs[i]->ident() == aIdent) {
622 unsigned int FGAirport::numSTARs() const
625 return mSTARs.size();
628 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
631 return mSTARs[aIndex];
634 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
637 for (unsigned int i=0; i<mSTARs.size(); ++i) {
638 if (mSTARs[i]->ident() == aIdent) {
646 unsigned int FGAirport::numApproaches() const
649 return mApproaches.size();
652 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
655 return mApproaches[aIndex];
658 // get airport elevation
659 double fgGetAirportElev( const string& id )
661 const FGAirport *a=fgFindAirportID( id);
663 return a->getElevation();
670 // get airport position
671 SGGeod fgGetAirportPos( const string& id )
673 const FGAirport *a = fgFindAirportID( id);
676 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
678 return SGGeod::fromDegM(0.0, 0.0, -9999.0);