]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/ilslist.cxx
ignore resets for now because every z/Z key press would trigger a call to NOAA. We...
[flightgear.git] / src / Navaids / ilslist.cxx
1 // ilslist.cxx -- ils 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 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31
32 #include "mkrbeacons.hxx"
33 #include "ilslist.hxx"
34
35
36 FGILSList *current_ilslist;
37
38
39 // Constructor
40 FGILSList::FGILSList( void ) {
41 }
42
43
44 // Destructor
45 FGILSList::~FGILSList( void ) {
46 }
47
48
49 // load the navaids and build the map
50 bool FGILSList::init( SGPath path ) {
51
52     ilslist.erase( ilslist.begin(), ilslist.end() );
53
54     sg_gzifstream in( path.str() );
55     if ( !in.is_open() ) {
56         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
57         exit(-1);
58     }
59
60     // read in each line of the file
61
62     in >> skipeol;
63     in >> skipcomment;
64
65     // double min = 1000000.0;
66     // double max = 0.0;
67
68 #ifdef __MWERKS__
69     char c = 0;
70     while ( in.get(c) && c != '\0' ) {
71         in.putback(c);
72 #else
73     while ( ! in.eof() ) {
74 #endif
75         
76         FGILS *ils = new FGILS;
77         in >> (*ils);
78         if ( ils->get_ilstype() == '[' ) {
79             break;
80         }
81
82         /* cout << "typename = " << ils.get_ilstypename() << endl;
83         cout << " aptcode = " << ils.get_aptcode() << endl;
84         cout << " twyno = " << ils.get_rwyno() << endl;
85         cout << " locfreq = " << ils.get_locfreq() << endl;
86         cout << " locident = " << ils.get_locident() << endl << endl; */
87
88         ilslist[ils->get_locfreq()].push_back(ils);
89         in >> skipcomment;
90
91         /* if ( ils.get_locfreq() < min ) {
92             min = ils.get_locfreq();
93         }
94         if ( ils.get_locfreq() > max ) {
95             max = ils.get_locfreq();
96         } */
97
98         // update the marker beacon list
99         if ( fabs(ils->get_omlon()) > SG_EPSILON ||
100              fabs(ils->get_omlat()) > SG_EPSILON ) {
101             current_beacons->add( ils->get_omlon(), ils->get_omlat(),
102                                   ils->get_gselev(), FGMkrBeacon::OUTER );
103         }
104         if ( fabs(ils->get_mmlon()) > SG_EPSILON ||
105              fabs(ils->get_mmlat()) > SG_EPSILON ) {
106             current_beacons->add( ils->get_mmlon(), ils->get_mmlat(),
107                                   ils->get_gselev(), FGMkrBeacon::MIDDLE );
108         }
109         if ( fabs(ils->get_imlon()) > SG_EPSILON ||
110              fabs(ils->get_imlat()) > SG_EPSILON ) {
111             current_beacons->add( ils->get_imlon(), ils->get_imlat(),
112                                   ils->get_gselev(), FGMkrBeacon::INNER );
113         }
114     }
115
116     // cout << "min freq = " << min << endl;
117     // cout << "max freq = " << max << endl;
118
119     return true;
120 }
121
122
123 // Query the database for the specified frequency.  It is assumed that
124 // there will be multiple stations with matching frequencies so a
125 // position must be specified.  Lon and lat are in degrees, elev is in
126 // meters.
127 FGILS *FGILSList::findByFreq( double freq,
128                               double lon, double lat, double elev )
129 {
130     FGILS *ils = NULL;
131
132     ils_list_type stations = ilslist[(int)(freq*100.0 + 0.5)];
133
134     double best_angle = 362.0;
135
136     // double az1, az2, s;
137     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
138     Point3D station;
139     double d2;
140     for ( unsigned int i = 0; i < stations.size(); ++i ) {
141         // cout << "  testing " << current->get_locident() << endl;
142         station = Point3D(stations[i]->get_x(), 
143                           stations[i]->get_y(),
144                           stations[i]->get_z());
145         // cout << "    aircraft = " << aircraft << " station = " << station 
146         //      << endl;
147
148         d2 = aircraft.distance3Dsquared( station );
149         // cout << "  distance = " << d << " (" 
150         //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
151         //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
152         //      << ")" << endl;
153
154         // cout << "  dist = " << s << endl;
155
156         // match up to twice the published range so we can model
157         // reduced signal strength.  The assumption is that there will
158         // be maximum of two possbile stations of a particular
159         // frequency in range.  If two exist, one will be associated
160         // with each end of the runway.  In this case, pick the
161         // station pointing most directly at us.
162         if ( d2 < (2* FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
163                    * 2 * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER) ) {
164
165             // Get our bearing from this station.
166             double reciprocal_bearing, dummy;
167             double a_lat_deg = lat * SGD_RADIANS_TO_DEGREES;
168             double a_lon_deg = lon * SGD_RADIANS_TO_DEGREES;
169             // Locator beam direction
170             double s_ils_deg = stations[i]->get_locheading() - 180.0;
171             if ( s_ils_deg < 0.0 ) { s_ils_deg += 360.0; }
172             double angle_to_beam_deg;
173
174             // printf("**ALI geting geo_inverse_wgs_84 with elev = %.2f, a.lat = %.2f, a.lon = %.2f,
175             // s.lat = %.2f, s.lon = %.2f\n", elev,a_lat_deg,a_lon_deg,stations[i]->get_loclat(),stations[i]->get_loclon());
176
177             geo_inverse_wgs_84( elev, stations[i]->get_loclat(),
178                                 stations[i]->get_loclon(), a_lat_deg, a_lon_deg,
179                                 &reciprocal_bearing, &dummy, &dummy );
180             angle_to_beam_deg = fabs(reciprocal_bearing - s_ils_deg);
181             if ( angle_to_beam_deg <= best_angle ) {
182                 ils = stations[i];
183                 best_angle = angle_to_beam_deg;
184             }
185         }
186     }
187
188     return ils;
189 }