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 c4_RowRef row = vRunway->GetAt(index);
150 // cout << "index = " << index " row = " << row << endl;
152 // explicitly check if we got what we were asking for
153 // because metakit is canse insensitive!
154 if ( strcmp(aptid.c_str(), pID(row)) ) {
158 next_index = index + 1;
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 // search for the specified apt id and runway no
176 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
178 string runwayno = rwyno;
179 c4_StringProp pID ("ID");
180 c4_StringProp pRwy ("Rwy");
181 c4_FloatProp pLon ("Longitude");
182 c4_FloatProp pLat ("Latitude");
183 c4_FloatProp pHdg ("Heading");
184 c4_FloatProp pLen ("Length");
185 c4_FloatProp pWid ("Width");
186 c4_StringProp pSurf ("SurfaceFlags");
187 c4_StringProp pEnd1 ("End1Flags");
188 c4_StringProp pEnd2 ("End2Flags");
190 int index = vRunway->Find(pID[aptid.c_str()]);
191 c4_RowRef row = vRunway->GetAt(index);
192 // cout << "index = " << index " row = " << row << endl;
194 // explicitly check if we got what we were asking for
195 // because metakit is canse insensitive!
196 if ( strcmp(aptid.c_str(), pID(row)) ) {
200 // standardize input number
201 string tmp = runwayno.substr(1, 1);
202 if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (runwayno.size() == 1)) {
204 runwayno = "0" + tmp;
205 SG_LOG(SG_GENERAL, SG_INFO, "Standardising rwy number from " << tmp
206 << " to " << runwayno );
209 string rowid = (const char *) pID(row);
210 string rowrwyno = (const char *) pRwy(row);
211 while ( rowid == aptid ) {
212 next_index = index + 1;
214 if ( rowrwyno == runwayno ) {
215 r->id = (const char *) pID(row);
216 r->rwy_no = (const char *) pRwy(row);
217 r->lon = (double) pLon(row);
218 r->lat = (double) pLat(row);
219 r->heading = (double) pHdg(row);
220 r->length = (double) pLen(row);
221 r->width = (double) pWid(row);
222 r->surface_flags = (const char *) pSurf(row);
223 r->end1_flags = (const char *) pEnd1(row);
224 r->end2_flags = (const char *) pEnd2(row);
229 // Search again with the other-end runway number
230 // Remember we have to munge the heading and rwy_no results if this one matches
231 rowrwyno = GetReverseRunwayNo(rowrwyno);
232 // cout << "New rowrwyno = " << rowrwyno << '\n';
233 if ( rowrwyno == runwayno ) {
234 r->id = (const char *) pID(row);
235 r->rwy_no = rowrwyno;
236 r->lon = (double) pLon(row);
237 r->lat = (double) pLat(row);
238 r->heading = (double) pHdg(row) + 180.0;
239 r->length = (double) pLen(row);
240 r->width = (double) pWid(row);
241 r->surface_flags = (const char *) pSurf(row);
242 r->end1_flags = (const char *) pEnd2(row);
243 r->end2_flags = (const char *) pEnd1(row);
244 // I've swapped the end flags as well
250 row = vRunway->GetAt(index);
251 rowid = (const char *) pID(row);
252 rowrwyno = (const char *) pRwy(row);
259 FGRunway FGRunways::search( const string& aptid ) {
266 // search for the specified id
267 bool FGRunways::next( FGRunway* r ) {
268 c4_StringProp pID ("ID");
269 c4_StringProp pRwy ("Rwy");
270 c4_FloatProp pLon ("Longitude");
271 c4_FloatProp pLat ("Latitude");
272 c4_FloatProp pHdg ("Heading");
273 c4_FloatProp pLen ("Length");
274 c4_FloatProp pWid ("Width");
275 c4_StringProp pSurf ("SurfaceFlags");
276 c4_StringProp pEnd1 ("End1Flags");
277 c4_StringProp pEnd2 ("End2Flags");
279 int size = vRunway->GetSize();
280 // cout << "total records = " << size << endl;
282 int index = next_index;
283 // cout << "index = " << index << endl;
285 if ( index == -1 || index >= size ) {
289 next_index = index + 1;
291 c4_RowRef row = vRunway->GetAt(index);
293 r->id = (const char *) pID(row);
294 r->rwy_no = (const char *) pRwy(row);
295 r->lon = (double) pLon(row);
296 r->lat = (double) pLat(row);
297 r->heading = (double) pHdg(row);
298 r->length = (double) pLen(row);
299 r->width = (double) pWid(row);
300 r->surface_flags = (const char *) pSurf(row);
301 r->end1_flags = (const char *) pEnd1(row);
302 r->end2_flags = (const char *) pEnd2(row);
308 // Return the runway closest to a given heading
309 bool FGRunways::search( const string& aptid, const int tgt_hdg,
312 string rwyNo = search(aptid, tgt_hdg);
313 return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
317 // Return the runway number of the runway closest to a given heading
318 string FGRunways::search( const string& aptid, const int tgt_hdg ) {
322 double found_dir = 0.0;
324 if ( !search( aptid, &tmp_r ) ) {
325 SG_LOG( SG_GENERAL, SG_ALERT,
326 "Failed to find " << aptid << " in database." );
331 double min_diff = 360.0;
333 while ( tmp_r.id == aptid ) {
337 diff = tgt_hdg - r.heading;
338 while ( diff < -180.0 ) { diff += 360.0; }
339 while ( diff > 180.0 ) { diff -= 360.0; }
341 // SG_LOG( SG_GENERAL, SG_INFO,
342 // "Runway " << r.rwy_no << " heading = " << r.heading <<
343 // " diff = " << diff );
344 if ( diff < min_diff ) {
351 diff = tgt_hdg - r.heading - 180.0;
352 while ( diff < -180.0 ) { diff += 360.0; }
353 while ( diff > 180.0 ) { diff -= 360.0; }
355 // SG_LOG( SG_GENERAL, SG_INFO,
356 // "Runway -" << r.rwy_no << " heading = " <<
357 // r.heading + 180.0 <<
358 // " diff = " << diff );
359 if ( diff < min_diff ) {
368 // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
369 // << " + " << found_dir );
371 // cout << "In search, rn = " << rn << endl;
372 if ( found_dir == 180 ) {
373 rn = GetReverseRunwayNo(rn);
374 //cout << "New rn = " << rn << '\n';
382 FGRunways::~FGRunways( void ) {
388 FGRunwaysUtil::FGRunwaysUtil() {
393 int FGRunwaysUtil::load( const string& file ) {
397 runways.erase( runways.begin(), runways.end() );
399 sg_gzifstream in( file );
400 if ( !in.is_open() ) {
401 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
405 // skip first line of file
407 in.getline( tmp, 2048 );
409 // read in each line of the file
415 while ( in.get(c) && c != '\0' ) {
419 } else if ( c == 'R' ) {
422 runways.push_back(r);
432 while ( ! in.eof() ) {
438 } else if ( c == 'R' ) {
441 // cout << apt_id << " " << r.rwy_no << endl;
442 runways.push_back(r);
455 // save the data in gdbm format
456 bool FGRunwaysUtil::dump_mk4( const string& file ) {
457 // open database for writing
458 c4_Storage storage( file.c_str(), true );
460 // need to do something about error handling here!
462 // define the properties
463 c4_StringProp pID ("ID");
464 c4_StringProp pRwy ("Rwy");
465 c4_FloatProp pLon ("Longitude");
466 c4_FloatProp pLat ("Latitude");
467 c4_FloatProp pHdg ("Heading");
468 c4_FloatProp pLen ("Length");
469 c4_FloatProp pWid ("Width");
470 c4_StringProp pSurf ("SurfaceFlags");
471 c4_StringProp pEnd1 ("End1Flags");
472 c4_StringProp pEnd2 ("End2Flags");
474 // Start with an empty view of the proper structure.
476 storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
480 iterator current = runways.begin();
481 const_iterator end = runways.end();
482 while ( current != end ) {
483 // add each runway record
484 pID (row) = current->id.c_str();
485 pRwy (row) = current->rwy_no.c_str();
486 pLon (row) = current->lon;
487 pLat (row) = current->lat;
488 pHdg (row) = current->heading;
489 pLen (row) = current->length;
490 pWid (row) = current->width;
491 pSurf (row) = current->surface_flags.c_str();
492 pEnd1 (row) = current->end1_flags.c_str();
493 pEnd2 (row) = current->end2_flags.c_str();
499 // commit our changes
507 // search for the specified id
509 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
511 cout << "ID " << id << endl;
512 const_iterator it = runways.find( FGRunway(id) );
513 cout << "it = " << it << " end = " << runways.end() << endl;
514 if ( it != runways.end() )
527 FGRunwaysUtil::search( const string& id ) const
530 this->search( id, &a );
536 FGRunwaysUtil::~FGRunwaysUtil( void ) {