1 // navlist.cxx -- navaids management class
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
32 #include "navlist.hxx"
35 FGNavList *current_navlist;
39 FGNavList::FGNavList( void ) {
44 FGNavList::~FGNavList( void ) {
48 // load the navaids and build the map
49 bool FGNavList::init( SGPath path ) {
51 navaids.erase( navaids.begin(), navaids.end() );
53 sg_gzifstream in( path.str() );
54 if ( !in.is_open() ) {
55 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
59 // read in each line of the file
64 // double min = 100000;
69 while ( in.get(c) && c != '\0' ) {
72 while ( ! in.eof() ) {
77 if ( n->get_type() == '[' ) {
81 /* cout << "id = " << n.get_ident() << endl;
82 cout << " type = " << n.get_type() << endl;
83 cout << " lon = " << n.get_lon() << endl;
84 cout << " lat = " << n.get_lat() << endl;
85 cout << " elev = " << n.get_elev() << endl;
86 cout << " freq = " << n.get_freq() << endl;
87 cout << " range = " << n.get_range() << endl << endl; */
89 navaids [n->get_freq() ].push_back(n);
90 ident_navaids[n->get_ident()].push_back(n);
94 /* if ( n.get_type() != 'N' ) {
95 if ( n.get_freq() < min ) {
98 if ( n.get_freq() > max ) {
104 // cout << "min freq = " << min << endl;
105 // cout << "max freq = " << max << endl;
111 // Query the database for the specified frequency. It is assumed that
112 // there will be multiple stations with matching frequencies so a
113 // position must be specified. Lon and lat are in degrees, elev is in
115 FGNav *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
117 nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
118 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
120 return findNavFromList( aircraft, stations );
124 FGNav *FGNavList::findByIdent( const char* ident,
125 const double lon, const double lat )
127 nav_list_type stations = ident_navaids[ident];
128 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, 0.0) );
130 return findNavFromList( aircraft, stations );
134 // Given an Ident and optional freqency, return the first matching
136 FGNav *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
138 nav_list_type stations = ident_navaids[ident];
141 // sometimes there can be duplicated idents. If a freq is
142 // specified, use it to refine the search.
143 int f = (int)(freq*100.0 + 0.5);
144 for ( unsigned int i = 0; i < stations.size(); ++i ) {
145 if ( f == stations[i]->get_freq() ) {
157 // Given a point and a list of stations, return the closest one to the
159 FGNav *FGNavList::findNavFromList( const Point3D &aircraft,
160 const nav_list_type &stations )
165 double min_dist = 999999999.0;
167 // prime the pump with info from stations[0]
168 if ( stations.size() > 0 ) {
170 station = Point3D( nav->get_x(), nav->get_y(), nav->get_z());
171 min_dist = aircraft.distance3Dsquared( station );
174 // check if any of the remaining stations are closer
175 for ( unsigned int i = 1; i < stations.size(); ++i ) {
176 // cout << "testing " << current->get_ident() << endl;
177 station = Point3D( stations[i]->get_x(),
178 stations[i]->get_y(),
179 stations[i]->get_z() );
181 d2 = aircraft.distance3Dsquared( station );
183 // cout << " dist = " << sqrt(d)
184 // << " range = " << current->get_range() * SG_NM_TO_METER
187 if ( d2 < min_dist ) {