]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
a1d82821a2ef38c7ae09c89edb0354850e85e09f
[flightgear.git] / src / Airports / runways.cxx
1 // runways.cxx -- a simple class to manage airport runway info
2 //
3 // Written by Curtis Olson, started August 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <math.h>               // fabs()
29 #include <stdio.h>              // sprintf()
30
31 #include <simgear/compiler.h>
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/sgstream.hxx>
35
36 #include STL_STRING
37 #include STL_FUNCTIONAL
38 #include STL_ALGORITHM
39
40 #include "runways.hxx"
41
42 SG_USING_NAMESPACE(std);
43
44 #ifndef _MSC_VER
45 #define NDEBUG                  // MSVC needs this
46 #endif // !_MSC_VER
47
48 #include <mk4.h>
49 #include <mk4str.h>
50
51 #ifndef _MSC_VER
52 #undef NDEBUG
53 #endif // !_MSC_VER
54
55 #ifdef SG_HAVE_STD_INCLUDES
56 #  include <istream>
57 #elif defined( __BORLANDC__ ) || defined (__APPLE__)
58 #  include <iostream>
59 #else
60 #  include <istream.h>
61 #endif
62
63 SG_USING_STD(istream);
64
65 inline istream&
66 operator >> ( istream& in, FGRunway& a )
67 {
68     int tmp;
69
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
72               >> tmp >> tmp;
73 }
74
75
76 FGRunways::FGRunways( const string& file ) {
77     // open the specified database readonly
78     storage = new c4_Storage( file.c_str(), false );
79
80     if ( !storage->Strategy().IsValid() ) {
81         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
82         exit(-1);
83     }
84
85     vRunway = new c4_View;
86     *vRunway = 
87         storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
88
89     next_index = 0;
90 }
91
92
93 // Return reverse rwy number
94 // eg 01 -> 19
95 // 03L -> 21R
96 static string GetReverseRunwayNo(string rwyno) {        
97     // cout << "Original rwyno = " << rwyNo << '\n';
98     
99     // standardize input number
100     string tmp = rwyno.substr(1, 1);
101     if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
102         tmp = rwyno;
103         rwyno = "0" + tmp;
104         SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from " << tmp
105                                      << " to " << rwyno );
106     }
107     
108     char buf[4];
109     int rn = atoi(rwyno.substr(0,2).c_str());
110     rn += 18;
111     while(rn > 36) {
112         rn -= 36;
113     }
114     sprintf(buf, "%02i", rn);
115     if(rwyno.size() == 3) {
116         if(rwyno.substr(2,1) == "L") {
117             buf[2] = 'R';
118             buf[3] = '\0';
119         } else if (rwyno.substr(2,1) == "R") {
120             buf[2] = 'L';
121             buf[3] = '\0';
122         } else if (rwyno.substr(2,1) == "C") {
123             buf[2] = 'C';
124             buf[3] = '\0';
125         } else {
126             SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
127             << rwyno << " passed to GetReverseRunwayNo(...)");
128         }
129     }
130     return((string)buf);
131 }
132
133
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");
146
147     int index = vRunway->Find(pID[aptid.c_str()]);
148     c4_RowRef row = vRunway->GetAt(index);
149
150     // cout << "index = " << index " row = " << row << endl;
151
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)) ) {
155         return false;
156     }
157
158     next_index = index + 1;
159
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);
170
171     return true;
172 }
173
174
175 // search for the specified apt id and runway no
176 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
177 {
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");
189
190     int index = vRunway->Find(pID[aptid.c_str()]);
191     c4_RowRef row = vRunway->GetAt(index);
192     // cout << "index = " << index " row = " << row << endl;
193
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)) ) {
197         return false;
198     }
199     
200     // standardize input number
201     string tmp = runwayno.substr(1, 1);
202     if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (runwayno.size() == 1)) {
203         tmp = runwayno;
204         runwayno = "0" + tmp;
205         SG_LOG(SG_GENERAL, SG_INFO, "Standardising rwy number from " << tmp
206                                      << " to " << runwayno );
207     }
208
209     string rowid = (const char *) pID(row);
210     string rowrwyno = (const char *) pRwy(row);
211     while ( rowid == aptid ) {
212         next_index = index + 1;
213
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);
225
226             return true;
227         }
228         
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 
245             
246             return true;
247         }
248
249         index++;
250         row = vRunway->GetAt(index);
251         rowid = (const char *) pID(row);
252         rowrwyno = (const char *) pRwy(row);
253     }
254
255     return false;
256 }
257
258
259 FGRunway FGRunways::search( const string& aptid ) {
260     FGRunway a;
261     search( aptid, &a );
262     return a;
263 }
264
265
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");
278
279     int size = vRunway->GetSize();
280     // cout << "total records = " << size << endl;
281
282     int index = next_index;
283     // cout << "index = " << index << endl;
284
285     if ( index == -1 || index >= size ) {
286         return false;
287     }
288
289     next_index = index + 1;
290
291     c4_RowRef row = vRunway->GetAt(index);
292
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);
303
304     return true;
305 }
306
307
308 // Return the runway closest to a given heading
309 bool FGRunways::search( const string& aptid, const int tgt_hdg,
310                         FGRunway* runway )
311 {
312     string rwyNo = search(aptid, tgt_hdg);
313     return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
314 }
315
316
317 // Return the runway number of the runway closest to a given heading
318 string FGRunways::search( const string& aptid, const int tgt_hdg ) {
319     FGRunway r;
320     FGRunway tmp_r;     
321     string rn;
322     double found_dir = 0.0;  
323  
324     if ( !search( aptid, &tmp_r ) ) {
325         SG_LOG( SG_GENERAL, SG_ALERT,
326                 "Failed to find " << aptid << " in database." );
327         return "NN";
328     }
329     
330     double diff;
331     double min_diff = 360.0;
332     
333     while ( tmp_r.id == aptid ) {
334         r = tmp_r;
335         
336         // forward direction
337         diff = tgt_hdg - r.heading;
338         while ( diff < -180.0 ) { diff += 360.0; }
339         while ( diff >  180.0 ) { diff -= 360.0; }
340         diff = fabs(diff);
341         // SG_LOG( SG_GENERAL, SG_INFO,
342         //         "Runway " << r.rwy_no << " heading = " << r.heading <<
343         //         " diff = " << diff );
344         if ( diff < min_diff ) {
345             min_diff = diff;
346             rn = r.rwy_no;
347             found_dir = 0;
348         }
349         
350         // reverse direction
351         diff = tgt_hdg - r.heading - 180.0;
352         while ( diff < -180.0 ) { diff += 360.0; }
353         while ( diff >  180.0 ) { diff -= 360.0; }
354         diff = fabs(diff);
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 ) {
360             min_diff = diff;
361             rn = r.rwy_no;
362             found_dir = 180.0;
363         }
364         
365         next( &tmp_r );
366     }
367     
368     // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
369     //         << " + " << found_dir );
370     rn = r.rwy_no;
371     // cout << "In search, rn = " << rn << endl;
372     if ( found_dir == 180 ) {
373         rn = GetReverseRunwayNo(rn);
374         //cout << "New rn = " << rn << '\n';
375     }   
376     
377     return rn;
378 }
379
380
381 // Destructor
382 FGRunways::~FGRunways( void ) {
383     delete storage;
384 }
385
386
387 // Constructor
388 FGRunwaysUtil::FGRunwaysUtil() {
389 }
390
391
392 // load the data
393 int FGRunwaysUtil::load( const string& file ) {
394     FGRunway r;
395     string apt_id;
396
397     runways.erase( runways.begin(), runways.end() );
398
399     sg_gzifstream in( file );
400     if ( !in.is_open() ) {
401         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
402         exit(-1);
403     }
404
405     // skip first line of file
406     char tmp[2048];
407     in.getline( tmp, 2048 );
408
409     // read in each line of the file
410
411 #ifdef __MWERKS__
412
413     in >> ::skipws;
414     char c = 0;
415     while ( in.get(c) && c != '\0' ) {
416         if ( c == 'A' ) {
417             in >> apt_id;
418             in >> skipeol;
419         } else if ( c == 'R' ) {
420             in >> r;
421             r.id = apt_id;
422             runways.push_back(r);
423         } else {
424             in >> skipeol;
425         }
426         in >> ::skipws;
427     }
428
429 #else
430
431     in >> ::skipws;
432     while ( ! in.eof() ) {
433         char c = 0;
434         in.get(c);
435         if ( c == 'A' ) {
436             in >> apt_id;
437             in >> skipeol;
438         } else if ( c == 'R' ) {
439             in >> r;
440             r.id = apt_id;
441             // cout << apt_id << " " << r.rwy_no << endl;
442             runways.push_back(r);
443         } else {
444             in >> skipeol;
445         }
446         in >> ::skipws;
447     }
448
449 #endif
450
451     return 1;
452 }
453
454
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 );
459
460     // need to do something about error handling here!
461
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");
473
474     // Start with an empty view of the proper structure.
475     c4_View vRunway =
476         storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
477
478     c4_Row row;
479
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();
494         vRunway.Add(row);
495
496         ++current;
497     }
498
499     // commit our changes
500     storage.Commit();
501
502     return true;
503 }
504
505
506 #if 0
507 // search for the specified id
508 bool
509 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
510 {
511     const_iterator it = runways.find( FGRunway(id) );
512     if ( it != runways.end() )
513     {
514         *a = *it;
515         return true;
516     }
517     else
518     {
519         return false;
520     }
521 }
522
523
524 FGRunway
525 FGRunwaysUtil::search( const string& id ) const
526 {
527     FGRunway a;
528     this->search( id, &a );
529     return a;
530 }
531 #endif
532
533 // Destructor
534 FGRunwaysUtil::~FGRunwaysUtil( void ) {
535 }
536
537