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, lon and lat are in
112 // degrees, elev is in meters
113 bool FGNavList::query( double lon, double lat, double elev, double freq,
116 nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
118 nav_list_iterator current = stations.begin();
119 nav_list_iterator last = stations.end();
121 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
122 return findNavFromList(aircraft, current, last, nav);
126 bool FGNavList::findByIdent(const char* ident, double lon, double lat,
129 nav_list_type stations = ident_navaids[ident];
131 nav_list_iterator current = stations.begin();
132 nav_list_iterator last = stations.end();
134 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, 0.0) );
135 return findNavFromList(aircraft, current, last, nav);
139 bool FGNavList::findByIdentAndFreq(const char* ident, const double& freq,
142 cout << "ident = " << ident << endl;
143 nav_list_type stations = ident_navaids[ident];
144 cout << " matches = " << stations.size() << endl;
145 nav_list_iterator current = stations.begin();
146 nav_list_iterator last = stations.end();
148 if ( stations.size() > 1 ) {
149 // more than one match on this ident, use freq to refine
150 int f = (int)(freq*100.0 + 0.5);
151 for ( ; current != last ; ++current ) {
152 if ( f == (*current)->get_freq() ) {
166 bool FGNavList::findNavFromList(const Point3D &aircraft,
167 nav_list_iterator current,
168 nav_list_iterator end, FGNav *nav)
170 // double az1, az2, s;
174 double min_dist = 99999999999999.9;
175 bool found_one = false;
176 for ( ; current != end ; ++current ) {
177 // cout << "testing " << current->get_ident() << endl;
178 station = Point3D((*current)->get_x(), (*current)->get_y(),
179 (*current)->get_z());
181 d2 = aircraft.distance3Dsquared( station );
183 // cout << " dist = " << sqrt(d)
184 // << " range = " << current->get_range() * SG_NM_TO_METER
187 // match d^2 < 2 * range^2 the published range so we can model
188 // reduced signal strength
189 double twiceRange = 2 * (*current)->get_range() * SG_NM_TO_METER;
190 if ( d2 < (twiceRange * twiceRange)) {
191 // cout << "d2 = " << d2 << " min_dist = " << min_dist << endl;
192 if ( d2 < min_dist ) {
196 // cout << "matched = " << (*current)->get_ident() << endl;
198 // cout << "matched, but too far away = "
199 // << (*current)->get_ident() << endl;