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 <stdio.h> // sprintf()
30 #include <simgear/compiler.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/misc/sgstream.hxx>
36 #include STL_FUNCTIONAL
37 #include STL_ALGORITHM
39 #include "runways.hxx"
41 SG_USING_NAMESPACE(std);
44 FGRunway::FGRunway() {
48 FGRunway::~FGRunway() {
52 FGRunways::FGRunways( const string& file ) {
53 // open the specified database readonly
54 storage = new c4_Storage( file.c_str(), false );
56 if ( !storage->Strategy().IsValid() ) {
57 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
61 vRunway = new c4_View;
63 storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
69 // search for the specified apt id
70 bool FGRunways::search( const string& aptid, FGRunway* r ) {
71 c4_StringProp pID ("ID");
72 c4_StringProp pRwy ("Rwy");
73 c4_FloatProp pLon ("Longitude");
74 c4_FloatProp pLat ("Latitude");
75 c4_FloatProp pHdg ("Heading");
76 c4_FloatProp pLen ("Length");
77 c4_FloatProp pWid ("Width");
78 c4_StringProp pSurf ("SurfaceFlags");
79 c4_StringProp pEnd1 ("End1Flags");
80 c4_StringProp pEnd2 ("End2Flags");
82 int index = vRunway->Find(pID[aptid.c_str()]);
83 cout << "index = " << index << endl;
89 next_index = index + 1;
91 c4_RowRef row = vRunway->GetAt(index);
93 r->id = (const char *) pID(row);
94 r->rwy_no = (const char *) pRwy(row);
95 r->lon = (double) pLon(row);
96 r->lat = (double) pLat(row);
97 r->heading = (double) pHdg(row);
98 r->length = (double) pLen(row);
99 r->width = (double) pWid(row);
100 r->surface_flags = (const char *) pSurf(row);
101 r->end1_flags = (const char *) pEnd1(row);
102 r->end2_flags = (const char *) pEnd2(row);
108 // search for the specified apt id and runway no
109 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
111 c4_StringProp pID ("ID");
112 c4_StringProp pRwy ("Rwy");
113 c4_FloatProp pLon ("Longitude");
114 c4_FloatProp pLat ("Latitude");
115 c4_FloatProp pHdg ("Heading");
116 c4_FloatProp pLen ("Length");
117 c4_FloatProp pWid ("Width");
118 c4_StringProp pSurf ("SurfaceFlags");
119 c4_StringProp pEnd1 ("End1Flags");
120 c4_StringProp pEnd2 ("End2Flags");
122 int index = vRunway->Find(pID[aptid.c_str()]);
123 cout << "index = " << index << endl;
129 c4_RowRef row = vRunway->GetAt(index);
130 string rowid = (const char *) pID(row);
131 string rowrwyno = (const char *) pRwy(row);
132 while ( rowid == aptid ) {
133 next_index = index + 1;
135 if ( rowrwyno == rwyno ) {
136 r->id = (const char *) pID(row);
137 r->rwy_no = (const char *) pRwy(row);
138 r->lon = (double) pLon(row);
139 r->lat = (double) pLat(row);
140 r->heading = (double) pHdg(row);
141 r->length = (double) pLen(row);
142 r->width = (double) pWid(row);
143 r->surface_flags = (const char *) pSurf(row);
144 r->end1_flags = (const char *) pEnd1(row);
145 r->end2_flags = (const char *) pEnd2(row);
151 c4_RowRef row = vRunway->GetAt(index);
152 string rowid = (const char *) pID(row);
153 string rowrwyno = (const char *) pRwy(row);
160 FGRunway FGRunways::search( const string& aptid ) {
167 // search for the specified id
168 bool FGRunways::next( FGRunway* r ) {
169 c4_StringProp pID ("ID");
170 c4_StringProp pRwy ("Rwy");
171 c4_FloatProp pLon ("Longitude");
172 c4_FloatProp pLat ("Latitude");
173 c4_FloatProp pHdg ("Heading");
174 c4_FloatProp pLen ("Length");
175 c4_FloatProp pWid ("Width");
176 c4_StringProp pSurf ("SurfaceFlags");
177 c4_StringProp pEnd1 ("End1Flags");
178 c4_StringProp pEnd2 ("End2Flags");
180 int size = vRunway->GetSize();
181 // cout << "total records = " << size << endl;
183 int index = next_index;
184 // cout << "index = " << index << endl;
186 if ( index == -1 || index >= size ) {
190 next_index = index + 1;
192 c4_RowRef row = vRunway->GetAt(index);
194 r->id = (const char *) pID(row);
195 r->rwy_no = (const char *) pRwy(row);
196 r->lon = (double) pLon(row);
197 r->lat = (double) pLat(row);
198 r->heading = (double) pHdg(row);
199 r->length = (double) pLen(row);
200 r->width = (double) pWid(row);
201 r->surface_flags = (const char *) pSurf(row);
202 r->end1_flags = (const char *) pEnd1(row);
203 r->end2_flags = (const char *) pEnd2(row);
209 // Return the runway closest to a given heading
210 bool FGRunways::search( const string& aptid, const int tgt_hdg,
214 double found_dir = 0.0;
216 if ( !search( aptid, &r ) ) {
217 SG_LOG( SG_GENERAL, SG_ALERT,
218 "Failed to find " << aptid << " in database." );
223 double min_diff = 360.0;
225 while ( r.id == aptid ) {
227 diff = tgt_hdg - r.heading;
228 while ( diff < -180.0 ) { diff += 360.0; }
229 while ( diff > 180.0 ) { diff -= 360.0; }
231 // SG_LOG( SG_GENERAL, SG_INFO,
232 // "Runway " << r.rwy_no << " heading = " << r.heading <<
233 // " diff = " << diff );
234 if ( diff < min_diff ) {
241 diff = tgt_hdg - r.heading - 180.0;
242 while ( diff < -180.0 ) { diff += 360.0; }
243 while ( diff > 180.0 ) { diff -= 360.0; }
245 // SG_LOG( SG_GENERAL, SG_INFO,
246 // "Runway -" << r.rwy_no << " heading = " <<
247 // r.heading + 180.0 <<
248 // " diff = " << diff );
249 if ( diff < min_diff ) {
258 // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << runway->rwy_no
259 // << " + " << found_dir );
261 double heading = runway->heading + found_dir;
262 while ( heading >= 360.0 ) { heading -= 360.0; }
263 runway->heading = heading;
269 // Return the runway number of the runway closest to a given heading
270 string FGRunways::search( const string& aptid, const int tgt_hdg ) {
273 double found_dir = 0.0;
275 if ( !search( aptid, &r ) ) {
276 SG_LOG( SG_GENERAL, SG_ALERT,
277 "Failed to find " << aptid << " in database." );
282 double min_diff = 360.0;
284 while ( r.id == aptid ) {
286 diff = tgt_hdg - r.heading;
287 while ( diff < -180.0 ) { diff += 360.0; }
288 while ( diff > 180.0 ) { diff -= 360.0; }
290 // SG_LOG( SG_GENERAL, SG_INFO,
291 // "Runway " << r.rwy_no << " heading = " << r.heading <<
292 // " diff = " << diff );
293 if ( diff < min_diff ) {
300 diff = tgt_hdg - r.heading - 180.0;
301 while ( diff < -180.0 ) { diff += 360.0; }
302 while ( diff > 180.0 ) { diff -= 360.0; }
304 // SG_LOG( SG_GENERAL, SG_INFO,
305 // "Runway -" << r.rwy_no << " heading = " <<
306 // r.heading + 180.0 <<
307 // " diff = " << diff );
308 if ( diff < min_diff ) {
317 // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
318 // << " + " << found_dir );
320 // cout << "In search, rn = " << rn << endl;
321 if ( found_dir == 180 ) {
322 int irn = atoi(rn.c_str());
327 char buf[4]; // 2 chars + string terminator + 1 for safety
328 sprintf(buf, "%i", irn);
337 FGRunways::~FGRunways( void ) {
343 FGRunwaysUtil::FGRunwaysUtil() {
348 int FGRunwaysUtil::load( const string& file ) {
352 runways.erase( runways.begin(), runways.end() );
354 sg_gzifstream in( file );
355 if ( !in.is_open() ) {
356 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
360 // skip first line of file
362 in.getline( tmp, 2048 );
364 // read in each line of the file
370 while ( in.get(c) && c != '\0' ) {
374 } else if ( c == 'R' ) {
377 runways.push_back(r);
387 while ( ! in.eof() ) {
393 } else if ( c == 'R' ) {
396 // cout << apt_id << " " << r.rwy_no << endl;
397 runways.push_back(r);
410 // save the data in gdbm format
411 bool FGRunwaysUtil::dump_mk4( const string& file ) {
413 // open database for writing
414 c4_Storage storage( file.c_str(), true );
416 // need to do something about error handling here!
418 // define the properties
419 c4_StringProp pID ("ID");
420 c4_StringProp pRwy ("Rwy");
421 c4_FloatProp pLon ("Longitude");
422 c4_FloatProp pLat ("Latitude");
423 c4_FloatProp pHdg ("Heading");
424 c4_FloatProp pLen ("Length");
425 c4_FloatProp pWid ("Width");
426 c4_StringProp pSurf ("SurfaceFlags");
427 c4_StringProp pEnd1 ("End1Flags");
428 c4_StringProp pEnd2 ("End2Flags");
430 // Start with an empty view of the proper structure.
432 storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
436 iterator current = runways.begin();
437 const_iterator end = runways.end();
438 while ( current != end ) {
439 // add each runway record
440 pID (row) = current->id.c_str();
441 pRwy (row) = current->rwy_no.c_str();
442 pLon (row) = current->lon;
443 pLat (row) = current->lat;
444 pHdg (row) = current->heading;
445 pLen (row) = current->length;
446 pWid (row) = current->width;
447 pSurf (row) = current->surface_flags.c_str();
448 pEnd1 (row) = current->end1_flags.c_str();
449 pEnd2 (row) = current->end2_flags.c_str();
455 // commit our changes
463 // search for the specified id
465 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
467 const_iterator it = runways.find( FGRunway(id) );
468 if ( it != runways.end() )
481 FGRunwaysUtil::search( const string& id ) const
484 this->search( id, &a );
490 FGRunwaysUtil::~FGRunwaysUtil( void ) {