]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navrecord.cxx
Merge branch 'jsd/atmos' into topic/atmos-merge
[flightgear.git] / src / Navaids / navrecord.cxx
1 // navrecord.cxx -- generic vor/dme/ndb class
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 <istream>
28
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/structure/exception.hxx>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/sg_inlines.h>
34 #include <simgear/props/props.hxx>
35 #include <simgear/props/props_io.hxx>
36
37 #include <Navaids/navrecord.hxx>
38 #include <Navaids/navdb.hxx>
39 #include <Airports/runways.hxx>
40 #include <Airports/simple.hxx>
41 #include <Airports/xmlloader.hxx>
42
43 #include <Main/fg_props.hxx>
44
45 FGNavRecord::FGNavRecord(Type aTy, const std::string& aIdent, 
46   const std::string& aName, const SGGeod& aPos,
47   int aFreq, int aRange, double aMultiuse) :
48   FGPositioned(aTy, aIdent, aPos),
49   freq(aFreq),
50   range(aRange),
51   multiuse(aMultiuse),
52   _name(aName),
53   mRunway(NULL),
54   serviceable(true),
55   trans_ident(aIdent)
56
57   initAirportRelation();
58
59   // Ranges are included with the latest data format, no need to
60   // assign our own defaults, unless the range is not set for some
61   // reason.
62   if (range < 0.1) {
63     SG_LOG(SG_GENERAL, SG_WARN, "navaid " << ident() << " has no range set, using defaults");
64     switch (type()) {
65     case NDB:
66     case VOR:
67       range = FG_NAV_DEFAULT_RANGE;
68       break;
69     
70     case LOC:
71     case ILS:
72     case GS:
73       range = FG_LOC_DEFAULT_RANGE;
74       break;
75       
76     case DME:
77       range = FG_DME_DEFAULT_RANGE;
78       break;
79     
80     default:
81       range = FG_LOC_DEFAULT_RANGE;
82     }
83   }
84   
85 }
86
87 void FGNavRecord::initAirportRelation()
88 {
89   if ((type() < ILS) || (type() > GS)) {
90     return; // not airport-located
91   }
92   
93   mRunway = getRunwayFromName(_name);  
94   
95   if (type() != GS) {
96     readAirportSceneryData();
97   }
98         
99   // fudge elevation to the runway elevation if it's not specified
100   if (fabs(elevation()) < 0.01) {
101     mPosition.setElevationFt(mRunway->elevation());
102   }
103   
104   if (type() == ILS) {
105     mRunway->setILS(this);
106   }
107   
108   // align localizers with their runway
109   if ((type() == ILS) || (type() == LOC)) {
110     if (!fgGetBool("/sim/navdb/localizers/auto-align", true)) {
111       return;
112     }
113     
114    double threshold 
115      = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg", 5.0 );
116     alignLocaliserWithRunway(threshold);
117   }
118 }
119
120 void FGNavRecord::readAirportSceneryData()
121 {
122   // allow users to disable the scenery data in the short-term
123   // longer term, this option can probably disappear
124   if (!fgGetBool("/sim/use-scenery-airport-data")) {
125     return; 
126   }
127   
128   SGPath path;
129   SGPropertyNode_ptr rootNode = new SGPropertyNode;
130   if (!XMLLoader::findAirportData(mRunway->airport()->ident(), "ils", path)) {
131     return;
132   }
133   
134   readProperties(path.str(), rootNode);
135   SGPropertyNode* runwayNode, *ilsNode;
136   for (int i=0; (runwayNode = rootNode->getChild("runway", i)) != NULL; ++i) {
137     for (int j=0; (ilsNode = runwayNode->getChild("ils", j)) != NULL; ++j) {
138       // must match on both nav-ident and runway ident, to support the following:
139       // - runways with multiple distinct ILS installations (KEWD, for example)
140       // - runways where both ends share the same nav ident (LFAT, for example)
141       if ((ilsNode->getStringValue("nav-id") == ident()) &&
142           (ilsNode->getStringValue("rwy") == mRunway->ident())) {
143         processSceneryILS(ilsNode);
144         return;
145       }
146     } // of ILS iteration
147   } // of runway iteration
148 }
149
150 void FGNavRecord::processSceneryILS(SGPropertyNode* aILSNode)
151 {
152   double hdgDeg = aILSNode->getDoubleValue("hdg-deg"),
153     lon = aILSNode->getDoubleValue("lon"),
154     lat = aILSNode->getDoubleValue("lat"),
155     elevM = aILSNode->getDoubleValue("elev-m");
156     
157   mPosition = SGGeod::fromDegM(lon, lat, elevM);
158   multiuse = hdgDeg;
159 }
160
161 void FGNavRecord::alignLocaliserWithRunway(double aThreshold)
162 {
163 // find the distance from the threshold to the localizer
164   double dist = SGGeodesy::distanceM(mPosition, mRunway->threshold());
165
166 // back project that distance along the runway center line
167   SGGeod newPos = mRunway->pointOnCenterline(dist);
168
169   double hdg_diff = get_multiuse() - mRunway->headingDeg();
170   SG_NORMALIZE_RANGE(hdg_diff, -180.0, 180.0);
171
172   if ( fabs(hdg_diff) <= aThreshold ) {
173     mPosition = SGGeod::fromGeodFt(newPos, mPosition.getElevationFt());
174     set_multiuse( mRunway->headingDeg() );
175   } else {
176     SG_LOG(SG_GENERAL, SG_WARN, "localizer:" << ident() << ", aligning with runway " 
177       << mRunway->ident() << " exceeded heading threshold");
178   }
179 }
180
181 FGTACANRecord::FGTACANRecord(void) :
182     channel(""),
183     freq(0)
184     
185 {
186 }
187
188 std::istream&
189 operator >> ( std::istream& in, FGTACANRecord& n )
190 {
191     in >> n.channel >> n.freq ;
192     //getline( in, n.name );
193
194     return in;
195 }