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/debug/logstream.hxx>
36 #include <simgear/sg_inlines.h>
38 #include <Environment/environment_mgr.hxx>
39 #include <Environment/environment.hxx>
40 #include <Main/fg_props.hxx>
41 #include <Airports/runways.hxx>
42 #include <Airports/dynamics.hxx>
43 #include <Airports/xmlloader.hxx>
45 // magic import of a helper which uses FGPositioned internals
46 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
48 /***************************************************************************
50 ***************************************************************************/
52 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
53 const string &name, bool has_metar, Type aType) :
54 FGPositioned(aType, id, location),
55 _tower_location(tower_location),
57 _has_metar(has_metar),
63 FGAirport::~FGAirport()
68 bool FGAirport::isAirport() const
70 return type() == AIRPORT;
73 bool FGAirport::isSeaport() const
75 return type() == SEAPORT;
78 bool FGAirport::isHeliport() const
80 return type() == HELIPORT;
83 FGAirportDynamics * FGAirport::getDynamics()
88 //cerr << "Trying to load dynamics for " << _id << endl;
89 _dynamics = new FGAirportDynamics(this);
90 XMLLoader::load(_dynamics);
92 FGRunwayPreference rwyPrefs(this);
93 XMLLoader::load(&rwyPrefs);
94 _dynamics->setRwyUse(rwyPrefs);
99 unsigned int FGAirport::numRunways() const
101 return mRunways.size();
104 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
106 assert(aIndex >= 0 && aIndex < mRunways.size());
107 return mRunways[aIndex];
110 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
112 return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
115 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
117 Runway_iterator it = getIteratorForRunwayIdent(aIdent);
118 if (it == mRunways.end()) {
119 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
120 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
126 FGAirport::Runway_iterator
127 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
129 string ident(aIdent);
130 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
131 ident = "0" + aIdent;
134 Runway_iterator it = mRunways.begin();
135 for (; it != mRunways.end(); ++it) {
136 if ((*it)->ident() == ident) {
144 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
146 Runway_iterator it = mRunways.begin();
147 FGRunway* result = NULL;
148 double currentBestQuality = 0.0;
150 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
151 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
152 double widthWeight = param->getDoubleValue("width-weight", 0.01);
153 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
154 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
156 for (; it != mRunways.end(); ++it) {
157 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
159 double dev = aHeading - (*it)->headingDeg();
160 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
161 double bad = fabs(deviationWeight * dev) + 1e-20;
162 double quality = good / bad;
164 if (quality > currentBestQuality) {
165 currentBestQuality = quality;
173 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
175 unsigned int numRunways(mRunways.size());
176 for (unsigned int r=0; r<numRunways; ++r) {
177 FGRunway* rwy = mRunways[r];
178 if (rwy->isReciprocal()) {
179 continue; // we only care about lengths, so don't do work twice
182 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
183 return true; // we're done!
185 } // of runways iteration
190 unsigned int FGAirport::numTaxiways() const
192 return mTaxiways.size();
195 FGRunway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
197 assert(aIndex >= 0 && aIndex < mTaxiways.size());
198 return mTaxiways[aIndex];
201 void FGAirport::addRunway(FGRunway* aRunway)
203 aRunway->setAirport(this);
205 if (aRunway->isTaxiway()) {
206 mTaxiways.push_back(aRunway);
208 mRunways.push_back(aRunway);
212 FGRunway* FGAirport::getActiveRunwayForUsage() const
214 static FGEnvironmentMgr* envMgr = NULL;
216 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
219 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
221 double windSpeed = stationWeather.get_wind_speed_kt();
222 double hdg = stationWeather.get_wind_from_heading_deg();
223 if (windSpeed <= 0.0) {
224 hdg = 270; // This forces West-facing rwys to be used in no-wind situations
225 // which is consistent with Flightgear's initial setup.
228 return findBestRunwayForHeading(hdg);
231 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
233 AirportFilter aptFilter;
234 if (filter == NULL) {
238 FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
243 return static_cast<FGAirport*>(r.ptr());
246 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
247 mMinLengthFt(minLengthFt)
251 bool FGAirport::HardSurfaceFilter::pass(FGPositioned* aPos) const
253 if (aPos->type() != AIRPORT) {
254 return false; // exclude seaports and heliports as well, we need a runways
257 return static_cast<FGAirport*>(aPos)->hasHardRunwayOfLengthFt(mMinLengthFt);
260 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
263 AirportFilter filter;
264 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
266 return NULL; // we don't warn here, let the caller do that
268 return static_cast<FGAirport*>(r.ptr());
271 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
274 AirportFilter filter;
275 r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
277 throw sg_range_exception("No such airport with ident: " + aIdent);
279 return static_cast<FGAirport*>(r.ptr());
282 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
284 // we delegate all the work to a horrible helper in FGPositioned, which can
285 // access the (private) index data.
286 return searchAirportNamesAndIdents(aFilter);
289 // find basic airport location info from airport database
290 const FGAirport *fgFindAirportID( const string& id)
296 return FGAirport::findByIdent(id);
300 // get airport elevation
301 double fgGetAirportElev( const string& id )
303 SG_LOG( SG_GENERAL, SG_BULK,
304 "Finding elevation for airport: " << id );
306 const FGAirport *a=fgFindAirportID( id);
308 return a->getElevation();
315 // get airport position
316 Point3D fgGetAirportPos( const string& id )
318 SG_LOG( SG_ATC, SG_BULK,
319 "Finding position for airport: " << id );
321 const FGAirport *a = fgFindAirportID( id);
324 return Point3D(a->getLongitude(), a->getLatitude(), a->getElevation());
326 return Point3D(0.0, 0.0, -9999.0);