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.
34 #include <simgear/compiler.h>
36 #include <Environment/environment_mgr.hxx>
37 #include <Environment/environment.hxx>
38 #include <simgear/misc/sg_path.hxx>
39 #include <simgear/props/props.hxx>
40 #include <simgear/structure/subsystem_mgr.hxx>
41 #include <simgear/debug/logstream.hxx>
42 #include <Main/globals.hxx>
43 #include <Main/fg_props.hxx>
44 #include <Airports/runways.hxx>
45 #include <Airports/dynamics.hxx>
50 #include "xmlloader.hxx"
53 using std::random_shuffle;
58 /***************************************************************************
60 ***************************************************************************/
62 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
63 const string &name, bool has_metar, Type aType) :
64 FGPositioned(aType, id, location),
65 _tower_location(tower_location),
67 _has_metar(has_metar),
73 FGAirport::~FGAirport()
78 bool FGAirport::isAirport() const
80 return type() == AIRPORT;
83 bool FGAirport::isSeaport() const
85 return type() == SEAPORT;
88 bool FGAirport::isHeliport() const
90 return type() == HELIPORT;
93 FGAirportDynamics * FGAirport::getDynamics()
98 //cerr << "Trying to load dynamics for " << _id << endl;
99 _dynamics = new FGAirportDynamics(this);
100 XMLLoader::load(_dynamics);
102 FGRunwayPreference rwyPrefs(this);
103 XMLLoader::load(&rwyPrefs);
104 _dynamics->setRwyUse(rwyPrefs);
109 unsigned int FGAirport::numRunways() const
111 return mRunways.size();
114 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
139 string ident(aIdent);
140 if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
141 ident = "0" + aIdent;
144 Runway_iterator it = mRunways.begin();
145 for (; it != mRunways.end(); ++it) {
146 if ((*it)->ident() == ident) {
154 static double normaliseBearing(double aBearing)
156 while (aBearing < -180) {
160 while (aBearing > 180.0) {
167 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
169 Runway_iterator it = mRunways.begin();
170 FGRunway* result = NULL;
171 double currentBestQuality = 0.0;
173 SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
174 double lengthWeight = param->getDoubleValue("length-weight", 0.01);
175 double widthWeight = param->getDoubleValue("width-weight", 0.01);
176 double surfaceWeight = param->getDoubleValue("surface-weight", 10);
177 double deviationWeight = param->getDoubleValue("deviation-weight", 1);
179 for (; it != mRunways.end(); ++it) {
180 double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
182 double dev = normaliseBearing(aHeading - (*it)->headingDeg());
183 double bad = fabs(deviationWeight * dev) + 1e-20;
184 double quality = good / bad;
186 if (quality > currentBestQuality) {
187 currentBestQuality = quality;
195 unsigned int FGAirport::numTaxiways() const
197 return mTaxiways.size();
200 FGRunway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
202 assert(aIndex >= 0 && aIndex < mTaxiways.size());
203 return mTaxiways[aIndex];
206 void FGAirport::addRunway(FGRunway* aRunway)
208 aRunway->setAirport(this);
210 if (aRunway->isTaxiway()) {
211 mTaxiways.push_back(aRunway);
213 mRunways.push_back(aRunway);
217 FGRunway* FGAirport::getActiveRunwayForUsage() const
219 static FGEnvironmentMgr* envMgr = NULL;
221 envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
224 FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
226 double windSpeed = stationWeather.get_wind_speed_kt();
227 double hdg = stationWeather.get_wind_from_heading_deg();
228 if (windSpeed <= 0.0) {
229 hdg = 270; // This forces West-facing rwys to be used in no-wind situations
230 // which is consistent with Flightgear's initial setup.
233 return findBestRunwayForHeading(hdg);
236 /******************************************************************************
238 *****************************************************************************/
240 // Populates a list of subdirectories of $FG_ROOT/Airports/AI so that
241 // the add() method doesn't have to try opening 2 XML files in each of
242 // thousands of non-existent directories. FIXME: should probably add
243 // code to free this list after parsing of apt.dat is finished;
244 // non-issue at the moment, however, as there are no AI subdirectories
245 // in the base package.
247 // Note: 2005/12/23: This is probably not necessary anymore, because I'm
248 // Switching to runtime airport dynamics loading (DT).
249 FGAirportList::FGAirportList()
253 // SGPath aid( globals->get_fg_root() );
254 // aid.append( "/Airports/AI" );
255 // if((d = ulOpenDir(aid.c_str())) == NULL)
257 // while((dent = ulReadDir(d)) != NULL) {
258 // SG_LOG( SG_GENERAL, SG_DEBUG, "Dent: " << dent->d_name );
259 // ai_dirs.insert(dent->d_name);
265 FGAirportList::~FGAirportList( void )
267 for (unsigned int i = 0; i < airports_array.size(); ++i) {
268 delete airports_array[i];
273 // add an entry to the list
274 FGAirport* FGAirportList::add( const string &id, const SGGeod& location, const SGGeod& tower_location,
275 const string &name, bool has_metar, FGPositioned::Type aType)
277 FGAirport* a = new FGAirport(id, location, tower_location, name, has_metar, aType);
278 airports_by_id[a->getId()] = a;
279 // try and read in an auxilary file
281 airports_array.push_back( a );
282 SG_LOG( SG_GENERAL, SG_BULK, "Adding " << id << " pos = " << location.getLongitudeDeg()
283 << ", " << location.getLatitudeDeg() << " elev = " << location.getElevationFt() );
289 // search for the specified id
290 FGAirport* FGAirportList::search( const string& id)
292 airport_map_iterator itr = airports_by_id.find(id);
293 return (itr == airports_by_id.end() ? NULL : itr->second);
296 // wrap an FGIdentOrdering in an STL-compatible functor. not the most
297 // efficent / pretty thing in the world, but avoids template nastiness in the
298 // headers, and we're only doing O(log(N)) comparisoms per search
299 class orderingFunctor
302 orderingFunctor(FGIdentOrdering* aOrder) :
306 bool operator()(const airport_map::value_type& aA, const std::string& aB) const
308 return mOrdering->compare(aA.first,aB);
311 bool operator()(const std::string& aA, const airport_map::value_type& aB) const
313 return mOrdering->compare(aA, aB.first);
316 bool operator()(const airport_map::value_type& aA, const airport_map::value_type& aB) const
318 return mOrdering->compare(aA.first, aB.first);
322 FGIdentOrdering* mOrdering;
325 const FGAirport* FGAirportList::findFirstById(const std::string& aIdent, FGIdentOrdering* aOrder)
327 airport_map_iterator itr;
329 orderingFunctor func(aOrder);
330 itr = std::lower_bound(airports_by_id.begin(),airports_by_id.end(), aIdent, func);
332 itr = airports_by_id.lower_bound(aIdent);
335 if (itr == airports_by_id.end()) {
342 // search for the airport nearest the specified position
343 FGAirport* FGAirportList::search(double lon_deg, double lat_deg, double max_range)
345 static FGAirportSearchFilter accept_any;
346 return search(lon_deg, lat_deg, max_range, accept_any);
350 // search for the airport nearest the specified position and
351 // passing the filter
352 FGAirport* FGAirportList::search(double lon_deg, double lat_deg,
354 FGAirportSearchFilter& filter)
356 double min_dist = max_range;
358 airport_list_iterator it = airports_array.begin();
359 airport_list_iterator end = airports_array.end();
360 airport_list_iterator closest = end;
361 for (; it != end; ++it) {
362 if (!filter.pass(*it))
365 // crude manhatten distance based on lat/lon difference
366 double d = fabs(lon_deg - (*it)->getLongitude())
367 + fabs(lat_deg - (*it)->getLatitude());
373 return closest != end ? *closest : 0;
378 FGAirportList::size () const
380 return airports_array.size();
384 const FGAirport *FGAirportList::getAirport( unsigned int index ) const
386 if (index < airports_array.size()) {
387 return(airports_array[index]);
395 * Mark the specified airport record as not having metar
397 void FGAirportList::no_metar( const string &id )
399 if(airports_by_id.find(id) != airports_by_id.end()) {
400 airports_by_id[id]->setMetar(false);
406 * Mark the specified airport record as (yes) having metar
408 void FGAirportList::has_metar( const string &id )
410 if(airports_by_id.find(id) != airports_by_id.end()) {
411 airports_by_id[id]->setMetar(true);
416 // find basic airport location info from airport database
417 const FGAirport *fgFindAirportID( const string& id)
419 const FGAirport* result = NULL;
421 SG_LOG( SG_GENERAL, SG_BULK, "Searching for airport code = " << id );
423 result = globals->get_airports()->search( id );
425 if ( result == NULL ) {
426 SG_LOG( SG_GENERAL, SG_ALERT,
427 "Failed to find " << id << " in apt.dat.gz" );
433 SG_LOG( SG_GENERAL, SG_BULK,
434 "Position for " << id << " is ("
435 << result->getLongitude() << ", "
436 << result->getLatitude() << ")" );
442 // get airport elevation
443 double fgGetAirportElev( const string& id )
445 SG_LOG( SG_GENERAL, SG_BULK,
446 "Finding elevation for airport: " << id );
448 const FGAirport *a=fgFindAirportID( id);
450 return a->getElevation();
457 // get airport position
458 Point3D fgGetAirportPos( const string& id )
460 SG_LOG( SG_ATC, SG_BULK,
461 "Finding position for airport: " << id );
463 const FGAirport *a = fgFindAirportID( id);
466 return Point3D(a->getLongitude(), a->getLatitude(), a->getElevation());
468 return Point3D(0.0, 0.0, -9999.0);