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>
37 #include STL_FUNCTIONAL
38 #include STL_ALGORITHM
40 #include "runways.hxx"
42 SG_USING_NAMESPACE(std);
45 #define NDEBUG // MSVC needs this
55 #ifdef SG_HAVE_STD_INCLUDES
57 #elif defined( __BORLANDC__ ) || defined (__APPLE__)
63 SG_USING_STD(istream);
66 operator >> ( istream& in, FGRunway& a )
70 return in >> a.rwy_no >> a.lat >> a.lon >> a.heading >> a.length >> a.width
71 >> a.surface_flags >> a.end1_flags >> tmp >> tmp >> a.end2_flags
76 FGRunways::FGRunways( const string& file ) {
77 // open the specified database readonly
78 storage = new c4_Storage( file.c_str(), false );
80 if ( !storage->Strategy().IsValid() ) {
81 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
85 vRunway = new c4_View;
87 storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
93 // Return reverse rwy number
96 static string GetReverseRunwayNo(string rwyno) {
97 // cout << "Original rwyno = " << rwyNo << '\n';
99 // standardize input number
100 string tmp = rwyno.substr(1, 1);
101 if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
104 SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from " << tmp
105 << " to " << rwyno );
109 int rn = atoi(rwyno.substr(0,2).c_str());
114 sprintf(buf, "%02i", rn);
115 if(rwyno.size() == 3) {
116 if(rwyno.substr(2,1) == "L") {
119 } else if (rwyno.substr(2,1) == "R") {
122 } else if (rwyno.substr(2,1) == "C") {
126 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
127 << rwyno << " passed to GetReverseRunwayNo(...)");
134 // search for the specified apt id
135 bool FGRunways::search( const string& aptid, FGRunway* r ) {
136 c4_StringProp pID ("ID");
137 c4_StringProp pRwy ("Rwy");
138 c4_FloatProp pLon ("Longitude");
139 c4_FloatProp pLat ("Latitude");
140 c4_FloatProp pHdg ("Heading");
141 c4_FloatProp pLen ("Length");
142 c4_FloatProp pWid ("Width");
143 c4_StringProp pSurf ("SurfaceFlags");
144 c4_StringProp pEnd1 ("End1Flags");
145 c4_StringProp pEnd2 ("End2Flags");
147 int index = vRunway->Find(pID[aptid.c_str()]);
148 // cout << "index = " << index << endl;
154 next_index = index + 1;
156 c4_RowRef row = vRunway->GetAt(index);
158 r->id = (const char *) pID(row);
159 r->rwy_no = (const char *) pRwy(row);
160 r->lon = (double) pLon(row);
161 r->lat = (double) pLat(row);
162 r->heading = (double) pHdg(row);
163 r->length = (double) pLen(row);
164 r->width = (double) pWid(row);
165 r->surface_flags = (const char *) pSurf(row);
166 r->end1_flags = (const char *) pEnd1(row);
167 r->end2_flags = (const char *) pEnd2(row);
173 // search for the specified apt id and runway no
174 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
176 string runwayno = rwyno;
177 c4_StringProp pID ("ID");
178 c4_StringProp pRwy ("Rwy");
179 c4_FloatProp pLon ("Longitude");
180 c4_FloatProp pLat ("Latitude");
181 c4_FloatProp pHdg ("Heading");
182 c4_FloatProp pLen ("Length");
183 c4_FloatProp pWid ("Width");
184 c4_StringProp pSurf ("SurfaceFlags");
185 c4_StringProp pEnd1 ("End1Flags");
186 c4_StringProp pEnd2 ("End2Flags");
188 int index = vRunway->Find(pID[aptid.c_str()]);
189 // cout << "index = " << index << endl;
195 // standardize input number
196 string tmp = runwayno.substr(1, 1);
197 if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (runwayno.size() == 1)) {
199 runwayno = "0" + tmp;
200 SG_LOG(SG_GENERAL, SG_INFO, "Standardising rwy number from " << tmp
201 << " to " << runwayno );
204 c4_RowRef row = vRunway->GetAt(index);
205 string rowid = (const char *) pID(row);
206 string rowrwyno = (const char *) pRwy(row);
207 while ( rowid == aptid ) {
208 next_index = index + 1;
210 if ( rowrwyno == runwayno ) {
211 r->id = (const char *) pID(row);
212 r->rwy_no = (const char *) pRwy(row);
213 r->lon = (double) pLon(row);
214 r->lat = (double) pLat(row);
215 r->heading = (double) pHdg(row);
216 r->length = (double) pLen(row);
217 r->width = (double) pWid(row);
218 r->surface_flags = (const char *) pSurf(row);
219 r->end1_flags = (const char *) pEnd1(row);
220 r->end2_flags = (const char *) pEnd2(row);
225 // Search again with the other-end runway number
226 // Remember we have to munge the heading and rwy_no results if this one matches
227 rowrwyno = GetReverseRunwayNo(rowrwyno);
228 // cout << "New rowrwyno = " << rowrwyno << '\n';
229 if ( rowrwyno == runwayno ) {
230 r->id = (const char *) pID(row);
231 r->rwy_no = rowrwyno;
232 r->lon = (double) pLon(row);
233 r->lat = (double) pLat(row);
234 r->heading = (double) pHdg(row) + 180.0;
235 r->length = (double) pLen(row);
236 r->width = (double) pWid(row);
237 r->surface_flags = (const char *) pSurf(row);
238 r->end1_flags = (const char *) pEnd2(row);
239 r->end2_flags = (const char *) pEnd1(row);
240 // I've swapped the end flags as well
246 row = vRunway->GetAt(index);
247 rowid = (const char *) pID(row);
248 rowrwyno = (const char *) pRwy(row);
255 FGRunway FGRunways::search( const string& aptid ) {
262 // search for the specified id
263 bool FGRunways::next( FGRunway* r ) {
264 c4_StringProp pID ("ID");
265 c4_StringProp pRwy ("Rwy");
266 c4_FloatProp pLon ("Longitude");
267 c4_FloatProp pLat ("Latitude");
268 c4_FloatProp pHdg ("Heading");
269 c4_FloatProp pLen ("Length");
270 c4_FloatProp pWid ("Width");
271 c4_StringProp pSurf ("SurfaceFlags");
272 c4_StringProp pEnd1 ("End1Flags");
273 c4_StringProp pEnd2 ("End2Flags");
275 int size = vRunway->GetSize();
276 // cout << "total records = " << size << endl;
278 int index = next_index;
279 // cout << "index = " << index << endl;
281 if ( index == -1 || index >= size ) {
285 next_index = index + 1;
287 c4_RowRef row = vRunway->GetAt(index);
289 r->id = (const char *) pID(row);
290 r->rwy_no = (const char *) pRwy(row);
291 r->lon = (double) pLon(row);
292 r->lat = (double) pLat(row);
293 r->heading = (double) pHdg(row);
294 r->length = (double) pLen(row);
295 r->width = (double) pWid(row);
296 r->surface_flags = (const char *) pSurf(row);
297 r->end1_flags = (const char *) pEnd1(row);
298 r->end2_flags = (const char *) pEnd2(row);
304 // Return the runway closest to a given heading
305 bool FGRunways::search( const string& aptid, const int tgt_hdg,
308 string rwyNo = search(aptid, tgt_hdg);
309 return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
313 // Return the runway number of the runway closest to a given heading
314 string FGRunways::search( const string& aptid, const int tgt_hdg ) {
318 double found_dir = 0.0;
320 if ( !search( aptid, &tmp_r ) ) {
321 SG_LOG( SG_GENERAL, SG_ALERT,
322 "Failed to find " << aptid << " in database." );
327 double min_diff = 360.0;
329 while ( tmp_r.id == aptid ) {
333 diff = tgt_hdg - r.heading;
334 while ( diff < -180.0 ) { diff += 360.0; }
335 while ( diff > 180.0 ) { diff -= 360.0; }
337 // SG_LOG( SG_GENERAL, SG_INFO,
338 // "Runway " << r.rwy_no << " heading = " << r.heading <<
339 // " diff = " << diff );
340 if ( diff < min_diff ) {
347 diff = tgt_hdg - r.heading - 180.0;
348 while ( diff < -180.0 ) { diff += 360.0; }
349 while ( diff > 180.0 ) { diff -= 360.0; }
351 // SG_LOG( SG_GENERAL, SG_INFO,
352 // "Runway -" << r.rwy_no << " heading = " <<
353 // r.heading + 180.0 <<
354 // " diff = " << diff );
355 if ( diff < min_diff ) {
364 // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
365 // << " + " << found_dir );
367 // cout << "In search, rn = " << rn << endl;
368 if ( found_dir == 180 ) {
369 rn = GetReverseRunwayNo(rn);
370 //cout << "New rn = " << rn << '\n';
378 FGRunways::~FGRunways( void ) {
384 FGRunwaysUtil::FGRunwaysUtil() {
389 int FGRunwaysUtil::load( const string& file ) {
393 runways.erase( runways.begin(), runways.end() );
395 sg_gzifstream in( file );
396 if ( !in.is_open() ) {
397 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
401 // skip first line of file
403 in.getline( tmp, 2048 );
405 // read in each line of the file
411 while ( in.get(c) && c != '\0' ) {
415 } else if ( c == 'R' ) {
418 runways.push_back(r);
428 while ( ! in.eof() ) {
434 } else if ( c == 'R' ) {
437 // cout << apt_id << " " << r.rwy_no << endl;
438 runways.push_back(r);
451 // save the data in gdbm format
452 bool FGRunwaysUtil::dump_mk4( const string& file ) {
453 // open database for writing
454 c4_Storage storage( file.c_str(), true );
456 // need to do something about error handling here!
458 // define the properties
459 c4_StringProp pID ("ID");
460 c4_StringProp pRwy ("Rwy");
461 c4_FloatProp pLon ("Longitude");
462 c4_FloatProp pLat ("Latitude");
463 c4_FloatProp pHdg ("Heading");
464 c4_FloatProp pLen ("Length");
465 c4_FloatProp pWid ("Width");
466 c4_StringProp pSurf ("SurfaceFlags");
467 c4_StringProp pEnd1 ("End1Flags");
468 c4_StringProp pEnd2 ("End2Flags");
470 // Start with an empty view of the proper structure.
472 storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
476 iterator current = runways.begin();
477 const_iterator end = runways.end();
478 while ( current != end ) {
479 // add each runway record
480 pID (row) = current->id.c_str();
481 pRwy (row) = current->rwy_no.c_str();
482 pLon (row) = current->lon;
483 pLat (row) = current->lat;
484 pHdg (row) = current->heading;
485 pLen (row) = current->length;
486 pWid (row) = current->width;
487 pSurf (row) = current->surface_flags.c_str();
488 pEnd1 (row) = current->end1_flags.c_str();
489 pEnd2 (row) = current->end2_flags.c_str();
495 // commit our changes
503 // search for the specified id
505 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
507 const_iterator it = runways.find( FGRunway(id) );
508 if ( it != runways.end() )
521 FGRunwaysUtil::search( const string& id ) const
524 this->search( id, &a );
530 FGRunwaysUtil::~FGRunwaysUtil( void ) {