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