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 ) {
53 ilslist.erase( ilslist.begin(), ilslist.end() );
55 sg_gzifstream in( path.str() );
56 if ( !in.is_open() ) {
57 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
61 // read in each line of the file
69 while ( in.get(c) && c != '\0' && ils.get_ilstype() != '[' ) {
72 if ( ils.get_ilstype() != '[' ) {
73 ilslist[ils.get_locfreq()].push_back(ils);
80 double min = 1000000.0;
83 while ( ! in.eof() ) {
86 if ( ils.get_ilstype() == '[' ) {
90 /* cout << "id = " << n.get_ident() << endl;
91 cout << " type = " << n.get_type() << endl;
92 cout << " lon = " << n.get_lon() << endl;
93 cout << " lat = " << n.get_lat() << endl;
94 cout << " elev = " << n.get_elev() << endl;
95 cout << " freq = " << n.get_freq() << endl;
96 cout << " range = " << n.get_range() << endl; */
98 ilslist[ils.get_locfreq()].push_back(ils);
101 if ( ils.get_locfreq() < min ) {
102 min = ils.get_locfreq();
104 if ( ils.get_locfreq() > max ) {
105 max = ils.get_locfreq();
108 // update the marker beacon list
109 if ( fabs(ils.get_omlon()) > SG_EPSILON ||
110 fabs(ils.get_omlat()) > SG_EPSILON ) {
111 current_beacons->add( ils.get_omlon(), ils.get_omlat(),
112 ils.get_gselev(), FGMkrBeacon::OUTER );
114 if ( fabs(ils.get_mmlon()) > SG_EPSILON ||
115 fabs(ils.get_mmlat()) > SG_EPSILON ) {
116 current_beacons->add( ils.get_mmlon(), ils.get_mmlat(),
117 ils.get_gselev(), FGMkrBeacon::MIDDLE );
119 if ( fabs(ils.get_imlon()) > SG_EPSILON ||
120 fabs(ils.get_imlat()) > SG_EPSILON ) {
121 current_beacons->add( ils.get_imlon(), ils.get_imlat(),
122 ils.get_gselev(), FGMkrBeacon::INNER );
126 // cout << "min freq = " << min << endl;
127 // cout << "max freq = " << max << endl;
135 // query the database for the specified frequency, lon and lat are in
136 // degrees, elev is in meters
137 bool FGILSList::query( double lon, double lat, double elev, double freq,
140 ils_list_type stations = ilslist[(int)(freq*100.0 + 0.5)];
142 ils_list_iterator current = stations.begin();
143 ils_list_iterator last = stations.end();
145 // double az1, az2, s;
146 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
149 for ( ; current != last ; ++current ) {
150 // cout << " testing " << current->get_locident() << endl;
151 station = Point3D(current->get_x(),
154 // cout << " aircraft = " << aircraft << " station = " << station
157 d = aircraft.distance3Dsquared( station );
158 // cout << " distance = " << d << " ("
159 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
160 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
163 // cout << " dist = " << s << endl;
165 // match up to twice the published range so we can model
166 // reduced signal strength
167 if ( d < (2* FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
168 * 2 * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER) ) {
170 // Get our bearing from this station.
171 double reciprocal_bearing, dummy;
172 double a_lat_deg = lat * SGD_RADIANS_TO_DEGREES;
173 double a_lon_deg = lon * SGD_RADIANS_TO_DEGREES;
174 // Locator beam direction
175 double s_ils_deg = current->get_locheading() - 180.0;
176 if ( s_ils_deg < 0.0 ) { s_ils_deg += 360.0; }
177 double angle_to_beam_deg;
179 // printf("**ALI geting geo_inverse_wgs_84 with elev = %.2f, a.lat = %.2f, a.lon = %.2f,
180 // s.lat = %.2f, s.lon = %.2f\n", elev,a_lat_deg,a_lon_deg,current->get_loclat(),current->get_loclon());
182 geo_inverse_wgs_84( elev, current->get_loclat(),
183 current->get_loclon(), a_lat_deg, a_lon_deg,
184 &reciprocal_bearing, &dummy, &dummy );
185 angle_to_beam_deg = fabs(reciprocal_bearing - s_ils_deg);
186 if ( angle_to_beam_deg < 90.0 ) {