1 // runways.cxx -- a simple class to manage airport runway info
3 // Written by Curtis Olson, started August 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <math.h> // fabs()
29 #include <stdio.h> // sprintf()
31 #include <simgear/compiler.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/sgstream.hxx>
40 #include "runways.hxx"
42 SG_USING_NAMESPACE(std);
43 SG_USING_STD(istream);
44 SG_USING_STD(multimap);
47 operator >> ( istream& in, FGRunway& a )
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;
69 FGRunwayList::FGRunwayList( const string& file ) {
70 SG_LOG( SG_GENERAL, SG_DEBUG, "Reading runway list: " << file );
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 );
86 runways.insert(pair<const string, FGRunway>(rwy.id, rwy));
92 // Return reverse rwy number
95 static string GetReverseRunwayNo(string rwyno) {
96 // cout << "Original rwyno = " << rwyNo << '\n';
98 // standardize input number
99 string tmp = rwyno.substr(1, 1);
100 if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
103 SG_LOG( SG_GENERAL, SG_INFO,
104 "Standardising rwy number from " << tmp << " to " << rwyno );
108 int rn = atoi(rwyno.substr(0,2).c_str());
113 sprintf(buf, "%02i", rn);
114 if(rwyno.size() == 3) {
115 if(rwyno.substr(2,1) == "L") {
118 } else if (rwyno.substr(2,1) == "R") {
121 } else if (rwyno.substr(2,1) == "C") {
124 } else if (rwyno.substr(2,1) == "T") {
128 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
129 << rwyno << " passed to GetReverseRunwayNo(...)");
136 // search for the specified apt id (wierd!)
137 bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
138 runway_map_iterator pos;
140 pos = runways.lower_bound(aptid);
141 if ( pos != runways.end() ) {
151 // search for the specified apt id and runway no
152 bool FGRunwayList::search( const string& aptid, const string& rwyno,
155 string revrwyno = "";
156 string runwayno = rwyno;
157 if ( runwayno.length() ) {
158 // standardize input number
159 string tmp = runwayno.substr(1, 1);
160 if (( tmp == "L" || tmp == "R" || tmp == "C" )
161 || (runwayno.size() == 1))
164 runwayno = "0" + tmp;
165 SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from "
166 << tmp << " to " << runwayno );
168 revrwyno = GetReverseRunwayNo(runwayno);
171 runway_map_iterator pos;
172 for ( pos = runways.lower_bound( aptid );
173 pos != runways.upper_bound( aptid ); ++pos)
175 if ( pos->second.rwy_no == runwayno ) {
179 } else if ( pos->second.rwy_no == revrwyno ) {
180 // Search again with the other-end runway number.
181 // Remember we have to munge the heading and rwy_no
182 // results if this one matches
185 // NOTE - matching revrwyno implies that runwayno was
187 r->rwy_no = runwayno;
189 string tmp = r->end1_flags;
190 r->end1_flags = r->end2_flags;
201 FGRunway FGRunwayList::search( const string& aptid ) {
208 // Return the runway closest to a given heading
209 bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
212 string rwyNo = search(aptid, tgt_hdg);
213 return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
217 // Return the runway number of the runway closest to a given heading
218 string FGRunwayList::search( const string& aptid, const int tgt_hdg ) {
222 double found_dir = 0.0;
224 if ( !search( aptid, &tmp_r ) ) {
225 SG_LOG( SG_GENERAL, SG_ALERT,
226 "Failed to find " << aptid << " in database." );
231 double min_diff = 360.0;
233 while ( tmp_r.id == aptid ) {
237 diff = tgt_hdg - r.heading;
238 while ( diff < -180.0 ) { diff += 360.0; }
239 while ( diff > 180.0 ) { diff -= 360.0; }
241 // SG_LOG( SG_GENERAL, SG_INFO,
242 // "Runway " << r.rwy_no << " heading = " << r.heading <<
243 // " diff = " << diff );
244 if ( diff < min_diff ) {
251 diff = tgt_hdg - r.heading - 180.0;
252 while ( diff < -180.0 ) { diff += 360.0; }
253 while ( diff > 180.0 ) { diff -= 360.0; }
255 // SG_LOG( SG_GENERAL, SG_INFO,
256 // "Runway -" << r.rwy_no << " heading = " <<
257 // r.heading + 180.0 <<
258 // " diff = " << diff );
259 if ( diff < min_diff ) {
268 // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
269 // << " + " << found_dir );
271 // cout << "In search, rn = " << rn << endl;
272 if ( found_dir == 180 ) {
273 rn = GetReverseRunwayNo(rn);
274 //cout << "New rn = " << rn << '\n';
281 bool FGRunwayList::next( FGRunway* runway ) {
283 if ( current != runways.end() ) {
284 *runway = current->second;
292 FGRunway FGRunwayList::next() {
296 if ( current != runways.end() ) {
297 result = current->second;
305 FGRunwayList::~FGRunwayList( void ) {