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