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/math/sg_geodesy.hxx>
33 #include <simgear/misc/strutils.hxx>
35 #include <Airports/runways.hxx>
36 #include <Airports/simple.hxx>
37 #include <Main/globals.hxx>
39 #include "navrecord.hxx"
45 // load and initialize the navigational databases
46 bool fgNavDBInit( FGAirportList *airports,
47 FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
48 FGNavList *dmelist, FGNavList *mkrlist,
49 FGNavList *tacanlist, FGNavList *carrierlist,
50 FGTACANList *channellist)
52 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
53 // SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
54 // SGPath p_nav( globals->get_fg_root() );
55 // p_nav.append( "Navaids/default.nav" );
56 // navlist->init( p_nav );
58 // SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
60 // SGPath p_ils( globals->get_fg_root() );
61 // p_ils.append( "Navaids/default.ils" );
62 // ilslist->init( p_ils );
65 SGPath path( globals->get_fg_root() );
66 path.append( "Navaids/nav.dat" );
68 sg_gzifstream in( path.str() );
69 if ( !in.is_open() ) {
70 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
74 // skip first two lines
78 while ( ! in.eof() ) {
79 FGNavRecord *r = new FGNavRecord;
81 if ( r->get_type() > 95 ) {
86 /*cout << "id = " << r->get_ident() << endl;
87 cout << " type = " << r->get_type() << endl;
88 cout << " lon = " << r->get_lon() << endl;
89 cout << " lat = " << r->get_lat() << endl;
90 cout << " elev = " <<r->get_elev_ft() << endl;
91 cout << " freq = " << r->get_freq() << endl;
92 cout << " range = " << r->get_range() << endl;
93 cout << " name = " << r->get_name() << endl << endl; */
95 // fudge elevation to the field elevation if it's not specified
96 if ( fabs(r->get_elev_ft()) < 0.01 && r->get_apt_id().length() ) {
97 // cout << r->get_type() << " " << r->get_apt_id() << " zero elev"
99 const FGAirport* a = airports->search( r->get_apt_id() );
101 r->set_elev_ft( a->getElevation() );
102 // cout << " setting to " << a.elevation << endl;
106 if ( r->get_type() == 2 || r->get_type() == 3 ) {
109 } else if ( r->get_type() == 4 || r->get_type() == 5 ) {
110 // ILS=4, LOC(only)=5
112 } else if ( r->get_type() == 6 ) {
115 } else if ( r->get_type() == 7 || r->get_type() == 8
116 || r->get_type() == 9 )
118 // Marker Beacon = 7,8,9
120 } else if ( r->get_type() == 12 || r->get_type() == 13) {
121 // DME with ILS=12; standalone DME=13
122 string str1( r->get_name() );
123 string::size_type loc1= str1.find( "TACAN", 0 );
124 string::size_type loc2 = str1.find( "VORTAC", 0 );
126 if( loc1 != string::npos || loc2 != string::npos ){
127 //cout << " name = " << r->get_name() ;
128 //cout << " freq = " << r->get_freq() ;
139 // load the carrier navaids file
143 path = globals->get_fg_root() ;
144 path.append( "Navaids/carrier_nav.dat" );
147 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
149 sg_gzifstream incarrier( path.str() );
151 if ( !incarrier.is_open() ) {
152 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
156 // skip first two lines
157 //incarrier >> skipeol;
158 //incarrier >> skipeol;
160 while ( ! incarrier.eof() ) {
161 FGNavRecord *r = new FGNavRecord;
163 carrierlist->add ( r );
164 /*cout << " carrier lon: "<< r->get_lon() ;
165 cout << " carrier lat: "<< r->get_lat() ;
166 cout << " freq: " << r->get_freq() ;
167 cout << " carrier name: "<< r->get_name() << endl;*/
169 //if ( r->get_type() > 95 ) {
173 // end loading the carrier navaids file
175 // load the channel/freqency file
176 string channel, freq;
178 path = globals->get_fg_root();
179 path.append( "Navaids/TACAN_freq.dat" );
181 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
183 sg_gzifstream inchannel( path.str() );
185 if ( !inchannel.is_open() ) {
186 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
191 inchannel >> skipeol;
192 while ( ! inchannel.eof() ) {
193 FGTACANRecord *r = new FGTACANRecord;
195 channellist->add ( r );
196 //cout << "channel = " << r->get_channel() ;
197 //cout << " freq = " << r->get_freq() << endl;
209 // Given a localizer record and it's corresponding runway record,
210 // adjust the localizer position so it is in perfect alignment with
212 static void update_loc_position( FGNavRecord *loc, FGRunway *rwy,
215 double hdg = rwy->_heading;
221 // calculate runway threshold point
222 double thresh_lat, thresh_lon, return_az;
223 geo_direct_wgs_84 ( 0.0, rwy->_lat, rwy->_lon, hdg,
224 rwy->_length/2.0 * SG_FEET_TO_METER,
225 &thresh_lat, &thresh_lon, &return_az );
226 // cout << "Threshold = " << thresh_lat << "," << thresh_lon << endl;
228 // calculate distance from threshold to localizer
229 double az1, az2, dist_m;
230 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
231 thresh_lat, thresh_lon,
232 &az1, &az2, &dist_m );
233 // cout << "Distance = " << dist_m << endl;
235 // back project that distance along the runway center line
236 double nloc_lat, nloc_lon;
237 geo_direct_wgs_84 ( 0.0, thresh_lat, thresh_lon, hdg + 180.0,
238 dist_m, &nloc_lat, &nloc_lon, &return_az );
239 // printf("New localizer = %.6f %.6f\n", nloc_lat, nloc_lon );
241 // sanity check, how far have we moved the localizer?
242 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
244 &az1, &az2, &dist_m );
245 // cout << "Distance moved = " << dist_m << endl;
247 // cout << "orig heading = " << loc->get_multiuse() << endl;
248 // cout << "new heading = " << rwy->_heading << endl;
250 double hdg_diff = loc->get_multiuse() - rwy->_heading;
252 // clamp to [-180.0 ... 180.0]
253 if ( hdg_diff < -180.0 ) {
255 } else if ( hdg_diff > 180.0 ) {
259 if ( fabs(hdg_diff) <= threshold ) {
260 loc->set_lat( nloc_lat );
261 loc->set_lon( nloc_lon );
262 loc->set_multiuse( rwy->_heading );
267 // This routines traverses the localizer list and attempts to match
268 // each entry with it's corresponding runway. When it is successful,
269 // it then "moves" the localizer and updates it's heading so it
270 // *perfectly* aligns with the runway, but is still the same distance
271 // from the runway threshold.
272 void fgNavDBAlignLOCwithRunway( FGAirportList *airports, FGNavList *loclist,
274 nav_map_type navmap = loclist->get_navaids();
276 nav_map_const_iterator freq = navmap.begin();
277 while ( freq != navmap.end() ) {
278 nav_list_type locs = freq->second;
279 nav_list_const_iterator loc = locs.begin();
280 while ( loc != locs.end() ) {
281 vector<string> parts = simgear::strutils::split((*loc)->get_name());
282 if (parts.size() < 2) {
283 SG_LOG(SG_GENERAL, SG_ALERT, "can't parse navaid " << (*loc)->get_ident()
284 << " name for airport/runway:" << (*loc)->get_name());
288 FGAirport* airport = airports->search(parts[0]);
289 if (!airport) continue; // not found
291 FGRunway r = airport->getRunwayByIdent(parts[1]);
292 update_loc_position( (*loc), &r, threshold );