1 // runways.hxx -- 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( SG_HAVE_NATIVE_SGI_COMPILERS )
58 # include <iostream.h>
59 #elif defined( __BORLANDC__ ) || defined (__APPLE__)
65 #if ! defined( SG_HAVE_NATIVE_SGI_COMPILERS )
66 SG_USING_STD(istream);
70 operator >> ( istream& in, FGRunway& a )
74 return in >> a.rwy_no >> a.lat >> a.lon >> a.heading >> a.length >> a.width
75 >> a.surface_flags >> a.end1_flags >> tmp >> tmp >> a.end2_flags
80 FGRunways::FGRunways( const string& file ) {
81 // open the specified database readonly
82 storage = new c4_Storage( file.c_str(), false );
84 if ( !storage->Strategy().IsValid() ) {
85 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
89 vRunway = new c4_View;
91 storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
97 // search for the specified apt id
98 bool FGRunways::search( const string& aptid, FGRunway* r ) {
99 c4_StringProp pID ("ID");
100 c4_StringProp pRwy ("Rwy");
101 c4_FloatProp pLon ("Longitude");
102 c4_FloatProp pLat ("Latitude");
103 c4_FloatProp pHdg ("Heading");
104 c4_FloatProp pLen ("Length");
105 c4_FloatProp pWid ("Width");
106 c4_StringProp pSurf ("SurfaceFlags");
107 c4_StringProp pEnd1 ("End1Flags");
108 c4_StringProp pEnd2 ("End2Flags");
110 int index = vRunway->Find(pID[aptid.c_str()]);
111 // cout << "index = " << index << endl;
117 next_index = index + 1;
119 c4_RowRef row = vRunway->GetAt(index);
121 r->id = (const char *) pID(row);
122 r->rwy_no = (const char *) pRwy(row);
123 r->lon = (double) pLon(row);
124 r->lat = (double) pLat(row);
125 r->heading = (double) pHdg(row);
126 r->length = (double) pLen(row);
127 r->width = (double) pWid(row);
128 r->surface_flags = (const char *) pSurf(row);
129 r->end1_flags = (const char *) pEnd1(row);
130 r->end2_flags = (const char *) pEnd2(row);
136 // search for the specified apt id and runway no
137 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
139 c4_StringProp pID ("ID");
140 c4_StringProp pRwy ("Rwy");
141 c4_FloatProp pLon ("Longitude");
142 c4_FloatProp pLat ("Latitude");
143 c4_FloatProp pHdg ("Heading");
144 c4_FloatProp pLen ("Length");
145 c4_FloatProp pWid ("Width");
146 c4_StringProp pSurf ("SurfaceFlags");
147 c4_StringProp pEnd1 ("End1Flags");
148 c4_StringProp pEnd2 ("End2Flags");
150 int index = vRunway->Find(pID[aptid.c_str()]);
151 // cout << "index = " << index << endl;
157 c4_RowRef row = vRunway->GetAt(index);
158 string rowid = (const char *) pID(row);
159 string rowrwyno = (const char *) pRwy(row);
160 while ( rowid == aptid ) {
161 next_index = index + 1;
163 if ( rowrwyno == rwyno ) {
164 r->id = (const char *) pID(row);
165 r->rwy_no = (const char *) pRwy(row);
166 r->lon = (double) pLon(row);
167 r->lat = (double) pLat(row);
168 r->heading = (double) pHdg(row);
169 r->length = (double) pLen(row);
170 r->width = (double) pWid(row);
171 r->surface_flags = (const char *) pSurf(row);
172 r->end1_flags = (const char *) pEnd1(row);
173 r->end2_flags = (const char *) pEnd2(row);
179 c4_RowRef row = vRunway->GetAt(index);
180 string rowid = (const char *) pID(row);
181 string rowrwyno = (const char *) pRwy(row);
188 FGRunway FGRunways::search( const string& aptid ) {
195 // search for the specified id
196 bool FGRunways::next( FGRunway* r ) {
197 c4_StringProp pID ("ID");
198 c4_StringProp pRwy ("Rwy");
199 c4_FloatProp pLon ("Longitude");
200 c4_FloatProp pLat ("Latitude");
201 c4_FloatProp pHdg ("Heading");
202 c4_FloatProp pLen ("Length");
203 c4_FloatProp pWid ("Width");
204 c4_StringProp pSurf ("SurfaceFlags");
205 c4_StringProp pEnd1 ("End1Flags");
206 c4_StringProp pEnd2 ("End2Flags");
208 int size = vRunway->GetSize();
209 // cout << "total records = " << size << endl;
211 int index = next_index;
212 // cout << "index = " << index << endl;
214 if ( index == -1 || index >= size ) {
218 next_index = index + 1;
220 c4_RowRef row = vRunway->GetAt(index);
222 r->id = (const char *) pID(row);
223 r->rwy_no = (const char *) pRwy(row);
224 r->lon = (double) pLon(row);
225 r->lat = (double) pLat(row);
226 r->heading = (double) pHdg(row);
227 r->length = (double) pLen(row);
228 r->width = (double) pWid(row);
229 r->surface_flags = (const char *) pSurf(row);
230 r->end1_flags = (const char *) pEnd1(row);
231 r->end2_flags = (const char *) pEnd2(row);
237 // Return the runway closest to a given heading
238 bool FGRunways::search( const string& aptid, const int tgt_hdg,
242 double found_dir = 0.0;
244 if ( !search( aptid, &r ) ) {
245 SG_LOG( SG_GENERAL, SG_ALERT,
246 "Failed to find " << aptid << " in database." );
251 double min_diff = 360.0;
253 while ( r.id == aptid ) {
255 diff = tgt_hdg - r.heading;
256 while ( diff < -180.0 ) { diff += 360.0; }
257 while ( diff > 180.0 ) { diff -= 360.0; }
259 // SG_LOG( SG_GENERAL, SG_INFO,
260 // "Runway " << r.rwy_no << " heading = " << r.heading <<
261 // " diff = " << diff );
262 if ( diff < min_diff ) {
269 diff = tgt_hdg - r.heading - 180.0;
270 while ( diff < -180.0 ) { diff += 360.0; }
271 while ( diff > 180.0 ) { diff -= 360.0; }
273 // SG_LOG( SG_GENERAL, SG_INFO,
274 // "Runway -" << r.rwy_no << " heading = " <<
275 // r.heading + 180.0 <<
276 // " diff = " << diff );
277 if ( diff < min_diff ) {
286 // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << runway->rwy_no
287 // << " + " << found_dir );
289 double heading = runway->heading + found_dir;
290 while ( heading >= 360.0 ) { heading -= 360.0; }
291 runway->heading = heading;
297 // Return the runway number of the runway closest to a given heading
298 string FGRunways::search( const string& aptid, const int tgt_hdg ) {
301 double found_dir = 0.0;
303 if ( !search( aptid, &r ) ) {
304 SG_LOG( SG_GENERAL, SG_ALERT,
305 "Failed to find " << aptid << " in database." );
310 double min_diff = 360.0;
312 while ( r.id == aptid ) {
314 diff = tgt_hdg - r.heading;
315 while ( diff < -180.0 ) { diff += 360.0; }
316 while ( diff > 180.0 ) { diff -= 360.0; }
318 // SG_LOG( SG_GENERAL, SG_INFO,
319 // "Runway " << r.rwy_no << " heading = " << r.heading <<
320 // " diff = " << diff );
321 if ( diff < min_diff ) {
328 diff = tgt_hdg - r.heading - 180.0;
329 while ( diff < -180.0 ) { diff += 360.0; }
330 while ( diff > 180.0 ) { diff -= 360.0; }
332 // SG_LOG( SG_GENERAL, SG_INFO,
333 // "Runway -" << r.rwy_no << " heading = " <<
334 // r.heading + 180.0 <<
335 // " diff = " << diff );
336 if ( diff < min_diff ) {
345 // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
346 // << " + " << found_dir );
348 // cout << "In search, rn = " << rn << endl;
349 if ( found_dir == 180 ) {
350 int irn = atoi(rn.c_str());
355 char buf[4]; // 2 chars + string terminator + 1 for safety
356 sprintf(buf, "%i", irn);
365 FGRunways::~FGRunways( void ) {
371 FGRunwaysUtil::FGRunwaysUtil() {
376 int FGRunwaysUtil::load( const string& file ) {
380 runways.erase( runways.begin(), runways.end() );
382 sg_gzifstream in( file );
383 if ( !in.is_open() ) {
384 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
388 // skip first line of file
390 in.getline( tmp, 2048 );
392 // read in each line of the file
398 while ( in.get(c) && c != '\0' ) {
402 } else if ( c == 'R' ) {
405 runways.push_back(r);
415 while ( ! in.eof() ) {
421 } else if ( c == 'R' ) {
424 // cout << apt_id << " " << r.rwy_no << endl;
425 runways.push_back(r);
438 // save the data in gdbm format
439 bool FGRunwaysUtil::dump_mk4( const string& file ) {
440 // open database for writing
441 c4_Storage storage( file.c_str(), true );
443 // need to do something about error handling here!
445 // define the properties
446 c4_StringProp pID ("ID");
447 c4_StringProp pRwy ("Rwy");
448 c4_FloatProp pLon ("Longitude");
449 c4_FloatProp pLat ("Latitude");
450 c4_FloatProp pHdg ("Heading");
451 c4_FloatProp pLen ("Length");
452 c4_FloatProp pWid ("Width");
453 c4_StringProp pSurf ("SurfaceFlags");
454 c4_StringProp pEnd1 ("End1Flags");
455 c4_StringProp pEnd2 ("End2Flags");
457 // Start with an empty view of the proper structure.
459 storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
463 iterator current = runways.begin();
464 const_iterator end = runways.end();
465 while ( current != end ) {
466 // add each runway record
467 pID (row) = current->id.c_str();
468 pRwy (row) = current->rwy_no.c_str();
469 pLon (row) = current->lon;
470 pLat (row) = current->lat;
471 pHdg (row) = current->heading;
472 pLen (row) = current->length;
473 pWid (row) = current->width;
474 pSurf (row) = current->surface_flags.c_str();
475 pEnd1 (row) = current->end1_flags.c_str();
476 pEnd2 (row) = current->end2_flags.c_str();
482 // commit our changes
490 // search for the specified id
492 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
494 const_iterator it = runways.find( FGRunway(id) );
495 if ( it != runways.end() )
508 FGRunwaysUtil::search( const string& id ) const
511 this->search( id, &a );
517 FGRunwaysUtil::~FGRunwaysUtil( void ) {