]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
Another clean-up iteration: FGAirportList::search is gone, replaced by two
[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 <math.h>
32 #include <algorithm>
33
34 #include <simgear/compiler.h>
35
36 #include <Environment/environment_mgr.hxx>
37 #include <Environment/environment.hxx>
38 #include <simgear/misc/sg_path.hxx>
39 #include <simgear/props/props.hxx>
40 #include <simgear/structure/subsystem_mgr.hxx>
41 #include <simgear/debug/logstream.hxx>
42 #include <Main/globals.hxx>
43 #include <Main/fg_props.hxx>
44 #include <Airports/runways.hxx>
45 #include <Airports/dynamics.hxx>
46
47 #include <string>
48
49 #include "simple.hxx"
50 #include "xmlloader.hxx"
51
52 using std::sort;
53 using std::random_shuffle;
54
55 // magic import of a helper which uses FGPositioned internals
56 extern char** searchAirportNamesAndIdents(const std::string& aFilter);
57
58 /***************************************************************************
59  * FGAirport
60  ***************************************************************************/
61
62 FGAirport::FGAirport(const string &id, const SGGeod& location, const SGGeod& tower_location,
63         const string &name, bool has_metar, Type aType) :
64     FGPositioned(aType, id, location),
65     _tower_location(tower_location),
66     _name(name),
67     _has_metar(has_metar),
68     _dynamics(0)
69 {
70 }
71
72
73 FGAirport::~FGAirport()
74 {
75     delete _dynamics;
76 }
77
78 bool FGAirport::isAirport() const
79 {
80   return type() == AIRPORT;
81 }
82
83 bool FGAirport::isSeaport() const
84 {
85   return type() == SEAPORT;
86 }
87
88 bool FGAirport::isHeliport() const
89 {
90   return type() == HELIPORT;
91 }
92
93 FGAirportDynamics * FGAirport::getDynamics()
94 {
95     if (_dynamics != 0) {
96         return _dynamics;
97     } else {
98         //cerr << "Trying to load dynamics for " << _id << endl;
99         _dynamics = new FGAirportDynamics(this);
100         XMLLoader::load(_dynamics);
101
102         FGRunwayPreference rwyPrefs(this);
103         XMLLoader::load(&rwyPrefs);
104         _dynamics->setRwyUse(rwyPrefs);
105    }
106     return _dynamics;
107 }
108
109 unsigned int FGAirport::numRunways() const
110 {
111   return mRunways.size();
112 }
113
114 FGRunway* FGAirport::getRunwayByIndex(unsigned int aIndex) const
115 {
116   assert(aIndex >= 0 && aIndex < mRunways.size());
117   return mRunways[aIndex];
118 }
119
120 bool FGAirport::hasRunwayWithIdent(const string& aIdent) const
121 {
122   return (getIteratorForRunwayIdent(aIdent) != mRunways.end());
123 }
124
125 FGRunway* FGAirport::getRunwayByIdent(const string& aIdent) const
126 {
127   Runway_iterator it = getIteratorForRunwayIdent(aIdent);
128   if (it == mRunways.end()) {
129     SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
130     throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
131   }
132   
133   return *it;
134 }
135
136 FGAirport::Runway_iterator
137 FGAirport::getIteratorForRunwayIdent(const string& aIdent) const
138 {
139   string ident(aIdent);
140   if ((aIdent.size() == 1) || !isdigit(aIdent[1])) {
141     ident = "0" + aIdent;
142   }
143
144   Runway_iterator it = mRunways.begin();
145   for (; it != mRunways.end(); ++it) {
146     if ((*it)->ident() == ident) {
147       return it;
148     }
149   }
150
151   return it; // end()
152 }
153
154 static double normaliseBearing(double aBearing)
155 {
156   while (aBearing < -180) {
157     aBearing += 360.0;
158   }
159   
160   while (aBearing > 180.0) {
161     aBearing -= 360.0;
162   }
163   
164   return aBearing;
165 }
166
167 FGRunway* FGAirport::findBestRunwayForHeading(double aHeading) const
168 {
169   Runway_iterator it = mRunways.begin();
170   FGRunway* result = NULL;
171   double currentBestQuality = 0.0;
172   
173   SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
174   double lengthWeight = param->getDoubleValue("length-weight", 0.01);
175   double widthWeight = param->getDoubleValue("width-weight", 0.01);
176   double surfaceWeight = param->getDoubleValue("surface-weight", 10);
177   double deviationWeight = param->getDoubleValue("deviation-weight", 1);
178     
179   for (; it != mRunways.end(); ++it) {
180     double good = (*it)->score(lengthWeight, widthWeight, surfaceWeight);
181     
182     double dev = normaliseBearing(aHeading - (*it)->headingDeg());
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 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
196 {
197   unsigned int numRunways(mRunways.size());
198   for (unsigned int r=0; r<numRunways; ++r) {
199     FGRunway* rwy = mRunways[r];
200     if (rwy->isReciprocal()) {
201       continue; // we only care about lengths, so don't do work twice
202     }
203
204     if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
205       return true; // we're done!
206     }
207   } // of runways iteration
208
209   return false;
210 }
211
212 unsigned int FGAirport::numTaxiways() const
213 {
214   return mTaxiways.size();
215 }
216
217 FGRunway* FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
218 {
219   assert(aIndex >= 0 && aIndex < mTaxiways.size());
220   return mTaxiways[aIndex];
221 }
222
223 void FGAirport::addRunway(FGRunway* aRunway)
224 {
225   aRunway->setAirport(this);
226   
227   if (aRunway->isTaxiway()) {
228     mTaxiways.push_back(aRunway);
229   } else {
230     mRunways.push_back(aRunway);
231   }
232 }
233
234 FGRunway* FGAirport::getActiveRunwayForUsage() const
235 {
236   static FGEnvironmentMgr* envMgr = NULL;
237   if (!envMgr) {
238     envMgr = (FGEnvironmentMgr *) globals->get_subsystem("environment");
239   }
240   
241   FGEnvironment stationWeather(envMgr->getEnvironment(mPosition));
242   
243   double windSpeed = stationWeather.get_wind_speed_kt();
244   double hdg = stationWeather.get_wind_from_heading_deg();
245   if (windSpeed <= 0.0) {
246     hdg = 270;  // This forces West-facing rwys to be used in no-wind situations
247     // which is consistent with Flightgear's initial setup.
248   }
249   
250   return findBestRunwayForHeading(hdg);
251 }
252
253 FGAirport* FGAirport::findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter)
254 {
255   AirportFilter aptFilter;
256   if (filter == NULL) {
257     filter = &aptFilter;
258   }
259   
260   FGPositionedRef r = FGPositioned::findClosest(aPos, aCuttofNm, filter);
261   if (!r) {
262     return NULL;
263   }
264   
265   return static_cast<FGAirport*>(r.ptr());
266 }
267
268 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
269   mMinLengthFt(minLengthFt)
270 {
271 }
272       
273 bool FGAirport::HardSurfaceFilter::pass(FGPositioned* aPos) const
274 {
275   if (aPos->type() != AIRPORT) {
276     return false; // exclude seaports and heliports as well, we need a runways
277   }
278    
279   return static_cast<FGAirport*>(aPos)->hasHardRunwayOfLengthFt(mMinLengthFt);
280 }
281
282 FGAirport* FGAirport::findByIdent(const std::string& aIdent)
283 {
284   FGPositionedRef r;
285   AirportFilter filter;
286   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
287   if (!r) {
288     return NULL; // we don't warn here, let the caller do that
289   }
290   return static_cast<FGAirport*>(r.ptr());
291 }
292
293 FGAirport* FGAirport::getByIdent(const std::string& aIdent)
294 {
295   FGPositionedRef r;
296   AirportFilter filter;
297   r = FGPositioned::findNextWithPartialId(r, aIdent, &filter);
298   if (!r) {
299     throw sg_range_exception("No such airport with ident: " + aIdent);
300   }
301   return static_cast<FGAirport*>(r.ptr());
302 }
303
304 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
305 {
306   // we delegate all the work to a horrible helper in FGPositioned, which can
307   // access the (private) index data.
308   return searchAirportNamesAndIdents(aFilter);
309 }
310
311 /******************************************************************************
312  * FGAirportList
313  *****************************************************************************/
314
315 FGAirportList::FGAirportList()
316 {
317 }
318
319
320 FGAirportList::~FGAirportList( void )
321 {
322     for (unsigned int i = 0; i < airports_array.size(); ++i) {
323         delete airports_array[i];
324     }
325 }
326
327
328 // add an entry to the list
329 FGAirport* FGAirportList::add( const string &id, const SGGeod& location, const SGGeod& tower_location,
330                          const string &name, bool has_metar, FGPositioned::Type aType)
331 {
332     FGAirport* a = new FGAirport(id, location, tower_location, name, has_metar, aType);
333     // try and read in an auxilary file
334     airports_array.push_back( a );
335     return a;
336 }
337
338 int
339 FGAirportList::size () const
340 {
341     return airports_array.size();
342 }
343
344
345 const FGAirport *FGAirportList::getAirport( unsigned int index ) const
346 {
347     if (index < airports_array.size()) {
348         return(airports_array[index]);
349     } else {
350         return(NULL);
351     }
352 }
353
354 // find basic airport location info from airport database
355 const FGAirport *fgFindAirportID( const string& id)
356 {
357     if ( id.empty() ) {
358         return NULL;
359     }
360     
361     return FGAirport::findByIdent(id);
362 }
363
364
365 // get airport elevation
366 double fgGetAirportElev( const string& id )
367 {
368     SG_LOG( SG_GENERAL, SG_BULK,
369             "Finding elevation for airport: " << id );
370
371     const FGAirport *a=fgFindAirportID( id);
372     if (a) {
373         return a->getElevation();
374     } else {
375         return -9999.0;
376     }
377 }
378
379
380 // get airport position
381 Point3D fgGetAirportPos( const string& id )
382 {
383     SG_LOG( SG_ATC, SG_BULK,
384             "Finding position for airport: " << id );
385
386     const FGAirport *a = fgFindAirportID( id);
387
388     if (a) {
389         return Point3D(a->getLongitude(), a->getLatitude(), a->getElevation());
390     } else {
391         return Point3D(0.0, 0.0, -9999.0);
392     }
393 }