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( __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 // search for the specified apt id
94 bool FGRunways::search( const string& aptid, FGRunway* r ) {
95 c4_StringProp pID ("ID");
96 c4_StringProp pRwy ("Rwy");
97 c4_FloatProp pLon ("Longitude");
98 c4_FloatProp pLat ("Latitude");
99 c4_FloatProp pHdg ("Heading");
100 c4_FloatProp pLen ("Length");
101 c4_FloatProp pWid ("Width");
102 c4_StringProp pSurf ("SurfaceFlags");
103 c4_StringProp pEnd1 ("End1Flags");
104 c4_StringProp pEnd2 ("End2Flags");
106 int index = vRunway->Find(pID[aptid.c_str()]);
107 // cout << "index = " << index << endl;
113 next_index = index + 1;
115 c4_RowRef row = vRunway->GetAt(index);
117 r->id = (const char *) pID(row);
118 r->rwy_no = (const char *) pRwy(row);
119 r->lon = (double) pLon(row);
120 r->lat = (double) pLat(row);
121 r->heading = (double) pHdg(row);
122 r->length = (double) pLen(row);
123 r->width = (double) pWid(row);
124 r->surface_flags = (const char *) pSurf(row);
125 r->end1_flags = (const char *) pEnd1(row);
126 r->end2_flags = (const char *) pEnd2(row);
132 // search for the specified apt id and runway no
133 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
135 c4_StringProp pID ("ID");
136 c4_StringProp pRwy ("Rwy");
137 c4_FloatProp pLon ("Longitude");
138 c4_FloatProp pLat ("Latitude");
139 c4_FloatProp pHdg ("Heading");
140 c4_FloatProp pLen ("Length");
141 c4_FloatProp pWid ("Width");
142 c4_StringProp pSurf ("SurfaceFlags");
143 c4_StringProp pEnd1 ("End1Flags");
144 c4_StringProp pEnd2 ("End2Flags");
146 int index = vRunway->Find(pID[aptid.c_str()]);
147 // cout << "index = " << index << endl;
153 c4_RowRef row = vRunway->GetAt(index);
154 string rowid = (const char *) pID(row);
155 string rowrwyno = (const char *) pRwy(row);
156 while ( rowid == aptid ) {
157 next_index = index + 1;
159 if ( rowrwyno == rwyno ) {
160 r->id = (const char *) pID(row);
161 r->rwy_no = (const char *) pRwy(row);
162 r->lon = (double) pLon(row);
163 r->lat = (double) pLat(row);
164 r->heading = (double) pHdg(row);
165 r->length = (double) pLen(row);
166 r->width = (double) pWid(row);
167 r->surface_flags = (const char *) pSurf(row);
168 r->end1_flags = (const char *) pEnd1(row);
169 r->end2_flags = (const char *) pEnd2(row);
175 c4_RowRef row = vRunway->GetAt(index);
176 string rowid = (const char *) pID(row);
177 string rowrwyno = (const char *) pRwy(row);
184 FGRunway FGRunways::search( const string& aptid ) {
191 // search for the specified id
192 bool FGRunways::next( FGRunway* r ) {
193 c4_StringProp pID ("ID");
194 c4_StringProp pRwy ("Rwy");
195 c4_FloatProp pLon ("Longitude");
196 c4_FloatProp pLat ("Latitude");
197 c4_FloatProp pHdg ("Heading");
198 c4_FloatProp pLen ("Length");
199 c4_FloatProp pWid ("Width");
200 c4_StringProp pSurf ("SurfaceFlags");
201 c4_StringProp pEnd1 ("End1Flags");
202 c4_StringProp pEnd2 ("End2Flags");
204 int size = vRunway->GetSize();
205 // cout << "total records = " << size << endl;
207 int index = next_index;
208 // cout << "index = " << index << endl;
210 if ( index == -1 || index >= size ) {
214 next_index = index + 1;
216 c4_RowRef row = vRunway->GetAt(index);
218 r->id = (const char *) pID(row);
219 r->rwy_no = (const char *) pRwy(row);
220 r->lon = (double) pLon(row);
221 r->lat = (double) pLat(row);
222 r->heading = (double) pHdg(row);
223 r->length = (double) pLen(row);
224 r->width = (double) pWid(row);
225 r->surface_flags = (const char *) pSurf(row);
226 r->end1_flags = (const char *) pEnd1(row);
227 r->end2_flags = (const char *) pEnd2(row);
233 // Return the runway closest to a given heading
234 bool FGRunways::search( const string& aptid, const int tgt_hdg,
238 double found_dir = 0.0;
240 if ( !search( aptid, &r ) ) {
241 SG_LOG( SG_GENERAL, SG_ALERT,
242 "Failed to find " << aptid << " in database." );
247 double min_diff = 360.0;
249 while ( r.id == aptid ) {
251 diff = tgt_hdg - r.heading;
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 = " << r.heading <<
257 // " diff = " << diff );
258 if ( diff < min_diff ) {
265 diff = tgt_hdg - r.heading - 180.0;
266 while ( diff < -180.0 ) { diff += 360.0; }
267 while ( diff > 180.0 ) { diff -= 360.0; }
269 // SG_LOG( SG_GENERAL, SG_INFO,
270 // "Runway -" << r.rwy_no << " heading = " <<
271 // r.heading + 180.0 <<
272 // " diff = " << diff );
273 if ( diff < min_diff ) {
282 // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << runway->rwy_no
283 // << " + " << found_dir );
285 double heading = runway->heading + found_dir;
286 while ( heading >= 360.0 ) { heading -= 360.0; }
287 runway->heading = heading;
293 // Return the runway number of the runway closest to a given heading
294 string FGRunways::search( const string& aptid, const int tgt_hdg ) {
297 double found_dir = 0.0;
299 if ( !search( aptid, &r ) ) {
300 SG_LOG( SG_GENERAL, SG_ALERT,
301 "Failed to find " << aptid << " in database." );
306 double min_diff = 360.0;
308 while ( r.id == aptid ) {
310 diff = tgt_hdg - r.heading;
311 while ( diff < -180.0 ) { diff += 360.0; }
312 while ( diff > 180.0 ) { diff -= 360.0; }
314 // SG_LOG( SG_GENERAL, SG_INFO,
315 // "Runway " << r.rwy_no << " heading = " << r.heading <<
316 // " diff = " << diff );
317 if ( diff < min_diff ) {
324 diff = tgt_hdg - r.heading - 180.0;
325 while ( diff < -180.0 ) { diff += 360.0; }
326 while ( diff > 180.0 ) { diff -= 360.0; }
328 // SG_LOG( SG_GENERAL, SG_INFO,
329 // "Runway -" << r.rwy_no << " heading = " <<
330 // r.heading + 180.0 <<
331 // " diff = " << diff );
332 if ( diff < min_diff ) {
341 // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
342 // << " + " << found_dir );
344 // cout << "In search, rn = " << rn << endl;
345 if ( found_dir == 180 ) {
346 int irn = atoi(rn.c_str());
351 char buf[4]; // 2 chars + string terminator + 1 for safety
352 sprintf(buf, "%i", irn);
361 FGRunways::~FGRunways( void ) {
367 FGRunwaysUtil::FGRunwaysUtil() {
372 int FGRunwaysUtil::load( const string& file ) {
376 runways.erase( runways.begin(), runways.end() );
378 sg_gzifstream in( file );
379 if ( !in.is_open() ) {
380 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
384 // skip first line of file
386 in.getline( tmp, 2048 );
388 // read in each line of the file
394 while ( in.get(c) && c != '\0' ) {
398 } else if ( c == 'R' ) {
401 runways.push_back(r);
411 while ( ! in.eof() ) {
417 } else if ( c == 'R' ) {
420 // cout << apt_id << " " << r.rwy_no << endl;
421 runways.push_back(r);
434 // save the data in gdbm format
435 bool FGRunwaysUtil::dump_mk4( const string& file ) {
436 // open database for writing
437 c4_Storage storage( file.c_str(), true );
439 // need to do something about error handling here!
441 // define the properties
442 c4_StringProp pID ("ID");
443 c4_StringProp pRwy ("Rwy");
444 c4_FloatProp pLon ("Longitude");
445 c4_FloatProp pLat ("Latitude");
446 c4_FloatProp pHdg ("Heading");
447 c4_FloatProp pLen ("Length");
448 c4_FloatProp pWid ("Width");
449 c4_StringProp pSurf ("SurfaceFlags");
450 c4_StringProp pEnd1 ("End1Flags");
451 c4_StringProp pEnd2 ("End2Flags");
453 // Start with an empty view of the proper structure.
455 storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
459 iterator current = runways.begin();
460 const_iterator end = runways.end();
461 while ( current != end ) {
462 // add each runway record
463 pID (row) = current->id.c_str();
464 pRwy (row) = current->rwy_no.c_str();
465 pLon (row) = current->lon;
466 pLat (row) = current->lat;
467 pHdg (row) = current->heading;
468 pLen (row) = current->length;
469 pWid (row) = current->width;
470 pSurf (row) = current->surface_flags.c_str();
471 pEnd1 (row) = current->end1_flags.c_str();
472 pEnd2 (row) = current->end2_flags.c_str();
478 // commit our changes
486 // search for the specified id
488 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
490 const_iterator it = runways.find( FGRunway(id) );
491 if ( it != runways.end() )
504 FGRunwaysUtil::search( const string& id ) const
507 this->search( id, &a );
513 FGRunwaysUtil::~FGRunwaysUtil( void ) {