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 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
255 double windSpeed = stationWeather.get_wind_speed_kt();
256 double hdg = stationWeather.get_wind_from_heading_deg();
257 if (windSpeed <= 0.0) {
258 hdg = 270; // This forces West-facing rwys to be used in no-wind situations
259 // which is consistent with Flightgear's initial setup.
262 return findBestRunwayForHeading(hdg);
265 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
267 AirportFilter aptFilter;
268 if (filter == NULL) {
272 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
277 return static_cast<FGAirport*>(r.ptr());
280 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
281 mMinLengthFt(minLengthFt)
285 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
287 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
290 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
293 AirportFilter filter;
294 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
296 return NULL; // we don't warn here, let the caller do that
298 return static_cast<FGAirport*>(r.ptr());
301 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
304 AirportFilter filter;
305 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
307 throw sg_range_exception("No such airport with ident: " + aIdent);
309 return static_cast<FGAirport*>(r.ptr());
312 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
314 // we delegate all the work to a horrible helper in FGPositioned, which can
315 // access the (private) index data.
316 return searchAirportNamesAndIdents(aFilter);
319 // find basic airport location info from airport database
320 const FGAirport *fgFindAirportID( const string& id)
326 return FGAirport::findByIdent(id);
329 void FGAirport::loadRunways() const
331 if (mRunwaysLoaded) {
332 return; // already loaded, great
335 mRunwaysLoaded = true;
336 loadSceneryDefintions();
339 void FGAirport::loadTaxiways() const
341 if (mTaxiwaysLoaded) {
342 return; // already loaded, great
346 void FGAirport::loadSceneryDefintions() const
348 // allow users to disable the scenery data in the short-term
349 // longer term, this option can probably disappear
350 if (!fgGetBool("/sim/use-scenery-airport-data")) {
355 SGPropertyNode_ptr rootNode = new SGPropertyNode;
356 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
357 readProperties(path.str(), rootNode);
358 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
361 // repeat for the tower data
362 rootNode = new SGPropertyNode;
363 if (XMLLoader::findAirportData(ident(), "twr", path)) {
364 readProperties(path.str(), rootNode);
365 const_cast<FGAirport*>(this)->readTowerData(rootNode);
369 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
371 SGPropertyNode* runway;
373 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
374 SGPropertyNode* t0 = runway->getChild("threshold", 0),
375 *t1 = runway->getChild("threshold", 1);
377 assert(t1); // too strict? mayeb we should finally allow single-ended runways
379 processThreshold(t0);
380 processThreshold(t1);
381 } // of runways iteration
384 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
386 // first, let's identify the current runway
387 string id(aThreshold->getStringValue("rwy"));
388 if (!hasRunwayWithIdent(id)) {
389 SG_LOG(SG_GENERAL, SG_WARN, "FGAirport::processThreshold: "
390 "found runway not defined in the global data:" << ident() << "/" << id);
394 FGRunway* rwy = getRunwayByIdent(id);
395 rwy->processThreshold(aThreshold);
398 void FGAirport::readTowerData(SGPropertyNode* aRoot)
400 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
401 double lat = twrNode->getDoubleValue("lat"),
402 lon = twrNode->getDoubleValue("lon"),
403 elevM = twrNode->getDoubleValue("elev-m");
405 _tower_location = SGGeod::fromDegM(lon, lat, elevM);
408 // get airport elevation
409 double fgGetAirportElev( const string& id )
411 const FGAirport *a=fgFindAirportID( id);
413 return a->getElevation();
420 // get airport position
421 SGGeod fgGetAirportPos( const string& id )
423 const FGAirport *a = fgFindAirportID( id);
426 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
428 return SGGeod::fromDegM(0.0, 0.0, -9999.0);