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),
66 FGAirport::~FGAirport()
71 bool FGAirport::isAirport() const
73 return type() == AIRPORT;
76 bool FGAirport::isSeaport() const
78 return type() == SEAPORT;
81 bool FGAirport::isHeliport() const
83 return type() == HELIPORT;
86 FGAirportDynamics * FGAirport::getDynamics()
91 //cerr << "Trying to load dynamics for " << _id << endl;
92 _dynamics = new FGAirportDynamics(this);
93 XMLLoader::load(_dynamics);
95 FGRunwayPreference rwyPrefs(this);
96 XMLLoader::load(&rwyPrefs);
97 _dynamics->setRwyUse(rwyPrefs);
99 //FGSidStar SIDs(this);
100 XMLLoader::load(_dynamics->getSIDs());
105 unsigned int FGAirport::numRunways() const
107 return mRunways.size();
110 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
112 assert(aIndex >= 0 && aIndex < mRunways.size());
113 return mRunways[aIndex];
116 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
118 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
121 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
123 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
124 if (it == mRunways.end()) {
125 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
126 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
132 FGAirport::Runway_iterator
133 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
135 string ident(aIdent);
136 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
137 ident = "0" + aIdent;
140 Runway_iterator it = mRunways.begin();
141 for (; it != mRunways.end(); ++it) {
142 if ((*it)->ident() == ident) {
150 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
152 Runway_iterator it = mRunways.begin();
153 FGRunway* result = NULL;
154 double currentBestQuality = 0.0;
156 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
157 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
158 double widthWeight = param->getDoubleValue("width-weight", 0.01);
159 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
160 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
162 for (; it != mRunways.end(); ++it) {
163 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
165 double dev = aHeading - (*it)->headingDeg();
166 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
167 double bad = fabs(deviationWeight * dev) + 1e-20;
168 double quality = good / bad;
170 if (quality > currentBestQuality) {
171 currentBestQuality = quality;
179 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
181 unsigned int numRunways(mRunways.size());
182 for (unsigned int r=0; r<numRunways; ++r) {
183 FGRunway* rwy = mRunways[r];
184 if (rwy->isReciprocal()) {
185 continue; // we only care about lengths, so don't do work twice
188 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
189 return true; // we're done!
191 } // of runways iteration
196 unsigned int FGAirport::numTaxiways() const
198 return mTaxiways.size();
201 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
203 assert(aIndex >= 0 && aIndex < mTaxiways.size());
204 return mTaxiways[aIndex];
207 unsigned int FGAirport::numPavements() const
209 return mPavements.size();
212 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
214 assert(aIndex >= 0 && aIndex < mPavements.size());
215 return mPavements[aIndex];
218 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
219 vector<FGTaxiwayPtr>& txwys,
220 vector<FGPavementPtr>& pvts)
223 Runway_iterator it = mRunways.begin();
224 for (; it != mRunways.end(); ++it) {
225 (*it)->setAirport(this);
228 mTaxiways.swap(txwys);
229 mPavements.swap(pvts);
232 FGRunway* FGAirport::getActiveRunwayForUsage() const
234 static FGEnvironmentMgr* envMgr = NULL;
236 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
239 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
241 double windSpeed = stationWeather.get_wind_speed_kt();
242 double hdg = stationWeather.get_wind_from_heading_deg();
243 if (windSpeed <= 0.0) {
244 hdg = 270; // This forces West-facing rwys to be used in no-wind situations
245 // which is consistent with Flightgear's initial setup.
248 return findBestRunwayForHeading(hdg);
251 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
253 AirportFilter aptFilter;
254 if (filter == NULL) {
258 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
263 return static_cast<FGAirport*>(r.ptr());
266 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
267 mMinLengthFt(minLengthFt)
271 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
273 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
276 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
279 AirportFilter filter;
280 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
282 return NULL; // we don't warn here, let the caller do that
284 return static_cast<FGAirport*>(r.ptr());
287 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
290 AirportFilter filter;
291 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
293 throw sg_range_exception("No such airport with ident: " + aIdent);
295 return static_cast<FGAirport*>(r.ptr());
298 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
300 // we delegate all the work to a horrible helper in FGPositioned, which can
301 // access the (private) index data.
302 return searchAirportNamesAndIdents(aFilter);
305 // find basic airport location info from airport database
306 const FGAirport *fgFindAirportID( const string& id)
312 return FGAirport::findByIdent(id);
316 void FGAirport::loadSceneryDefintions() const
320 // allow users to disable the scenery data in the short-term
321 // longer term, this option can probably disappear
322 if (fgGetBool("/sim/use-scenery-airport-data") == false) {
327 SGPropertyNode_ptr rootNode = new SGPropertyNode;
328 if (XMLLoader::findAirportData(ident(), "threshold", path)) {
329 readProperties(path.str(), rootNode);
330 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
333 // repeat for the tower data
334 rootNode = new SGPropertyNode;
335 if (XMLLoader::findAirportData(ident(), "twr", path)) {
336 readProperties(path.str(), rootNode);
337 const_cast<FGAirport*>(this)->readTowerData(rootNode);
341 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
343 SGPropertyNode* runway;
345 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
346 SGPropertyNode* t0 = runway->getChild("threshold", 0),
347 *t1 = runway->getChild("threshold", 1);
349 assert(t1); // too strict? mayeb we should finally allow single-ended runways
351 processThreshold(t0);
352 processThreshold(t1);
353 } // of runways iteration
356 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
358 // first, let's identify the current runway
359 string id(aThreshold->getStringValue("rwy"));
360 if (!hasRunwayWithIdent(id)) {
361 SG_LOG(SG_GENERAL, SG_WARN, "FGAirport::processThreshold: "
362 "found runway not defined in the global data:" << ident() << "/" << id);
366 FGRunway* rwy = getRunwayByIdent(id);
367 rwy->processThreshold(aThreshold);
370 void FGAirport::readTowerData(SGPropertyNode* aRoot)
372 SGPropertyNode* twrNode = aRoot->getChild("twr");
373 double lat = twrNode->getDoubleValue("lat"),
374 lon = twrNode->getDoubleValue("lon"),
375 elevM = twrNode->getDoubleValue("elev-m");
377 _tower_location = SGGeod::fromDegM(lon, lat, elevM);
380 // get airport elevation
381 double fgGetAirportElev( const string& id )
383 const FGAirport *a=fgFindAirportID( id);
385 return a->getElevation();
392 // get airport position
393 SGGeod fgGetAirportPos( const string& id )
395 const FGAirport *a = fgFindAirportID( id);
398 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
400 return SGGeod::fromDegM(0.0, 0.0, -9999.0);