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