]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
I had to make a small mod to the runways.dat format, code changes to handle
[flightgear.git] / src / Airports / runways.cxx
1 // runways.cxx -- a simple class to manage airport runway info
2 //
3 // Written by Curtis Olson, started August 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <math.h>               // fabs()
29 #include <stdio.h>              // sprintf()
30
31 #include <simgear/compiler.h>
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/sgstream.hxx>
35
36 #include STL_STRING
37 #include STL_IOSTREAM
38 #include <map>
39
40 #include "runways.hxx"
41
42 SG_USING_NAMESPACE(std);
43 SG_USING_STD(istream);
44 SG_USING_STD(multimap);
45
46 inline istream&
47 operator >> ( istream& in, FGRunway& a )
48 {
49     string type;
50     int tmp;
51
52     in >> a.type;
53     if ( a.type == "R" ) {
54         in >> a.id >> a.rwy_no >> a.lat >> a.lon >> a.heading
55            >> a.length >> a.width >> a.surface_flags >> a.end1_flags
56            >> tmp >> tmp >> a.end2_flags >> tmp >> tmp;
57     } else if ( a.type == "T" ) {
58         // in >> a.id >> a.rwy_no >> a.lat >> a.lon >> a.heading
59         //    >> a.length >> a.width >> a.surface_flags;
60         in >> skipeol;
61     } else {
62         in >> skipeol;
63     }
64
65     return in;
66 }
67
68
69 FGRunwayList::FGRunwayList( const string& file ) {
70     SG_LOG( SG_GENERAL, SG_DEBUG, "Reading runway list: " << file );
71
72     // open the specified file for reading
73     sg_gzifstream in( file );
74     if ( !in.is_open() ) {
75         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
76         exit(-1);
77     }
78
79     // skip header line
80     in >> skipeol;
81
82     FGRunway rwy;
83     while ( in ) {
84         in >> rwy;
85         runways.insert(pair<const string, FGRunway>(rwy.id, rwy));
86     }
87 }
88
89
90 // Return reverse rwy number
91 // eg 01 -> 19
92 // 03L -> 21R
93 static string GetReverseRunwayNo(string rwyno) {        
94     // cout << "Original rwyno = " << rwyNo << '\n';
95     
96     // standardize input number
97     string tmp = rwyno.substr(1, 1);
98     if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
99         tmp = rwyno;
100         rwyno = "0" + tmp;
101         SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from " << tmp
102                                      << " to " << rwyno );
103     }
104     
105     char buf[4];
106     int rn = atoi(rwyno.substr(0,2).c_str());
107     rn += 18;
108     while(rn > 36) {
109         rn -= 36;
110     }
111     sprintf(buf, "%02i", rn);
112     if(rwyno.size() == 3) {
113         if(rwyno.substr(2,1) == "L") {
114             buf[2] = 'R';
115             buf[3] = '\0';
116         } else if (rwyno.substr(2,1) == "R") {
117             buf[2] = 'L';
118             buf[3] = '\0';
119         } else if (rwyno.substr(2,1) == "C") {
120             buf[2] = 'C';
121             buf[3] = '\0';
122         } else {
123             SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
124             << rwyno << " passed to GetReverseRunwayNo(...)");
125         }
126     }
127     return((string)buf);
128 }
129
130
131 // search for the specified apt id (wierd!)
132 bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
133     runway_map_iterator pos;
134
135     pos = runways.lower_bound(aptid);
136     if ( pos != runways.end() ) {
137         current = pos;
138         *r = pos->second;
139         return true;
140     } else {
141         return false;
142     }
143 }
144
145
146 // search for the specified apt id and runway no
147 bool FGRunwayList::search( const string& aptid, const string& rwyno,
148                            FGRunway *r )
149 {
150     // standardize input number
151     string runwayno = rwyno;
152     string tmp = runwayno.substr(1, 1);
153     if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (runwayno.size() == 1)) {
154         tmp = runwayno;
155         runwayno = "0" + tmp;
156         SG_LOG( SG_GENERAL, SG_INFO,
157                 "Standardising rwy number from " << tmp << " to " << runwayno );
158     }
159     string revrwyno = GetReverseRunwayNo(runwayno);
160
161     runway_map_iterator pos;
162     for ( pos = runways.lower_bound( aptid );
163           pos != runways.upper_bound( aptid ); ++pos)
164     {
165         if ( pos->second.rwy_no == runwayno ) {
166             current = pos;
167             *r = pos->second;
168             return true;
169         } else if ( pos->second.rwy_no == revrwyno ) {
170             // Search again with the other-end runway number.
171             // Remember we have to munge the heading and rwy_no
172             // results if this one matches
173             current = pos;
174             *r = pos->second;
175             r->rwy_no = revrwyno;
176             r->heading += 180.0;
177             string tmp = r->end1_flags;
178             r->end1_flags = r->end2_flags;
179             r->end2_flags = tmp;
180             return true;
181         }
182     }
183
184     return false;
185 }
186
187
188 // (wierd!)
189 FGRunway FGRunwayList::search( const string& aptid ) {
190     FGRunway a;
191     search( aptid, &a );
192     return a;
193 }
194
195
196 // Return the runway closest to a given heading
197 bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
198                            FGRunway *runway )
199 {
200     string rwyNo = search(aptid, tgt_hdg);
201     return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
202 }
203
204
205 // Return the runway number of the runway closest to a given heading
206 string FGRunwayList::search( const string& aptid, const int tgt_hdg ) {
207     FGRunway r;
208     FGRunway tmp_r;     
209     string rn;
210     double found_dir = 0.0;  
211  
212     if ( !search( aptid, &tmp_r ) ) {
213         SG_LOG( SG_GENERAL, SG_ALERT,
214                 "Failed to find " << aptid << " in database." );
215         return "NN";
216     }
217     
218     double diff;
219     double min_diff = 360.0;
220     
221     while ( tmp_r.id == aptid ) {
222         r = tmp_r;
223         
224         // forward direction
225         diff = tgt_hdg - r.heading;
226         while ( diff < -180.0 ) { diff += 360.0; }
227         while ( diff >  180.0 ) { diff -= 360.0; }
228         diff = fabs(diff);
229         // SG_LOG( SG_GENERAL, SG_INFO,
230         //         "Runway " << r.rwy_no << " heading = " << r.heading <<
231         //         " diff = " << diff );
232         if ( diff < min_diff ) {
233             min_diff = diff;
234             rn = r.rwy_no;
235             found_dir = 0;
236         }
237         
238         // reverse direction
239         diff = tgt_hdg - r.heading - 180.0;
240         while ( diff < -180.0 ) { diff += 360.0; }
241         while ( diff >  180.0 ) { diff -= 360.0; }
242         diff = fabs(diff);
243         // SG_LOG( SG_GENERAL, SG_INFO,
244         //         "Runway -" << r.rwy_no << " heading = " <<
245         //         r.heading + 180.0 <<
246         //         " diff = " << diff );
247         if ( diff < min_diff ) {
248             min_diff = diff;
249             rn = r.rwy_no;
250             found_dir = 180.0;
251         }
252         
253         next( &tmp_r );
254     }
255     
256     // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
257     //         << " + " << found_dir );
258     rn = r.rwy_no;
259     // cout << "In search, rn = " << rn << endl;
260     if ( found_dir == 180 ) {
261         rn = GetReverseRunwayNo(rn);
262         //cout << "New rn = " << rn << '\n';
263     }   
264     
265     return rn;
266 }
267
268
269 bool FGRunwayList::next( FGRunway* runway ) {
270     ++current;
271     if ( current != runways.end() ) {
272         *runway = current->second;
273         return true;
274     } else {
275         return false;
276     }
277 }
278
279
280 FGRunway FGRunwayList::next() {
281     FGRunway result;
282
283     ++current;
284     if ( current != runways.end() ) {
285         result = current->second;
286     }
287
288     return result;
289 }
290
291
292 // Destructor
293 FGRunwayList::~FGRunwayList( void ) {
294 }