1 // navdb.cxx -- top level navaids management routines
3 // Written by Curtis Olson, started May 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
27 #include <simgear/compiler.h>
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>
38 #include "navrecord.hxx"
39 #include "navlist.hxx"
41 #include "Main/globals.hxx"
42 #include "Navaids/markerbeacon.hxx"
43 #include "Airports/simple.hxx"
47 static FGPositioned::Type
48 mapRobinTypeToFGPType(int aTy)
52 case 2: return FGPositioned::NDB;
53 case 3: return FGPositioned::VOR;
54 case 4: return FGPositioned::LOC;
55 case 5: return FGPositioned::ILS;
56 case 6: return FGPositioned::GS;
58 case 13: return FGPositioned::DME;
59 case 99: return FGPositioned::INVALID; // end-of-file code
61 throw sg_range_exception("Got a nav.dat type we don't recognize", "FGNavRecord::createFromStream");
65 static FGNavRecord* createNavFromStream(std::istream& aStream)
69 if (aStream.eof() || (rawType == 99)) {
70 return NULL; // happens with, eg, carrier_nav.dat
73 double lat, lon, elev_ft, multiuse;
75 std::string name, ident;
76 aStream >> lat >> lon >> elev_ft >> freq >> range >> multiuse >> ident;
77 getline(aStream, name);
79 SGGeod pos(SGGeod::fromDegFt(lon, lat, elev_ft));
80 name = simgear::strutils::strip(name);
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
88 FGPositioned::Type type = mapRobinTypeToFGPType(rawType);
89 if (type == FGPositioned::INVALID) {
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) {
99 return new FGNavRecord(type, ident, name, pos,
100 freq, range, multiuse);
103 // load and initialize the navigational databases
104 bool fgNavDBInit( FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
106 FGNavList *tacanlist, FGNavList *carrierlist,
107 FGTACANList *channellist)
109 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
111 SGPath path( globals->get_fg_root() );
112 path.append( "Navaids/nav.dat" );
114 sg_gzifstream in( path.str() );
115 if ( !in.is_open() ) {
116 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
120 // skip first two lines
125 FGNavRecord *r = createNavFromStream(in);
131 case FGPositioned::NDB:
132 case FGPositioned::VOR:
136 case FGPositioned::ILS:
137 case FGPositioned::LOC:
141 case FGPositioned::GS:
145 case FGPositioned::DME:
148 string::size_type loc1= r->name().find( "TACAN", 0 );
149 string::size_type loc2 = r->name().find( "VORTAC", 0 );
151 if( loc1 != string::npos || loc2 != string::npos) {
159 throw sg_range_exception("got unsupported NavRecord type", "fgNavDBInit");
163 } // of stream data loop
165 // load the carrier navaids file
168 path = globals->get_fg_root() ;
169 path.append( "Navaids/carrier_nav.dat" );
172 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
174 sg_gzifstream incarrier( path.str() );
176 if ( !incarrier.is_open() ) {
177 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
181 // skip first two lines
182 //incarrier >> skipeol;
183 //incarrier >> skipeol;
185 while ( ! incarrier.eof() ) {
186 FGNavRecord *r = createNavFromStream(incarrier);
191 carrierlist->add (r);
194 // end loading the carrier navaids file
196 // load the channel/freqency file
197 string channel, freq;
199 path = globals->get_fg_root();
200 path.append( "Navaids/TACAN_freq.dat" );
202 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
204 sg_gzifstream inchannel( path.str() );
206 if ( !inchannel.is_open() ) {
207 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
212 inchannel >> skipeol;
213 while ( ! inchannel.eof() ) {
214 FGTACANRecord *r = new FGTACANRecord;
216 channellist->add ( r );
217 //cout << "channel = " << r->get_channel() ;
218 //cout << " freq = " << r->get_freq() << endl;
229 FGRunway* getRunwayFromName(const std::string& aName)
231 vector<string> parts = simgear::strutils::split(aName);
232 if (parts.size() < 2) {
233 SG_LOG(SG_GENERAL, SG_WARN, "getRunwayFromName: malformed name:" << aName);
237 const FGAirport* apt = fgFindAirportID(parts[0]);
239 SG_LOG(SG_GENERAL, SG_WARN, "navaid " << aName << " associated with bogus airport ID:" << parts[0]);
243 FGRunway* runway = apt->getRunwayByIdent(parts[1]);
245 SG_LOG(SG_GENERAL, SG_WARN, "navaid " << aName << " associated with bogus runway ID:" << parts[1]);