]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/fixlist.cxx
Fix line endings
[flightgear.git] / src / Navaids / fixlist.cxx
1 // fixlist.cxx -- fix list management class
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
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 <simgear/debug/logstream.hxx>
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31
32 #include "fixlist.hxx"
33
34
35 FGFixList *current_fixlist;
36
37
38 // Constructor
39 FGFixList::FGFixList( void ) {
40 }
41
42
43 // Destructor
44 FGFixList::~FGFixList( void ) {
45 }
46
47
48 // load the navaids and build the map
49 bool FGFixList::init( SGPath path ) {
50     fixlist.erase( fixlist.begin(), fixlist.end() );
51
52     sg_gzifstream in( path.str() );
53     if ( !in.is_open() ) {
54         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
55         exit(-1);
56     }
57
58     // toss the first two lines of the file
59     in >> skipeol;
60     in >> skipeol;
61
62     // read in each remaining line of the file
63
64 #ifdef __MWERKS__
65     char c = 0;
66     while ( in.get(c) && c != '\0' ) {
67         in.putback(c);
68 #else   
69     while ( ! in.eof() ) {
70 #endif
71
72         FGFix fix;
73         in >> fix;
74         if ( fix.get_lat() > 95.0 ) {
75             break;
76         }
77
78         /* cout << "ident=" << fix.get_ident()
79              << ", lat=" << fix.get_lat()
80              << ", lon=" << fix.get_lon() << endl; */
81
82         fixlist[fix.get_ident()] = fix;
83         in >> skipcomment;
84     }
85     return true;
86 }
87
88
89 // query the database for the specified fix, lon and lat are in
90 // degrees, elev is in meters
91 bool FGFixList::query( const string& ident, FGFix *fix ) {
92     *fix = fixlist[ident];
93     if ( ! fix->get_ident().empty() ) {
94         return true;
95     } else {
96         return false;
97     }
98 }
99
100
101 // query the database for the specified fix, lon and lat are in
102 // degrees, elev is in meters
103 bool FGFixList::query_and_offset( const string& ident, double lon, double lat,
104                                   double elev, FGFix *fix, double *heading,
105                                   double *dist )
106 {
107     *fix = fixlist[ident];
108     if ( fix->get_ident().empty() ) {
109         return false;
110     }
111
112     double az1, az2, s;
113     geo_inverse_wgs_84( elev, lat, lon, 
114                         fix->get_lat(), fix->get_lon(),
115                         &az1, &az2, &s );
116     // cout << "  dist = " << s << endl;
117     *heading = az2;
118     *dist = s;
119     return true;
120 }
121
122 const FGFix* FGFixList::findFirstByIdent( const string& ident, bool exact)
123 {
124     fix_map_iterator itr;
125     if(exact) {
126         itr = fixlist.find(ident);
127     } else {
128         itr = fixlist.lower_bound(ident);
129     }
130     if(itr == fixlist.end()) {
131         return(NULL);
132     } else {
133         return(&(itr->second));
134     }
135 }