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() && ils.get_ilstype() != '[' ) {
85 /* cout << "id = " << n.get_ident() << endl;
86 cout << " type = " << n.get_type() << endl;
87 cout << " lon = " << n.get_lon() << endl;
88 cout << " lat = " << n.get_lat() << endl;
89 cout << " elev = " << n.get_elev() << endl;
90 cout << " freq = " << n.get_freq() << endl;
91 cout << " range = " << n.get_range() << endl; */
92 if ( ils.get_ilstype() != '[' ) {
93 ilslist[ils.get_locfreq()].push_back(ils);
97 if ( ils.get_locfreq() < min ) {
98 min = ils.get_locfreq();
100 if ( ils.get_locfreq() > max ) {
101 max = ils.get_locfreq();
104 // update the marker beacon list
105 if ( fabs(ils.get_omlon()) > SG_EPSILON ||
106 fabs(ils.get_omlat()) > SG_EPSILON ) {
107 current_beacons->add( ils.get_omlon(), ils.get_omlat(),
108 ils.get_gselev(), FGMkrBeacon::OUTER );
110 if ( fabs(ils.get_mmlon()) > SG_EPSILON ||
111 fabs(ils.get_mmlat()) > SG_EPSILON ) {
112 current_beacons->add( ils.get_mmlon(), ils.get_mmlat(),
113 ils.get_gselev(), FGMkrBeacon::MIDDLE );
115 if ( fabs(ils.get_imlon()) > SG_EPSILON ||
116 fabs(ils.get_imlat()) > SG_EPSILON ) {
117 current_beacons->add( ils.get_imlon(), ils.get_imlat(),
118 ils.get_gselev(), FGMkrBeacon::INNER );
122 // cout << "min freq = " << min << endl;
123 // cout << "max freq = " << max << endl;
131 // query the database for the specified frequency, lon and lat are in
132 // degrees, elev is in meters
133 bool FGILSList::query( double lon, double lat, double elev, double freq,
136 ils_list_type stations = ilslist[(int)(freq*100.0 + 0.5)];
138 ils_list_iterator current = stations.begin();
139 ils_list_iterator last = stations.end();
141 // double az1, az2, s;
142 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
145 for ( ; current != last ; ++current ) {
146 // cout << " testing " << current->get_locident() << endl;
147 station = Point3D(current->get_x(),
150 // cout << " aircraft = " << aircraft << " station = " << station
153 d = aircraft.distance3Dsquared( station );
154 // cout << " distance = " << d << " ("
155 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
156 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
159 // cout << " dist = " << s << endl;
161 // match up to twice the published range so we can model
162 // reduced signal strength
163 if ( d < (2* FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
164 * 2 * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER) ) {
166 // Get our bearing from this station.
167 double reciprocal_bearing, dummy;
168 double a_lat_deg = lat * SGD_RADIANS_TO_DEGREES;
169 double a_lon_deg = lon * SGD_RADIANS_TO_DEGREES;
170 // Locator beam direction
171 double s_ils_deg = current->get_locheading() - 180.0;
172 if ( s_ils_deg < 0.0 ) { s_ils_deg += 360.0; }
173 double angle_to_beam_deg;
175 // printf("**ALI geting geo_inverse_wgs_84 with elev = %.2f, a.lat = %.2f, a.lon = %.2f,
176 // s.lat = %.2f, s.lon = %.2f\n", elev,a_lat_deg,a_lon_deg,current->get_loclat(),current->get_loclon());
178 geo_inverse_wgs_84( elev, current->get_loclat(),
179 current->get_loclon(), a_lat_deg, a_lon_deg,
180 &reciprocal_bearing, &dummy, &dummy );
181 angle_to_beam_deg = fabs(reciprocal_bearing - s_ils_deg);
182 if ( angle_to_beam_deg < 90.0 ) {