]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/fixlist.cxx
make attribute strings lowercase with hyphen instead of underscore;
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 // Constructor
36 FGFixList::FGFixList( void ) {
37 }
38
39
40 // Destructor
41 FGFixList::~FGFixList( void ) {
42 }
43
44
45 // load the navaids and build the map
46 bool FGFixList::init( SGPath path ) {
47     fixlist.erase( fixlist.begin(), fixlist.end() );
48
49     sg_gzifstream in( path.str() );
50     if ( !in.is_open() ) {
51         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
52         exit(-1);
53     }
54
55     // toss the first two lines of the file
56     in >> skipeol;
57     in >> skipeol;
58
59     // read in each remaining line of the file
60
61 #ifdef __MWERKS__
62     char c = 0;
63     while ( in.get(c) && c != '\0' ) {
64         in.putback(c);
65 #else   
66     while ( ! in.eof() ) {
67 #endif
68
69         FGFix fix;
70         in >> fix;
71         if ( fix.get_lat() > 95.0 ) {
72             break;
73         }
74
75         /* cout << "ident=" << fix.get_ident()
76              << ", lat=" << fix.get_lat()
77              << ", lon=" << fix.get_lon() << endl; */
78
79         fixlist[fix.get_ident()] = fix;
80         in >> skipcomment;
81     }
82     return true;
83 }
84
85
86 // query the database for the specified fix, lon and lat are in
87 // degrees, elev is in meters
88 bool FGFixList::query( const string& ident, FGFix *fix ) {
89     *fix = fixlist[ident];
90     if ( ! fix->get_ident().empty() ) {
91         return true;
92     } else {
93         return false;
94     }
95 }
96
97
98 // query the database for the specified fix, lon and lat are in
99 // degrees, elev is in meters
100 bool FGFixList::query_and_offset( const string& ident, double lon, double lat,
101                                   double elev, FGFix *fix, double *heading,
102                                   double *dist )
103 {
104     *fix = fixlist[ident];
105     if ( fix->get_ident().empty() ) {
106         return false;
107     }
108
109     double az1, az2, s;
110     geo_inverse_wgs_84( elev, lat, lon, 
111                         fix->get_lat(), fix->get_lon(),
112                         &az1, &az2, &s );
113     // cout << "  dist = " << s << endl;
114     *heading = az2;
115     *dist = s;
116     return true;
117 }
118
119 const FGFix* FGFixList::findFirstByIdent( const string& ident, bool exact)
120 {
121     fix_map_iterator itr;
122     if(exact) {
123         itr = fixlist.find(ident);
124     } else {
125         itr = fixlist.lower_bound(ident);
126     }
127     if(itr == fixlist.end()) {
128         return(NULL);
129     } else {
130         return(&(itr->second));
131     }
132 }