]> git.mxchange.org Git - flightgear.git/blob - src/Airports/airport.cxx
Bug http://code.google.com/p/flightgear-bugs/issues/detail?id=1077
[flightgear.git] / src / Airports / airport.cxx
1 //
2 // simple.cxx -- a really simplistic class to manage airport ID,
3 //               lat, lon of the center of one of it's runways, and
4 //               elevation in feet.
5 //
6 // Written by Curtis Olson, started April 1998.
7 // Updated by Durk Talsma, started December, 2004.
8 //
9 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
10 //
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.
15 //
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.
20 //
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.
24 //
25 // $Id$
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include "airport.hxx"
32
33 #include <algorithm>
34 #include <cassert>
35 #include <boost/foreach.hpp>
36
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/props/props.hxx>
39 #include <simgear/props/props_io.hxx>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/sg_inlines.h>
42 #include <simgear/structure/exception.hxx>
43
44 #include <Environment/environment_mgr.hxx>
45 #include <Environment/environment.hxx>
46 #include <Main/fg_props.hxx>
47 #include <Airports/runways.hxx>
48 #include <Airports/pavement.hxx>
49 #include <Airports/dynamics.hxx>
50 #include <Airports/xmlloader.hxx>
51 #include <Navaids/procedure.hxx>
52 #include <Navaids/waypoint.hxx>
53 #include <ATC/CommStation.hxx>
54 #include <Navaids/NavDataCache.hxx>
55
56 using std::vector;
57 using std::pair;
58
59 using namespace flightgear;
60
61 /***************************************************************************
62  * FGAirport
63  ***************************************************************************/
64
65 AirportCache FGAirport::airportCache;
66
67 FGAirport::FGAirport( PositionedID aGuid,
68                       const std::string &id,
69                       const SGGeod& location,
70                       const std::string &name,
71                       bool has_metar,
72                       Type aType ):
73     FGPositioned(aGuid, aType, id, location),
74     _name(name),
75     _has_metar(has_metar),
76     _dynamics(0),
77     mTowerDataLoaded(false),
78     mRunwaysLoaded(false),
79     mHelipadsLoaded(false),
80     mTaxiwaysLoaded(false),
81     mProceduresLoaded(false),
82     mILSDataLoaded(false)
83 {
84 }
85
86
87 FGAirport::~FGAirport()
88 {
89     delete _dynamics;
90 }
91
92 bool FGAirport::isAirport() const
93 {
94   return type() == AIRPORT;
95 }
96
97 bool FGAirport::isSeaport() const
98 {
99   return type() == SEAPORT;
100 }
101
102 bool FGAirport::isHeliport() const
103 {
104   return type() == HELIPORT;
105 }
106
107 bool FGAirport::isAirportType(FGPositioned* pos)
108 {
109     if (!pos) {
110         return false;
111     }
112     
113     return (pos->type() >= AIRPORT) && (pos->type() <= SEAPORT);
114 }
115
116 FGAirportDynamics * FGAirport::getDynamics()
117 {
118     if (_dynamics) {
119         return _dynamics;
120     }
121     
122     _dynamics = new FGAirportDynamics(this);
123     XMLLoader::load(_dynamics);
124     _dynamics->init();
125   
126     FGRunwayPreference rwyPrefs(this);
127     XMLLoader::load(&rwyPrefs);
128     _dynamics->setRwyUse(rwyPrefs);
129     
130     return _dynamics;
131 }
132
133 //------------------------------------------------------------------------------
134 unsigned int FGAirport::numRunways() const
135 {
136   loadRunways();
137   return mRunways.size();
138 }
139
140 //------------------------------------------------------------------------------
141 unsigned int FGAirport::numHelipads() const
142 {
143   loadHelipads();
144   return mHelipads.size();
145 }
146
147 //------------------------------------------------------------------------------
148 FGRunwayRef FGAirport::getRunwayByIndex(unsigned int aIndex) const
149 {
150   loadRunways();
151   return loadById<FGRunway>(mRunways, aIndex);
152 }
153
154 //------------------------------------------------------------------------------
155 FGHelipadRef FGAirport::getHelipadByIndex(unsigned int aIndex) const
156 {
157   loadHelipads();
158   return loadById<FGHelipad>(mHelipads, aIndex);
159 }
160
161 //------------------------------------------------------------------------------
162 FGRunwayMap FGAirport::getRunwayMap() const
163 {
164   loadRunways();
165   FGRunwayMap map;
166
167   double minLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft");
168
169   BOOST_FOREACH(PositionedID id, mRunways)
170   {
171     FGRunway* rwy = loadById<FGRunway>(id);
172
173     // ignore unusably short runways
174     // TODO other methods don't check this...
175     if( rwy->lengthFt() >= minLengthFt )
176       map[ rwy->ident() ] = rwy;
177   }
178
179   return map;
180 }
181
182 //------------------------------------------------------------------------------
183 FGHelipadMap FGAirport::getHelipadMap() const
184 {
185   loadHelipads();
186   FGHelipadMap map;
187
188   BOOST_FOREACH(PositionedID id, mHelipads)
189   {
190     FGHelipad* rwy = loadById<FGHelipad>(id);
191     map[ rwy->ident() ] = rwy;
192   }
193
194   return map;
195 }
196
197 //------------------------------------------------------------------------------
198 bool FGAirport::hasRunwayWithIdent(const std::string& aIdent) const
199 {
200   return flightgear::NavDataCache::instance()
201     ->airportItemWithIdent(guid(), FGPositioned::RUNWAY, aIdent) != 0;
202 }
203
204 //------------------------------------------------------------------------------
205 bool FGAirport::hasHelipadWithIdent(const std::string& aIdent) const
206 {
207   return flightgear::NavDataCache::instance()
208     ->airportItemWithIdent(guid(), FGPositioned::HELIPAD, aIdent) != 0;
209 }
210
211 //------------------------------------------------------------------------------
212 FGRunwayRef FGAirport::getRunwayByIdent(const std::string& aIdent) const
213 {
214   PositionedID id =
215     flightgear::NavDataCache::instance()
216       ->airportItemWithIdent(guid(), FGPositioned::RUNWAY, aIdent);
217
218   if (id == 0) {
219     SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
220     throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
221   }
222   
223   return loadById<FGRunway>(id);
224 }
225
226 //------------------------------------------------------------------------------
227 FGHelipadRef FGAirport::getHelipadByIdent(const std::string& aIdent) const
228 {
229   PositionedID id = flightgear::NavDataCache::instance()->airportItemWithIdent(guid(), FGPositioned::HELIPAD, aIdent);
230   if (id == 0) {
231     SG_LOG(SG_GENERAL, SG_ALERT, "no such helipad '" << aIdent << "' at airport " << ident());
232     throw sg_range_exception("unknown helipad " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
233   }
234
235   return loadById<FGHelipad>(id);
236 }
237
238 //------------------------------------------------------------------------------
239 FGRunwayRef FGAirport::findBestRunwayForHeading(double aHeading) const
240 {
241   loadRunways();
242   
243   FGRunway* result = NULL;
244   double currentBestQuality = 0.0;
245   
246   SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
247   double lengthWeight = param->getDoubleValue("length-weight", 0.01);
248   double widthWeight = param->getDoubleValue("width-weight", 0.01);
249   double surfaceWeight = param->getDoubleValue("surface-weight", 10);
250   double deviationWeight = param->getDoubleValue("deviation-weight", 1);
251     
252   BOOST_FOREACH(PositionedID id, mRunways) {
253     FGRunway* rwy = loadById<FGRunway>(id);
254     double good = rwy->score(lengthWeight, widthWeight, surfaceWeight);
255     double dev = aHeading - rwy->headingDeg();
256     SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
257     double bad = fabs(deviationWeight * dev) + 1e-20;
258     double quality = good / bad;
259     
260     if (quality > currentBestQuality) {
261       currentBestQuality = quality;
262       result = rwy;
263     }
264   }
265
266   return result;
267 }
268
269 //------------------------------------------------------------------------------
270 FGRunwayRef FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
271 {
272   loadRunways();
273   
274   FGRunway* result = NULL;
275   double currentLowestDev = 180.0;
276   
277   BOOST_FOREACH(PositionedID id, mRunways) {
278     FGRunway* rwy = loadById<FGRunway>(id);
279
280     double inboundCourse = SGGeodesy::courseDeg(aPos, rwy->end());
281     double dev = inboundCourse - rwy->headingDeg();
282     SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
283
284     dev = fabs(dev);
285     if (dev < currentLowestDev) { // new best match
286       currentLowestDev = dev;
287       result = rwy;
288     }
289   } // of runway iteration
290   
291   return result;
292
293 }
294
295 //------------------------------------------------------------------------------
296 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
297 {
298   loadRunways();
299   
300   BOOST_FOREACH(PositionedID id, mRunways) {
301     FGRunway* rwy = loadById<FGRunway>(id);
302     if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
303       return true; // we're done!
304     }
305   } // of runways iteration
306
307   return false;
308 }
309
310 //------------------------------------------------------------------------------
311 FGRunwayList FGAirport::getRunwaysWithoutReciprocals() const
312 {
313   loadRunways();
314   
315   FGRunwayList r;
316   
317   BOOST_FOREACH(PositionedID id, mRunways) {
318     FGRunway* rwy = loadById<FGRunway>(id);
319     FGRunway* recip = rwy->reciprocalRunway();
320     if (recip) {
321       FGRunwayList::iterator it = std::find(r.begin(), r.end(), recip);
322       if (it != r.end()) {
323         continue; // reciprocal already in result set, don't include us
324       }
325     }
326     
327     r.push_back(rwy);
328   }
329   
330   return r;
331 }
332
333 //------------------------------------------------------------------------------
334 unsigned int FGAirport::numTaxiways() const
335 {
336   loadTaxiways();
337   return mTaxiways.size();
338 }
339
340 //------------------------------------------------------------------------------
341 FGTaxiwayRef FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
342 {
343   loadTaxiways();
344   return loadById<FGTaxiway>(mTaxiways, aIndex);
345 }
346
347 //------------------------------------------------------------------------------
348 FGTaxiwayList FGAirport::getTaxiways() const
349 {
350   loadTaxiways();
351   return loadAllById<FGTaxiway>(mTaxiways);
352 }
353
354 //------------------------------------------------------------------------------
355 unsigned int FGAirport::numPavements() const
356 {
357   loadTaxiways();
358   return mPavements.size();
359 }
360
361 //------------------------------------------------------------------------------
362 FGPavementRef FGAirport::getPavementByIndex(unsigned int aIndex) const
363 {
364   loadTaxiways();
365   return loadById<FGPavement>(mPavements, aIndex);
366 }
367
368 //------------------------------------------------------------------------------
369 FGPavementList FGAirport::getPavements() const
370 {
371   loadTaxiways();
372   return loadAllById<FGPavement>(mPavements);
373 }
374
375 //------------------------------------------------------------------------------
376 FGRunwayRef FGAirport::getActiveRunwayForUsage() const
377 {
378   FGEnvironmentMgr* envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
379   
380   // This forces West-facing rwys to be used in no-wind situations
381   // which is consistent with Flightgear's initial setup.
382   double hdg = 270;
383   
384   if (envMgr) {
385     FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
386   
387     double windSpeed = stationWeather.get_wind_speed_kt();
388     if (windSpeed > 0.0) {
389       hdg = stationWeather.get_wind_from_heading_deg();
390     }
391   }
392   
393   return findBestRunwayForHeading(hdg);
394 }
395
396 //------------------------------------------------------------------------------
397 FGAirportRef FGAirport::findClosest( const SGGeod& aPos,
398                                      double aCuttofNm,
399                                      Filter* filter )
400 {
401   AirportFilter aptFilter;
402   if( !filter )
403     filter = &aptFilter;
404   
405   return static_pointer_cast<FGAirport>
406   (
407     FGPositioned::findClosest(aPos, aCuttofNm, filter)
408   );
409 }
410
411 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
412   mMinLengthFt(minLengthFt)
413 {
414   if (minLengthFt < 0.0) {
415     mMinLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
416   }
417 }
418
419 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
420 {
421   return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
422 }
423
424 //------------------------------------------------------------------------------
425 FGAirport::TypeRunwayFilter::TypeRunwayFilter():
426   _type(FGPositioned::AIRPORT),
427   _min_runway_length_ft( fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0) )
428 {
429
430 }
431
432 //------------------------------------------------------------------------------
433 bool FGAirport::TypeRunwayFilter::fromTypeString(const std::string& type)
434 {
435   if(      type == "heliport" ) _type = FGPositioned::HELIPORT;
436   else if( type == "seaport"  ) _type = FGPositioned::SEAPORT;
437   else if( type == "airport"  ) _type = FGPositioned::AIRPORT;
438   else                          return false;
439
440   return true;
441 }
442
443 //------------------------------------------------------------------------------
444 bool FGAirport::TypeRunwayFilter::pass(FGPositioned* pos) const
445 {
446   FGAirport* apt = static_cast<FGAirport*>(pos);
447   if(  (apt->type() == FGPositioned::AIRPORT)
448     && !apt->hasHardRunwayOfLengthFt(_min_runway_length_ft)
449     )
450     return false;
451
452   return true;
453 }
454
455 //------------------------------------------------------------------------------
456 FGAirportRef FGAirport::findByIdent(const std::string& aIdent)
457 {
458   AirportCache::iterator it = airportCache.find(aIdent);
459   if (it != airportCache.end())
460    return it->second;
461
462   PortsFilter filter;
463   FGAirportRef r = static_pointer_cast<FGAirport>
464   (
465     FGPositioned::findFirstWithIdent(aIdent, &filter)
466   );
467
468   // add airport to the cache (even when it's NULL, so we don't need to search in vain again)
469   airportCache[aIdent] = r;
470
471   // we don't warn here when r==NULL, let the caller do that
472   return r;
473 }
474
475 //------------------------------------------------------------------------------
476 FGAirportRef FGAirport::getByIdent(const std::string& aIdent)
477 {
478   FGAirportRef r = findByIdent(aIdent);
479   if (!r)
480     throw sg_range_exception("No such airport with ident: " + aIdent);
481   return r;
482 }
483
484 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
485 {
486   return NavDataCache::instance()->searchAirportNamesAndIdents(aFilter);
487 }
488
489 // find basic airport location info from airport database
490 const FGAirport *fgFindAirportID( const std::string& id)
491 {
492     if ( id.empty() ) {
493         return NULL;
494     }
495     
496     return FGAirport::findByIdent(id);
497 }
498
499 void FGAirport::loadRunways() const
500 {
501   if (mRunwaysLoaded) {
502     return; // already loaded, great
503   }
504   
505   loadSceneryDefinitions();
506   
507   mRunwaysLoaded = true;
508   mRunways = flightgear::NavDataCache::instance()->airportItemsOfType(guid(), FGPositioned::RUNWAY);
509 }
510
511 void FGAirport::loadHelipads() const
512 {
513   if (mHelipadsLoaded) {
514     return; // already loaded, great
515   }
516
517   loadSceneryDefinitions();
518
519   mHelipadsLoaded = true;
520   mHelipads = flightgear::NavDataCache::instance()->airportItemsOfType(guid(), FGPositioned::HELIPAD);
521 }
522
523 void FGAirport::loadTaxiways() const
524 {
525   if (mTaxiwaysLoaded) {
526     return; // already loaded, great
527   }
528   
529   mTaxiwaysLoaded =  true;
530   mTaxiways = flightgear::NavDataCache::instance()->airportItemsOfType(guid(), FGPositioned::TAXIWAY);
531 }
532
533 void FGAirport::loadProcedures() const
534 {
535   if (mProceduresLoaded) {
536     return;
537   }
538   
539   mProceduresLoaded = true;
540   SGPath path;
541   if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
542     SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
543     return;
544   }
545   
546   SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
547   RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
548 }
549
550 void FGAirport::loadSceneryDefinitions() const
551 {
552   NavDataCache* cache = NavDataCache::instance();
553   SGPath path;
554   if (!XMLLoader::findAirportData(ident(), "threshold", path)) {
555     return; // no XML threshold data
556   }
557   
558   if (!cache->isCachedFileModified(path)) {
559     // cached values are correct, we're all done
560     return;
561   }
562   
563     flightgear::NavDataCache::Transaction txn(cache);
564     SGPropertyNode_ptr rootNode = new SGPropertyNode;
565     readProperties(path.str(), rootNode);
566     const_cast<FGAirport*>(this)->readThresholdData(rootNode);
567     cache->stampCacheFile(path);
568     txn.commit();
569 }
570
571 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
572 {
573   SGPropertyNode* runway;
574   int runwayIndex = 0;
575   for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
576     SGPropertyNode* t0 = runway->getChild("threshold", 0),
577       *t1 = runway->getChild("threshold", 1);
578     assert(t0);
579     assert(t1); // too strict? maybe we should finally allow single-ended runways
580     
581     processThreshold(t0);
582     processThreshold(t1);
583   } // of runways iteration
584 }
585
586 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
587 {
588   // first, let's identify the current runway
589   std::string rwyIdent(aThreshold->getStringValue("rwy"));
590   NavDataCache* cache = NavDataCache::instance(); 
591   PositionedID id = cache->airportItemWithIdent(guid(), FGPositioned::RUNWAY, rwyIdent);
592   if (id == 0) {
593     SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
594            "found runway not defined in the global data:" << ident() << "/" << rwyIdent);
595     return;
596   }
597   
598   double lon = aThreshold->getDoubleValue("lon"),
599   lat = aThreshold->getDoubleValue("lat");
600   SGGeod newThreshold(SGGeod::fromDegM(lon, lat, mPosition.getElevationM()));
601   
602   double newHeading = aThreshold->getDoubleValue("hdg-deg");
603   double newDisplacedThreshold = aThreshold->getDoubleValue("displ-m");
604   double newStopway = aThreshold->getDoubleValue("stopw-m");
605   
606   cache->updateRunwayThreshold(id, newThreshold,
607                                newHeading, newDisplacedThreshold, newStopway);
608 }
609
610 SGGeod FGAirport::getTowerLocation() const
611 {
612   validateTowerData();
613   
614   NavDataCache* cache = NavDataCache::instance();
615   PositionedIDVec towers = cache->airportItemsOfType(guid(), FGPositioned::TOWER);
616   if (towers.empty()) {
617     SG_LOG(SG_GENERAL, SG_ALERT, "No towers defined for:" <<ident());
618     return SGGeod();
619   }
620   
621   FGPositionedRef tower = cache->loadById(towers.front());
622   return tower->geod();
623 }
624
625 void FGAirport::validateTowerData() const
626 {
627   if (mTowerDataLoaded) {
628     return;
629   }
630
631   mTowerDataLoaded = true;
632   NavDataCache* cache = NavDataCache::instance();
633   SGPath path;
634   if (!XMLLoader::findAirportData(ident(), "twr", path)) {
635     return; // no XML tower data
636   }
637   
638   if (!cache->isCachedFileModified(path)) {
639   // cached values are correct, we're all done
640     return;
641   }
642    
643   flightgear::NavDataCache::Transaction txn(cache);
644   SGPropertyNode_ptr rootNode = new SGPropertyNode;
645   readProperties(path.str(), rootNode);
646   const_cast<FGAirport*>(this)->readTowerData(rootNode);
647   cache->stampCacheFile(path);
648   txn.commit();
649 }
650
651 void FGAirport::readTowerData(SGPropertyNode* aRoot)
652 {
653   SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
654   double lat = twrNode->getDoubleValue("lat"), 
655     lon = twrNode->getDoubleValue("lon"), 
656     elevM = twrNode->getDoubleValue("elev-m");  
657 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
658 // scenery for a precise terrain elevation, we use the field elevation
659 // (this is also what the apt.dat code does)
660   double fieldElevationM = geod().getElevationM();
661   SGGeod towerLocation(SGGeod::fromDegM(lon, lat, fieldElevationM + elevM));
662   
663   NavDataCache* cache = NavDataCache::instance();
664   PositionedIDVec towers = cache->airportItemsOfType(guid(), FGPositioned::TOWER);
665   if (towers.empty()) {
666     cache->insertTower(guid(), towerLocation);
667   } else {
668     // update the position
669     cache->updatePosition(towers.front(), towerLocation);
670   }
671 }
672
673 bool FGAirport::validateILSData()
674 {
675   if (mILSDataLoaded) {
676     return false;
677   }
678   
679   mILSDataLoaded = true;
680   NavDataCache* cache = NavDataCache::instance();
681   SGPath path;
682   if (!XMLLoader::findAirportData(ident(), "ils", path)) {
683     return false; // no XML tower data
684   }
685   
686   if (!cache->isCachedFileModified(path)) {
687     // cached values are correct, we're all done
688     return false;
689   }
690   
691   SGPropertyNode_ptr rootNode = new SGPropertyNode;
692   readProperties(path.str(), rootNode);
693
694   flightgear::NavDataCache::Transaction txn(cache);
695   readILSData(rootNode);
696   cache->stampCacheFile(path);
697   txn.commit();
698     
699 // we loaded data, tell the caller it might need to reload things
700   return true;
701 }
702
703 void FGAirport::readILSData(SGPropertyNode* aRoot)
704 {
705   NavDataCache* cache = NavDataCache::instance();
706   
707   // find the entry matching the runway
708   SGPropertyNode* runwayNode, *ilsNode;
709   for (int i=0; (runwayNode = aRoot->getChild("runway", i)) != NULL; ++i) {
710     for (int j=0; (ilsNode = runwayNode->getChild("ils", j)) != NULL; ++j) {
711       // must match on both nav-ident and runway ident, to support the following:
712       // - runways with multiple distinct ILS installations (KEWD, for example)
713       // - runways where both ends share the same nav ident (LFAT, for example)
714       PositionedID ils = cache->findILS(guid(), ilsNode->getStringValue("rwy"),
715                                         ilsNode->getStringValue("nav-id"));
716       if (ils == 0) {
717         SG_LOG(SG_GENERAL, SG_INFO, "reading ILS data for " << ident() <<
718                ", couldn;t find runway/navaid for:" <<
719                ilsNode->getStringValue("rwy") << "/" <<
720                ilsNode->getStringValue("nav-id"));
721         continue;
722       }
723       
724       double hdgDeg = ilsNode->getDoubleValue("hdg-deg"),
725         lon = ilsNode->getDoubleValue("lon"),
726         lat = ilsNode->getDoubleValue("lat"),
727         elevM = ilsNode->getDoubleValue("elev-m");
728  
729       cache->updateILS(ils, SGGeod::fromDegM(lon, lat, elevM), hdgDeg);
730     } // of ILS iteration
731   } // of runway iteration
732 }
733
734 void FGAirport::addSID(flightgear::SID* aSid)
735 {
736   mSIDs.push_back(aSid);
737 }
738
739 void FGAirport::addSTAR(STAR* aStar)
740 {
741   mSTARs.push_back(aStar);
742 }
743
744 void FGAirport::addApproach(Approach* aApp)
745 {
746   mApproaches.push_back(aApp);
747 }
748
749 //------------------------------------------------------------------------------
750 unsigned int FGAirport::numSIDs() const
751 {
752   loadProcedures();
753   return mSIDs.size();
754 }
755
756 //------------------------------------------------------------------------------
757 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
758 {
759   loadProcedures();
760   return mSIDs[aIndex];
761 }
762
763 //------------------------------------------------------------------------------
764 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
765 {
766   loadProcedures();
767   for (unsigned int i=0; i<mSIDs.size(); ++i) {
768     if (mSIDs[i]->ident() == aIdent) {
769       return mSIDs[i];
770     }
771   }
772   
773   return NULL;
774 }
775
776 //------------------------------------------------------------------------------
777 flightgear::SIDList FGAirport::getSIDs() const
778 {
779   loadProcedures();
780   return flightgear::SIDList(mSIDs.begin(), mSIDs.end());
781 }
782
783 //------------------------------------------------------------------------------
784 unsigned int FGAirport::numSTARs() const
785 {
786   loadProcedures();
787   return mSTARs.size();
788 }
789
790 //------------------------------------------------------------------------------
791 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
792 {
793   loadProcedures();
794   return mSTARs[aIndex];
795 }
796
797 //------------------------------------------------------------------------------
798 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
799 {
800   loadProcedures();
801   for (unsigned int i=0; i<mSTARs.size(); ++i) {
802     if (mSTARs[i]->ident() == aIdent) {
803       return mSTARs[i];
804     }
805   }
806   
807   return NULL;
808 }
809
810 //------------------------------------------------------------------------------
811 STARList FGAirport::getSTARs() const
812 {
813   loadProcedures();
814   return STARList(mSTARs.begin(), mSTARs.end());
815 }
816
817 unsigned int FGAirport::numApproaches() const
818 {
819   loadProcedures();
820   return mApproaches.size();
821 }
822
823 //------------------------------------------------------------------------------
824 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
825 {
826   loadProcedures();
827   return mApproaches[aIndex];
828 }
829
830 //------------------------------------------------------------------------------
831 Approach* FGAirport::findApproachWithIdent(const std::string& aIdent) const
832 {
833   loadProcedures();
834   for (unsigned int i=0; i<mApproaches.size(); ++i) {
835     if (mApproaches[i]->ident() == aIdent) {
836       return mApproaches[i];
837     }
838   }
839   
840   return NULL;
841 }
842
843 //------------------------------------------------------------------------------
844 ApproachList FGAirport::getApproaches(ProcedureType type) const
845 {
846   loadProcedures();
847   if( type == PROCEDURE_INVALID )
848     return ApproachList(mApproaches.begin(), mApproaches.end());
849
850   ApproachList ret;
851   for(size_t i = 0; i < mApproaches.size(); ++i)
852   {
853     if( mApproaches[i]->type() == type )
854       ret.push_back(mApproaches[i]);
855   }
856   return ret;
857 }
858
859 CommStationList
860 FGAirport::commStations() const
861 {
862   NavDataCache* cache = NavDataCache::instance();
863   CommStationList result;
864   BOOST_FOREACH(PositionedID pos, cache->airportItemsOfType(guid(),
865                                                             FGPositioned::FREQ_GROUND,
866                                                             FGPositioned::FREQ_UNICOM))
867   {
868     result.push_back( loadById<CommStation>(pos) );
869   }
870   
871   return result;
872 }
873
874 CommStationList
875 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
876 {
877   NavDataCache* cache = NavDataCache::instance();
878   CommStationList result;
879   BOOST_FOREACH(PositionedID pos, cache->airportItemsOfType(guid(), aTy)) {
880     result.push_back( loadById<CommStation>(pos) );
881   }
882   
883   return result;
884 }
885
886 // get airport elevation
887 double fgGetAirportElev( const std::string& id )
888 {
889     const FGAirport *a=fgFindAirportID( id);
890     if (a) {
891         return a->getElevation();
892     } else {
893         return -9999.0;
894     }
895 }
896
897
898 // get airport position
899 SGGeod fgGetAirportPos( const std::string& id )
900 {
901     const FGAirport *a = fgFindAirportID( id);
902
903     if (a) {
904         return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
905     } else {
906         return SGGeod::fromDegM(0.0, 0.0, -9999.0);
907     }
908 }