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.
8 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <simgear/compiler.h>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sgstream.hxx>
42 SG_USING_NAMESPACE(std);
43 SG_USING_STD(istream);
47 operator >> ( istream& in, FGAirport& a )
50 in >> junk >> a.id >> a.latitude >> a.longitude >> a.elevation
55 // Remove the space before the name
56 if ( a.name.substr(0,1) == " " ) {
57 a.name = a.name.erase(0,1);
63 // As a quick seed for the has_metar value, only airports with
64 // four-letter codes can have metar stations
65 a.has_metar = (isalpha(a.id[0]) && isalpha(a.id[1]) && isalpha(a.id[2])
66 && isalpha(a.id[3]) && !a.id[4]);
73 FGAirportList::FGAirportList( const string &airport_file,
74 const string &metar_file ) {
75 SG_LOG( SG_GENERAL, SG_INFO, "Reading simple airport list: "
78 // open the specified file for reading
79 sg_gzifstream apt_in( airport_file );
80 if ( !apt_in.is_open() ) {
81 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << airport_file );
91 airports_by_id[a.id] = a;
92 airports_array.push_back( &airports_by_id[a.id] );
96 SG_LOG( SG_GENERAL, SG_INFO, "Reading simple metar station list: "
99 // open the specified file for reading
100 sg_gzifstream metar_in( metar_file );
101 if ( !metar_in.is_open() ) {
102 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
108 if ( ident == "#" || ident == "//" ) {
111 airport_map_iterator apt = airports_by_id.find( ident );
112 if ( apt == airports_by_id.end() ) {
113 SG_LOG( SG_GENERAL, SG_DEBUG, "no apt = " << ident );
115 SG_LOG( SG_GENERAL, SG_DEBUG, "metar = " << ident );
116 airports_by_id[ident].has_metar = true;
123 // search for the specified id
124 FGAirport FGAirportList::search( const string& id) {
125 return airports_by_id[id];
129 // search for the airport nearest the specified position
130 FGAirport FGAirportList::search( double lon_deg, double lat_deg,
133 double min_dist = 360.0;
135 for ( i = 0; i < airports_array.size(); ++i ) {
136 // crude manhatten distance based on lat/lon difference
137 double d = fabs(lon_deg - airports_array[i]->longitude)
138 + fabs(lat_deg - airports_array[i]->latitude);
139 if ( d < min_dist ) {
140 if ( !with_metar || (with_metar && airports_array[i]->has_metar) ) {
147 return *airports_array[closest];
152 FGAirportList::~FGAirportList( void ) {
156 FGAirportList::size () const
158 return airports_array.size();
161 const FGAirport *FGAirportList::getAirport( int index ) const
163 return airports_array[index];
168 * Mark the specified airport record as not having metar
170 void FGAirportList::no_metar( const string &id ) {
171 airports_by_id[id].has_metar = false;