]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
Create a real FlightPlan (and Leg) class
[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 }
333       
334 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
335 {
336   return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
337 }
338
339 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
340 {
341   FGPositionedRef r;
342   PortsFilter filter;
343   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
344   if (!r) {
345     return NULL; // we don't warn here, let the caller do that
346   }
347   return static_cast<FGAirport*>(r.ptr());
348 }
349
350 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
351 {
352   FGPositionedRef r;
353   PortsFilter filter;
354   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
355   if (!r) {
356     throw sg_range_exception("No such airport with ident: " + aIdent);
357   }
358   return static_cast<FGAirport*>(r.ptr());
359 }
360
361 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
362 {
363   // we delegate all the work to a horrible helper in FGPositioned, which can
364   // access the (private) index data.
365   return searchAirportNamesAndIdents(aFilter);
366 }
367
368 // find basic airport location info from airport database
369 const FGAirport *fgFindAirportID( const string& id)
370 {
371     if ( id.empty() ) {
372         return NULL;
373     }
374     
375     return FGAirport::findByIdent(id);
376 }
377
378 void FGAirport::loadRunways() const
379 {
380   if (mRunwaysLoaded) {
381     return; // already loaded, great
382   }
383   
384   mRunwaysLoaded = true;
385   loadSceneryDefinitions();
386 }
387
388 void FGAirport::loadTaxiways() const
389 {
390   if (mTaxiwaysLoaded) {
391     return; // already loaded, great
392   }
393 }
394
395 void FGAirport::loadProcedures() const
396 {
397   if (mProceduresLoaded) {
398     return;
399   }
400   
401   mProceduresLoaded = true;
402   SGPath path;
403   if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
404     SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
405     return;
406   }
407   
408   SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path.str());
409   RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
410 }
411
412 void FGAirport::loadSceneryDefinitions() const
413 {  
414   SGPath path;
415   SGPropertyNode_ptr rootNode = new SGPropertyNode;
416   if (XMLLoader::findAirportData(ident(), "threshold", path)) {
417     readProperties(path.str(), rootNode);
418     const_cast<FGAirport*>(this)->readThresholdData(rootNode);
419   }
420   
421   // repeat for the tower data
422   rootNode = new SGPropertyNode;
423   if (XMLLoader::findAirportData(ident(), "twr", path)) {
424     readProperties(path.str(), rootNode);
425     const_cast<FGAirport*>(this)->readTowerData(rootNode);
426   }
427 }
428
429 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
430 {
431   SGPropertyNode* runway;
432   int runwayIndex = 0;
433   for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
434     SGPropertyNode* t0 = runway->getChild("threshold", 0),
435       *t1 = runway->getChild("threshold", 1);
436     assert(t0);
437     assert(t1); // too strict? mayeb we should finally allow single-ended runways
438     
439     processThreshold(t0);
440     processThreshold(t1);
441   } // of runways iteration
442 }
443
444 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
445 {
446   // first, let's identify the current runway
447   string id(aThreshold->getStringValue("rwy"));
448   if (!hasRunwayWithIdent(id)) {
449     SG_LOG(SG_GENERAL, SG_DEBUG, "FGAirport::processThreshold: "
450       "found runway not defined in the global data:" << ident() << "/" << id);
451     return;
452   }
453   
454   FGRunway* rwy = getRunwayByIdent(id);
455   rwy->processThreshold(aThreshold);
456 }
457
458 void FGAirport::readTowerData(SGPropertyNode* aRoot)
459 {
460   SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
461   double lat = twrNode->getDoubleValue("lat"), 
462     lon = twrNode->getDoubleValue("lon"), 
463     elevM = twrNode->getDoubleValue("elev-m");  
464 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
465 // scenery for a precise terrain elevation, we use the field elevation
466 // (this is also what the apt.dat code does)
467   double fieldElevationM = geod().getElevationM();
468   
469   _tower_location = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
470 }
471
472 void FGAirport::addSID(flightgear::SID* aSid)
473 {
474   mSIDs.push_back(aSid);
475 }
476
477 void FGAirport::addSTAR(STAR* aStar)
478 {
479   mSTARs.push_back(aStar);
480 }
481
482 void FGAirport::addApproach(Approach* aApp)
483 {
484   mApproaches.push_back(aApp);
485 }
486
487 unsigned int FGAirport::numSIDs() const
488 {
489   loadProcedures();
490   return mSIDs.size();
491 }
492
493 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
494 {
495   loadProcedures();
496   return mSIDs[aIndex];
497 }
498
499 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
500 {
501   loadProcedures();
502   for (unsigned int i=0; i<mSIDs.size(); ++i) {
503     if (mSIDs[i]->ident() == aIdent) {
504       return mSIDs[i];
505     }
506   }
507   
508   return NULL;
509 }
510
511 unsigned int FGAirport::numSTARs() const
512 {
513   loadProcedures();
514   return mSTARs.size();
515 }
516
517 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
518 {
519   loadProcedures();
520   return mSTARs[aIndex];
521 }
522
523 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
524 {
525   loadProcedures();
526   for (unsigned int i=0; i<mSTARs.size(); ++i) {
527     if (mSTARs[i]->ident() == aIdent) {
528       return mSTARs[i];
529     }
530   }
531   
532   return NULL;
533 }
534
535 unsigned int FGAirport::numApproaches() const
536 {
537   loadProcedures();
538   return mApproaches.size();
539 }
540
541 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
542 {
543   loadProcedures();
544   return mApproaches[aIndex];
545 }
546
547 Approach* FGAirport::findApproachWithIdent(const std::string& aIdent) const
548 {
549   loadProcedures();
550   for (unsigned int i=0; i<mApproaches.size(); ++i) {
551     if (mApproaches[i]->ident() == aIdent) {
552       return mApproaches[i];
553     }
554   }
555   
556   return NULL;
557 }
558
559 void FGAirport::setCommStations(CommStationList& comms)
560 {
561     mCommStations.swap(comms);
562     for (unsigned int c=0; c<mCommStations.size(); ++c) {
563         mCommStations[c]->setAirport(this);
564     }
565 }
566
567 CommStationList
568 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
569 {
570     CommStationList result;
571     for (unsigned int c=0; c<mCommStations.size(); ++c) {
572         if (mCommStations[c]->type() == aTy) {
573             result.push_back(mCommStations[c]);
574         }
575     }
576     return result;
577 }
578
579 // get airport elevation
580 double fgGetAirportElev( const string& id )
581 {
582     const FGAirport *a=fgFindAirportID( id);
583     if (a) {
584         return a->getElevation();
585     } else {
586         return -9999.0;
587     }
588 }
589
590
591 // get airport position
592 SGGeod fgGetAirportPos( const string& id )
593 {
594     const FGAirport *a = fgFindAirportID( id);
595
596     if (a) {
597         return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
598     } else {
599         return SGGeod::fromDegM(0.0, 0.0, -9999.0);
600     }
601 }