1 // navdb.cxx -- top level navaids management routines
3 // Written by Curtis Olson, started May 2004.
5 // Copyright (C) 2004 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.
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 )
45 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
46 // SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
47 // SGPath p_nav( globals->get_fg_root() );
48 // p_nav.append( "Navaids/default.nav" );
49 // navlist->init( p_nav );
51 // SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
53 // SGPath p_ils( globals->get_fg_root() );
54 // p_ils.append( "Navaids/default.ils" );
55 // ilslist->init( p_ils );
58 SGPath path( globals->get_fg_root() );
59 path.append( "Navaids/nav.dat" );
61 sg_gzifstream in( path.str() );
62 if ( !in.is_open() ) {
63 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
67 // skip first two lines
74 while ( in.get(c) && c != '\0' ) {
77 while ( ! in.eof() ) {
80 FGNavRecord *r = new FGNavRecord;
82 if ( r->get_type() > 95 ) {
86 /* cout << "id = " << n.get_ident() << endl;
87 cout << " type = " << n.get_type() << endl;
88 cout << " lon = " << n.get_lon() << endl;
89 cout << " lat = " << n.get_lat() << endl;
90 cout << " elev = " << n.get_elev() << endl;
91 cout << " freq = " << n.get_freq() << endl;
92 cout << " range = " << n.get_range() << endl << endl; */
94 // fudge elevation to the field elevation if it's not specified
95 if ( fabs(r->get_elev_ft()) < 0.01 && r->get_apt_id().length() ) {
96 // cout << r->get_type() << " " << r->get_apt_id() << " zero elev"
98 FGAirport a = airports->search( r->get_apt_id() );
99 if ( a.id == r->get_apt_id() ) {
100 r->set_elev_ft( a.elevation );
101 // cout << " setting to " << a.elevation << endl;
105 if ( r->get_type() == 2 || r->get_type() == 3 ) {
108 } else if ( r->get_type() == 4 || r->get_type() == 5 ) {
109 // ILS=4, LOC(only)=5
111 } else if ( r->get_type() == 6 ) {
114 } else if ( r->get_type() == 7 || r->get_type() == 8
115 || r->get_type() == 9 )
117 // Marker Beacon = 7,8,9
119 } else if ( r->get_type() == 12 ) {
127 // cout << "min freq = " << min << endl;
128 // cout << "max freq = " << max << endl;
134 // Given a localizer record and it's corresponding runway record,
135 // adjust the localizer position so it is in perfect alignment with
137 static void update_loc_position( FGNavRecord *loc, FGRunway *rwy,
140 double hdg = rwy->heading;
146 // calculate runway threshold point
147 double thresh_lat, thresh_lon, return_az;
148 geo_direct_wgs_84 ( 0.0, rwy->lat, rwy->lon, hdg,
149 rwy->length/2.0 * SG_FEET_TO_METER,
150 &thresh_lat, &thresh_lon, &return_az );
151 // cout << "Threshold = " << thresh_lat << "," << thresh_lon << endl;
153 // calculate distance from threshold to localizer
154 double az1, az2, dist_m;
155 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
156 thresh_lat, thresh_lon,
157 &az1, &az2, &dist_m );
158 // cout << "Distance = " << dist_m << endl;
160 // back project that distance along the runway center line
161 double nloc_lat, nloc_lon;
162 geo_direct_wgs_84 ( 0.0, thresh_lat, thresh_lon, hdg + 180.0,
163 dist_m, &nloc_lat, &nloc_lon, &return_az );
164 // printf("New localizer = %.6f %.6f\n", nloc_lat, nloc_lon );
166 // sanity check, how far have we moved the localizer?
167 geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
169 &az1, &az2, &dist_m );
170 // cout << "Distance moved = " << dist_m << endl;
172 // cout << "orig heading = " << loc->get_multiuse() << endl;
173 // cout << "new heading = " << rwy->heading << endl;
175 double hdg_diff = loc->get_multiuse() - rwy->heading;
177 // clamp to [-180.0 ... 180.0]
178 if ( hdg_diff < -180.0 ) {
180 } else if ( hdg_diff > 180.0 ) {
184 if ( fabs(hdg_diff) <= threshold ) {
185 loc->set_lat( nloc_lat );
186 loc->set_lon( nloc_lon );
187 loc->set_multiuse( rwy->heading );
192 // This routines traverses the localizer list and attempts to match
193 // each entry with it's corresponding runway. When it is successful,
194 // it then "moves" the localizer and updates it's heading so it
195 // *perfectly* aligns with the runway, but is still the same distance
196 // from the runway threshold.
197 void fgNavDBAlignLOCwithRunway( FGRunwayList *runways, FGNavList *loclist,
199 nav_map_type navmap = loclist->get_navaids();
201 nav_map_iterator freq = navmap.begin();
202 while ( freq != navmap.end() ) {
203 nav_list_type locs = freq->second;
204 nav_list_iterator loc = locs.begin();
205 while ( loc != locs.end() ) {
206 string name = (*loc)->get_name();
207 string::size_type pos1 = name.find(" ");
208 string id = name.substr(0, pos1);
209 name = name.substr(pos1+1);
210 string::size_type pos2 = name.find(" ");
211 string rwy = name.substr(0, pos2);
214 if ( runways->search(id, rwy, &r) ) {
215 update_loc_position( (*loc), &r, threshold );