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)
332 if (minLengthFt < 0.0) {
333 mMinLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
337 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
339 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
342 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
346 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
348 return NULL; // we don't warn here, let the caller do that
350 return static_cast<FGAirport*>(r.ptr());
353 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
357 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
359 throw sg_range_exception("No such airport with ident: " + aIdent);
361 return static_cast<FGAirport*>(r.ptr());
364 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
366 // we delegate all the work to a horrible helper in FGPositioned, which can
367 // access the (private) index data.
368 return searchAirportNamesAndIdents(aFilter);
371 // find basic airport location info from airport database
372 const FGAirport *fgFindAirportID( const string& id)
378 return FGAirport::findByIdent(id);
381 void FGAirport::loadRunways() const
383 if (mRunwaysLoaded) {
384 return; // already loaded, great
387 mRunwaysLoaded = true;
388 loadSceneryDefinitions();
391 void FGAirport::loadTaxiways() const
393 if (mTaxiwaysLoaded) {
394 return; // already loaded, great
398 void FGAirport::loadProcedures() const
400 if (mProceduresLoaded) {
404 mProceduresLoaded = true;
406 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
407 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
411 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
412 RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
415 void FGAirport::loadSceneryDefinitions() const
418 SGPropertyNode_ptr rootNode = new SGPropertyNode;
419 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
420 readProperties(path.str(), rootNode);
421 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
424 // repeat for the tower data
425 rootNode = new SGPropertyNode;
426 if (XMLLoader::findAirportData(ident(), "twr", path)) {
427 readProperties(path.str(), rootNode);
428 const_cast<FGAirport*>(this)->readTowerData(rootNode);
432 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
434 SGPropertyNode* runway;
436 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
437 SGPropertyNode* t0 = runway->getChild("threshold", 0),
438 *t1 = runway->getChild("threshold", 1);
440 assert(t1); // too strict? mayeb we should finally allow single-ended runways
442 processThreshold(t0);
443 processThreshold(t1);
444 } // of runways iteration
447 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
449 // first, let's identify the current runway
450 string id(aThreshold->getStringValue("rwy"));
451 if (!hasRunwayWithIdent(id)) {
452 SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
453 "found runway not defined in the global data:" << ident() << "/" << id);
457 FGRunway* rwy = getRunwayByIdent(id);
458 rwy->processThreshold(aThreshold);
461 void FGAirport::readTowerData(SGPropertyNode* aRoot)
463 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
464 double lat = twrNode->getDoubleValue("lat"),
465 lon = twrNode->getDoubleValue("lon"),
466 elevM = twrNode->getDoubleValue("elev-m");
467 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
468 // scenery for a precise terrain elevation, we use the field elevation
469 // (this is also what the apt.dat code does)
470 double fieldElevationM = geod().getElevationM();
472 _tower_location = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
475 void FGAirport::addSID(flightgear::SID* aSid)
477 mSIDs.push_back(aSid);
480 void FGAirport::addSTAR(STAR* aStar)
482 mSTARs.push_back(aStar);
485 void FGAirport::addApproach(Approach* aApp)
487 mApproaches.push_back(aApp);
490 unsigned int FGAirport::numSIDs() const
496 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
499 return mSIDs[aIndex];
502 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
505 for (unsigned int i=0; i<mSIDs.size(); ++i) {
506 if (mSIDs[i]->ident() == aIdent) {
514 unsigned int FGAirport::numSTARs() const
517 return mSTARs.size();
520 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
523 return mSTARs[aIndex];
526 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
529 for (unsigned int i=0; i<mSTARs.size(); ++i) {
530 if (mSTARs[i]->ident() == aIdent) {
538 unsigned int FGAirport::numApproaches() const
541 return mApproaches.size();
544 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
547 return mApproaches[aIndex];
550 Approach* FGAirport::findApproachWithIdent(const std::string& aIdent) const
553 for (unsigned int i=0; i<mApproaches.size(); ++i) {
554 if (mApproaches[i]->ident() == aIdent) {
555 return mApproaches[i];
562 void FGAirport::setCommStations(CommStationList& comms)
564 mCommStations.swap(comms);
565 for (unsigned int c=0; c<mCommStations.size(); ++c) {
566 mCommStations[c]->setAirport(this);
571 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
573 CommStationList result;
574 for (unsigned int c=0; c<mCommStations.size(); ++c) {
575 if (mCommStations[c]->type() == aTy) {
576 result.push_back(mCommStations[c]);
582 // get airport elevation
583 double fgGetAirportElev( const string& id )
585 const FGAirport *a=fgFindAirportID( id);
587 return a->getElevation();
594 // get airport position
595 SGGeod fgGetAirportPos( const string& id )
597 const FGAirport *a = fgFindAirportID( id);
600 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
602 return SGGeod::fromDegM(0.0, 0.0, -9999.0);