2 // simple.cxx -- a really simplistic class to manage airport ID,
3 // lat, lon of the center of one of it's runways, and
6 // Written by Curtis Olson, started April 1998.
7 // Updated by Durk Talsma, started December, 2004.
9 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
34 #include <simgear/compiler.h>
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>
55 SG_USING_STD(random_shuffle);
60 /***************************************************************************
62 ***************************************************************************/
63 FGAirport::FGAirport() : _longitude(0), _latitude(0), _elevation(0)
69 FGAirport::FGAirport(const string &id, double lon, double lat, double elev, const string &name, bool has_metar)
76 _has_metar = has_metar;
81 FGAirport::~FGAirport()
87 FGAirportDynamics * FGAirport::getDynamics()
92 FGRunwayPreference rwyPrefs;
93 //cerr << "Trying to load dynamics for " << _id << endl;
94 dynamics = new FGAirportDynamics(_latitude, _longitude, _elevation, _id);
96 SGPath parkpath( globals->get_fg_root() );
97 parkpath.append( "/AI/Airports/" );
99 parkpath.append("parking.xml");
101 SGPath rwyPrefPath( globals->get_fg_root() );
102 rwyPrefPath.append( "AI/Airports/" );
103 rwyPrefPath.append(_id);
104 rwyPrefPath.append("rwyuse.xml");
106 //if (ai_dirs.find(id.c_str()) != ai_dirs.end()
107 // && parkpath.exists())
108 if (parkpath.exists()) {
110 readXML(parkpath.str(),*dynamics);
111 //cerr << "Initializing " << getId() << endl;
113 dynamics->getGroundNetwork()->setParent(this);
114 } catch (const sg_exception &e) {
115 //cerr << "unable to read " << parkpath.str() << endl;
119 //if (ai_dirs.find(id.c_str()) != ai_dirs.end()
120 // && rwyPrefPath.exists())
121 if (rwyPrefPath.exists()) {
123 readXML(rwyPrefPath.str(), rwyPrefs);
124 dynamics->setRwyUse(rwyPrefs);
125 } catch (const sg_exception &e) {
126 //cerr << "unable to read " << rwyPrefPath.str() << endl;
139 /******************************************************************************
141 *****************************************************************************/
143 // Populates a list of subdirectories of $FG_ROOT/Airports/AI so that
144 // the add() method doesn't have to try opening 2 XML files in each of
145 // thousands of non-existent directories. FIXME: should probably add
146 // code to free this list after parsing of apt.dat is finished;
147 // non-issue at the moment, however, as there are no AI subdirectories
148 // in the base package.
150 // Note: 2005/12/23: This is probably not necessary anymore, because I'm
151 // Switching to runtime airport dynamics loading (DT).
152 FGAirportList::FGAirportList()
156 // SGPath aid( globals->get_fg_root() );
157 // aid.append( "/Airports/AI" );
158 // if((d = ulOpenDir(aid.c_str())) == NULL)
160 // while((dent = ulReadDir(d)) != NULL) {
161 // SG_LOG( SG_GENERAL, SG_DEBUG, "Dent: " << dent->d_name );
162 // ai_dirs.insert(dent->d_name);
168 FGAirportList::~FGAirportList( void )
170 for (unsigned int i = 0; i < airports_array.size(); ++i) {
171 delete airports_array[i];
176 // add an entry to the list
177 void FGAirportList::add( const string &id, const double longitude,
178 const double latitude, const double elevation,
179 const string &name, const bool has_metar )
181 FGRunwayPreference rwyPrefs;
182 FGAirport* a = new FGAirport(id, longitude, latitude, elevation, name, has_metar);
184 airports_by_id[a->getId()] = a;
185 // try and read in an auxilary file
187 airports_array.push_back( a );
188 SG_LOG( SG_GENERAL, SG_BULK, "Adding " << id << " pos = " << longitude
189 << ", " << latitude << " elev = " << elevation );
193 // search for the specified id
194 FGAirport* FGAirportList::search( const string& id)
196 airport_map_iterator itr = airports_by_id.find(id);
197 return (itr == airports_by_id.end() ? NULL : itr->second);
201 // search for first subsequent alphabetically to supplied id
202 const FGAirport* FGAirportList::findFirstById( const string& id, bool exact )
204 airport_map_iterator itr;
206 itr = airports_by_id.find(id);
208 itr = airports_by_id.lower_bound(id);
210 if (itr == airports_by_id.end()) {
213 return (itr->second);
218 // search for the airport nearest the specified position
219 FGAirport* FGAirportList::search( double lon_deg, double lat_deg,
223 double min_dist = 360.0;
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()) ) {
237 return ( closest > -1 ? airports_array[closest] : NULL );
242 FGAirportList::size () const
244 return airports_array.size();
248 const FGAirport *FGAirportList::getAirport( unsigned int index ) const
250 if (index < airports_array.size()) {
251 return(airports_array[index]);
259 * Mark the specified airport record as not having metar
261 void FGAirportList::no_metar( const string &id )
263 if(airports_by_id.find(id) != airports_by_id.end()) {
264 airports_by_id[id]->setMetar(false);
270 * Mark the specified airport record as (yes) having metar
272 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);
280 // find basic airport location info from airport database
281 const FGAirport *fgFindAirportID( const string& id)
283 const FGAirport* result = NULL;
285 SG_LOG( SG_GENERAL, SG_BULK, "Searching for airport code = " << id );
287 result = globals->get_airports()->search( id );
289 if ( result == NULL ) {
290 SG_LOG( SG_GENERAL, SG_ALERT,
291 "Failed to find " << id << " in apt.dat.gz" );
297 SG_LOG( SG_GENERAL, SG_BULK,
298 "Position for " << id << " is ("
299 << result->getLongitude() << ", "
300 << result->getLatitude() << ")" );
306 // get airport elevation
307 double fgGetAirportElev( const string& id )
309 SG_LOG( SG_GENERAL, SG_BULK,
310 "Finding elevation for airport: " << id );
312 const FGAirport *a=fgFindAirportID( id);
314 return a->getElevation();
321 // get airport position
322 Point3D fgGetAirportPos( const string& id )
324 SG_LOG( SG_ATC, SG_BULK,
325 "Finding position for airport: " << id );
327 const FGAirport *a = fgFindAirportID( id);
330 return Point3D(a->getLongitude(), a->getLatitude(), a->getElevation());
332 return Point3D(0.0, 0.0, -9999.0);