]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
noise reduction (flooding the terminal isn't exactly informative)
[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., 675 Mass Ave, Cambridge, MA 02139, 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 <plib/sg.h>
37 #include <plib/ul.h>
38
39 #include <Environment/environment_mgr.hxx>
40 #include <Environment/environment.hxx>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/props/props.hxx>
43 #include <simgear/structure/subsystem_mgr.hxx>
44 //#include <simgear/route/waypoint.hxx>
45 #include <simgear/debug/logstream.hxx>
46 #include <Main/globals.hxx>
47 #include <Main/fg_props.hxx>
48 #include <Airports/runways.hxx>
49
50 #include STL_STRING
51
52 #include "simple.hxx"
53
54 SG_USING_STD(sort);
55 SG_USING_STD(random_shuffle);
56
57
58
59
60
61
62 /***************************************************************************
63  * FGAirport
64  ***************************************************************************/
65 FGAirport::FGAirport() : _longitude(0), _latitude(0), _elevation(0)
66 {
67   dynamics = 0;
68 }
69
70
71 FGAirport::FGAirport(const string &id, double lon, double lat, double elev, const string &name, bool has_metar)
72 {
73   _id = id;
74   _longitude = lon;
75   _latitude  = lat;
76   _elevation = elev;
77   _name      = name;
78   _has_metar = has_metar;
79   dynamics   = 0;
80 }
81
82 FGAirport::~FGAirport()
83 {
84     delete dynamics;
85 }
86
87
88 FGAirportDynamics * FGAirport::getDynamics()
89 {
90   
91   if (dynamics != 0)
92     return dynamics;
93   else
94     {
95       FGRunwayPreference rwyPrefs;
96       //cerr << "Trying to load dynamics for " << _id << endl;
97       dynamics = new FGAirportDynamics(_latitude, _longitude, _elevation, _id);
98
99       SGPath parkpath( globals->get_fg_root() );
100       parkpath.append( "/Airports/AI/" );
101       parkpath.append(_id);
102       parkpath.append("parking.xml"); 
103       
104       SGPath rwyPrefPath( globals->get_fg_root() );
105       rwyPrefPath.append( "/Airports/AI/" );
106       rwyPrefPath.append(_id);
107       rwyPrefPath.append("rwyuse.xml");
108       //if (ai_dirs.find(id.c_str()) != ai_dirs.end()
109       //  && parkpath.exists()) 
110       if (parkpath.exists())
111         {
112           try {
113             readXML(parkpath.str(),*dynamics);
114             dynamics->init();
115           } 
116           catch  (const sg_exception &e) {
117             //cerr << "unable to read " << parkpath.str() << endl;
118           }
119         }
120       //if (ai_dirs.find(id.c_str()) != ai_dirs.end()
121       //  && rwyPrefPath.exists()) 
122       if (rwyPrefPath.exists())
123         {
124           try {
125             readXML(rwyPrefPath.str(), rwyPrefs);
126             dynamics->setRwyUse(rwyPrefs);
127           }
128           catch  (const sg_exception &e) {
129             //cerr << "unable to read " << rwyPrefPath.str() << endl;
130             //exit(1);
131           }
132         }
133       //exit(1);
134     }
135   return dynamics;
136 }
137
138
139
140
141
142 /******************************************************************************
143  * FGAirportList
144  *****************************************************************************/
145
146 // Populates a list of subdirectories of $FG_ROOT/Airports/AI so that
147 // the add() method doesn't have to try opening 2 XML files in each of
148 // thousands of non-existent directories.  FIXME: should probably add
149 // code to free this list after parsing of apt.dat is finished;
150 // non-issue at the moment, however, as there are no AI subdirectories
151 // in the base package.
152 //
153 // Note: 2005/12/23: This is probably not necessary anymore, because I'm
154 // Switching to runtime airport dynamics loading (DT). 
155 FGAirportList::FGAirportList()
156 {
157 //     ulDir* d;
158 //     ulDirEnt* dent;
159 //     SGPath aid( globals->get_fg_root() );
160 //     aid.append( "/Airports/AI" );
161 //     if((d = ulOpenDir(aid.c_str())) == NULL)
162 //         return;
163 //     while((dent = ulReadDir(d)) != NULL) {
164 //         SG_LOG( SG_GENERAL, SG_DEBUG, "Dent: " << dent->d_name );
165 //         ai_dirs.insert(dent->d_name);
166 //     }
167 //     ulCloseDir(d);
168 }
169
170
171 FGAirportList::~FGAirportList( void ) {
172     for(unsigned int i = 0; i < airports_array.size(); ++i) {
173         delete airports_array[i];
174     }
175 }
176
177
178 // add an entry to the list
179 void FGAirportList::add( const string &id, const double longitude,
180                          const double latitude, const double elevation,
181                          const string &name, const bool has_metar )
182 {
183     FGRunwayPreference rwyPrefs;
184     FGAirport* a = new FGAirport(id, longitude, latitude, elevation, name, has_metar);
185
186     
187     airports_by_id[a->getId()] = a;
188     // try and read in an auxilary file
189     
190     airports_array.push_back( a );
191     SG_LOG( SG_GENERAL, SG_BULK, "Adding " << id << " pos = " << longitude
192             << ", " << latitude << " elev = " << elevation );
193 }
194
195
196 // search for the specified id
197 FGAirport* FGAirportList::search( const string& id) {
198     airport_map_iterator itr = airports_by_id.find(id); 
199     return(itr == airports_by_id.end() ? NULL : itr->second);
200 }
201
202
203 // search for first subsequent alphabetically to supplied id
204 const FGAirport* FGAirportList::findFirstById( const string& id, bool exact ) {
205     airport_map_iterator itr;
206     if(exact) {
207         itr = airports_by_id.find(id);
208     } else {
209         itr = airports_by_id.lower_bound(id);
210     }
211     if(itr == airports_by_id.end()) {
212         return(NULL);
213     } else {
214         return(itr->second);
215     }
216 }
217
218
219 // search for the airport nearest the specified position
220 FGAirport* FGAirportList::search( double lon_deg, double lat_deg,
221                                  bool with_metar ) {
222     int closest = -1;
223     double min_dist = 360.0;
224     unsigned int i;
225     for ( i = 0; i < airports_array.size(); ++i ) {
226         // crude manhatten distance based on lat/lon difference
227         double d = fabs(lon_deg - airports_array[i]->getLongitude())
228             + fabs(lat_deg - airports_array[i]->getLatitude());
229         if ( d < min_dist ) {
230             if ( !with_metar || (with_metar&&airports_array[i]->getMetar()) ) {
231                 closest = i;
232                 min_dist = d;
233             }
234         }
235     }
236
237     return ( closest > -1 ? airports_array[closest] : NULL );
238 }
239
240
241 int
242 FGAirportList::size () const
243 {
244     return airports_array.size();
245 }
246
247 const FGAirport *FGAirportList::getAirport( unsigned int index ) const
248 {
249     if(index < airports_array.size()) {
250         return(airports_array[index]);
251     } else {
252         return(NULL);
253     }
254 }
255
256
257 /**
258  * Mark the specified airport record as not having metar
259  */
260 void FGAirportList::no_metar( const string &id ) {
261     if(airports_by_id.find(id) != airports_by_id.end()) { 
262         airports_by_id[id]->setMetar(false);
263     }
264 }
265
266
267 /**
268  * Mark the specified airport record as (yes) having metar
269  */
270 void FGAirportList::has_metar( const string &id ) {
271     if(airports_by_id.find(id) != airports_by_id.end()) { 
272         airports_by_id[id]->setMetar(true);
273     }
274 }
275
276 // find basic airport location info from airport database
277 const FGAirport *fgFindAirportID( const string& id) {
278     const FGAirport* result = NULL;
279     if ( id.length() ) {
280         SG_LOG( SG_GENERAL, SG_BULK, "Searching for airport code = " << id );
281
282         result = globals->get_airports()->search( id );
283
284         if ( result == NULL ) {
285             SG_LOG( SG_GENERAL, SG_ALERT,
286                     "Failed to find " << id << " in apt.dat.gz" );
287             return NULL;
288         }
289     } else {
290         return NULL;
291     }
292     SG_LOG( SG_GENERAL, SG_BULK,
293             "Position for " << id << " is ("
294             << result->getLongitude() << ", "
295             << result->getLatitude() << ")" );
296
297     return result;
298 }
299
300
301 // get airport elevation
302 double fgGetAirportElev( const string& id ) {
303     
304     // double lon, lat;
305
306     SG_LOG( SG_GENERAL, SG_BULK,
307             "Finding elevation for airport: " << id );
308
309     const FGAirport *a=fgFindAirportID( id);
310     if (a) {
311         return a->getElevation();
312     } else {
313         return -9999.0;
314     }
315 }
316
317 // get airport position
318 Point3D fgGetAirportPos( const string& id ) {
319     // double lon, lat;
320
321     SG_LOG( SG_ATC, SG_BULK,
322             "Finding position for airport: " << id );
323
324     const FGAirport *a = fgFindAirportID( id);
325     
326     if (a) {
327         return Point3D(a->getLongitude(), a->getLatitude(), a->getElevation());
328     } else {
329         return Point3D(0.0, 0.0, -9999.0);
330     }
331 }