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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/compiler.h>
28 #include <simgear/debug/logstream.hxx>
30 #include <Airports/runways.hxx>
31 #include <Airports/simple.hxx>
32 #include <Main/globals.hxx>
34 #include "navrecord.hxx"
37 SG_USING_STD( string );
40 // load and initialize the navigational databases
41 bool fgNavDBInit( FGAirportList *airports,
42 FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
43 FGNavList *dmelist, FGNavList *mkrlist,
44 FGNavList *tacanlist, FGNavList *carrierlist,
45 FGTACANList *channellist)
47 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
48 // SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
49 // SGPath p_nav( globals->get_fg_root() );
50 // p_nav.append( "Navaids/default.nav" );
51 // navlist->init( p_nav );
53 // SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
55 // SGPath p_ils( globals->get_fg_root() );
56 // p_ils.append( "Navaids/default.ils" );
57 // ilslist->init( p_ils );
60 SGPath path( globals->get_fg_root() );
61 path.append( "Navaids/nav.dat" );
63 sg_gzifstream in( path.str() );
64 if ( !in.is_open() ) {
65 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
69 // skip first two lines
76 while ( in.get(c) && c != '\0' ) {
79 while ( ! in.eof() ) {
82 FGNavRecord *r = new FGNavRecord;
84 if ( r->get_type() > 95 ) {
88 /*cout << "id = " << r->get_ident() << endl;
89 cout << " type = " << r->get_type() << endl;
90 cout << " lon = " << r->get_lon() << endl;
91 cout << " lat = " << r->get_lat() << endl;
92 cout << " elev = " <<r->get_elev_ft() << endl;
93 cout << " freq = " << r->get_freq() << endl;
94 cout << " range = " << r->get_range() << endl;
95 cout << " name = " << r->get_name() << endl << endl; */
97 // fudge elevation to the field elevation if it's not specified
98 if ( fabs(r->get_elev_ft()) < 0.01 && r->get_apt_id().length() ) {
99 // cout << r->get_type() << " " << r->get_apt_id() << " zero elev"
101 const FGAirport* a = airports->search( r->get_apt_id() );
103 r->set_elev_ft( a->getElevation() );
104 // cout << " setting to " << a.elevation << endl;
108 if ( r->get_type() == 2 || r->get_type() == 3 ) {
111 } else if ( r->get_type() == 4 || r->get_type() == 5 ) {
112 // ILS=4, LOC(only)=5
114 } else if ( r->get_type() == 6 ) {
117 } else if ( r->get_type() == 7 || r->get_type() == 8
118 || r->get_type() == 9 )
120 // Marker Beacon = 7,8,9
122 } else if ( r->get_type() == 12 ) {
124 string str1( r->get_name() );
125 unsigned int loc1= str1.find( "TACAN", 0 );
126 unsigned int loc2 = str1.find( "VORTAC", 0 );
128 if( loc1 != string::npos || loc2 != string::npos ){
129 //cout << " name = " << r->get_name() ;
130 //cout << " freq = " << r->get_freq() ;
141 // load the carrier navaids file
145 path = globals->get_fg_root() ;
146 path.append( "Navaids/carrier_nav.dat" );
149 SG_LOG( SG_GENERAL, SG_ALERT, "opening file: " << path.str() );
151 sg_gzifstream incarrier( path.str() );
153 if ( !incarrier.is_open() ) {
154 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
158 // skip first two lines
159 //incarrier >> skipeol;
160 //incarrier >> skipeol;
164 while ( incarrier.get(c) && c != '\0' ) {
165 incarrier.putback(c);
167 while ( ! incarrier.eof() ) {
170 FGNavRecord *r = new FGNavRecord;
172 carrierlist->add ( r );
173 /*cout << " carrier lon: "<< r->get_lon() ;
174 cout << " carrier lat: "<< r->get_lat() ;
175 cout << " freq: " << r->get_freq() ;
176 cout << " carrier name: "<< r->get_name() << endl;*/
178 //if ( r->get_type() > 95 ) {
182 // end loading the carrier navaids file
184 // load the channel/freqency file
185 string channel, freq;
187 path = globals->get_fg_root();
188 path.append( "Navaids/TACAN_freq.dat" );
191 cout << file << endl;
193 sg_gzifstream inchannel( path.str() );
195 if ( !inchannel.is_open() ) {
196 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
201 inchannel >> skipeol;
205 while ( inchannel.get(c) && c != '\0' ) {
208 while ( ! inchannel.eof() ) {
211 FGTACANRecord *r = new FGTACANRecord;
213 channellist->add ( r );
214 //cout << "channel = " << r->get_channel() ;
215 //cout << " freq = " << r->get_freq() << endl;
227 // Given a localizer record and it's corresponding runway record,
228 // adjust the localizer position so it is in perfect alignment with
230 static void update_loc_position( FGNavRecord *loc, FGRunway *rwy,
233 double hdg = rwy->_heading;
239 // calculate runway threshold point
240 double thresh_lat, thresh_lon, return_az;
241 geo_direct_wgs_84 ( 0.0, rwy->_lat, rwy->_lon, hdg,
242 rwy->_length/2.0 * SG_FEET_TO_METER,
243 &thresh_lat, &thresh_lon, &return_az );
244 // cout << "Threshold = " << thresh_lat << "," << thresh_lon << endl;
246 // calculate distance from threshold to localizer
247 double az1, az2, dist_m;
248 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
249 thresh_lat, thresh_lon,
250 &az1, &az2, &dist_m );
251 // cout << "Distance = " << dist_m << endl;
253 // back project that distance along the runway center line
254 double nloc_lat, nloc_lon;
255 geo_direct_wgs_84 ( 0.0, thresh_lat, thresh_lon, hdg + 180.0,
256 dist_m, &nloc_lat, &nloc_lon, &return_az );
257 // printf("New localizer = %.6f %.6f\n", nloc_lat, nloc_lon );
259 // sanity check, how far have we moved the localizer?
260 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
262 &az1, &az2, &dist_m );
263 // cout << "Distance moved = " << dist_m << endl;
265 // cout << "orig heading = " << loc->get_multiuse() << endl;
266 // cout << "new heading = " << rwy->_heading << endl;
268 double hdg_diff = loc->get_multiuse() - rwy->_heading;
270 // clamp to [-180.0 ... 180.0]
271 if ( hdg_diff < -180.0 ) {
273 } else if ( hdg_diff > 180.0 ) {
277 if ( fabs(hdg_diff) <= threshold ) {
278 loc->set_lat( nloc_lat );
279 loc->set_lon( nloc_lon );
280 loc->set_multiuse( rwy->_heading );
285 // This routines traverses the localizer list and attempts to match
286 // each entry with it's corresponding runway. When it is successful,
287 // it then "moves" the localizer and updates it's heading so it
288 // *perfectly* aligns with the runway, but is still the same distance
289 // from the runway threshold.
290 void fgNavDBAlignLOCwithRunway( FGRunwayList *runways, FGNavList *loclist,
292 nav_map_type navmap = loclist->get_navaids();
294 nav_map_const_iterator freq = navmap.begin();
295 while ( freq != navmap.end() ) {
296 nav_list_type locs = freq->second;
297 nav_list_const_iterator loc = locs.begin();
298 while ( loc != locs.end() ) {
299 string name = (*loc)->get_name();
300 string::size_type pos1 = name.find(" ");
301 string id = name.substr(0, pos1);
302 name = name.substr(pos1+1);
303 string::size_type pos2 = name.find(" ");
304 string rwy = name.substr(0, pos2);
307 if ( runways->search(id, rwy, &r) ) {
308 update_loc_position( (*loc), &r, threshold );