]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navdb.cxx
22cac4f03801f5da206396614cd261cbc6d5d400
[flightgear.git] / src / Navaids / navdb.cxx
1 // navdb.cxx -- top level navaids management routines
2 //
3 // Written by Curtis Olson, started May 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <simgear/compiler.h>
25
26 #include STL_STRING
27
28 #include <simgear/debug/logstream.hxx>
29
30 #include <Airports/runways.hxx>
31 #include <Airports/simple.hxx>
32 #include <Main/globals.hxx>
33
34 #include "navrecord.hxx"
35 #include "navdb.hxx"
36
37 SG_USING_STD( string );
38
39
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 {
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 );
50
51     // SG_LOG(SG_GENERAL, SG_INFO, "  ILS and Marker Beacons");
52     // beacons->init();
53     // SGPath p_ils( globals->get_fg_root() );
54     // p_ils.append( "Navaids/default.ils" );
55     // ilslist->init( p_ils );
56
57
58     SGPath path( globals->get_fg_root() );
59     path.append( "Navaids/nav.dat" );
60
61     sg_gzifstream in( path.str() );
62     if ( !in.is_open() ) {
63         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
64         exit(-1);
65     }
66
67     // skip first two lines
68     in >> skipeol;
69     in >> skipeol;
70
71
72 #ifdef __MWERKS__
73     char c = 0;
74     while ( in.get(c) && c != '\0'  ) {
75         in.putback(c);
76 #else
77     while ( ! in.eof() ) {
78 #endif
79
80         FGNavRecord *r = new FGNavRecord;
81         in >> (*r);
82         if ( r->get_type() > 95 ) {
83             break;
84         }
85
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; */
93
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"
97             //      << endl;
98             FGAirport a = airports->search( r->get_apt_id() );
99             if ( a.getId() == r->get_apt_id() ) {
100                 r->set_elev_ft( a.getElevation() );
101                 // cout << "  setting to " << a.elevation << endl;
102             }
103         }
104
105         if ( r->get_type() == 2 || r->get_type() == 3 ) {
106             // NDB=2, VOR=3
107             navlist->add( r );
108         } else if ( r->get_type() == 4 || r->get_type() == 5 ) {
109             // ILS=4, LOC(only)=5
110             loclist->add( r );
111         } else if ( r->get_type() == 6 ) {
112             // GS=6
113             gslist->add( r );
114         } else if ( r->get_type() == 7 || r->get_type() == 8
115                     || r->get_type() == 9 )
116         {
117             // Marker Beacon = 7,8,9
118             mkrlist->add( r );
119         } else if ( r->get_type() == 12 ) {
120             // DME=12
121             dmelist->add( r );
122         }
123                 
124         in >> skipcomment;
125     }
126
127     // cout << "min freq = " << min << endl;
128     // cout << "max freq = " << max << endl;
129
130     return true;
131 }
132
133
134 // Given a localizer record and it's corresponding runway record,
135 // adjust the localizer position so it is in perfect alignment with
136 // the runway.
137 static void update_loc_position( FGNavRecord *loc, FGRunway *rwy,
138                                  double threshold )
139 {
140     double hdg = rwy->_heading;
141     hdg += 180.0;
142     if ( hdg > 360.0 ) {
143         hdg -= 360.0;
144     }
145
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;
152
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;
159
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 );
165
166     // sanity check, how far have we moved the localizer?
167     geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
168                         nloc_lat, nloc_lon,
169                         &az1, &az2, &dist_m );
170     // cout << "Distance moved = " << dist_m << endl;
171
172     // cout << "orig heading = " << loc->get_multiuse() << endl;
173     // cout << "new heading = " << rwy->_heading << endl;
174
175     double hdg_diff = loc->get_multiuse() - rwy->_heading;
176
177     // clamp to [-180.0 ... 180.0]
178     if ( hdg_diff < -180.0 ) {
179         hdg_diff += 360.0;
180     } else if ( hdg_diff > 180.0 ) {
181         hdg_diff -= 360.0;
182     }
183
184     if ( fabs(hdg_diff) <= threshold ) {
185         loc->set_lat( nloc_lat );
186         loc->set_lon( nloc_lon );
187         loc->set_multiuse( rwy->_heading );
188     }
189 }
190
191
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,
198                                 double threshold ) {
199     nav_map_type navmap = loclist->get_navaids();
200
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);
212
213             FGRunway r;
214             if ( runways->search(id, rwy, &r) ) {
215                 update_loc_position( (*loc), &r, threshold );
216             }
217             ++loc;
218         }
219         ++freq;
220     }
221 }