]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
Fix a bug with converting frequencies to "int".
[flightgear.git] / src / Navaids / navlist.cxx
1 // navaids.cxx -- navaids management class
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/misc/fgstream.hxx>
26 #include <simgear/math/fg_geodesy.hxx>
27
28 #include "navlist.hxx"
29
30
31 FGNavList *current_navlist;
32
33
34 // Constructor
35 FGNavList::FGNavList( void ) {
36 }
37
38
39 // Destructor
40 FGNavList::~FGNavList( void ) {
41 }
42
43
44 // load the navaids and build the map
45 bool FGNavList::init( FGPath path ) {
46     FGNav n;
47
48     navaids.erase( navaids.begin(), navaids.end() );
49
50     fg_gzifstream in( path.str() );
51     if ( !in.is_open() ) {
52         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << path.str() );
53         exit(-1);
54     }
55
56     // read in each line of the file
57
58     in >> skipeol;
59     in >> skipcomment;
60
61 #ifdef __MWERKS__
62
63     char c = 0;
64     while ( in.get(c) && c != '\0' && n.get_type() != '[' ) {
65         in.putback(c);
66         in >> n;
67         if ( n.get_type() != '[' ) {
68             navaids[n.get_freq()].push_back(n);
69         }
70         in >> skipcomment;
71     }
72
73 #else
74
75     while ( ! in.eof() && n.get_type() != '[' ) {
76         in >> n;
77         /* cout << "id = " << n.get_ident() << endl;
78         cout << " type = " << n.get_type() << endl;
79         cout << " lon = " << n.get_lon() << endl;
80         cout << " lat = " << n.get_lat() << endl;
81         cout << " elev = " << n.get_elev() << endl;
82         cout << " freq = " << n.get_freq() << endl;
83         cout << " range = " << n.get_range() << endl; */
84         if ( n.get_type() != '[' ) {
85             navaids[n.get_freq()].push_back(n);
86         }
87         in >> skipcomment;
88     }
89
90 #endif
91
92     return true;
93 }
94
95
96 // query the database for the specified frequency, lon and lat are in
97 // degrees, elev is in meters
98 bool FGNavList::query( double lon, double lat, double elev, double freq,
99                        FGNav *n, double *heading, double *dist )
100 {
101     nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
102
103     nav_list_iterator current = stations.begin();
104     nav_list_iterator last = stations.end();
105
106     double az1, az2, s;
107     for ( ; current != last ; ++current ) {
108         // cout << "testing " << current->get_ident() << endl;
109         geo_inverse_wgs_84( elev, lat, lon, 
110                             current->get_lat(), current->get_lon(),
111                             &az1, &az2, &s );
112         // cout << "  dist = " << s << endl;
113         if ( s < ( current->get_range() * NM_TO_METER ) ) {
114             *n = *current;
115             *heading = az2;
116             *dist = s;
117             return true;
118         }
119     }
120
121     return false;
122 }