]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
Patches from Erik Hofman for SGI compatibility:
[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 #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 // 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");
105
106     int index = vRunway->Find(pID[aptid.c_str()]);
107     // cout << "index = " << index << endl;
108
109     if ( index == -1 ) {
110         return false;
111     }
112
113     next_index = index + 1;
114
115     c4_RowRef row = vRunway->GetAt(index);
116
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);
127
128     return true;
129 }
130
131
132 // search for the specified apt id and runway no
133 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
134 {
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");
145
146     int index = vRunway->Find(pID[aptid.c_str()]);
147     // cout << "index = " << index << endl;
148
149     if ( index == -1 ) {
150         return false;
151     }
152
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;
158
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);
170
171             return true;
172         }
173
174         index++;
175         c4_RowRef row = vRunway->GetAt(index);
176         string rowid = (const char *) pID(row);
177         string rowrwyno = (const char *) pRwy(row);
178     }
179
180     return false;
181 }
182
183
184 FGRunway FGRunways::search( const string& aptid ) {
185     FGRunway a;
186     search( aptid, &a );
187     return a;
188 }
189
190
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");
203
204     int size = vRunway->GetSize();
205     // cout << "total records = " << size << endl;
206
207     int index = next_index;
208     // cout << "index = " << index << endl;
209
210     if ( index == -1 || index >= size ) {
211         return false;
212     }
213
214     next_index = index + 1;
215
216     c4_RowRef row = vRunway->GetAt(index);
217
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);
228
229     return true;
230 }
231
232
233 // Return the runway closest to a given heading
234 bool FGRunways::search( const string& aptid, const int tgt_hdg,
235                         FGRunway* runway )
236 {
237     FGRunway r;
238     double found_dir = 0.0;  
239  
240     if ( !search( aptid, &r ) ) {
241         SG_LOG( SG_GENERAL, SG_ALERT,
242                 "Failed to find " << aptid << " in database." );
243         return false;
244     }
245     
246     double diff;
247     double min_diff = 360.0;
248     
249     while ( r.id == aptid ) {
250         // forward direction
251         diff = tgt_hdg - r.heading;
252         while ( diff < -180.0 ) { diff += 360.0; }
253         while ( diff >  180.0 ) { diff -= 360.0; }
254         diff = fabs(diff);
255         // SG_LOG( SG_GENERAL, SG_INFO,
256         //         "Runway " << r.rwy_no << " heading = " << r.heading <<
257         //         " diff = " << diff );
258         if ( diff < min_diff ) {
259             min_diff = diff;
260             runway = &r;
261             found_dir = 0;
262         }
263         
264         // reverse direction
265         diff = tgt_hdg - r.heading - 180.0;
266         while ( diff < -180.0 ) { diff += 360.0; }
267         while ( diff >  180.0 ) { diff -= 360.0; }
268         diff = fabs(diff);
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 ) {
274             min_diff = diff;
275             runway = &r;
276             found_dir = 180.0;
277         }
278         
279         next( &r );
280     }
281     
282     // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << runway->rwy_no
283     //         << " + " << found_dir );
284     
285     double heading = runway->heading + found_dir;
286     while ( heading >= 360.0 ) { heading -= 360.0; }
287     runway->heading = heading;
288
289     return true;
290 }
291
292
293 // Return the runway number of the runway closest to a given heading
294 string FGRunways::search( const string& aptid, const int tgt_hdg ) {
295     FGRunway r;
296     string rn;
297     double found_dir = 0.0;  
298  
299     if ( !search( aptid, &r ) ) {
300         SG_LOG( SG_GENERAL, SG_ALERT,
301                 "Failed to find " << aptid << " in database." );
302         return "NN";
303     }
304     
305     double diff;
306     double min_diff = 360.0;
307     
308     while ( r.id == aptid ) {
309         // forward direction
310         diff = tgt_hdg - r.heading;
311         while ( diff < -180.0 ) { diff += 360.0; }
312         while ( diff >  180.0 ) { diff -= 360.0; }
313         diff = fabs(diff);
314         // SG_LOG( SG_GENERAL, SG_INFO,
315         //         "Runway " << r.rwy_no << " heading = " << r.heading <<
316         //         " diff = " << diff );
317         if ( diff < min_diff ) {
318             min_diff = diff;
319             rn = r.rwy_no;
320             found_dir = 0;
321         }
322         
323         // reverse direction
324         diff = tgt_hdg - r.heading - 180.0;
325         while ( diff < -180.0 ) { diff += 360.0; }
326         while ( diff >  180.0 ) { diff -= 360.0; }
327         diff = fabs(diff);
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 ) {
333             min_diff = diff;
334             rn = r.rwy_no;
335             found_dir = 180.0;
336         }
337         
338         next( &r );
339     }
340     
341     // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
342     //         << " + " << found_dir );
343     // rn = r.rwy_no;
344     // cout << "In search, rn = " << rn << endl;
345     if ( found_dir == 180 ) {
346         int irn = atoi(rn.c_str());
347         irn += 18;
348         if ( irn > 36 ) {
349             irn -= 36;
350         }
351         char buf[4];            // 2 chars + string terminator + 1 for safety
352         sprintf(buf, "%i", irn);
353         rn = buf;
354     }   
355     
356     return rn;
357 }
358
359
360 // Destructor
361 FGRunways::~FGRunways( void ) {
362     delete storage;
363 }
364
365
366 // Constructor
367 FGRunwaysUtil::FGRunwaysUtil() {
368 }
369
370
371 // load the data
372 int FGRunwaysUtil::load( const string& file ) {
373     FGRunway r;
374     string apt_id;
375
376     runways.erase( runways.begin(), runways.end() );
377
378     sg_gzifstream in( file );
379     if ( !in.is_open() ) {
380         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
381         exit(-1);
382     }
383
384     // skip first line of file
385     char tmp[2048];
386     in.getline( tmp, 2048 );
387
388     // read in each line of the file
389
390 #ifdef __MWERKS__
391
392     in >> ::skipws;
393     char c = 0;
394     while ( in.get(c) && c != '\0' ) {
395         if ( c == 'A' ) {
396             in >> apt_id;
397             in >> skipeol;
398         } else if ( c == 'R' ) {
399             in >> r;
400             r.id = apt_id;
401             runways.push_back(r);
402         } else {
403             in >> skipeol;
404         }
405         in >> ::skipws;
406     }
407
408 #else
409
410     in >> ::skipws;
411     while ( ! in.eof() ) {
412         char c = 0;
413         in.get(c);
414         if ( c == 'A' ) {
415             in >> apt_id;
416             in >> skipeol;
417         } else if ( c == 'R' ) {
418             in >> r;
419             r.id = apt_id;
420             // cout << apt_id << " " << r.rwy_no << endl;
421             runways.push_back(r);
422         } else {
423             in >> skipeol;
424         }
425         in >> ::skipws;
426     }
427
428 #endif
429
430     return 1;
431 }
432
433
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 );
438
439     // need to do something about error handling here!
440
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");
452
453     // Start with an empty view of the proper structure.
454     c4_View vRunway =
455         storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
456
457     c4_Row row;
458
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();
473         vRunway.Add(row);
474
475         ++current;
476     }
477
478     // commit our changes
479     storage.Commit();
480
481     return true;
482 }
483
484
485 #if 0
486 // search for the specified id
487 bool
488 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
489 {
490     const_iterator it = runways.find( FGRunway(id) );
491     if ( it != runways.end() )
492     {
493         *a = *it;
494         return true;
495     }
496     else
497     {
498         return false;
499     }
500 }
501
502
503 FGRunway
504 FGRunwaysUtil::search( const string& id ) const
505 {
506     FGRunway a;
507     this->search( id, &a );
508     return a;
509 }
510 #endif
511
512 // Destructor
513 FGRunwaysUtil::~FGRunwaysUtil( void ) {
514 }
515
516