]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navdb.cxx
Interim windows build fix
[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 "navdb.hxx"
28
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>
38
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>
48
49 using std::string;
50 using std::vector;
51
52 static FGPositioned::Type
53 mapRobinTypeToFGPType(int aTy)
54 {
55   switch (aTy) {
56  // case 1:
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;
65   case 12:
66   case 13: return FGPositioned::DME;
67   case 99: return FGPositioned::INVALID; // end-of-file code
68   default:
69     throw sg_range_exception("Got a nav.dat type we don't recognize", "FGNavRecord::createFromStream");
70   }
71 }
72
73 static bool autoAlignLocalizers = false;
74 static double autoAlignThreshold = 0.0;
75
76 /**
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).
81  *
82  * The positioned and heading argument are modified if changes are made.
83  */
84 void alignLocaliserWithRunway(FGRunway* rwy, const string& ident, SGGeod& pos, double& heading)
85 {
86   assert(rwy);
87   // find the distance from the threshold to the localizer
88   double dist = SGGeodesy::distanceM(pos, rwy->threshold());
89   
90   // back project that distance along the runway center line
91   SGGeod newPos = rwy->pointOnCenterline(dist);
92   
93   double hdg_diff = heading - rwy->headingDeg();
94   SG_NORMALIZE_RANGE(hdg_diff, -180.0, 180.0);
95   
96   if ( fabs(hdg_diff) <= autoAlignThreshold ) {
97     pos = SGGeod::fromGeodFt(newPos, pos.getElevationFt());
98     heading = rwy->headingDeg();
99   } else {
100     SG_LOG(SG_NAVAID, SG_DEBUG, "localizer:" << ident << ", aligning with runway "
101            << rwy->ident() << " exceeded heading threshold");
102   }
103 }
104
105 static double defaultNavRange(const string& ident, FGPositioned::Type type)
106 {
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
109   // reason.
110   SG_LOG(SG_NAVAID, SG_DEBUG, "navaid " << ident << " has no range set, using defaults");
111   switch (type) {
112     case FGPositioned::NDB:
113     case FGPositioned::VOR:
114       return FG_NAV_DEFAULT_RANGE;
115       
116     case FGPositioned::LOC:
117     case FGPositioned::ILS:
118     case FGPositioned::GS:
119       return FG_LOC_DEFAULT_RANGE;
120       
121     case FGPositioned::DME:
122       return FG_DME_DEFAULT_RANGE;
123
124     case FGPositioned::TACAN:
125     case FGPositioned::MOBILE_TACAN:
126       return FG_TACAN_DEFAULT_RANGE;
127
128     default:
129       return FG_LOC_DEFAULT_RANGE;
130   }
131 }
132
133
134 namespace flightgear
135 {
136
137 static PositionedID readNavFromStream(std::istream& aStream,
138                                         FGPositioned::Type type = FGPositioned::INVALID)
139 {
140   NavDataCache* cache = NavDataCache::instance();
141   
142   int rawType;
143   aStream >> rawType;
144   if( aStream.eof() || (rawType == 99) || (rawType == 0) )
145     return 0; // happens with, eg, carrier_nav.dat
146   
147   double lat, lon, elev_ft, multiuse;
148   int freq, range;
149   std::string name, ident;
150   aStream >> lat >> lon >> elev_ft >> freq >> range >> multiuse >> ident;
151   getline(aStream, name);
152   
153   SGGeod pos(SGGeod::fromDegFt(lon, lat, elev_ft));
154   name = simgear::strutils::strip(name);
155
156 // the type can be forced by our caller, but normally we use th value
157 // supplied in the .dat file
158   if (type == FGPositioned::INVALID) {
159     type = mapRobinTypeToFGPType(rawType);
160   }
161   if (type == FGPositioned::INVALID) {
162     return 0;
163   }
164
165   if ((type >= FGPositioned::OM) && (type <= FGPositioned::IM)) {
166     AirportRunwayPair arp(cache->findAirportRunway(name));
167     if (arp.second && (elev_ft < 0.01)) {
168     // snap to runway elevation
169       FGPositioned* runway = cache->loadById(arp.second);
170       assert(runway);
171       pos.setElevationFt(runway->geod().getElevationFt());
172     }
173
174     return cache->insertNavaid(type, string(), name, pos, 0, 0, 0,
175                                arp.first, arp.second);
176   }
177   
178   if (range < 1) {
179     range = defaultNavRange(ident, type);
180   }
181   
182   AirportRunwayPair arp;
183   FGRunway* runway = NULL;
184   PositionedID navaid_dme = 0;
185
186   if (type == FGPositioned::DME) {
187       // complication here: the database doesn't record TACAN sites at all,
188       // we simply infer their existence from DMEs whose name includes 'VORTAC'
189       // or 'TACAN' (since all TACAN stations include DME)
190       // hence the cache never actually includes entries of type TACAN
191     FGPositioned::TypeFilter f(FGPositioned::INVALID);
192     if ( name.find("VOR-DME") != std::string::npos ) {
193       f.addType(FGPositioned::VOR);
194     } else if ( name.find("DME-ILS") != std::string::npos ) {
195       f.addType(FGPositioned::ILS);
196       f.addType(FGPositioned::LOC);
197     } else if ( name.find("VORTAC") != std::string::npos ) {
198       f.addType(FGPositioned::VOR);
199     } else if ( name.find("NDB-DME") != std::string::npos ) {
200       f.addType(FGPositioned::NDB);
201     }
202
203     if (f.maxType() > 0) {
204       FGPositionedRef ref = FGPositioned::findClosestWithIdent(ident, pos, &f);
205       if (ref.valid()) {
206         string_list dme_part = simgear::strutils::split(name , 0 ,1);
207         string_list navaid_part = simgear::strutils::split(ref.get()->name(), 0 ,1);
208
209         if ( simgear::strutils::uppercase(navaid_part[0]) == simgear::strutils::uppercase(dme_part[0]) ) {
210           navaid_dme = ref.get()->guid();
211         } else {
212             SG_LOG(SG_NAVAID, SG_WARN, "DME " << ident << " (" << name << "), closest match has wrong name:"
213                    << ref->ident() << " (" << ref->name() << ")");
214         }
215       } else {
216           SG_LOG(SG_NAVAID, SG_WARN, "Couldn't find navaid for DME:" << ident << " (" << name << ")");
217       }
218     } // of have a co-located navaid to locate
219   }
220
221   if ((type >= FGPositioned::ILS) && (type <= FGPositioned::GS)) {
222     arp = cache->findAirportRunway(name);
223     if (arp.second) {
224       runway = FGPositioned::loadById<FGRunway>(arp.second);
225       assert(runway);
226 #if 0
227       // code is disabled since it's causing some problems, see
228       // http://code.google.com/p/flightgear-bugs/issues/detail?id=926
229       if (elev_ft < 0.01) {
230         // snap to runway elevation
231         pos.setElevationFt(runway->geod().getElevationFt());
232       }
233 #endif
234     } // of found runway in the DB
235   } // of type is runway-related
236   
237   bool isLoc = (type == FGPositioned::ILS) || (type == FGPositioned::LOC);
238   if (runway && autoAlignLocalizers && isLoc) {
239     alignLocaliserWithRunway(runway, ident, pos, multiuse);
240   }
241     
242   // silently multiply adf frequencies by 100 so that adf
243   // vs. nav/loc frequency lookups can use the same code.
244   if (type == FGPositioned::NDB) {
245     freq *= 100;
246   }
247   
248   PositionedID r = cache->insertNavaid(type, ident, name, pos, freq, range, multiuse,
249                              arp.first, arp.second);
250   
251   if (isLoc) {
252     cache->setRunwayILS(arp.second, r);
253   }
254
255   if (navaid_dme) {
256     cache->setNavaidColocated(navaid_dme, r);
257   }
258   
259   return r;
260 }
261
262 // generated by wc -l on uncomrpessed nav.dat
263 const unsigned int LINES_IN_NAV_DAT = 26775;
264   
265 // load and initialize the navigational databases
266 bool navDBInit(const SGPath& path)
267 {
268     sg_gzifstream in( path.str() );
269     if ( !in.is_open() ) {
270         SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
271       return false;
272     }
273
274   autoAlignLocalizers = fgGetBool("/sim/navdb/localizers/auto-align", true);
275   autoAlignThreshold = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg", 5.0 );
276     NavDataCache* cache = NavDataCache::instance();
277
278     // skip first two lines
279     in >> skipeol;
280     in >> skipeol;
281     unsigned int lineNumber = 2;
282
283     while (!in.eof()) {
284       readNavFromStream(in);
285       in >> skipcomment;
286
287         ++lineNumber;
288         if ((lineNumber % 100) == 0) {
289             // every 100 lines
290             unsigned int percent = (lineNumber * 100) / LINES_IN_NAV_DAT;
291             cache->setRebuildPhaseProgress(NavDataCache::REBUILD_NAVAIDS, percent);
292         }
293
294     } // of stream data loop
295   
296   return true;
297 }
298   
299   
300 bool loadCarrierNav(const SGPath& path)
301 {    
302     SG_LOG( SG_NAVAID, SG_DEBUG, "opening file: " << path.str() );
303     sg_gzifstream incarrier( path.str() );
304     
305     if ( !incarrier.is_open() ) {
306         SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
307       return false;
308     }
309     
310     while ( ! incarrier.eof() ) {
311       incarrier >> skipcomment;
312       // force the type to be MOBILE_TACAN
313       readNavFromStream(incarrier, FGPositioned::MOBILE_TACAN);
314     }
315
316   return true;
317 }
318   
319 bool loadTacan(const SGPath& path, FGTACANList *channellist)
320 {
321     SG_LOG( SG_NAVAID, SG_DEBUG, "opening file: " << path.str() );
322     sg_gzifstream inchannel( path.str() );
323     
324     if ( !inchannel.is_open() ) {
325         SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
326       return false;
327     }
328     
329     // skip first line
330     inchannel >> skipeol;
331     while ( ! inchannel.eof() ) {
332         FGTACANRecord *r = new FGTACANRecord;
333         inchannel >> (*r);
334         channellist->add ( r );
335         
336     } // end while
337
338     return true;
339 }
340   
341 } // of namespace flightgear