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 <ATC/CommStation.hxx>
55 using namespace flightgear;
57 // magic import of a helper which uses FGPositioned internals
58 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
60 /***************************************************************************
62 ***************************************************************************/
64 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
65 const string &name, bool has_metar, Type aType) :
66 FGPositioned(aType, id, location),
67 _tower_location(tower_location),
69 _has_metar(has_metar),
71 mRunwaysLoaded(false),
74 init(true); // init FGPositioned
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);
119 XMLLoader::load(_dynamics->getSIDs());
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 mRunways[aIndex];
138 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
140 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
143 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
145 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
146 if (it == mRunways.end()) {
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");
154 FGAirport::Runway_iterator
155 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
158 return mRunways.end();
162 string ident(aIdent);
163 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
164 ident = "0" + aIdent;
167 Runway_iterator it = mRunways.begin();
168 for (; it != mRunways.end(); ++it) {
169 if ((*it)->ident() == ident) {
177 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
181 Runway_iterator it = mRunways.begin();
182 FGRunway* result = NULL;
183 double currentBestQuality = 0.0;
185 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
186 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
187 double widthWeight = param->getDoubleValue("width-weight", 0.01);
188 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
189 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
191 for (; it != mRunways.end(); ++it) {
192 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
194 double dev = aHeading - (*it)->headingDeg();
195 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
196 double bad = fabs(deviationWeight * dev) + 1e-20;
197 double quality = good / bad;
199 if (quality > currentBestQuality) {
200 currentBestQuality = quality;
208 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
212 Runway_iterator it = mRunways.begin();
213 FGRunway* result = NULL;
214 double currentLowestDev = 180.0;
216 for (; it != mRunways.end(); ++it) {
217 double inboundCourse = SGGeodesy::courseDeg(aPos, (*it)->end());
218 double dev = inboundCourse - (*it)->headingDeg();
219 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
222 if (dev < currentLowestDev) { // new best match
223 currentLowestDev = dev;
226 } // of runway iteration
232 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
236 unsigned int numRunways(mRunways.size());
237 for (unsigned int r=0; r<numRunways; ++r) {
238 FGRunway* rwy = mRunways[r];
239 if (rwy->isReciprocal()) {
240 continue; // we only care about lengths, so don't do work twice
243 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
244 return true; // we're done!
246 } // of runways iteration
251 unsigned int FGAirport::numTaxiways() const
254 return mTaxiways.size();
257 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
260 assert(aIndex >= 0 && aIndex < mTaxiways.size());
261 return mTaxiways[aIndex];
264 unsigned int FGAirport::numPavements() const
267 return mPavements.size();
270 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
273 assert(aIndex >= 0 && aIndex < mPavements.size());
274 return mPavements[aIndex];
277 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
278 vector<FGTaxiwayPtr>& txwys,
279 vector<FGPavementPtr>& pvts)
282 Runway_iterator it = mRunways.begin();
283 for (; it != mRunways.end(); ++it) {
284 (*it)->setAirport(this);
287 mTaxiways.swap(txwys);
288 mPavements.swap(pvts);
291 FGRunway* FGAirport::getActiveRunwayForUsage() const
293 static FGEnvironmentMgr* envMgr = NULL;
295 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
298 // This forces West-facing rwys to be used in no-wind situations
299 // which is consistent with Flightgear's initial setup.
303 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
305 double windSpeed = stationWeather.get_wind_speed_kt();
306 if (windSpeed > 0.0) {
307 hdg = stationWeather.get_wind_from_heading_deg();
311 return findBestRunwayForHeading(hdg);
314 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
316 AirportFilter aptFilter;
317 if (filter == NULL) {
321 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
326 return static_cast<FGAirport*>(r.ptr());
329 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
330 mMinLengthFt(minLengthFt)
334 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
336 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
339 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
343 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
345 return NULL; // we don't warn here, let the caller do that
347 return static_cast<FGAirport*>(r.ptr());
350 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
354 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
356 throw sg_range_exception("No such airport with ident: " + aIdent);
358 return static_cast<FGAirport*>(r.ptr());
361 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
363 // we delegate all the work to a horrible helper in FGPositioned, which can
364 // access the (private) index data.
365 return searchAirportNamesAndIdents(aFilter);
368 // find basic airport location info from airport database
369 const FGAirport *fgFindAirportID( const string& id)
375 return FGAirport::findByIdent(id);
378 void FGAirport::loadRunways() const
380 if (mRunwaysLoaded) {
381 return; // already loaded, great
384 mRunwaysLoaded = true;
385 loadSceneryDefinitions();
388 void FGAirport::loadTaxiways() const
390 if (mTaxiwaysLoaded) {
391 return; // already loaded, great
395 void FGAirport::loadProcedures() const
397 if (mProceduresLoaded) {
401 mProceduresLoaded = true;
403 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
404 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
408 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
409 RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
412 void FGAirport::loadSceneryDefinitions() const
415 SGPropertyNode_ptr rootNode = new SGPropertyNode;
416 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
417 readProperties(path.str(), rootNode);
418 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
421 // repeat for the tower data
422 rootNode = new SGPropertyNode;
423 if (XMLLoader::findAirportData(ident(), "twr", path)) {
424 readProperties(path.str(), rootNode);
425 const_cast<FGAirport*>(this)->readTowerData(rootNode);
429 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
431 SGPropertyNode* runway;
433 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
434 SGPropertyNode* t0 = runway->getChild("threshold", 0),
435 *t1 = runway->getChild("threshold", 1);
437 assert(t1); // too strict? mayeb we should finally allow single-ended runways
439 processThreshold(t0);
440 processThreshold(t1);
441 } // of runways iteration
444 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
446 // first, let's identify the current runway
447 string id(aThreshold->getStringValue("rwy"));
448 if (!hasRunwayWithIdent(id)) {
449 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
450 "found runway not defined in the global data:" << ident() << "/" << id);
454 FGRunway* rwy = getRunwayByIdent(id);
455 rwy->processThreshold(aThreshold);
458 void FGAirport::readTowerData(SGPropertyNode* aRoot)
460 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
461 double lat = twrNode->getDoubleValue("lat"),
462 lon = twrNode->getDoubleValue("lon"),
463 elevM = twrNode->getDoubleValue("elev-m");
464 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
465 // scenery for a precise terrain elevation, we use the field elevation
466 // (this is also what the apt.dat code does)
467 double fieldElevationM = geod().getElevationM();
469 _tower_location = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
472 void FGAirport::addSID(flightgear::SID* aSid)
474 mSIDs.push_back(aSid);
477 void FGAirport::addSTAR(STAR* aStar)
479 mSTARs.push_back(aStar);
482 void FGAirport::addApproach(Approach* aApp)
484 mApproaches.push_back(aApp);
487 unsigned int FGAirport::numSIDs() const
493 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
496 return mSIDs[aIndex];
499 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
502 for (unsigned int i=0; i<mSIDs.size(); ++i) {
503 if (mSIDs[i]->ident() == aIdent) {
511 unsigned int FGAirport::numSTARs() const
514 return mSTARs.size();
517 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
520 return mSTARs[aIndex];
523 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
526 for (unsigned int i=0; i<mSTARs.size(); ++i) {
527 if (mSTARs[i]->ident() == aIdent) {
535 unsigned int FGAirport::numApproaches() const
538 return mApproaches.size();
541 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
544 return mApproaches[aIndex];
547 Approach* FGAirport::findApproachWithIdent(const std::string& aIdent) const
550 for (unsigned int i=0; i<mApproaches.size(); ++i) {
551 if (mApproaches[i]->ident() == aIdent) {
552 return mApproaches[i];
559 void FGAirport::setCommStations(CommStationList& comms)
561 mCommStations.swap(comms);
562 for (unsigned int c=0; c<mCommStations.size(); ++c) {
563 mCommStations[c]->setAirport(this);
568 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
570 CommStationList result;
571 for (unsigned int c=0; c<mCommStations.size(); ++c) {
572 if (mCommStations[c]->type() == aTy) {
573 result.push_back(mCommStations[c]);
579 // get airport elevation
580 double fgGetAirportElev( const string& id )
582 const FGAirport *a=fgFindAirportID( id);
584 return a->getElevation();
591 // get airport position
592 SGGeod fgGetAirportPos( const string& id )
594 const FGAirport *a = fgFindAirportID( id);
597 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
599 return SGGeod::fromDegM(0.0, 0.0, -9999.0);