1 // navdb.cxx -- top level navaids management routines
3 // Written by Curtis Olson, started May 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
33 #include <Airports/runways.hxx>
34 #include <Airports/simple.hxx>
35 #include <Main/globals.hxx>
37 #include "navrecord.hxx"
43 // load and initialize the navigational databases
44 bool fgNavDBInit( FGAirportList *airports,
45 FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
46 FGNavList *dmelist, FGNavList *mkrlist,
47 FGNavList *tacanlist, FGNavList *carrierlist,
48 FGTACANList *channellist)
50 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
51 // SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
52 // SGPath p_nav( globals->get_fg_root() );
53 // p_nav.append( "Navaids/default.nav" );
54 // navlist->init( p_nav );
56 // SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
58 // SGPath p_ils( globals->get_fg_root() );
59 // p_ils.append( "Navaids/default.ils" );
60 // ilslist->init( p_ils );
63 SGPath path( globals->get_fg_root() );
64 path.append( "Navaids/nav.dat" );
66 sg_gzifstream in( path.str() );
67 if ( !in.is_open() ) {
68 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
72 // skip first two lines
76 while ( ! in.eof() ) {
77 FGNavRecord *r = new FGNavRecord;
79 if ( r->get_type() > 95 ) {
84 /*cout << "id = " << r->get_ident() << endl;
85 cout << " type = " << r->get_type() << endl;
86 cout << " lon = " << r->get_lon() << endl;
87 cout << " lat = " << r->get_lat() << endl;
88 cout << " elev = " <<r->get_elev_ft() << endl;
89 cout << " freq = " << r->get_freq() << endl;
90 cout << " range = " << r->get_range() << endl;
91 cout << " name = " << r->get_name() << endl << endl; */
93 // fudge elevation to the field elevation if it's not specified
94 if ( fabs(r->get_elev_ft()) < 0.01 && r->get_apt_id().length() ) {
95 // cout << r->get_type() << " " << r->get_apt_id() << " zero elev"
97 const FGAirport* a = airports->search( r->get_apt_id() );
99 r->set_elev_ft( a->getElevation() );
100 // cout << " setting to " << a.elevation << endl;
104 if ( r->get_type() == 2 || r->get_type() == 3 ) {
107 } else if ( r->get_type() == 4 || r->get_type() == 5 ) {
108 // ILS=4, LOC(only)=5
110 } else if ( r->get_type() == 6 ) {
113 } else if ( r->get_type() == 7 || r->get_type() == 8
114 || r->get_type() == 9 )
116 // Marker Beacon = 7,8,9
118 } else if ( r->get_type() == 12 || r->get_type() == 13) {
119 // DME with ILS=12; standalone DME=13
120 string str1( r->get_name() );
121 string::size_type loc1= str1.find( "TACAN", 0 );
122 string::size_type loc2 = str1.find( "VORTAC", 0 );
124 if( loc1 != string::npos || loc2 != string::npos ){
125 //cout << " name = " << r->get_name() ;
126 //cout << " freq = " << r->get_freq() ;
137 // load the carrier navaids file
141 path = globals->get_fg_root() ;
142 path.append( "Navaids/carrier_nav.dat" );
145 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
147 sg_gzifstream incarrier( path.str() );
149 if ( !incarrier.is_open() ) {
150 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
154 // skip first two lines
155 //incarrier >> skipeol;
156 //incarrier >> skipeol;
158 while ( ! incarrier.eof() ) {
159 FGNavRecord *r = new FGNavRecord;
161 carrierlist->add ( r );
162 /*cout << " carrier lon: "<< r->get_lon() ;
163 cout << " carrier lat: "<< r->get_lat() ;
164 cout << " freq: " << r->get_freq() ;
165 cout << " carrier name: "<< r->get_name() << endl;*/
167 //if ( r->get_type() > 95 ) {
171 // end loading the carrier navaids file
173 // load the channel/freqency file
174 string channel, freq;
176 path = globals->get_fg_root();
177 path.append( "Navaids/TACAN_freq.dat" );
179 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
181 sg_gzifstream inchannel( path.str() );
183 if ( !inchannel.is_open() ) {
184 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
189 inchannel >> skipeol;
190 while ( ! inchannel.eof() ) {
191 FGTACANRecord *r = new FGTACANRecord;
193 channellist->add ( r );
194 //cout << "channel = " << r->get_channel() ;
195 //cout << " freq = " << r->get_freq() << endl;
207 // Given a localizer record and it's corresponding runway record,
208 // adjust the localizer position so it is in perfect alignment with
210 static void update_loc_position( FGNavRecord *loc, FGRunway *rwy,
213 double hdg = rwy->_heading;
219 // calculate runway threshold point
220 double thresh_lat, thresh_lon, return_az;
221 geo_direct_wgs_84 ( 0.0, rwy->_lat, rwy->_lon, hdg,
222 rwy->_length/2.0 * SG_FEET_TO_METER,
223 &thresh_lat, &thresh_lon, &return_az );
224 // cout << "Threshold = " << thresh_lat << "," << thresh_lon << endl;
226 // calculate distance from threshold to localizer
227 double az1, az2, dist_m;
228 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
229 thresh_lat, thresh_lon,
230 &az1, &az2, &dist_m );
231 // cout << "Distance = " << dist_m << endl;
233 // back project that distance along the runway center line
234 double nloc_lat, nloc_lon;
235 geo_direct_wgs_84 ( 0.0, thresh_lat, thresh_lon, hdg + 180.0,
236 dist_m, &nloc_lat, &nloc_lon, &return_az );
237 // printf("New localizer = %.6f %.6f\n", nloc_lat, nloc_lon );
239 // sanity check, how far have we moved the localizer?
240 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
242 &az1, &az2, &dist_m );
243 // cout << "Distance moved = " << dist_m << endl;
245 // cout << "orig heading = " << loc->get_multiuse() << endl;
246 // cout << "new heading = " << rwy->_heading << endl;
248 double hdg_diff = loc->get_multiuse() - rwy->_heading;
250 // clamp to [-180.0 ... 180.0]
251 if ( hdg_diff < -180.0 ) {
253 } else if ( hdg_diff > 180.0 ) {
257 if ( fabs(hdg_diff) <= threshold ) {
258 loc->set_lat( nloc_lat );
259 loc->set_lon( nloc_lon );
260 loc->set_multiuse( rwy->_heading );
265 // This routines traverses the localizer list and attempts to match
266 // each entry with it's corresponding runway. When it is successful,
267 // it then "moves" the localizer and updates it's heading so it
268 // *perfectly* aligns with the runway, but is still the same distance
269 // from the runway threshold.
270 void fgNavDBAlignLOCwithRunway( FGRunwayList *runways, FGNavList *loclist,
272 nav_map_type navmap = loclist->get_navaids();
274 nav_map_const_iterator freq = navmap.begin();
275 while ( freq != navmap.end() ) {
276 nav_list_type locs = freq->second;
277 nav_list_const_iterator loc = locs.begin();
278 while ( loc != locs.end() ) {
279 string name = (*loc)->get_name();
280 string::size_type pos1 = name.find(" ");
281 string id = name.substr(0, pos1);
282 name = name.substr(pos1+1);
283 string::size_type pos2 = name.find(" ");
284 string rwy = name.substr(0, pos2);
287 if ( runways->search(id, rwy, &r) ) {
288 update_loc_position( (*loc), &r, threshold );