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>
32 #include <simgear/misc/sgstream.hxx>
34 #include <simgear/math/sg_geodesy.hxx>
35 #include <simgear/misc/strutils.hxx>
37 #include <Airports/runways.hxx>
38 #include <Airports/simple.hxx>
39 #include <Main/globals.hxx>
41 #include "navrecord.hxx"
47 // load and initialize the navigational databases
48 bool fgNavDBInit( FGAirportList *airports,
49 FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
50 FGNavList *dmelist, FGNavList *mkrlist,
51 FGNavList *tacanlist, FGNavList *carrierlist,
52 FGTACANList *channellist)
54 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
55 // SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
56 // SGPath p_nav( globals->get_fg_root() );
57 // p_nav.append( "Navaids/default.nav" );
58 // navlist->init( p_nav );
60 // SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
62 // SGPath p_ils( globals->get_fg_root() );
63 // p_ils.append( "Navaids/default.ils" );
64 // ilslist->init( p_ils );
67 SGPath path( globals->get_fg_root() );
68 path.append( "Navaids/nav.dat" );
70 sg_gzifstream in( path.str() );
71 if ( !in.is_open() ) {
72 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
76 // skip first two lines
80 while ( ! in.eof() ) {
81 FGNavRecord *r = new FGNavRecord;
83 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 || r->get_type() == 13) {
123 // DME with ILS=12; standalone DME=13
124 string str1( r->get_name() );
125 string::size_type loc1= str1.find( "TACAN", 0 );
126 string::size_type 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_INFO, "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;
162 while ( ! incarrier.eof() ) {
163 FGNavRecord *r = new FGNavRecord;
165 carrierlist->add ( r );
166 /*cout << " carrier lon: "<< r->get_lon() ;
167 cout << " carrier lat: "<< r->get_lat() ;
168 cout << " freq: " << r->get_freq() ;
169 cout << " carrier name: "<< r->get_name() << endl;*/
171 //if ( r->get_type() > 95 ) {
175 // end loading the carrier navaids file
177 // load the channel/freqency file
178 string channel, freq;
180 path = globals->get_fg_root();
181 path.append( "Navaids/TACAN_freq.dat" );
183 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
185 sg_gzifstream inchannel( path.str() );
187 if ( !inchannel.is_open() ) {
188 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
193 inchannel >> skipeol;
194 while ( ! inchannel.eof() ) {
195 FGTACANRecord *r = new FGTACANRecord;
197 channellist->add ( r );
198 //cout << "channel = " << r->get_channel() ;
199 //cout << " freq = " << r->get_freq() << endl;
211 // Given a localizer record and it's corresponding runway record,
212 // adjust the localizer position so it is in perfect alignment with
214 static void update_loc_position( FGNavRecord *loc, FGRunway *rwy,
217 double hdg = rwy->headingDeg();
223 // calculate runway threshold point
224 double thresh_lat, thresh_lon, return_az;
225 geo_direct_wgs_84 ( 0.0, rwy->latitude(), rwy->longitude(), hdg,
226 rwy->lengthM() * 0.5,
227 &thresh_lat, &thresh_lon, &return_az );
228 // cout << "Threshold = " << thresh_lat << "," << thresh_lon << endl;
230 // calculate distance from threshold to localizer
231 double az1, az2, dist_m;
232 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
233 thresh_lat, thresh_lon,
234 &az1, &az2, &dist_m );
235 // cout << "Distance = " << dist_m << endl;
237 // back project that distance along the runway center line
238 double nloc_lat, nloc_lon;
239 geo_direct_wgs_84 ( 0.0, thresh_lat, thresh_lon, hdg + 180.0,
240 dist_m, &nloc_lat, &nloc_lon, &return_az );
241 // printf("New localizer = %.6f %.6f\n", nloc_lat, nloc_lon );
243 // sanity check, how far have we moved the localizer?
244 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
246 &az1, &az2, &dist_m );
247 // cout << "Distance moved = " << dist_m << endl;
249 // cout << "orig heading = " << loc->get_multiuse() << endl;
250 // cout << "new heading = " << rwy->_heading << endl;
252 double hdg_diff = loc->get_multiuse() - rwy->headingDeg();
254 // clamp to [-180.0 ... 180.0]
255 if ( hdg_diff < -180.0 ) {
257 } else if ( hdg_diff > 180.0 ) {
261 if ( fabs(hdg_diff) <= threshold ) {
262 loc->set_lat( nloc_lat );
263 loc->set_lon( nloc_lon );
264 loc->set_multiuse( rwy->headingDeg() );
269 // This routines traverses the localizer list and attempts to match
270 // each entry with it's corresponding runway. When it is successful,
271 // it then "moves" the localizer and updates it's heading so it
272 // *perfectly* aligns with the runway, but is still the same distance
273 // from the runway threshold.
274 void fgNavDBAlignLOCwithRunway( FGAirportList *airports, FGNavList *loclist,
276 nav_map_type navmap = loclist->get_navaids();
278 nav_map_const_iterator freq = navmap.begin();
279 while ( freq != navmap.end() ) {
280 nav_list_type locs = freq->second;
281 nav_list_const_iterator loc = locs.begin();
282 while ( loc != locs.end() ) {
283 vector<string> parts = simgear::strutils::split((*loc)->get_name());
284 if (parts.size() < 2) {
285 SG_LOG(SG_GENERAL, SG_ALERT, "can't parse navaid " << (*loc)->get_ident()
286 << " name for airport/runway:" << (*loc)->get_name());
290 FGAirport* airport = airports->search(parts[0]);
291 if (!airport) continue; // not found
293 FGRunway* r = airport->getRunwayByIdent(parts[1]);
294 update_loc_position( (*loc), r, threshold );