]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
Patch from Andy Ross:
[flightgear.git] / src / Airports / runways.cxx
1 // runways.hxx -- 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
45 FGRunway::FGRunway() {
46 }
47
48
49 FGRunway::~FGRunway() {
50 }
51
52
53 FGRunways::FGRunways( const string& file ) {
54     // open the specified database readonly
55     storage = new c4_Storage( file.c_str(), false );
56
57     if ( !storage->Strategy().IsValid() ) {
58         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
59         exit(-1);
60     }
61
62     vRunway = new c4_View;
63     *vRunway = 
64         storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
65
66     next_index = 0;
67 }
68
69
70 // search for the specified apt id
71 bool FGRunways::search( const string& aptid, FGRunway* r ) {
72     c4_StringProp pID ("ID");
73     c4_StringProp pRwy ("Rwy");
74     c4_FloatProp pLon ("Longitude");
75     c4_FloatProp pLat ("Latitude");
76     c4_FloatProp pHdg ("Heading");
77     c4_FloatProp pLen ("Length");
78     c4_FloatProp pWid ("Width");
79     c4_StringProp pSurf ("SurfaceFlags");
80     c4_StringProp pEnd1 ("End1Flags");
81     c4_StringProp pEnd2 ("End2Flags");
82
83     int index = vRunway->Find(pID[aptid.c_str()]);
84     // cout << "index = " << index << endl;
85
86     if ( index == -1 ) {
87         return false;
88     }
89
90     next_index = index + 1;
91
92     c4_RowRef row = vRunway->GetAt(index);
93
94     r->id =      (const char *) pID(row);
95     r->rwy_no =  (const char *) pRwy(row);
96     r->lon =     (double) pLon(row);
97     r->lat =     (double) pLat(row);
98     r->heading = (double) pHdg(row);
99     r->length =  (double) pLen(row);
100     r->width =   (double) pWid(row);
101     r->surface_flags = (const char *) pSurf(row);
102     r->end1_flags =    (const char *) pEnd1(row);
103     r->end2_flags =    (const char *) pEnd2(row);
104
105     return true;
106 }
107
108
109 // search for the specified apt id and runway no
110 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
111 {
112     c4_StringProp pID ("ID");
113     c4_StringProp pRwy ("Rwy");
114     c4_FloatProp pLon ("Longitude");
115     c4_FloatProp pLat ("Latitude");
116     c4_FloatProp pHdg ("Heading");
117     c4_FloatProp pLen ("Length");
118     c4_FloatProp pWid ("Width");
119     c4_StringProp pSurf ("SurfaceFlags");
120     c4_StringProp pEnd1 ("End1Flags");
121     c4_StringProp pEnd2 ("End2Flags");
122
123     int index = vRunway->Find(pID[aptid.c_str()]);
124     // cout << "index = " << index << endl;
125
126     if ( index == -1 ) {
127         return false;
128     }
129
130     c4_RowRef row = vRunway->GetAt(index);
131     string rowid = (const char *) pID(row);
132     string rowrwyno = (const char *) pRwy(row);
133     while ( rowid == aptid ) {
134         next_index = index + 1;
135
136         if ( rowrwyno == rwyno ) {
137             r->id =      (const char *) pID(row);
138             r->rwy_no =  (const char *) pRwy(row);
139             r->lon =     (double) pLon(row);
140             r->lat =     (double) pLat(row);
141             r->heading = (double) pHdg(row);
142             r->length =  (double) pLen(row);
143             r->width =   (double) pWid(row);
144             r->surface_flags = (const char *) pSurf(row);
145             r->end1_flags =    (const char *) pEnd1(row);
146             r->end2_flags =    (const char *) pEnd2(row);
147
148             return true;
149         }
150
151         index++;
152         c4_RowRef row = vRunway->GetAt(index);
153         string rowid = (const char *) pID(row);
154         string rowrwyno = (const char *) pRwy(row);
155     }
156
157     return false;
158 }
159
160
161 FGRunway FGRunways::search( const string& aptid ) {
162     FGRunway a;
163     search( aptid, &a );
164     return a;
165 }
166
167
168 // search for the specified id
169 bool FGRunways::next( FGRunway* r ) {
170     c4_StringProp pID ("ID");
171     c4_StringProp pRwy ("Rwy");
172     c4_FloatProp pLon ("Longitude");
173     c4_FloatProp pLat ("Latitude");
174     c4_FloatProp pHdg ("Heading");
175     c4_FloatProp pLen ("Length");
176     c4_FloatProp pWid ("Width");
177     c4_StringProp pSurf ("SurfaceFlags");
178     c4_StringProp pEnd1 ("End1Flags");
179     c4_StringProp pEnd2 ("End2Flags");
180
181     int size = vRunway->GetSize();
182     // cout << "total records = " << size << endl;
183
184     int index = next_index;
185     // cout << "index = " << index << endl;
186
187     if ( index == -1 || index >= size ) {
188         return false;
189     }
190
191     next_index = index + 1;
192
193     c4_RowRef row = vRunway->GetAt(index);
194
195     r->id =      (const char *) pID(row);
196     r->rwy_no =  (const char *) pRwy(row);
197     r->lon =     (double) pLon(row);
198     r->lat =     (double) pLat(row);
199     r->heading = (double) pHdg(row);
200     r->length =  (double) pLen(row);
201     r->width =   (double) pWid(row);
202     r->surface_flags = (const char *) pSurf(row);
203     r->end1_flags =    (const char *) pEnd1(row);
204     r->end2_flags =    (const char *) pEnd2(row);
205
206     return true;
207 }
208
209
210 // Return the runway closest to a given heading
211 bool FGRunways::search( const string& aptid, const int tgt_hdg,
212                         FGRunway* runway )
213 {
214     FGRunway r;
215     double found_dir = 0.0;  
216  
217     if ( !search( aptid, &r ) ) {
218         SG_LOG( SG_GENERAL, SG_ALERT,
219                 "Failed to find " << aptid << " in database." );
220         return false;
221     }
222     
223     double diff;
224     double min_diff = 360.0;
225     
226     while ( r.id == aptid ) {
227         // forward direction
228         diff = tgt_hdg - r.heading;
229         while ( diff < -180.0 ) { diff += 360.0; }
230         while ( diff >  180.0 ) { diff -= 360.0; }
231         diff = fabs(diff);
232         // SG_LOG( SG_GENERAL, SG_INFO,
233         //         "Runway " << r.rwy_no << " heading = " << r.heading <<
234         //         " diff = " << diff );
235         if ( diff < min_diff ) {
236             min_diff = diff;
237             runway = &r;
238             found_dir = 0;
239         }
240         
241         // reverse direction
242         diff = tgt_hdg - r.heading - 180.0;
243         while ( diff < -180.0 ) { diff += 360.0; }
244         while ( diff >  180.0 ) { diff -= 360.0; }
245         diff = fabs(diff);
246         // SG_LOG( SG_GENERAL, SG_INFO,
247         //         "Runway -" << r.rwy_no << " heading = " <<
248         //         r.heading + 180.0 <<
249         //         " diff = " << diff );
250         if ( diff < min_diff ) {
251             min_diff = diff;
252             runway = &r;
253             found_dir = 180.0;
254         }
255         
256         next( &r );
257     }
258     
259     // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << runway->rwy_no
260     //         << " + " << found_dir );
261     
262     double heading = runway->heading + found_dir;
263     while ( heading >= 360.0 ) { heading -= 360.0; }
264     runway->heading = heading;
265
266     return true;
267 }
268
269
270 // Return the runway number of the runway closest to a given heading
271 string FGRunways::search( const string& aptid, const int tgt_hdg ) {
272     FGRunway r;
273     string rn;
274     double found_dir = 0.0;  
275  
276     if ( !search( aptid, &r ) ) {
277         SG_LOG( SG_GENERAL, SG_ALERT,
278                 "Failed to find " << aptid << " in database." );
279         return "NN";
280     }
281     
282     double diff;
283     double min_diff = 360.0;
284     
285     while ( r.id == aptid ) {
286         // forward direction
287         diff = tgt_hdg - r.heading;
288         while ( diff < -180.0 ) { diff += 360.0; }
289         while ( diff >  180.0 ) { diff -= 360.0; }
290         diff = fabs(diff);
291         // SG_LOG( SG_GENERAL, SG_INFO,
292         //         "Runway " << r.rwy_no << " heading = " << r.heading <<
293         //         " diff = " << diff );
294         if ( diff < min_diff ) {
295             min_diff = diff;
296             rn = r.rwy_no;
297             found_dir = 0;
298         }
299         
300         // reverse direction
301         diff = tgt_hdg - r.heading - 180.0;
302         while ( diff < -180.0 ) { diff += 360.0; }
303         while ( diff >  180.0 ) { diff -= 360.0; }
304         diff = fabs(diff);
305         // SG_LOG( SG_GENERAL, SG_INFO,
306         //         "Runway -" << r.rwy_no << " heading = " <<
307         //         r.heading + 180.0 <<
308         //         " diff = " << diff );
309         if ( diff < min_diff ) {
310             min_diff = diff;
311             rn = r.rwy_no;
312             found_dir = 180.0;
313         }
314         
315         next( &r );
316     }
317     
318     // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
319     //         << " + " << found_dir );
320     // rn = r.rwy_no;
321     // cout << "In search, rn = " << rn << endl;
322     if ( found_dir == 180 ) {
323         int irn = atoi(rn.c_str());
324         irn += 18;
325         if ( irn > 36 ) {
326             irn -= 36;
327         }
328         char buf[4];            // 2 chars + string terminator + 1 for safety
329         sprintf(buf, "%i", irn);
330         rn = buf;
331     }   
332     
333     return rn;
334 }
335
336
337 // Destructor
338 FGRunways::~FGRunways( void ) {
339     delete storage;
340 }
341
342
343 // Constructor
344 FGRunwaysUtil::FGRunwaysUtil() {
345 }
346
347
348 // load the data
349 int FGRunwaysUtil::load( const string& file ) {
350     FGRunway r;
351     string apt_id;
352
353     runways.erase( runways.begin(), runways.end() );
354
355     sg_gzifstream in( file );
356     if ( !in.is_open() ) {
357         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
358         exit(-1);
359     }
360
361     // skip first line of file
362     char tmp[2048];
363     in.getline( tmp, 2048 );
364
365     // read in each line of the file
366
367 #ifdef __MWERKS__
368
369     in >> ::skipws;
370     char c = 0;
371     while ( in.get(c) && c != '\0' ) {
372         if ( c == 'A' ) {
373             in >> apt_id;
374             in >> skipeol;
375         } else if ( c == 'R' ) {
376             in >> r;
377             r.id = apt_id;
378             runways.push_back(r);
379         } else {
380             in >> skipeol;
381         }
382         in >> ::skipws;
383     }
384
385 #else
386
387     in >> ::skipws;
388     while ( ! in.eof() ) {
389         char c = 0;
390         in.get(c);
391         if ( c == 'A' ) {
392             in >> apt_id;
393             in >> skipeol;
394         } else if ( c == 'R' ) {
395             in >> r;
396             r.id = apt_id;
397             // cout << apt_id << " " << r.rwy_no << endl;
398             runways.push_back(r);
399         } else {
400             in >> skipeol;
401         }
402         in >> ::skipws;
403     }
404
405 #endif
406
407     return 1;
408 }
409
410
411 // save the data in gdbm format
412 bool FGRunwaysUtil::dump_mk4( const string& file ) {
413
414     // open database for writing
415     c4_Storage storage( file.c_str(), true );
416
417     // need to do something about error handling here!
418
419     // define the properties
420     c4_StringProp pID ("ID");
421     c4_StringProp pRwy ("Rwy");
422     c4_FloatProp pLon ("Longitude");
423     c4_FloatProp pLat ("Latitude");
424     c4_FloatProp pHdg ("Heading");
425     c4_FloatProp pLen ("Length");
426     c4_FloatProp pWid ("Width");
427     c4_StringProp pSurf ("SurfaceFlags");
428     c4_StringProp pEnd1 ("End1Flags");
429     c4_StringProp pEnd2 ("End2Flags");
430
431     // Start with an empty view of the proper structure.
432     c4_View vRunway =
433         storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
434
435     c4_Row row;
436
437     iterator current = runways.begin();
438     const_iterator end = runways.end();
439     while ( current != end ) {
440         // add each runway record
441         pID (row) = current->id.c_str();
442         pRwy (row) = current->rwy_no.c_str();
443         pLon (row) = current->lon;
444         pLat (row) = current->lat;
445         pHdg (row) = current->heading;
446         pLen (row) = current->length;
447         pWid (row) = current->width;
448         pSurf (row) = current->surface_flags.c_str();
449         pEnd1 (row) = current->end1_flags.c_str();
450         pEnd2 (row) = current->end2_flags.c_str();
451         vRunway.Add(row);
452
453         ++current;
454     }
455
456     // commit our changes
457     storage.Commit();
458
459     return true;
460 }
461
462
463 #if 0
464 // search for the specified id
465 bool
466 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
467 {
468     const_iterator it = runways.find( FGRunway(id) );
469     if ( it != runways.end() )
470     {
471         *a = *it;
472         return true;
473     }
474     else
475     {
476         return false;
477     }
478 }
479
480
481 FGRunway
482 FGRunwaysUtil::search( const string& id ) const
483 {
484     FGRunway a;
485     this->search( id, &a );
486     return a;
487 }
488 #endif
489
490 // Destructor
491 FGRunwaysUtil::~FGRunwaysUtil( void ) {
492 }
493
494