1 // ilslist.cxx -- ils 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 "mkrbeacons.hxx"
33 #include "ilslist.hxx"
36 FGILSList *current_ilslist;
40 FGILSList::FGILSList( void ) {
45 FGILSList::~FGILSList( void ) {
49 // load the navaids and build the map
50 bool FGILSList::init( SGPath path ) {
52 ilslist.erase( ilslist.begin(), ilslist.end() );
54 sg_gzifstream in( path.str() );
55 if ( !in.is_open() ) {
56 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
60 // read in each line of the file
65 // double min = 1000000.0;
70 while ( in.get(c) && c != '\0' ) {
73 while ( ! in.eof() ) {
76 FGILS *ils = new FGILS;
78 if ( ils->get_ilstype() == '[' ) {
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; */
88 ilslist[ils->get_locfreq()].push_back(ils);
91 /* if ( ils.get_locfreq() < min ) {
92 min = ils.get_locfreq();
94 if ( ils.get_locfreq() > max ) {
95 max = ils.get_locfreq();
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 );
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 );
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 );
116 // cout << "min freq = " << min << endl;
117 // cout << "max freq = " << max << endl;
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
127 FGILS *FGILSList::findByFreq( double freq,
128 double lon, double lat, double elev )
132 ils_list_type stations = ilslist[(int)(freq*100.0 + 0.5)];
134 double best_angle = 362.0;
136 // double az1, az2, s;
137 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
140 for ( unsigned int i = 1; 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
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
154 // cout << " dist = " << s << endl;
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) ) {
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;
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());
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 ) {
183 best_angle = angle_to_beam_deg;