]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navdb.cxx
Whoops, type codes for localizers and ILSs were swapped in the nav.dat reader
[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/math/sg_geodesy.hxx>
33 #include <simgear/misc/strutils.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/misc/sgstream.hxx>
37
38 #include "navrecord.hxx"
39 #include "navlist.hxx"
40 #include "navdb.hxx"
41 #include <Main/globals.hxx>
42 #include <Navaids/markerbeacon.hxx>
43 #include <Airports/simple.hxx>
44
45 using std::string;
46
47 static FGPositioned::Type
48 mapRobinTypeToFGPType(int aTy)
49 {
50   switch (aTy) {
51  // case 1:
52   case 2: return FGPositioned::NDB;
53   case 3: return FGPositioned::VOR;
54   case 4: return FGPositioned::ILS;
55   case 5: return FGPositioned::LOC;
56   case 6: return FGPositioned::GS;
57   case 12:
58   case 13: return FGPositioned::DME;
59   case 99: return FGPositioned::INVALID; // end-of-file code
60   default:
61     throw sg_range_exception("Got a nav.dat type we don't recognize", "FGNavRecord::createFromStream");
62   }
63 }
64
65 static FGNavRecord* createNavFromStream(std::istream& aStream)
66 {
67   int rawType;
68   aStream >> rawType;
69   if (aStream.eof() || (rawType == 99)) {
70     return NULL; // happens with, eg, carrier_nav.dat
71   }
72   
73   double lat, lon, elev_ft, multiuse;
74   int freq, range;
75   std::string name, ident;
76   aStream >> lat >> lon >> elev_ft >> freq >> range >> multiuse >> ident;
77   getline(aStream, name);
78   
79   SGGeod pos(SGGeod::fromDegFt(lon, lat, elev_ft));
80   name = simgear::strutils::strip(name);
81   
82   if ((rawType >= 7) && (rawType <= 9)) {
83     // marker beacons use a different run-time class now
84      FGMarkerBeaconRecord::create(rawType, name, pos);
85      return NULL; // not a nav-record, but that's okay
86   }
87   
88   FGPositioned::Type type = mapRobinTypeToFGPType(rawType);
89   if (type == FGPositioned::INVALID) {
90     return NULL;
91   }
92   
93   // silently multiply adf frequencies by 100 so that adf
94   // vs. nav/loc frequency lookups can use the same code.
95   if (type == FGPositioned::NDB) {
96     freq *= 100;
97   }
98   
99   return new FGNavRecord(type, ident, name, pos,
100     freq, range, multiuse);
101 }
102
103 // load and initialize the navigational databases
104 bool fgNavDBInit( FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
105                   FGNavList *dmelist, 
106                   FGNavList *tacanlist, FGNavList *carrierlist,
107                   FGTACANList *channellist)
108 {
109     SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
110
111     SGPath path( globals->get_fg_root() );
112     path.append( "Navaids/nav.dat" );
113
114     sg_gzifstream in( path.str() );
115     if ( !in.is_open() ) {
116         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
117         exit(-1);
118     }
119
120     // skip first two lines
121     in >> skipeol;
122     in >> skipeol;
123
124     while (!in.eof()) {
125       FGNavRecord *r = createNavFromStream(in);
126       if (!r) {
127         continue;
128       }
129       
130       switch (r->type()) {
131       case FGPositioned::NDB:
132       case FGPositioned::VOR:
133         navlist->add(r);
134         break;
135         
136       case FGPositioned::ILS:
137       case FGPositioned::LOC:
138         loclist->add(r);
139         break;
140         
141       case FGPositioned::GS:
142         gslist->add(r);
143         break;
144       
145       case FGPositioned::DME:
146       {
147         dmelist->add(r);
148         string::size_type loc1= r->name().find( "TACAN", 0 );
149         string::size_type loc2 = r->name().find( "VORTAC", 0 );
150                        
151         if( loc1 != string::npos || loc2 != string::npos) {
152           tacanlist->add(r);
153         }
154
155         break;
156       }
157       
158       default:
159         throw sg_range_exception("got unsupported NavRecord type", "fgNavDBInit");
160       }
161
162       in >> skipcomment;
163     } // of stream data loop
164
165 // load the carrier navaids file
166     
167     string file, name;
168     path = globals->get_fg_root() ;
169     path.append( "Navaids/carrier_nav.dat" );
170     
171     file = path.str();
172     SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
173     
174     sg_gzifstream incarrier( path.str() );
175     
176     if ( !incarrier.is_open() ) {
177         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
178         exit(-1);
179     }
180     
181     // skip first two lines
182     //incarrier >> skipeol;
183     //incarrier >> skipeol;
184     
185     while ( ! incarrier.eof() ) {
186       FGNavRecord *r = createNavFromStream(incarrier);
187       if (!r) {
188         continue;
189       }
190       
191       carrierlist->add (r);
192     } // end while
193
194 // end loading the carrier navaids file
195
196 // load the channel/freqency file
197     string channel, freq;
198     path="";
199     path = globals->get_fg_root();
200     path.append( "Navaids/TACAN_freq.dat" );
201     
202     SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
203         
204     sg_gzifstream inchannel( path.str() );
205     
206     if ( !inchannel.is_open() ) {
207         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
208         exit(-1);
209     }
210     
211     // skip first line
212     inchannel >> skipeol;
213     while ( ! inchannel.eof() ) {
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 FGRunway* getRunwayFromName(const std::string& aName)
230 {
231   vector<string> parts = simgear::strutils::split(aName);
232   if (parts.size() < 2) {
233     SG_LOG(SG_GENERAL, SG_WARN, "getRunwayFromName: malformed name:" << aName);
234     return NULL;
235   }
236   
237   const FGAirport* apt = fgFindAirportID(parts[0]);
238   if (!apt) {
239     SG_LOG(SG_GENERAL, SG_WARN, "navaid " << aName << " associated with bogus airport ID:" << parts[0]);
240     return NULL;
241   }
242   
243   FGRunway* runway = apt->getRunwayByIdent(parts[1]);
244   if (!runway) {
245     SG_LOG(SG_GENERAL, SG_WARN, "navaid " << aName << " associated with bogus runway ID:" << parts[1]);
246     return NULL;
247   }
248
249   return runway;
250 }