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