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.
29 #include <simgear/compiler.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/math/sg_geodesy.hxx>
32 #include <simgear/misc/strutils.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/structure/exception.hxx>
35 #include <simgear/misc/sgstream.hxx>
36 #include <simgear/props/props_io.hxx>
37 #include <simgear/sg_inlines.h>
39 #include "navrecord.hxx"
40 #include "navlist.hxx"
41 #include <Main/globals.hxx>
42 #include <Navaids/markerbeacon.hxx>
43 #include <Airports/airport.hxx>
44 #include <Airports/runways.hxx>
45 #include <Airports/xmlloader.hxx>
46 #include <Main/fg_props.hxx>
47 #include <Navaids/NavDataCache.hxx>
52 static FGPositioned::Type
53 mapRobinTypeToFGPType(int aTy)
57 case 2: return FGPositioned::NDB;
58 case 3: return FGPositioned::VOR;
59 case 4: return FGPositioned::ILS;
60 case 5: return FGPositioned::LOC;
61 case 6: return FGPositioned::GS;
62 case 7: return FGPositioned::OM;
63 case 8: return FGPositioned::MM;
64 case 9: return FGPositioned::IM;
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 bool autoAlignLocalizers = false;
74 static double autoAlignThreshold = 0.0;
77 * Given a runway, and proposed localizer data (ident, positioned and heading),
78 * precisely align the localizer with the actual runway heading, providing the
79 * difference between the localizer course and runway heading is less than a
80 * threshold. (To allow for localizers such as Kai-Tak requiring a turn on final).
82 * The positioned and heading argument are modified if changes are made.
84 void alignLocaliserWithRunway(FGRunway* rwy, const string& ident, SGGeod& pos, double& heading)
87 // find the distance from the threshold to the localizer
88 double dist = SGGeodesy::distanceM(pos, rwy->threshold());
90 // back project that distance along the runway center line
91 SGGeod newPos = rwy->pointOnCenterline(dist);
93 double hdg_diff = heading - rwy->headingDeg();
94 SG_NORMALIZE_RANGE(hdg_diff, -180.0, 180.0);
96 if ( fabs(hdg_diff) <= autoAlignThreshold ) {
97 pos = SGGeod::fromGeodFt(newPos, pos.getElevationFt());
98 heading = rwy->headingDeg();
100 SG_LOG(SG_NAVAID, SG_DEBUG, "localizer:" << ident << ", aligning with runway "
101 << rwy->ident() << " exceeded heading threshold");
105 static double defaultNavRange(const string& ident, FGPositioned::Type type)
107 // Ranges are included with the latest data format, no need to
108 // assign our own defaults, unless the range is not set for some
110 SG_LOG(SG_NAVAID, SG_DEBUG, "navaid " << ident << " has no range set, using defaults");
112 case FGPositioned::NDB:
113 case FGPositioned::VOR:
114 return FG_NAV_DEFAULT_RANGE;
116 case FGPositioned::LOC:
117 case FGPositioned::ILS:
118 case FGPositioned::GS:
119 return FG_LOC_DEFAULT_RANGE;
121 case FGPositioned::DME: return FG_DME_DEFAULT_RANGE;
122 default: return FG_LOC_DEFAULT_RANGE;
130 static PositionedID readNavFromStream(std::istream& aStream,
131 FGPositioned::Type type = FGPositioned::INVALID)
133 NavDataCache* cache = NavDataCache::instance();
137 if (aStream.eof() || (rawType == 99)) {
138 return 0; // happens with, eg, carrier_nav.dat
141 double lat, lon, elev_ft, multiuse;
143 std::string name, ident;
144 aStream >> lat >> lon >> elev_ft >> freq >> range >> multiuse >> ident;
145 getline(aStream, name);
147 SGGeod pos(SGGeod::fromDegFt(lon, lat, elev_ft));
148 name = simgear::strutils::strip(name);
150 // the type can be forced by our caller, but normally we use th value
151 // supplied in the .dat file
152 if (type == FGPositioned::INVALID) {
153 type = mapRobinTypeToFGPType(rawType);
155 if (type == FGPositioned::INVALID) {
159 if ((type >= FGPositioned::OM) && (type <= FGPositioned::IM)) {
160 AirportRunwayPair arp(cache->findAirportRunway(name));
161 if (arp.second && (elev_ft < 0.01)) {
162 // snap to runway elevation
163 FGPositioned* runway = cache->loadById(arp.second);
165 pos.setElevationFt(runway->geod().getElevationFt());
168 return cache->insertNavaid(type, string(), name, pos, 0, 0, 0,
169 arp.first, arp.second);
173 range = defaultNavRange(ident, type);
176 AirportRunwayPair arp;
177 FGRunway* runway = NULL;
178 PositionedID navaid_dme = 0;
180 if (type == FGPositioned::DME) {
181 FGPositioned::TypeFilter f(FGPositioned::INVALID);
182 if ( name.find("VOR-DME") != std::string::npos ) {
183 f.addType(FGPositioned::VOR);
184 } else if ( name.find("DME-ILS") != std::string::npos ) {
185 f.addType(FGPositioned::ILS);
186 f.addType(FGPositioned::LOC);
187 } else if ( name.find("VORTAC") != std::string::npos ) {
188 f.addType(FGPositioned::VOR);
189 } else if ( name.find("NDB-DME") != std::string::npos ) {
190 f.addType(FGPositioned::NDB);
191 } else if ( name.find("TACAN") != std::string::npos ) {
192 f.addType(FGPositioned::VOR);
195 if (f.maxType() > 0) {
196 FGPositionedRef ref = FGPositioned::findClosestWithIdent(ident, pos, &f);
198 string_list dme_part = simgear::strutils::split(name , 0 ,1);
199 string_list navaid_part = simgear::strutils::split(ref.get()->name(), 0 ,1);
201 if ( simgear::strutils::uppercase(navaid_part[0]) == simgear::strutils::uppercase(dme_part[0]) ) {
202 navaid_dme = ref.get()->guid();
208 if ((type >= FGPositioned::ILS) && (type <= FGPositioned::GS)) {
209 arp = cache->findAirportRunway(name);
211 runway = FGPositioned::loadById<FGRunway>(arp.second);
214 // code is disabled since it's causing some problems, see
215 // http://code.google.com/p/flightgear-bugs/issues/detail?id=926
216 if (elev_ft < 0.01) {
217 // snap to runway elevation
218 pos.setElevationFt(runway->geod().getElevationFt());
221 } // of found runway in the DB
222 } // of type is runway-related
224 bool isLoc = (type == FGPositioned::ILS) || (type == FGPositioned::LOC);
225 if (runway && autoAlignLocalizers && isLoc) {
226 alignLocaliserWithRunway(runway, ident, pos, multiuse);
229 // silently multiply adf frequencies by 100 so that adf
230 // vs. nav/loc frequency lookups can use the same code.
231 if (type == FGPositioned::NDB) {
235 PositionedID r = cache->insertNavaid(type, ident, name, pos, freq, range, multiuse,
236 arp.first, arp.second);
239 cache->setRunwayILS(arp.second, r);
243 cache->setNavaidColocated(navaid_dme, r);
249 // load and initialize the navigational databases
250 bool navDBInit(const SGPath& path)
252 sg_gzifstream in( path.str() );
253 if ( !in.is_open() ) {
254 SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
258 autoAlignLocalizers = fgGetBool("/sim/navdb/localizers/auto-align", true);
259 autoAlignThreshold = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg", 5.0 );
261 // skip first two lines
266 readNavFromStream(in);
268 } // of stream data loop
274 bool loadCarrierNav(const SGPath& path)
276 SG_LOG( SG_NAVAID, SG_INFO, "opening file: " << path.str() );
277 sg_gzifstream incarrier( path.str() );
279 if ( !incarrier.is_open() ) {
280 SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
284 while ( ! incarrier.eof() ) {
285 // force the type to be MOBILE_TACAN
286 readNavFromStream(incarrier, FGPositioned::MOBILE_TACAN);
292 bool loadTacan(const SGPath& path, FGTACANList *channellist)
294 SG_LOG( SG_NAVAID, SG_INFO, "opening file: " << path.str() );
295 sg_gzifstream inchannel( path.str() );
297 if ( !inchannel.is_open() ) {
298 SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
303 inchannel >> skipeol;
304 while ( ! inchannel.eof() ) {
305 FGTACANRecord *r = new FGTACANRecord;
307 channellist->add ( r );
314 } // of namespace flightgear