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.
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/props/props.hxx>
35 #include <simgear/props/props_io.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/sg_inlines.h>
39 #include <Environment/environment_mgr.hxx>
40 #include <Environment/environment.hxx>
41 #include <Main/fg_props.hxx>
42 #include <Airports/runways.hxx>
43 #include <Airports/pavement.hxx>
44 #include <Airports/dynamics.hxx>
45 #include <Airports/xmlloader.hxx>
47 // magic import of a helper which uses FGPositioned internals
48 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
50 /***************************************************************************
52 ***************************************************************************/
54 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
55 const string &name, bool has_metar, Type aType) :
56 FGPositioned(aType, id, location),
57 _tower_location(tower_location),
59 _has_metar(has_metar),
61 mRunwaysLoaded(false),
67 FGAirport::~FGAirport()
72 bool FGAirport::isAirport() const
74 return type() == AIRPORT;
77 bool FGAirport::isSeaport() const
79 return type() == SEAPORT;
82 bool FGAirport::isHeliport() const
84 return type() == HELIPORT;
87 FGAirportDynamics * FGAirport::getDynamics()
92 //cerr << "Trying to load dynamics for " << _id << endl;
93 _dynamics = new FGAirportDynamics(this);
94 XMLLoader::load(_dynamics);
96 FGRunwayPreference rwyPrefs(this);
97 XMLLoader::load(&rwyPrefs);
98 _dynamics->setRwyUse(rwyPrefs);
100 //FGSidStar SIDs(this);
101 XMLLoader::load(_dynamics->getSIDs());
106 unsigned int FGAirport::numRunways() const
109 return mRunways.size();
112 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
116 assert(aIndex >= 0 && aIndex < mRunways.size());
117 return mRunways[aIndex];
120 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
122 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
125 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
127 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
128 if (it == mRunways.end()) {
129 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
130 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
136 FGAirport::Runway_iterator
137 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
141 string ident(aIdent);
142 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
143 ident = "0" + aIdent;
146 Runway_iterator it = mRunways.begin();
147 for (; it != mRunways.end(); ++it) {
148 if ((*it)->ident() == ident) {
156 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
160 Runway_iterator it = mRunways.begin();
161 FGRunway* result = NULL;
162 double currentBestQuality = 0.0;
164 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
165 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
166 double widthWeight = param->getDoubleValue("width-weight", 0.01);
167 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
168 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
170 for (; it != mRunways.end(); ++it) {
171 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
173 double dev = aHeading - (*it)->headingDeg();
174 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
175 double bad = fabs(deviationWeight * dev) + 1e-20;
176 double quality = good / bad;
178 if (quality > currentBestQuality) {
179 currentBestQuality = quality;
187 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
191 unsigned int numRunways(mRunways.size());
192 for (unsigned int r=0; r<numRunways; ++r) {
193 FGRunway* rwy = mRunways[r];
194 if (rwy->isReciprocal()) {
195 continue; // we only care about lengths, so don't do work twice
198 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
199 return true; // we're done!
201 } // of runways iteration
206 unsigned int FGAirport::numTaxiways() const
209 return mTaxiways.size();
212 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
215 assert(aIndex >= 0 && aIndex < mTaxiways.size());
216 return mTaxiways[aIndex];
219 unsigned int FGAirport::numPavements() const
222 return mPavements.size();
225 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
228 assert(aIndex >= 0 && aIndex < mPavements.size());
229 return mPavements[aIndex];
232 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
233 vector<FGTaxiwayPtr>& txwys,
234 vector<FGPavementPtr>& pvts)
237 Runway_iterator it = mRunways.begin();
238 for (; it != mRunways.end(); ++it) {
239 (*it)->setAirport(this);
242 mTaxiways.swap(txwys);
243 mPavements.swap(pvts);
246 FGRunway* FGAirport::getActiveRunwayForUsage() const
248 static FGEnvironmentMgr* envMgr = NULL;
250 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
253 // This forces West-facing rwys to be used in no-wind situations
254 // which is consistent with Flightgear's initial setup.
258 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
260 double windSpeed = stationWeather.get_wind_speed_kt();
261 if (windSpeed > 0.0) {
262 hdg = stationWeather.get_wind_from_heading_deg();
266 return findBestRunwayForHeading(hdg);
269 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
271 AirportFilter aptFilter;
272 if (filter == NULL) {
276 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
281 return static_cast<FGAirport*>(r.ptr());
284 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
285 mMinLengthFt(minLengthFt)
289 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
291 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
294 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
297 AirportFilter filter;
298 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
300 return NULL; // we don't warn here, let the caller do that
302 return static_cast<FGAirport*>(r.ptr());
305 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
308 AirportFilter filter;
309 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
311 throw sg_range_exception("No such airport with ident: " + aIdent);
313 return static_cast<FGAirport*>(r.ptr());
316 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
318 // we delegate all the work to a horrible helper in FGPositioned, which can
319 // access the (private) index data.
320 return searchAirportNamesAndIdents(aFilter);
323 // find basic airport location info from airport database
324 const FGAirport *fgFindAirportID( const string& id)
330 return FGAirport::findByIdent(id);
333 void FGAirport::loadRunways() const
335 if (mRunwaysLoaded) {
336 return; // already loaded, great
339 mRunwaysLoaded = true;
340 loadSceneryDefintions();
343 void FGAirport::loadTaxiways() const
345 if (mTaxiwaysLoaded) {
346 return; // already loaded, great
350 void FGAirport::loadSceneryDefintions() const
352 // allow users to disable the scenery data in the short-term
353 // longer term, this option can probably disappear
354 if (!fgGetBool("/sim/use-scenery-airport-data")) {
359 SGPropertyNode_ptr rootNode = new SGPropertyNode;
360 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
361 readProperties(path.str(), rootNode);
362 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
365 // repeat for the tower data
366 rootNode = new SGPropertyNode;
367 if (XMLLoader::findAirportData(ident(), "twr", path)) {
368 readProperties(path.str(), rootNode);
369 const_cast<FGAirport*>(this)->readTowerData(rootNode);
373 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
375 SGPropertyNode* runway;
377 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
378 SGPropertyNode* t0 = runway->getChild("threshold", 0),
379 *t1 = runway->getChild("threshold", 1);
381 assert(t1); // too strict? mayeb we should finally allow single-ended runways
383 processThreshold(t0);
384 processThreshold(t1);
385 } // of runways iteration
388 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
390 // first, let's identify the current runway
391 string id(aThreshold->getStringValue("rwy"));
392 if (!hasRunwayWithIdent(id)) {
393 SG_LOG(SG_GENERAL, SG_WARN, "FGAirport::processThreshold: "
394 "found runway not defined in the global data:" << ident() << "/" << id);
398 FGRunway* rwy = getRunwayByIdent(id);
399 rwy->processThreshold(aThreshold);
402 void FGAirport::readTowerData(SGPropertyNode* aRoot)
404 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
405 double lat = twrNode->getDoubleValue("lat"),
406 lon = twrNode->getDoubleValue("lon"),
407 elevM = twrNode->getDoubleValue("elev-m");
409 _tower_location = SGGeod::fromDegM(lon, lat, elevM);
412 // get airport elevation
413 double fgGetAirportElev( const string& id )
415 const FGAirport *a=fgFindAirportID( id);
417 return a->getElevation();
424 // get airport position
425 SGGeod fgGetAirportPos( const string& id )
427 const FGAirport *a = fgFindAirportID( id);
430 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
432 return SGGeod::fromDegM(0.0, 0.0, -9999.0);