]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
Thorsten Brehm: GPWS: Fixed permanent blocking of lower prio warnings
[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
49 // magic import of a helper which uses FGPositioned internals
50 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
51
52 /***************************************************************************
53  * FGAirport
54  ***************************************************************************/
55
56 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
57         const string &name, bool has_metar, Type aType) :
58     FGPositioned(aType, id, location),
59     _tower_location(tower_location),
60     _name(name),
61     _has_metar(has_metar),
62     _dynamics(0),
63     mRunwaysLoaded(false),
64     mTaxiwaysLoaded(true)
65 {
66 }
67
68
69 FGAirport::~FGAirport()
70 {
71     delete _dynamics;
72 }
73
74 bool FGAirport::isAirport() const
75 {
76   return type() == AIRPORT;
77 }
78
79 bool FGAirport::isSeaport() const
80 {
81   return type() == SEAPORT;
82 }
83
84 bool FGAirport::isHeliport() const
85 {
86   return type() == HELIPORT;
87 }
88
89 FGAirportDynamics * FGAirport::getDynamics()
90 {
91     if (_dynamics != 0) {
92         return _dynamics;
93     } else {
94         //cerr << "Trying to load dynamics for " << _id << endl;
95         _dynamics = new FGAirportDynamics(this);
96         XMLLoader::load(_dynamics);
97
98         FGRunwayPreference rwyPrefs(this);
99         XMLLoader::load(&rwyPrefs);
100         _dynamics->setRwyUse(rwyPrefs);
101
102         //FGSidStar SIDs(this);
103         XMLLoader::load(_dynamics->getSIDs());
104    }
105     return _dynamics;
106 }
107
108 unsigned int FGAirport::numRunways() const
109 {
110   loadRunways();
111   return mRunways.size();
112 }
113
114 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
115 {
116   loadRunways();
117   
118   assert(aIndex >= 0 && aIndex < mRunways.size());
119   return mRunways[aIndex];
120 }
121
122 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
123 {
124   return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
125 }
126
127 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
128 {
129   Runway_iterator it = getIteratorForRunwayIdent(aIdent);
130   if (it == mRunways.end()) {
131     SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
132     throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
133   }
134   
135   return *it;
136 }
137
138 FGAirport::Runway_iterator
139 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
140
141   loadRunways();
142   
143   string ident(aIdent);
144   if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
145     ident = "0" + aIdent;
146   }
147
148   Runway_iterator it = mRunways.begin();
149   for (; it != mRunways.end(); ++it) {
150     if ((*it)->ident() == ident) {
151       return it;
152     }
153   }
154
155   return it; // end()
156 }
157
158 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
159 {
160   loadRunways();
161   
162   Runway_iterator it = mRunways.begin();
163   FGRunway* result = NULL;
164   double currentBestQuality = 0.0;
165   
166   SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
167   double lengthWeight = param->getDoubleValue("length-weight", 0.01);
168   double widthWeight = param->getDoubleValue("width-weight", 0.01);
169   double surfaceWeight = param->getDoubleValue("surface-weight", 10);
170   double deviationWeight = param->getDoubleValue("deviation-weight", 1);
171     
172   for (; it != mRunways.end(); ++it) {
173     double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
174     
175     double dev = aHeading - (*it)->headingDeg();
176     SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
177     double bad = fabs(deviationWeight * dev) + 1e-20;
178     double quality = good / bad;
179     
180     if (quality > currentBestQuality) {
181       currentBestQuality = quality;
182       result = *it;
183     }
184   }
185
186   return result;
187 }
188
189 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
190 {
191   loadRunways();
192   
193   unsigned int numRunways(mRunways.size());
194   for (unsigned int r=0; r<numRunways; ++r) {
195     FGRunway* rwy = mRunways[r];
196     if (rwy->isReciprocal()) {
197       continue; // we only care about lengths, so don't do work twice
198     }
199
200     if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
201       return true; // we're done!
202     }
203   } // of runways iteration
204
205   return false;
206 }
207
208 unsigned int FGAirport::numTaxiways() const
209 {
210   loadTaxiways();
211   return mTaxiways.size();
212 }
213
214 FGTaxiway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
215 {
216   loadTaxiways();
217   assert(aIndex >= 0 && aIndex < mTaxiways.size());
218   return mTaxiways[aIndex];
219 }
220
221 unsigned int FGAirport::numPavements() const
222 {
223   loadTaxiways();
224   return mPavements.size();
225 }
226
227 FGPavement* FGAirport::getPavementByIndex(unsigned int aIndex) const
228 {
229   loadTaxiways();
230   assert(aIndex >= 0 && aIndex < mPavements.size());
231   return mPavements[aIndex];
232 }
233
234 void FGAirport::setRunwaysAndTaxiways(vector<FGRunwayPtr>& rwys,
235        vector<FGTaxiwayPtr>& txwys,
236        vector<FGPavementPtr>& pvts)
237 {
238   mRunways.swap(rwys);
239   Runway_iterator it = mRunways.begin();
240   for (; it != mRunways.end(); ++it) {
241     (*it)->setAirport(this);
242   }
243
244   mTaxiways.swap(txwys);
245   mPavements.swap(pvts);
246 }
247
248 FGRunway* FGAirport::getActiveRunwayForUsage() const
249 {
250   static FGEnvironmentMgr* envMgr = NULL;
251   if (!envMgr) {
252     envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
253   }
254   
255   // This forces West-facing rwys to be used in no-wind situations
256   // which is consistent with Flightgear's initial setup.
257   double hdg = 270;
258   
259   if (envMgr) {
260     FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
261   
262     double windSpeed = stationWeather.get_wind_speed_kt();
263     if (windSpeed > 0.0) {
264       hdg = stationWeather.get_wind_from_heading_deg();
265     }
266   }
267   
268   return findBestRunwayForHeading(hdg);
269 }
270
271 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
272 {
273   AirportFilter aptFilter;
274   if (filter == NULL) {
275     filter = &aptFilter;
276   }
277   
278   FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
279   if (!r) {
280     return NULL;
281   }
282   
283   return static_cast<FGAirport*>(r.ptr());
284 }
285
286 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
287   mMinLengthFt(minLengthFt)
288 {
289 }
290       
291 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
292 {
293   return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
294 }
295
296 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
297 {
298   FGPositionedRef r;
299   PortsFilter filter;
300   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
301   if (!r) {
302     return NULL; // we don't warn here, let the caller do that
303   }
304   return static_cast<FGAirport*>(r.ptr());
305 }
306
307 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
308 {
309   FGPositionedRef r;
310   PortsFilter filter;
311   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
312   if (!r) {
313     throw sg_range_exception("No such airport with ident: " + aIdent);
314   }
315   return static_cast<FGAirport*>(r.ptr());
316 }
317
318 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
319 {
320   // we delegate all the work to a horrible helper in FGPositioned, which can
321   // access the (private) index data.
322   return searchAirportNamesAndIdents(aFilter);
323 }
324
325 // find basic airport location info from airport database
326 const FGAirport *fgFindAirportID( const string& id)
327 {
328     if ( id.empty() ) {
329         return NULL;
330     }
331     
332     return FGAirport::findByIdent(id);
333 }
334
335 void FGAirport::loadRunways() const
336 {
337   if (mRunwaysLoaded) {
338     return; // already loaded, great
339   }
340   
341   mRunwaysLoaded = true;
342   loadSceneryDefintions();
343 }
344
345 void FGAirport::loadTaxiways() const
346 {
347   if (mTaxiwaysLoaded) {
348     return; // already loaded, great
349   }
350 }
351
352 void FGAirport::loadSceneryDefintions() const
353 {  
354   // allow users to disable the scenery data in the short-term
355   // longer term, this option can probably disappear
356   if (!fgGetBool("/sim/paths/use-custom-scenery-data")) {
357     return; 
358   }
359   
360   SGPath path;
361   SGPropertyNode_ptr rootNode = new SGPropertyNode;
362   if (XMLLoader::findAirportData(ident(), "threshold", path)) {
363     readProperties(path.str(), rootNode);
364     const_cast<FGAirport*>(this)->readThresholdData(rootNode);
365   }
366   
367   // repeat for the tower data
368   rootNode = new SGPropertyNode;
369   if (XMLLoader::findAirportData(ident(), "twr", path)) {
370     readProperties(path.str(), rootNode);
371     const_cast<FGAirport*>(this)->readTowerData(rootNode);
372   }
373 }
374
375 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
376 {
377   SGPropertyNode* runway;
378   int runwayIndex = 0;
379   for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
380     SGPropertyNode* t0 = runway->getChild("threshold", 0),
381       *t1 = runway->getChild("threshold", 1);
382     assert(t0);
383     assert(t1); // too strict? mayeb we should finally allow single-ended runways
384     
385     processThreshold(t0);
386     processThreshold(t1);
387   } // of runways iteration
388 }
389
390 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
391 {
392   // first, let's identify the current runway
393   string id(aThreshold->getStringValue("rwy"));
394   if (!hasRunwayWithIdent(id)) {
395     SG_LOG(SG_GENERAL, SG_WARN, "FGAirport::processThreshold: "
396       "found runway not defined in the global data:" << ident() << "/" << id);
397     return;
398   }
399   
400   FGRunway* rwy = getRunwayByIdent(id);
401   rwy->processThreshold(aThreshold);
402 }
403
404 void FGAirport::readTowerData(SGPropertyNode* aRoot)
405 {
406   SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
407   double lat = twrNode->getDoubleValue("lat"), 
408     lon = twrNode->getDoubleValue("lon"), 
409     elevM = twrNode->getDoubleValue("elev-m");
410     
411   _tower_location = SGGeod::fromDegM(lon, lat, elevM);
412 }
413
414 // get airport elevation
415 double fgGetAirportElev( const string& id )
416 {
417     const FGAirport *a=fgFindAirportID( id);
418     if (a) {
419         return a->getElevation();
420     } else {
421         return -9999.0;
422     }
423 }
424
425
426 // get airport position
427 SGGeod fgGetAirportPos( const string& id )
428 {
429     const FGAirport *a = fgFindAirportID( id);
430
431     if (a) {
432         return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
433     } else {
434         return SGGeod::fromDegM(0.0, 0.0, -9999.0);
435     }
436 }