]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
04a528f956cf27454f0b25d3cd8370bf3c01b0b2
[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 //
8 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
9 //
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <simgear/compiler.h>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/misc/sgstream.hxx>
34
35 #include STL_STRING
36 #include STL_IOSTREAM
37
38 #include "simple.hxx"
39
40 SG_USING_NAMESPACE(std);
41 SG_USING_STD(istream);
42
43
44 inline istream&
45 operator >> ( istream& in, FGAirport& a )
46 {
47     string junk;
48     in >> junk >> a.id >> a.latitude >> a.longitude >> a.elevation
49        >> a.code;
50
51     char name[256];             // should never be longer than this, right? :-)
52     in.getline( name, 256 );
53     a.name = name;
54
55     a.has_metar = true;         // assume true
56
57     return in;
58 }
59
60
61 FGAirportList::FGAirportList( const string& file ) {
62     SG_LOG( SG_GENERAL, SG_DEBUG, "Reading simple airport list: " << file );
63
64     // open the specified file for reading
65     sg_gzifstream in( file );
66     if ( !in.is_open() ) {
67         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
68         exit(-1);
69     }
70
71     // skip header line
72     in >> skipeol;
73
74     FGAirport a;
75     int size = 0;
76     while ( in ) {
77         in >> a;
78         airports_by_id[a.id] = a;
79         airports_array.push_back( &airports_by_id[a.id] );
80         size++;
81     }
82 }
83
84
85 // search for the specified id
86 FGAirport FGAirportList::search( const string& id) {
87     
88     return airports_by_id[id];
89 }
90
91
92 // search for the airport nearest the specified position
93 FGAirport FGAirportList::search( double lon_deg, double lat_deg,
94                                  bool with_metar ) {
95     int closest = 0;
96     double min_dist = 360.0;
97     unsigned int i;
98     for ( i = 0; i < airports_array.size(); ++i ) {
99         // crude manhatten distance based on lat/lon difference
100         double d = fabs(lon_deg - airports_array[i]->longitude)
101             + fabs(lat_deg - airports_array[i]->latitude);
102         if ( d < min_dist ) {
103             if ( !with_metar || (with_metar && airports_array[i]->has_metar) ) {
104                 closest = i;
105                 min_dist = d;
106             }
107         }
108     }
109
110     return *airports_array[closest];
111 }
112
113
114 // Destructor
115 FGAirportList::~FGAirportList( void ) {
116 }
117
118 int
119 FGAirportList::size () const
120 {
121     return airports_array.size();
122 }
123
124 const FGAirport FGAirportList::getAirport( int index ) const
125 {
126     return *airports_array[index];
127 }
128
129
130 /**
131  * Mark the specified airport record as not having metar
132  */
133 void FGAirportList::no_metar( const string &id ) {
134     airports_by_id[id].has_metar = false;
135 }