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>
51 typedef std::map<FGAirport*, SGPropertyNode_ptr> AirportPropertyMap;
53 static AirportPropertyMap static_airportIlsData;
55 static FGPositioned::Type
56 mapRobinTypeToFGPType(int aTy)
60 case 2: return FGPositioned::NDB;
61 case 3: return FGPositioned::VOR;
62 case 4: return FGPositioned::ILS;
63 case 5: return FGPositioned::LOC;
64 case 6: return FGPositioned::GS;
66 case 13: return FGPositioned::DME;
67 case 99: return FGPositioned::INVALID; // end-of-file code
69 throw sg_range_exception("Got a nav.dat type we don't recognize", "FGNavRecord::createFromStream");
73 static FGNavRecord* createNavFromStream(std::istream& aStream)
77 if (aStream.eof() || (rawType == 99)) {
78 return NULL; // happens with, eg, carrier_nav.dat
81 double lat, lon, elev_ft, multiuse;
83 std::string name, ident;
84 aStream >> lat >> lon >> elev_ft >> freq >> range >> multiuse >> ident;
85 getline(aStream, name);
87 SGGeod pos(SGGeod::fromDegFt(lon, lat, elev_ft));
88 name = simgear::strutils::strip(name);
90 if ((rawType >= 7) && (rawType <= 9)) {
91 // marker beacons use a different run-time class now
92 FGMarkerBeaconRecord::create(rawType, name, pos);
93 return NULL; // not a nav-record, but that's okay
96 FGPositioned::Type type = mapRobinTypeToFGPType(rawType);
97 if (type == FGPositioned::INVALID) {
101 // silently multiply adf frequencies by 100 so that adf
102 // vs. nav/loc frequency lookups can use the same code.
103 if (type == FGPositioned::NDB) {
107 return new FGNavRecord(type, ident, name, pos,
108 freq, range, multiuse);
111 // load and initialize the navigational databases
112 bool fgNavDBInit( FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
114 FGNavList *tacanlist, FGNavList *carrierlist,
115 FGTACANList *channellist)
117 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
119 SGPath path( globals->get_fg_root() );
120 path.append( "Navaids/nav.dat" );
122 sg_gzifstream in( path.str() );
123 if ( !in.is_open() ) {
124 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
128 // skip first two lines
133 FGNavRecord *r = createNavFromStream(in);
139 case FGPositioned::NDB:
140 case FGPositioned::VOR:
144 case FGPositioned::ILS:
145 case FGPositioned::LOC:
149 case FGPositioned::GS:
153 case FGPositioned::DME:
156 string::size_type loc1= r->name().find( "TACAN", 0 );
157 string::size_type loc2 = r->name().find( "VORTAC", 0 );
159 if( loc1 != string::npos || loc2 != string::npos) {
167 throw sg_range_exception("got unsupported NavRecord type", "fgNavDBInit");
171 } // of stream data loop
173 // load the carrier navaids file
176 path = globals->get_fg_root() ;
177 path.append( "Navaids/carrier_nav.dat" );
180 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
182 sg_gzifstream incarrier( path.str() );
184 if ( !incarrier.is_open() ) {
185 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
189 // skip first two lines
190 //incarrier >> skipeol;
191 //incarrier >> skipeol;
193 while ( ! incarrier.eof() ) {
194 FGNavRecord *r = createNavFromStream(incarrier);
199 carrierlist->add (r);
202 // end loading the carrier navaids file
204 // load the channel/freqency file
205 string channel, freq;
207 path = globals->get_fg_root();
208 path.append( "Navaids/TACAN_freq.dat" );
210 SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
212 sg_gzifstream inchannel( path.str() );
214 if ( !inchannel.is_open() ) {
215 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
220 inchannel >> skipeol;
221 while ( ! inchannel.eof() ) {
222 FGTACANRecord *r = new FGTACANRecord;
224 channellist->add ( r );
225 //cout << "channel = " << r->get_channel() ;
226 //cout << " freq = " << r->get_freq() << endl;
234 // flush all the parsed ils.xml data, we don't need it anymore,
235 // since it's been meregd into the FGNavRecords
236 static_airportIlsData.clear();
241 SGPropertyNode* ilsDataForRunwayAndNavaid(FGRunway* aRunway, const std::string& aNavIdent)
243 if (!fgGetBool("/sim/paths/use-custom-scenery-data")) {
251 FGAirport* apt = aRunway->airport();
252 // find (or load) the airprot ILS data
253 AirportPropertyMap::iterator it = static_airportIlsData.find(apt);
254 if (it == static_airportIlsData.end()) {
256 if (!XMLLoader::findAirportData(apt->ident(), "ils", path)) {
257 // no ils.xml file for this airpot, insert a NULL entry so we don't
259 static_airportIlsData.insert(it, std::make_pair(apt, SGPropertyNode_ptr()));
263 SGPropertyNode_ptr rootNode = new SGPropertyNode;
264 readProperties(path.str(), rootNode);
265 it = static_airportIlsData.insert(it, std::make_pair(apt, rootNode));
266 } // of ils.xml file not loaded
272 // find the entry matching the runway
273 SGPropertyNode* runwayNode, *ilsNode;
274 for (int i=0; (runwayNode = it->second->getChild("runway", i)) != NULL; ++i) {
275 for (int j=0; (ilsNode = runwayNode->getChild("ils", j)) != NULL; ++j) {
276 // must match on both nav-ident and runway ident, to support the following:
277 // - runways with multiple distinct ILS installations (KEWD, for example)
278 // - runways where both ends share the same nav ident (LFAT, for example)
279 if ((ilsNode->getStringValue("nav-id") == aNavIdent) &&
280 (ilsNode->getStringValue("rwy") == aRunway->ident()))
284 } // of ILS iteration
285 } // of runway iteration
290 FGRunway* getRunwayFromName(const std::string& aName)
292 vector<string> parts = simgear::strutils::split(aName);
293 if (parts.size() < 2) {
294 SG_LOG(SG_GENERAL, SG_WARN, "getRunwayFromName: malformed name:" << aName);
298 const FGAirport* apt = fgFindAirportID(parts[0]);
300 SG_LOG(SG_GENERAL, SG_WARN, "navaid " << aName << " associated with bogus airport ID:" << parts[0]);
304 if (!apt->hasRunwayWithIdent(parts[1])) {
305 SG_LOG(SG_GENERAL, SG_WARN, "navaid " << aName << " associated with bogus runway ID:" << parts[1]);
309 return apt->getRunwayByIdent(parts[1]);