]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navdb.cxx
54ab04b17d19594b4684b2f2f63204a4f916edc6
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <string>
30
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/sgstream.hxx>
33
34 #include <simgear/math/sg_geodesy.hxx>
35 #include <simgear/misc/strutils.hxx>
36
37 #include <Airports/runways.hxx>
38 #include <Airports/simple.hxx>
39 #include <Main/globals.hxx>
40
41 #include "navrecord.hxx"
42 #include "navdb.hxx"
43
44 using std::string;
45
46
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)
53 {
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 );
59
60     // SG_LOG(SG_GENERAL, SG_INFO, "  ILS and Marker Beacons");
61     // beacons->init();
62     // SGPath p_ils( globals->get_fg_root() );
63     // p_ils.append( "Navaids/default.ils" );
64     // ilslist->init( p_ils );
65
66
67     SGPath path( globals->get_fg_root() );
68     path.append( "Navaids/nav.dat" );
69
70     sg_gzifstream in( path.str() );
71     if ( !in.is_open() ) {
72         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
73         exit(-1);
74     }
75
76     // skip first two lines
77     in >> skipeol;
78     in >> skipeol;
79
80     while ( ! in.eof() ) {
81         FGNavRecord *r = new FGNavRecord;
82         in >> (*r);
83         if ( r->get_type() > 95 ) {
84             delete r;
85             break;
86         }
87
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; */
96
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"
100             //      << endl;
101             const FGAirport* a = airports->search( r->get_apt_id() );
102             if ( a ) {
103                 r->set_elev_ft( a->getElevation() );
104                 // cout << "  setting to " << a.elevation << endl;
105             }
106         }
107
108         if ( r->get_type() == 2 || r->get_type() == 3 ) {
109             // NDB=2, VOR=3
110             navlist->add( r );
111         } else if ( r->get_type() == 4 || r->get_type() == 5 ) {
112             // ILS=4, LOC(only)=5
113             loclist->add( r );
114         } else if ( r->get_type() == 6 ) {
115             // GS=6
116             gslist->add( r );
117         } else if ( r->get_type() == 7 || r->get_type() == 8
118                     || r->get_type() == 9 )
119         {
120             // Marker Beacon = 7,8,9
121             mkrlist->add( r );
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 );
127                        
128             if( loc1 != string::npos || loc2 != string::npos ){
129                  //cout << " name = " << r->get_name() ;
130                  //cout << " freq = " << r->get_freq() ;
131                  tacanlist->add( r );
132                  }
133                 
134             dmelist->add( r );
135             
136         }
137                 
138         in >> skipcomment;
139     }
140
141 // load the carrier navaids file
142     
143     string file, name;
144     path = "";
145     path = globals->get_fg_root() ;
146     path.append( "Navaids/carrier_nav.dat" );
147     
148     file = path.str();
149     SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
150     
151     sg_gzifstream incarrier( path.str() );
152     
153     if ( !incarrier.is_open() ) {
154         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
155         exit(-1);
156     }
157     
158     // skip first two lines
159     //incarrier >> skipeol;
160     //incarrier >> skipeol;
161     
162     while ( ! incarrier.eof() ) {
163         FGNavRecord *r = new FGNavRecord;
164         incarrier >> (*r);
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;*/
170                 
171         //if ( r->get_type() > 95 ) {
172         //   break;}
173     } // end while
174
175 // end loading the carrier navaids file
176
177 // load the channel/freqency file
178     string channel, freq;
179     path="";
180     path = globals->get_fg_root();
181     path.append( "Navaids/TACAN_freq.dat" );
182     
183     SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
184         
185     sg_gzifstream inchannel( path.str() );
186     
187     if ( !inchannel.is_open() ) {
188         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
189         exit(-1);
190     }
191     
192     // skip first line
193     inchannel >> skipeol;
194     while ( ! inchannel.eof() ) {
195         FGTACANRecord *r = new FGTACANRecord;
196         inchannel >> (*r);
197         channellist->add ( r );
198         //cout << "channel = " << r->get_channel() ;
199         //cout << " freq = " << r->get_freq() << endl;
200         
201     } // end while
202
203  
204  // end ReadChanFile
205
206
207     return true;
208 }
209
210
211 // Given a localizer record and it's corresponding runway record,
212 // adjust the localizer position so it is in perfect alignment with
213 // the runway.
214 static void update_loc_position( FGNavRecord *loc, FGRunway *rwy,
215                                  double threshold )
216 {
217     double hdg = rwy->_heading;
218     hdg += 180.0;
219     if ( hdg > 360.0 ) {
220         hdg -= 360.0;
221     }
222
223     // calculate runway threshold point
224     double thresh_lat, thresh_lon, return_az;
225     geo_direct_wgs_84 ( 0.0, rwy->_lat, rwy->_lon, hdg,
226                         rwy->_length/2.0 * SG_FEET_TO_METER,
227                         &thresh_lat, &thresh_lon, &return_az );
228     // cout << "Threshold = " << thresh_lat << "," << thresh_lon << endl;
229
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;
236
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 );
242
243     // sanity check, how far have we moved the localizer?
244     geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
245                         nloc_lat, nloc_lon,
246                         &az1, &az2, &dist_m );
247     // cout << "Distance moved = " << dist_m << endl;
248
249     // cout << "orig heading = " << loc->get_multiuse() << endl;
250     // cout << "new heading = " << rwy->_heading << endl;
251
252     double hdg_diff = loc->get_multiuse() - rwy->_heading;
253
254     // clamp to [-180.0 ... 180.0]
255     if ( hdg_diff < -180.0 ) {
256         hdg_diff += 360.0;
257     } else if ( hdg_diff > 180.0 ) {
258         hdg_diff -= 360.0;
259     }
260
261     if ( fabs(hdg_diff) <= threshold ) {
262         loc->set_lat( nloc_lat );
263         loc->set_lon( nloc_lon );
264         loc->set_multiuse( rwy->_heading );
265     }
266 }
267
268
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,
275                                 double threshold ) {
276     nav_map_type navmap = loclist->get_navaids();
277
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());
287             continue;
288           }
289           
290           FGAirport* airport = airports->search(parts[0]);
291           if (!airport) continue; // not found
292             
293           FGRunway r = airport->getRunwayByIdent(parts[1]);
294           update_loc_position( (*loc), &r, threshold );
295           ++loc;
296         }
297         ++freq;
298     }
299 }
300