]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
c7e486f45a56b95cd0ddac27e5179fe9d7a22fa7
[flightgear.git] / src / Airports / simple.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 "simple.hxx"
32
33 #include <cassert>
34
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/props/props.hxx>
37 #include <simgear/props/props_io.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/sg_inlines.h>
40
41 #include <Environment/environment_mgr.hxx>
42 #include <Environment/environment.hxx>
43 #include <Main/fg_props.hxx>
44 #include <Airports/runways.hxx>
45 #include <Airports/pavement.hxx>
46 #include <Airports/dynamics.hxx>
47 #include <Airports/xmlloader.hxx>
48 #include <Navaids/procedure.hxx>
49 #include <Navaids/waypoint.hxx>
50
51 using std::vector;
52 using namespace flightgear;
53
54 // magic import of a helper which uses FGPositioned internals
55 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
56
57 /***************************************************************************
58  * FGAirport
59  ***************************************************************************/
60
61 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
62         const string &name, bool has_metar, Type aType) :
63     FGPositioned(aType, id, location),
64     _tower_location(tower_location),
65     _name(name),
66     _has_metar(has_metar),
67     _dynamics(0),
68     mRunwaysLoaded(false),
69     mTaxiwaysLoaded(true)
70 {
71   init(true); // init FGPositioned
72 }
73
74
75 FGAirport::~FGAirport()
76 {
77     delete _dynamics;
78 }
79
80 bool FGAirport::isAirport() const
81 {
82   return type() == AIRPORT;
83 }
84
85 bool FGAirport::isSeaport() const
86 {
87   return type() == SEAPORT;
88 }
89
90 bool FGAirport::isHeliport() const
91 {
92   return type() == HELIPORT;
93 }
94
95 FGAirportDynamics * FGAirport::getDynamics()
96 {
97     if (_dynamics != 0) {
98         return _dynamics;
99     } else {
100         //cerr << "Trying to load dynamics for " << _id << endl;
101         _dynamics = new FGAirportDynamics(this);
102         XMLLoader::load(_dynamics);
103
104         FGRunwayPreference rwyPrefs(this);
105         XMLLoader::load(&rwyPrefs);
106         _dynamics->setRwyUse(rwyPrefs);
107
108         //FGSidStar SIDs(this);
109         XMLLoader::load(_dynamics->getSIDs());
110    }
111     return _dynamics;
112 }
113
114 unsigned int FGAirport::numRunways() const
115 {
116   loadRunways();
117   return mRunways.size();
118 }
119
120 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
121 {
122   loadRunways();
123   
124   assert(aIndex >= 0 && aIndex < mRunways.size());
125   return mRunways[aIndex];
126 }
127
128 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
129 {
130   return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
131 }
132
133 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
134 {
135   Runway_iterator it = getIteratorForRunwayIdent(aIdent);
136   if (it == mRunways.end()) {
137     SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
138     throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
139   }
140   
141   return *it;
142 }
143
144 FGAirport::Runway_iterator
145 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
146
147   loadRunways();
148   
149   string ident(aIdent);
150   if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
151     ident = "0" + aIdent;
152   }
153
154   Runway_iterator it = mRunways.begin();
155   for (; it != mRunways.end(); ++it) {
156     if ((*it)->ident() == ident) {
157       return it;
158     }
159   }
160
161   return it; // end()
162 }
163
164 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
165 {
166   loadRunways();
167   
168   Runway_iterator it = mRunways.begin();
169   FGRunway* result = NULL;
170   double currentBestQuality = 0.0;
171   
172   SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
173   double lengthWeight = param->getDoubleValue("length-weight", 0.01);
174   double widthWeight = param->getDoubleValue("width-weight", 0.01);
175   double surfaceWeight = param->getDoubleValue("surface-weight", 10);
176   double deviationWeight = param->getDoubleValue("deviation-weight", 1);
177     
178   for (; it != mRunways.end(); ++it) {
179     double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
180     
181     double dev = aHeading - (*it)->headingDeg();
182     SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
183     double bad = fabs(deviationWeight * dev) + 1e-20;
184     double quality = good / bad;
185     
186     if (quality > currentBestQuality) {
187       currentBestQuality = quality;
188       result = *it;
189     }
190   }
191
192   return result;
193 }
194
195 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
196 {
197   loadRunways();
198   
199   Runway_iterator it = mRunways.begin();
200   FGRunway* result = NULL;
201   double currentLowestDev = 180.0;
202   
203   for (; it != mRunways.end(); ++it) {
204     double inboundCourse = SGGeodesy::courseDeg(aPos, (*it)->end());
205     double dev = inboundCourse - (*it)->headingDeg();
206     SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
207
208     dev = fabs(dev);
209     if (dev < currentLowestDev) { // new best match
210       currentLowestDev = dev;
211       result = *it;
212     }
213   } // of runway iteration
214   
215   return result;
216
217 }
218
219 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
220 {
221   loadRunways();
222   
223   unsigned int numRunways(mRunways.size());
224   for (unsigned int r=0; r<numRunways; ++r) {
225     FGRunway* rwy = mRunways[r];
226     if (rwy->isReciprocal()) {
227       continue; // we only care about lengths, so don't do work twice
228     }
229
230     if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
231       return true; // we're done!
232     }
233   } // of runways iteration
234
235   return false;
236 }
237
238 unsigned int FGAirport::numTaxiways() const
239 {
240   loadTaxiways();
241   return mTaxiways.size();
242 }
243
244 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
245 {
246   loadTaxiways();
247   assert(aIndex >= 0 && aIndex < mTaxiways.size());
248   return mTaxiways[aIndex];
249 }
250
251 unsigned int FGAirport::numPavements() const
252 {
253   loadTaxiways();
254   return mPavements.size();
255 }
256
257 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
258 {
259   loadTaxiways();
260   assert(aIndex >= 0 && aIndex < mPavements.size());
261   return mPavements[aIndex];
262 }
263
264 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
265        vector<FGTaxiwayPtr>& txwys,
266        vector<FGPavementPtr>& pvts)
267 {
268   mRunways.swap(rwys);
269   Runway_iterator it = mRunways.begin();
270   for (; it != mRunways.end(); ++it) {
271     (*it)->setAirport(this);
272   }
273
274   mTaxiways.swap(txwys);
275   mPavements.swap(pvts);
276 }
277
278 FGRunway* FGAirport::getActiveRunwayForUsage() const
279 {
280   static FGEnvironmentMgr* envMgr = NULL;
281   if (!envMgr) {
282     envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
283   }
284   
285   // This forces West-facing rwys to be used in no-wind situations
286   // which is consistent with Flightgear's initial setup.
287   double hdg = 270;
288   
289   if (envMgr) {
290     FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
291   
292     double windSpeed = stationWeather.get_wind_speed_kt();
293     if (windSpeed > 0.0) {
294       hdg = stationWeather.get_wind_from_heading_deg();
295     }
296   }
297   
298   return findBestRunwayForHeading(hdg);
299 }
300
301 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
302 {
303   AirportFilter aptFilter;
304   if (filter == NULL) {
305     filter = &aptFilter;
306   }
307   
308   FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
309   if (!r) {
310     return NULL;
311   }
312   
313   return static_cast<FGAirport*>(r.ptr());
314 }
315
316 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
317   mMinLengthFt(minLengthFt)
318 {
319 }
320       
321 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
322 {
323   return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
324 }
325
326 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
327 {
328   FGPositionedRef r;
329   PortsFilter filter;
330   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
331   if (!r) {
332     return NULL; // we don't warn here, let the caller do that
333   }
334   return static_cast<FGAirport*>(r.ptr());
335 }
336
337 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
338 {
339   FGPositionedRef r;
340   PortsFilter filter;
341   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
342   if (!r) {
343     throw sg_range_exception("No such airport with ident: " + aIdent);
344   }
345   return static_cast<FGAirport*>(r.ptr());
346 }
347
348 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
349 {
350   // we delegate all the work to a horrible helper in FGPositioned, which can
351   // access the (private) index data.
352   return searchAirportNamesAndIdents(aFilter);
353 }
354
355 // find basic airport location info from airport database
356 const FGAirport *fgFindAirportID( const string& id)
357 {
358     if ( id.empty() ) {
359         return NULL;
360     }
361     
362     return FGAirport::findByIdent(id);
363 }
364
365 void FGAirport::loadRunways() const
366 {
367   if (mRunwaysLoaded) {
368     return; // already loaded, great
369   }
370   
371   mRunwaysLoaded = true;
372   loadSceneryDefintions();
373 }
374
375 void FGAirport::loadTaxiways() const
376 {
377   if (mTaxiwaysLoaded) {
378     return; // already loaded, great
379   }
380 }
381
382 void FGAirport::loadProcedures() const
383 {
384   if (mProceduresLoaded) {
385     return;
386   }
387   
388   mProceduresLoaded = true;
389   SGPath path;
390   if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
391     SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
392     return;
393   }
394   
395   SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
396   Route::loadAirportProcedures(path, const_cast<FGAirport*>(this));
397 }
398
399 void FGAirport::loadSceneryDefintions() const
400 {  
401   // allow users to disable the scenery data in the short-term
402   // longer term, this option can probably disappear
403   if (!fgGetBool("/sim/paths/use-custom-scenery-data")) {
404     return; 
405   }
406   
407   SGPath path;
408   SGPropertyNode_ptr rootNode = new SGPropertyNode;
409   if (XMLLoader::findAirportData(ident(), "threshold", path)) {
410     readProperties(path.str(), rootNode);
411     const_cast<FGAirport*>(this)->readThresholdData(rootNode);
412   }
413   
414   // repeat for the tower data
415   rootNode = new SGPropertyNode;
416   if (XMLLoader::findAirportData(ident(), "twr", path)) {
417     readProperties(path.str(), rootNode);
418     const_cast<FGAirport*>(this)->readTowerData(rootNode);
419   }
420 }
421
422 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
423 {
424   SGPropertyNode* runway;
425   int runwayIndex = 0;
426   for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
427     SGPropertyNode* t0 = runway->getChild("threshold", 0),
428       *t1 = runway->getChild("threshold", 1);
429     assert(t0);
430     assert(t1); // too strict? mayeb we should finally allow single-ended runways
431     
432     processThreshold(t0);
433     processThreshold(t1);
434   } // of runways iteration
435 }
436
437 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
438 {
439   // first, let's identify the current runway
440   string id(aThreshold->getStringValue("rwy"));
441   if (!hasRunwayWithIdent(id)) {
442     SG_LOG(SG_GENERAL, SG_WARN, "FGAirport::processThreshold: "
443       "found runway not defined in the global data:" << ident() << "/" << id);
444     return;
445   }
446   
447   FGRunway* rwy = getRunwayByIdent(id);
448   rwy->processThreshold(aThreshold);
449 }
450
451 void FGAirport::readTowerData(SGPropertyNode* aRoot)
452 {
453   SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
454   double lat = twrNode->getDoubleValue("lat"), 
455     lon = twrNode->getDoubleValue("lon"), 
456     elevM = twrNode->getDoubleValue("elev-m");
457     
458   _tower_location = SGGeod::fromDegM(lon, lat, elevM);
459 }
460
461 bool FGAirport::buildApproach(Waypt* aEnroute, STAR* aSTAR, FGRunway* aRwy, WayptVec& aRoute)
462 {
463   loadProcedures();
464
465   if ((aRwy && (aRwy->airport() != this))) {
466     throw sg_exception("invalid parameters", "FGAirport::buildApproach");
467   }
468   
469   if (aSTAR) {
470     bool ok = aSTAR->route(aRwy, aEnroute, aRoute);
471     if (!ok) {
472       SG_LOG(SG_GENERAL, SG_WARN, ident() << ": build approach, STAR " << aSTAR->ident() 
473          << " failed to route from transition " << aEnroute->ident());
474       return false;
475     }
476   } else if (aEnroute) {
477     // no a STAR specified, just use enroute point directly
478     aRoute.push_back(aEnroute);
479   }
480   
481   if (!aRwy) {
482     // no runway selected yet, but we loaded the STAR, so that's fine, we're done
483     return true;
484   }
485   
486 // build the approach (possibly including transition), and including the missed segment
487   vector<Approach*> aps;
488   for (unsigned int j=0; j<mApproaches.size();++j) {
489     if (mApproaches[j]->runway() == aRwy) {
490       aps.push_back(mApproaches[j]);
491     }
492   } // of approach filter by runway
493   
494   if (aps.empty()) {
495     SG_LOG(SG_GENERAL, SG_INFO, ident() << "; no approaches defined for runway " << aRwy->ident());
496     // could build a fallback approach here
497     return false;
498   }
499   
500   for (unsigned int k=0; k<aps.size(); ++k) {
501     if (aps[k]->route(aRoute.back(), aRoute)) {
502       return true;
503     }
504   } // of initial approach iteration
505   
506   SG_LOG(SG_GENERAL, SG_INFO, ident() << ": unable to find transition to runway "
507     << aRwy->ident() << ", assume vectors");
508   
509   WayptRef v(new ATCVectors(NULL, this));
510   aRoute.push_back(v);
511   return aps.front()->routeFromVectors(aRoute);
512 }
513
514 pair<flightgear::SID*, WayptRef>
515 FGAirport::selectSID(const SGGeod& aDest, FGRunway* aRwy)
516 {
517   loadProcedures();
518   
519   WayptRef enroute;
520   flightgear::SID* sid = NULL;
521   double d = 1e9;
522   
523   for (unsigned int i=0; i<mSIDs.size(); ++i) {
524     if (aRwy && !mSIDs[i]->isForRunway(aRwy)) {
525       continue;
526     }
527   
528     WayptRef e = mSIDs[i]->findBestTransition(aDest);
529     if (!e) {
530       continue; // strange, but let's not worry about it
531     }
532     
533     // assert(e->isFixedPosition());
534     double ed = SGGeodesy::distanceM(aDest, e->position());
535     if (ed < d) { // new best match
536       enroute = e;
537       d = ed;
538       sid = mSIDs[i];
539     }
540   } // of SID iteration
541   
542   if (!mSIDs.empty() && !sid) {
543     SG_LOG(SG_GENERAL, SG_INFO, ident() << "selectSID, no SID found (runway=" 
544       << (aRwy ? aRwy->ident() : "no runway preference"));
545   }
546   
547   return make_pair(sid, enroute);
548 }
549     
550 pair<STAR*, WayptRef>
551 FGAirport::selectSTAR(const SGGeod& aOrigin, FGRunway* aRwy)
552 {
553   loadProcedures();
554   
555   WayptRef enroute;
556   STAR* star = NULL;
557   double d = 1e9;
558   
559   for (unsigned int i=0; i<mSTARs.size(); ++i) {
560     if (!mSTARs[i]->isForRunway(aRwy)) {
561       continue;
562     }
563     
564     SG_LOG(SG_GENERAL, SG_INFO, "STAR " << mSTARs[i]->ident() << " is valid for runway");
565     WayptRef e = mSTARs[i]->findBestTransition(aOrigin);
566     if (!e) {
567       continue; // strange, but let's not worry about it
568     }
569     
570     // assert(e->isFixedPosition());
571     double ed = SGGeodesy::distanceM(aOrigin, e->position());
572     if (ed < d) { // new best match
573       enroute = e;
574       d = ed;
575       star = mSTARs[i];
576     }
577   } // of STAR iteration
578   
579   return make_pair(star, enroute);
580 }
581
582
583 void FGAirport::addSID(flightgear::SID* aSid)
584 {
585   mSIDs.push_back(aSid);
586 }
587
588 void FGAirport::addSTAR(STAR* aStar)
589 {
590   mSTARs.push_back(aStar);
591 }
592
593 void FGAirport::addApproach(Approach* aApp)
594 {
595   mApproaches.push_back(aApp);
596 }
597
598 unsigned int FGAirport::numSIDs() const
599 {
600   loadProcedures();
601   return mSIDs.size();
602 }
603
604 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
605 {
606   loadProcedures();
607   return mSIDs[aIndex];
608 }
609
610 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
611 {
612   loadProcedures();
613   for (unsigned int i=0; i<mSIDs.size(); ++i) {
614     if (mSIDs[i]->ident() == aIdent) {
615       return mSIDs[i];
616     }
617   }
618   
619   return NULL;
620 }
621
622 unsigned int FGAirport::numSTARs() const
623 {
624   loadProcedures();
625   return mSTARs.size();
626 }
627
628 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
629 {
630   loadProcedures();
631   return mSTARs[aIndex];
632 }
633
634 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
635 {
636   loadProcedures();
637   for (unsigned int i=0; i<mSTARs.size(); ++i) {
638     if (mSTARs[i]->ident() == aIdent) {
639       return mSTARs[i];
640     }
641   }
642   
643   return NULL;
644 }
645
646 unsigned int FGAirport::numApproaches() const
647 {
648   loadProcedures();
649   return mApproaches.size();
650 }
651
652 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
653 {
654   loadProcedures();
655   return mApproaches[aIndex];
656 }
657
658 // get airport elevation
659 double fgGetAirportElev( const string& id )
660 {
661     const FGAirport *a=fgFindAirportID( id);
662     if (a) {
663         return a->getElevation();
664     } else {
665         return -9999.0;
666     }
667 }
668
669
670 // get airport position
671 SGGeod fgGetAirportPos( const string& id )
672 {
673     const FGAirport *a = fgFindAirportID( id);
674
675     if (a) {
676         return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
677     } else {
678         return SGGeod::fromDegM(0.0, 0.0, -9999.0);
679     }
680 }