]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[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 #include <ATC/CommStation.hxx>
51
52 using std::vector;
53 using std::pair;
54
55 using namespace flightgear;
56
57 // magic import of a helper which uses FGPositioned internals
58 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
59
60 /***************************************************************************
61  * FGAirport
62  ***************************************************************************/
63
64 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
65         const string &name, bool has_metar, Type aType) :
66     FGPositioned(aType, id, location),
67     _tower_location(tower_location),
68     _name(name),
69     _has_metar(has_metar),
70     _dynamics(0),
71     mRunwaysLoaded(false),
72     mTaxiwaysLoaded(true)
73 {
74   init(true); // init FGPositioned
75 }
76
77
78 FGAirport::~FGAirport()
79 {
80     delete _dynamics;
81 }
82
83 bool FGAirport::isAirport() const
84 {
85   return type() == AIRPORT;
86 }
87
88 bool FGAirport::isSeaport() const
89 {
90   return type() == SEAPORT;
91 }
92
93 bool FGAirport::isHeliport() const
94 {
95   return type() == HELIPORT;
96 }
97
98 bool FGAirport::isAirportType(FGPositioned* pos)
99 {
100     if (!pos) {
101         return false;
102     }
103     
104     return (pos->type() >= AIRPORT) && (pos->type() <= SEAPORT);
105 }
106
107 FGAirportDynamics * FGAirport::getDynamics()
108 {
109     if (_dynamics) {
110         return _dynamics;
111     }
112     
113     _dynamics = new FGAirportDynamics(this);
114     XMLLoader::load(_dynamics);
115
116     FGRunwayPreference rwyPrefs(this);
117     XMLLoader::load(&rwyPrefs);
118     _dynamics->setRwyUse(rwyPrefs);
119     XMLLoader::load(_dynamics->getSIDs());
120     
121     return _dynamics;
122 }
123
124 unsigned int FGAirport::numRunways() const
125 {
126   loadRunways();
127   return mRunways.size();
128 }
129
130 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
131 {
132   loadRunways();
133   
134   assert(aIndex >= 0 && aIndex < mRunways.size());
135   return mRunways[aIndex];
136 }
137
138 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
139 {
140   return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
141 }
142
143 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
144 {
145   Runway_iterator it = getIteratorForRunwayIdent(aIdent);
146   if (it == mRunways.end()) {
147     SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
148     throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
149   }
150   
151   return *it;
152 }
153
154 FGAirport::Runway_iterator
155 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
156 {
157   if (aIdent.empty())
158     return mRunways.end();
159
160   loadRunways();
161   
162   string ident(aIdent);
163   if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
164     ident = "0" + aIdent;
165   }
166
167   Runway_iterator it = mRunways.begin();
168   for (; it != mRunways.end(); ++it) {
169     if ((*it)->ident() == ident) {
170       return it;
171     }
172   }
173
174   return it; // end()
175 }
176
177 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
178 {
179   loadRunways();
180   
181   Runway_iterator it = mRunways.begin();
182   FGRunway* result = NULL;
183   double currentBestQuality = 0.0;
184   
185   SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
186   double lengthWeight = param->getDoubleValue("length-weight", 0.01);
187   double widthWeight = param->getDoubleValue("width-weight", 0.01);
188   double surfaceWeight = param->getDoubleValue("surface-weight", 10);
189   double deviationWeight = param->getDoubleValue("deviation-weight", 1);
190     
191   for (; it != mRunways.end(); ++it) {
192     double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
193     
194     double dev = aHeading - (*it)->headingDeg();
195     SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
196     double bad = fabs(deviationWeight * dev) + 1e-20;
197     double quality = good / bad;
198     
199     if (quality > currentBestQuality) {
200       currentBestQuality = quality;
201       result = *it;
202     }
203   }
204
205   return result;
206 }
207
208 FGRunway* FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
209 {
210   loadRunways();
211   
212   Runway_iterator it = mRunways.begin();
213   FGRunway* result = NULL;
214   double currentLowestDev = 180.0;
215   
216   for (; it != mRunways.end(); ++it) {
217     double inboundCourse = SGGeodesy::courseDeg(aPos, (*it)->end());
218     double dev = inboundCourse - (*it)->headingDeg();
219     SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
220
221     dev = fabs(dev);
222     if (dev < currentLowestDev) { // new best match
223       currentLowestDev = dev;
224       result = *it;
225     }
226   } // of runway iteration
227   
228   return result;
229
230 }
231
232 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
233 {
234   loadRunways();
235   
236   unsigned int numRunways(mRunways.size());
237   for (unsigned int r=0; r<numRunways; ++r) {
238     FGRunway* rwy = mRunways[r];
239     if (rwy->isReciprocal()) {
240       continue; // we only care about lengths, so don't do work twice
241     }
242
243     if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
244       return true; // we're done!
245     }
246   } // of runways iteration
247
248   return false;
249 }
250
251 unsigned int FGAirport::numTaxiways() const
252 {
253   loadTaxiways();
254   return mTaxiways.size();
255 }
256
257 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
258 {
259   loadTaxiways();
260   assert(aIndex >= 0 && aIndex < mTaxiways.size());
261   return mTaxiways[aIndex];
262 }
263
264 unsigned int FGAirport::numPavements() const
265 {
266   loadTaxiways();
267   return mPavements.size();
268 }
269
270 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
271 {
272   loadTaxiways();
273   assert(aIndex >= 0 && aIndex < mPavements.size());
274   return mPavements[aIndex];
275 }
276
277 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
278        vector<FGTaxiwayPtr>& txwys,
279        vector<FGPavementPtr>& pvts)
280 {
281   mRunways.swap(rwys);
282   Runway_iterator it = mRunways.begin();
283   for (; it != mRunways.end(); ++it) {
284     (*it)->setAirport(this);
285   }
286
287   mTaxiways.swap(txwys);
288   mPavements.swap(pvts);
289 }
290
291 FGRunway* FGAirport::getActiveRunwayForUsage() const
292 {
293   static FGEnvironmentMgr* envMgr = NULL;
294   if (!envMgr) {
295     envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
296   }
297   
298   // This forces West-facing rwys to be used in no-wind situations
299   // which is consistent with Flightgear's initial setup.
300   double hdg = 270;
301   
302   if (envMgr) {
303     FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
304   
305     double windSpeed = stationWeather.get_wind_speed_kt();
306     if (windSpeed > 0.0) {
307       hdg = stationWeather.get_wind_from_heading_deg();
308     }
309   }
310   
311   return findBestRunwayForHeading(hdg);
312 }
313
314 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
315 {
316   AirportFilter aptFilter;
317   if (filter == NULL) {
318     filter = &aptFilter;
319   }
320   
321   FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
322   if (!r) {
323     return NULL;
324   }
325   
326   return static_cast<FGAirport*>(r.ptr());
327 }
328
329 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
330   mMinLengthFt(minLengthFt)
331 {
332   if (minLengthFt < 0.0) {
333     mMinLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
334   }
335 }
336       
337 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
338 {
339   return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
340 }
341
342 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
343 {
344   FGPositionedRef r;
345   PortsFilter filter;
346   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
347   if (!r) {
348     return NULL; // we don't warn here, let the caller do that
349   }
350   return static_cast<FGAirport*>(r.ptr());
351 }
352
353 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
354 {
355   FGPositionedRef r;
356   PortsFilter filter;
357   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
358   if (!r) {
359     throw sg_range_exception("No such airport with ident: " + aIdent);
360   }
361   return static_cast<FGAirport*>(r.ptr());
362 }
363
364 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
365 {
366   // we delegate all the work to a horrible helper in FGPositioned, which can
367   // access the (private) index data.
368   return searchAirportNamesAndIdents(aFilter);
369 }
370
371 // find basic airport location info from airport database
372 const FGAirport *fgFindAirportID( const string& id)
373 {
374     if ( id.empty() ) {
375         return NULL;
376     }
377     
378     return FGAirport::findByIdent(id);
379 }
380
381 void FGAirport::loadRunways() const
382 {
383   if (mRunwaysLoaded) {
384     return; // already loaded, great
385   }
386   
387   mRunwaysLoaded = true;
388   loadSceneryDefinitions();
389 }
390
391 void FGAirport::loadTaxiways() const
392 {
393   if (mTaxiwaysLoaded) {
394     return; // already loaded, great
395   }
396 }
397
398 void FGAirport::loadProcedures() const
399 {
400   if (mProceduresLoaded) {
401     return;
402   }
403   
404   mProceduresLoaded = true;
405   SGPath path;
406   if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
407     SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
408     return;
409   }
410   
411   SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
412   RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
413 }
414
415 void FGAirport::loadSceneryDefinitions() const
416 {  
417   SGPath path;
418   SGPropertyNode_ptr rootNode = new SGPropertyNode;
419   if (XMLLoader::findAirportData(ident(), "threshold", path)) {
420     readProperties(path.str(), rootNode);
421     const_cast<FGAirport*>(this)->readThresholdData(rootNode);
422   }
423   
424   // repeat for the tower data
425   rootNode = new SGPropertyNode;
426   if (XMLLoader::findAirportData(ident(), "twr", path)) {
427     readProperties(path.str(), rootNode);
428     const_cast<FGAirport*>(this)->readTowerData(rootNode);
429   }
430 }
431
432 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
433 {
434   SGPropertyNode* runway;
435   int runwayIndex = 0;
436   for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
437     SGPropertyNode* t0 = runway->getChild("threshold", 0),
438       *t1 = runway->getChild("threshold", 1);
439     assert(t0);
440     assert(t1); // too strict? mayeb we should finally allow single-ended runways
441     
442     processThreshold(t0);
443     processThreshold(t1);
444   } // of runways iteration
445 }
446
447 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
448 {
449   // first, let's identify the current runway
450   string id(aThreshold->getStringValue("rwy"));
451   if (!hasRunwayWithIdent(id)) {
452     SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
453       "found runway not defined in the global data:" << ident() << "/" << id);
454     return;
455   }
456   
457   FGRunway* rwy = getRunwayByIdent(id);
458   rwy->processThreshold(aThreshold);
459 }
460
461 void FGAirport::readTowerData(SGPropertyNode* aRoot)
462 {
463   SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
464   double lat = twrNode->getDoubleValue("lat"), 
465     lon = twrNode->getDoubleValue("lon"), 
466     elevM = twrNode->getDoubleValue("elev-m");  
467 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
468 // scenery for a precise terrain elevation, we use the field elevation
469 // (this is also what the apt.dat code does)
470   double fieldElevationM = geod().getElevationM();
471   
472   _tower_location = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
473 }
474
475 void FGAirport::addSID(flightgear::SID* aSid)
476 {
477   mSIDs.push_back(aSid);
478 }
479
480 void FGAirport::addSTAR(STAR* aStar)
481 {
482   mSTARs.push_back(aStar);
483 }
484
485 void FGAirport::addApproach(Approach* aApp)
486 {
487   mApproaches.push_back(aApp);
488 }
489
490 unsigned int FGAirport::numSIDs() const
491 {
492   loadProcedures();
493   return mSIDs.size();
494 }
495
496 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
497 {
498   loadProcedures();
499   return mSIDs[aIndex];
500 }
501
502 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
503 {
504   loadProcedures();
505   for (unsigned int i=0; i<mSIDs.size(); ++i) {
506     if (mSIDs[i]->ident() == aIdent) {
507       return mSIDs[i];
508     }
509   }
510   
511   return NULL;
512 }
513
514 unsigned int FGAirport::numSTARs() const
515 {
516   loadProcedures();
517   return mSTARs.size();
518 }
519
520 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
521 {
522   loadProcedures();
523   return mSTARs[aIndex];
524 }
525
526 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
527 {
528   loadProcedures();
529   for (unsigned int i=0; i<mSTARs.size(); ++i) {
530     if (mSTARs[i]->ident() == aIdent) {
531       return mSTARs[i];
532     }
533   }
534   
535   return NULL;
536 }
537
538 unsigned int FGAirport::numApproaches() const
539 {
540   loadProcedures();
541   return mApproaches.size();
542 }
543
544 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
545 {
546   loadProcedures();
547   return mApproaches[aIndex];
548 }
549
550 Approach* FGAirport::findApproachWithIdent(const std::string& aIdent) const
551 {
552   loadProcedures();
553   for (unsigned int i=0; i<mApproaches.size(); ++i) {
554     if (mApproaches[i]->ident() == aIdent) {
555       return mApproaches[i];
556     }
557   }
558   
559   return NULL;
560 }
561
562 void FGAirport::setCommStations(CommStationList& comms)
563 {
564     mCommStations.swap(comms);
565     for (unsigned int c=0; c<mCommStations.size(); ++c) {
566         mCommStations[c]->setAirport(this);
567     }
568 }
569
570 CommStationList
571 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
572 {
573     CommStationList result;
574     for (unsigned int c=0; c<mCommStations.size(); ++c) {
575         if (mCommStations[c]->type() == aTy) {
576             result.push_back(mCommStations[c]);
577         }
578     }
579     return result;
580 }
581
582 // get airport elevation
583 double fgGetAirportElev( const string& id )
584 {
585     const FGAirport *a=fgFindAirportID( id);
586     if (a) {
587         return a->getElevation();
588     } else {
589         return -9999.0;
590     }
591 }
592
593
594 // get airport position
595 SGGeod fgGetAirportPos( const string& id )
596 {
597     const FGAirport *a = fgFindAirportID( id);
598
599     if (a) {
600         return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
601     } else {
602         return SGGeod::fromDegM(0.0, 0.0, -9999.0);
603     }
604 }