]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navdb.cxx
Modified Files:
[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 STL_STRING
30
31 #include <simgear/debug/logstream.hxx>
32
33 #include <Airports/runways.hxx>
34 #include <Airports/simple.hxx>
35 #include <Main/globals.hxx>
36
37 #include "navrecord.hxx"
38 #include "navdb.hxx"
39
40 SG_USING_STD( string );
41
42
43 // load and initialize the navigational databases
44 bool fgNavDBInit( FGAirportList *airports,
45                   FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
46                   FGNavList *dmelist, FGNavList *mkrlist, 
47                   FGNavList *tacanlist, FGNavList *carrierlist,
48                   FGTACANList *channellist)
49 {
50     SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
51     // SG_LOG(SG_GENERAL, SG_INFO, "  VOR/NDB");
52     // SGPath p_nav( globals->get_fg_root() );
53     // p_nav.append( "Navaids/default.nav" );
54     // navlist->init( p_nav );
55
56     // SG_LOG(SG_GENERAL, SG_INFO, "  ILS and Marker Beacons");
57     // beacons->init();
58     // SGPath p_ils( globals->get_fg_root() );
59     // p_ils.append( "Navaids/default.ils" );
60     // ilslist->init( p_ils );
61
62
63     SGPath path( globals->get_fg_root() );
64     path.append( "Navaids/nav.dat" );
65
66     sg_gzifstream in( path.str() );
67     if ( !in.is_open() ) {
68         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
69         exit(-1);
70     }
71
72     // skip first two lines
73     in >> skipeol;
74     in >> skipeol;
75
76
77 #ifdef __MWERKS__
78     char c = 0;
79     while ( in.get(c) && c != '\0'  ) {
80         in.putback(c);
81 #else
82     while ( ! in.eof() ) {
83 #endif
84
85         FGNavRecord *r = new FGNavRecord;
86         in >> (*r);
87         if ( r->get_type() > 95 ) {
88             delete r;
89             break;
90         }
91
92         /*cout << "id = " << r->get_ident() << endl;
93         cout << " type = " << r->get_type() << endl;
94         cout << " lon = " << r->get_lon() << endl;
95         cout << " lat = " << r->get_lat() << endl;
96         cout << " elev = " <<r->get_elev_ft() << endl;
97         cout << " freq = " << r->get_freq() << endl;
98         cout << " range = " << r->get_range() << endl;
99         cout << " name = " << r->get_name() << endl << endl; */
100
101         // fudge elevation to the field elevation if it's not specified
102         if ( fabs(r->get_elev_ft()) < 0.01 && r->get_apt_id().length() ) {
103             // cout << r->get_type() << " " << r->get_apt_id() << " zero elev"
104             //      << endl;
105             const FGAirport* a = airports->search( r->get_apt_id() );
106             if ( a ) {
107                 r->set_elev_ft( a->getElevation() );
108                 // cout << "  setting to " << a.elevation << endl;
109             }
110         }
111
112         if ( r->get_type() == 2 || r->get_type() == 3 ) {
113             // NDB=2, VOR=3
114             navlist->add( r );
115         } else if ( r->get_type() == 4 || r->get_type() == 5 ) {
116             // ILS=4, LOC(only)=5
117             loclist->add( r );
118         } else if ( r->get_type() == 6 ) {
119             // GS=6
120             gslist->add( r );
121         } else if ( r->get_type() == 7 || r->get_type() == 8
122                     || r->get_type() == 9 )
123         {
124             // Marker Beacon = 7,8,9
125             mkrlist->add( r );
126         } else if ( r->get_type() == 12 || r->get_type() == 13) {
127             // DME with ILS=12; standalone DME=13
128             string str1( r->get_name() );
129             string::size_type loc1= str1.find( "TACAN", 0 );
130             string::size_type loc2 = str1.find( "VORTAC", 0 );
131                        
132             if( loc1 != string::npos || loc2 != string::npos ){
133                  //cout << " name = " << r->get_name() ;
134                  //cout << " freq = " << r->get_freq() ;
135                  tacanlist->add( r );
136                  }
137                 
138             dmelist->add( r );
139             
140         }
141                 
142         in >> skipcomment;
143     }
144
145 // load the carrier navaids file
146     
147     string file, name;
148     path = "";
149     path = globals->get_fg_root() ;
150     path.append( "Navaids/carrier_nav.dat" );
151     
152     file = path.str();
153     SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
154     
155     sg_gzifstream incarrier( path.str() );
156     
157     if ( !incarrier.is_open() ) {
158         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
159         exit(-1);
160     }
161     
162     // skip first two lines
163     //incarrier >> skipeol;
164     //incarrier >> skipeol;
165     
166 #ifdef __MWERKS__
167     char c = 0;
168     while ( incarrier.get(c) && c != '\0'  ) {
169         incarrier.putback(c);
170 #else
171     while ( ! incarrier.eof() ) {
172 #endif
173         
174         FGNavRecord *r = new FGNavRecord;
175         incarrier >> (*r);
176         carrierlist->add ( r );
177         /*cout << " carrier lon: "<<  r->get_lon() ;
178         cout << " carrier lat: "<<  r->get_lat() ;
179         cout << " freq: " << r->get_freq() ;
180         cout << " carrier name: "<<  r->get_name() << endl;*/
181                 
182         //if ( r->get_type() > 95 ) {
183         //   break;}
184     } // end while
185
186 // end loading the carrier navaids file
187
188 // load the channel/freqency file
189     string channel, freq;
190     path="";
191     path = globals->get_fg_root();
192     path.append( "Navaids/TACAN_freq.dat" );
193     
194     SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
195         
196     sg_gzifstream inchannel( path.str() );
197     
198     if ( !inchannel.is_open() ) {
199         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
200         exit(-1);
201     }
202     
203     // skip first line
204     inchannel >> skipeol;
205     
206 #ifdef __MWERKS__
207     char c = 0;
208     while ( inchannel.get(c) && c != '\0'  ) {
209         in.putback(c);
210 #else
211     while ( ! inchannel.eof() ) {
212 #endif
213
214         FGTACANRecord *r = new FGTACANRecord;
215         inchannel >> (*r);
216         channellist->add ( r );
217         //cout << "channel = " << r->get_channel() ;
218         //cout << " freq = " << r->get_freq() << endl;
219         
220     } // end while
221
222  
223  // end ReadChanFile
224
225
226     return true;
227 }
228
229
230 // Given a localizer record and it's corresponding runway record,
231 // adjust the localizer position so it is in perfect alignment with
232 // the runway.
233 static void update_loc_position( FGNavRecord *loc, FGRunway *rwy,
234                                  double threshold )
235 {
236     double hdg = rwy->_heading;
237     hdg += 180.0;
238     if ( hdg > 360.0 ) {
239         hdg -= 360.0;
240     }
241
242     // calculate runway threshold point
243     double thresh_lat, thresh_lon, return_az;
244     geo_direct_wgs_84 ( 0.0, rwy->_lat, rwy->_lon, hdg,
245                         rwy->_length/2.0 * SG_FEET_TO_METER,
246                         &thresh_lat, &thresh_lon, &return_az );
247     // cout << "Threshold = " << thresh_lat << "," << thresh_lon << endl;
248
249     // calculate distance from threshold to localizer
250     double az1, az2, dist_m;
251     geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
252                         thresh_lat, thresh_lon,
253                         &az1, &az2, &dist_m );
254     // cout << "Distance = " << dist_m << endl;
255
256     // back project that distance along the runway center line
257     double nloc_lat, nloc_lon;
258     geo_direct_wgs_84 ( 0.0, thresh_lat, thresh_lon, hdg + 180.0, 
259                         dist_m, &nloc_lat, &nloc_lon, &return_az );
260     // printf("New localizer = %.6f %.6f\n", nloc_lat, nloc_lon );
261
262     // sanity check, how far have we moved the localizer?
263     geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
264                         nloc_lat, nloc_lon,
265                         &az1, &az2, &dist_m );
266     // cout << "Distance moved = " << dist_m << endl;
267
268     // cout << "orig heading = " << loc->get_multiuse() << endl;
269     // cout << "new heading = " << rwy->_heading << endl;
270
271     double hdg_diff = loc->get_multiuse() - rwy->_heading;
272
273     // clamp to [-180.0 ... 180.0]
274     if ( hdg_diff < -180.0 ) {
275         hdg_diff += 360.0;
276     } else if ( hdg_diff > 180.0 ) {
277         hdg_diff -= 360.0;
278     }
279
280     if ( fabs(hdg_diff) <= threshold ) {
281         loc->set_lat( nloc_lat );
282         loc->set_lon( nloc_lon );
283         loc->set_multiuse( rwy->_heading );
284     }
285 }
286
287
288 // This routines traverses the localizer list and attempts to match
289 // each entry with it's corresponding runway.  When it is successful,
290 // it then "moves" the localizer and updates it's heading so it
291 // *perfectly* aligns with the runway, but is still the same distance
292 // from the runway threshold.
293 void fgNavDBAlignLOCwithRunway( FGRunwayList *runways, FGNavList *loclist,
294                                 double threshold ) {
295     nav_map_type navmap = loclist->get_navaids();
296
297     nav_map_const_iterator freq = navmap.begin();
298     while ( freq != navmap.end() ) {
299         nav_list_type locs = freq->second;
300         nav_list_const_iterator loc = locs.begin();
301         while ( loc != locs.end() ) {
302             string name = (*loc)->get_name();
303             string::size_type pos1 = name.find(" ");
304             string id = name.substr(0, pos1);
305             name = name.substr(pos1+1);
306             string::size_type pos2 = name.find(" ");
307             string rwy = name.substr(0, pos2);
308
309             FGRunway r;
310             if ( runways->search(id, rwy, &r) ) {
311                 update_loc_position( (*loc), &r, threshold );
312             }
313             ++loc;
314         }
315         ++freq;
316     }
317 }
318