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