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>
37 #include <simgear/props/props_io.hxx>
39 #include "navrecord.hxx"
40 #include "navlist.hxx"
42 #include <Main/globals.hxx>
43 #include <Navaids/markerbeacon.hxx>
44 #include <Airports/simple.hxx>
45 #include <Airports/runways.hxx>
46 #include <Airports/xmlloader.hxx>
47 #include <Main/fg_props.hxx>
52 typedef std::map<FGAirport*, SGPropertyNode_ptr> AirportPropertyMap;
54 static AirportPropertyMap static_airportIlsData;
56 static FGPositioned::Type
57 mapRobinTypeToFGPType(int aTy)
61 case 2: return FGPositioned::NDB;
62 case 3: return FGPositioned::VOR;
63 case 4: return FGPositioned::ILS;
64 case 5: return FGPositioned::LOC;
65 case 6: return FGPositioned::GS;
67 case 13: return FGPositioned::DME;
68 case 99: return FGPositioned::INVALID; // end-of-file code
70 throw sg_range_exception("Got a nav.dat type we don't recognize", "FGNavRecord::createFromStream");
74 static FGNavRecord* createNavFromStream(std::istream& aStream)
78 if (aStream.eof() || (rawType == 99)) {
79 return NULL; // happens with, eg, carrier_nav.dat
82 double lat, lon, elev_ft, multiuse;
84 std::string name, ident;
85 aStream >> lat >> lon >> elev_ft >> freq >> range >> multiuse >> ident;
86 getline(aStream, name);
88 SGGeod pos(SGGeod::fromDegFt(lon, lat, elev_ft));
89 name = simgear::strutils::strip(name);
91 if ((rawType >= 7) && (rawType <= 9)) {
92 // marker beacons use a different run-time class now
93 FGMarkerBeaconRecord::create(rawType, name, pos);
94 return NULL; // not a nav-record, but that's okay
97 FGPositioned::Type type = mapRobinTypeToFGPType(rawType);
98 if (type == FGPositioned::INVALID) {
102 // silently multiply adf frequencies by 100 so that adf
103 // vs. nav/loc frequency lookups can use the same code.
104 if (type == FGPositioned::NDB) {
108 return new FGNavRecord(type, ident, name, pos,
109 freq, range, multiuse);
112 // load and initialize the navigational databases
113 bool fgNavDBInit( FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
115 FGNavList *tacanlist, FGNavList *carrierlist,
116 FGTACANList *channellist)
118 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
120 SGPath path( globals->get_fg_root() );
121 path.append( "Navaids/nav.dat" );
123 sg_gzifstream in( path.str() );
124 if ( !in.is_open() ) {
125 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
129 // skip first two lines
134 FGNavRecord *r = createNavFromStream(in);
140 case FGPositioned::NDB:
141 case FGPositioned::VOR:
145 case FGPositioned::ILS:
146 case FGPositioned::LOC:
150 case FGPositioned::GS:
154 case FGPositioned::DME:
157 string::size_type loc1= r->name().find( "TACAN", 0 );
158 string::size_type loc2 = r->name().find( "VORTAC", 0 );
160 if( loc1 != string::npos || loc2 != string::npos) {
168 throw sg_range_exception("got unsupported NavRecord type", "fgNavDBInit");
172 } // of stream data loop
174 // load the carrier navaids file
177 path = globals->get_fg_root() ;
178 path.append( "Navaids/carrier_nav.dat" );
181 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
183 sg_gzifstream incarrier( path.str() );
185 if ( !incarrier.is_open() ) {
186 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
190 // skip first two lines
191 //incarrier >> skipeol;
192 //incarrier >> skipeol;
194 while ( ! incarrier.eof() ) {
195 FGNavRecord *r = createNavFromStream(incarrier);
200 carrierlist->add (r);
203 // end loading the carrier navaids file
205 // load the channel/freqency file
206 string channel, freq;
208 path = globals->get_fg_root();
209 path.append( "Navaids/TACAN_freq.dat" );
211 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
213 sg_gzifstream inchannel( path.str() );
215 if ( !inchannel.is_open() ) {
216 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
221 inchannel >> skipeol;
222 while ( ! inchannel.eof() ) {
223 FGTACANRecord *r = new FGTACANRecord;
225 channellist->add ( r );
226 //cout << "channel = " << r->get_channel() ;
227 //cout << " freq = " << r->get_freq() << endl;
235 // flush all the parsed ils.xml data, we don't need it anymore,
236 // since it's been meregd into the FGNavRecords
237 static_airportIlsData.clear();
242 SGPropertyNode* ilsDataForRunwayAndNavaid(FGRunway* aRunway, const std::string& aNavIdent)
248 FGAirport* apt = aRunway->airport();
249 // find (or load) the airprot ILS data
250 AirportPropertyMap::iterator it = static_airportIlsData.find(apt);
251 if (it == static_airportIlsData.end()) {
253 if (!XMLLoader::findAirportData(apt->ident(), "ils", path)) {
254 // no ils.xml file for this airpot, insert a NULL entry so we don't
256 static_airportIlsData.insert(it, std::make_pair(apt, SGPropertyNode_ptr()));
260 SGPropertyNode_ptr rootNode = new SGPropertyNode;
261 readProperties(path.str(), rootNode);
262 it = static_airportIlsData.insert(it, std::make_pair(apt, rootNode));
263 } // of ils.xml file not loaded
269 // find the entry matching the runway
270 SGPropertyNode* runwayNode, *ilsNode;
271 for (int i=0; (runwayNode = it->second->getChild("runway", i)) != NULL; ++i) {
272 for (int j=0; (ilsNode = runwayNode->getChild("ils", j)) != NULL; ++j) {
273 // must match on both nav-ident and runway ident, to support the following:
274 // - runways with multiple distinct ILS installations (KEWD, for example)
275 // - runways where both ends share the same nav ident (LFAT, for example)
276 if ((ilsNode->getStringValue("nav-id") == aNavIdent) &&
277 (ilsNode->getStringValue("rwy") == aRunway->ident()))
281 } // of ILS iteration
282 } // of runway iteration
287 FGRunway* getRunwayFromName(const std::string& aName)
289 vector<string> parts = simgear::strutils::split(aName);
290 if (parts.size() < 2) {
291 SG_LOG(SG_GENERAL, SG_WARN, "getRunwayFromName: malformed name:" << aName);
295 const FGAirport* apt = fgFindAirportID(parts[0]);
297 SG_LOG(SG_GENERAL, SG_DEBUG, "navaid " << aName << " associated with bogus airport ID:" << parts[0]);
301 if (!apt->hasRunwayWithIdent(parts[1])) {
302 SG_LOG(SG_GENERAL, SG_DEBUG, "navaid " << aName << " associated with bogus runway ID:" << parts[1]);
306 return apt->getRunwayByIdent(parts[1]);