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>
49 // magic import of a helper which uses FGPositioned internals
50 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
52 /***************************************************************************
54 ***************************************************************************/
56 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
57 const string &name, bool has_metar, Type aType) :
58 FGPositioned(aType, id, location),
59 _tower_location(tower_location),
61 _has_metar(has_metar),
63 mRunwaysLoaded(false),
69 FGAirport::~FGAirport()
74 bool FGAirport::isAirport() const
76 return type() == AIRPORT;
79 bool FGAirport::isSeaport() const
81 return type() == SEAPORT;
84 bool FGAirport::isHeliport() const
86 return type() == HELIPORT;
89 FGAirportDynamics * FGAirport::getDynamics()
94 //cerr << "Trying to load dynamics for " << _id << endl;
95 _dynamics = new FGAirportDynamics(this);
96 XMLLoader::load(_dynamics);
98 FGRunwayPreference rwyPrefs(this);
99 XMLLoader::load(&rwyPrefs);
100 _dynamics->setRwyUse(rwyPrefs);
102 //FGSidStar SIDs(this);
103 XMLLoader::load(_dynamics->getSIDs());
108 unsigned int FGAirport::numRunways() const
111 return mRunways.size();
114 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
118 assert(aIndex >= 0 && aIndex < mRunways.size());
119 return mRunways[aIndex];
122 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
124 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
127 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
129 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
130 if (it == mRunways.end()) {
131 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
132 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
138 FGAirport::Runway_iterator
139 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
143 string ident(aIdent);
144 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
145 ident = "0" + aIdent;
148 Runway_iterator it = mRunways.begin();
149 for (; it != mRunways.end(); ++it) {
150 if ((*it)->ident() == ident) {
158 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
162 Runway_iterator it = mRunways.begin();
163 FGRunway* result = NULL;
164 double currentBestQuality = 0.0;
166 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
167 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
168 double widthWeight = param->getDoubleValue("width-weight", 0.01);
169 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
170 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
172 for (; it != mRunways.end(); ++it) {
173 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
175 double dev = aHeading - (*it)->headingDeg();
176 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
177 double bad = fabs(deviationWeight * dev) + 1e-20;
178 double quality = good / bad;
180 if (quality > currentBestQuality) {
181 currentBestQuality = quality;
189 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
193 unsigned int numRunways(mRunways.size());
194 for (unsigned int r=0; r<numRunways; ++r) {
195 FGRunway* rwy = mRunways[r];
196 if (rwy->isReciprocal()) {
197 continue; // we only care about lengths, so don't do work twice
200 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
201 return true; // we're done!
203 } // of runways iteration
208 unsigned int FGAirport::numTaxiways() const
211 return mTaxiways.size();
214 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
217 assert(aIndex >= 0 && aIndex < mTaxiways.size());
218 return mTaxiways[aIndex];
221 unsigned int FGAirport::numPavements() const
224 return mPavements.size();
227 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
230 assert(aIndex >= 0 && aIndex < mPavements.size());
231 return mPavements[aIndex];
234 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
235 vector<FGTaxiwayPtr>& txwys,
236 vector<FGPavementPtr>& pvts)
239 Runway_iterator it = mRunways.begin();
240 for (; it != mRunways.end(); ++it) {
241 (*it)->setAirport(this);
244 mTaxiways.swap(txwys);
245 mPavements.swap(pvts);
248 FGRunway* FGAirport::getActiveRunwayForUsage() const
250 static FGEnvironmentMgr* envMgr = NULL;
252 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
255 // This forces West-facing rwys to be used in no-wind situations
256 // which is consistent with Flightgear's initial setup.
260 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
262 double windSpeed = stationWeather.get_wind_speed_kt();
263 if (windSpeed > 0.0) {
264 hdg = stationWeather.get_wind_from_heading_deg();
268 return findBestRunwayForHeading(hdg);
271 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
273 AirportFilter aptFilter;
274 if (filter == NULL) {
278 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
283 return static_cast<FGAirport*>(r.ptr());
286 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
287 mMinLengthFt(minLengthFt)
291 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
293 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
296 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
300 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
302 return NULL; // we don't warn here, let the caller do that
304 return static_cast<FGAirport*>(r.ptr());
307 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
311 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
313 throw sg_range_exception("No such airport with ident: " + aIdent);
315 return static_cast<FGAirport*>(r.ptr());
318 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
320 // we delegate all the work to a horrible helper in FGPositioned, which can
321 // access the (private) index data.
322 return searchAirportNamesAndIdents(aFilter);
325 // find basic airport location info from airport database
326 const FGAirport *fgFindAirportID( const string& id)
332 return FGAirport::findByIdent(id);
335 void FGAirport::loadRunways() const
337 if (mRunwaysLoaded) {
338 return; // already loaded, great
341 mRunwaysLoaded = true;
342 loadSceneryDefintions();
345 void FGAirport::loadTaxiways() const
347 if (mTaxiwaysLoaded) {
348 return; // already loaded, great
352 void FGAirport::loadSceneryDefintions() const
354 // allow users to disable the scenery data in the short-term
355 // longer term, this option can probably disappear
356 if (!fgGetBool("/sim/paths/use-custom-scenery-data")) {
361 SGPropertyNode_ptr rootNode = new SGPropertyNode;
362 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
363 readProperties(path.str(), rootNode);
364 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
367 // repeat for the tower data
368 rootNode = new SGPropertyNode;
369 if (XMLLoader::findAirportData(ident(), "twr", path)) {
370 readProperties(path.str(), rootNode);
371 const_cast<FGAirport*>(this)->readTowerData(rootNode);
375 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
377 SGPropertyNode* runway;
379 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
380 SGPropertyNode* t0 = runway->getChild("threshold", 0),
381 *t1 = runway->getChild("threshold", 1);
383 assert(t1); // too strict? mayeb we should finally allow single-ended runways
385 processThreshold(t0);
386 processThreshold(t1);
387 } // of runways iteration
390 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
392 // first, let's identify the current runway
393 string id(aThreshold->getStringValue("rwy"));
394 if (!hasRunwayWithIdent(id)) {
395 SG_LOG(SG_GENERAL, SG_WARN, "FGAirport::processThreshold: "
396 "found runway not defined in the global data:" << ident() << "/" << id);
400 FGRunway* rwy = getRunwayByIdent(id);
401 rwy->processThreshold(aThreshold);
404 void FGAirport::readTowerData(SGPropertyNode* aRoot)
406 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
407 double lat = twrNode->getDoubleValue("lat"),
408 lon = twrNode->getDoubleValue("lon"),
409 elevM = twrNode->getDoubleValue("elev-m");
411 _tower_location = SGGeod::fromDegM(lon, lat, elevM);
414 // get airport elevation
415 double fgGetAirportElev( const string& id )
417 const FGAirport *a=fgFindAirportID( id);
419 return a->getElevation();
426 // get airport position
427 SGGeod fgGetAirportPos( const string& id )
429 const FGAirport *a = fgFindAirportID( id);
432 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
434 return SGGeod::fromDegM(0.0, 0.0, -9999.0);