1 // fixlist.cxx -- fix list management class
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/misc/sgstream.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
34 #include "fixlist.hxx"
35 #include "Navaids/fix.hxx"
36 #include "Airports/simple.hxx"
38 FGFix::FGFix(const std::string& aIdent, const SGGeod& aPos) :
39 FGPositioned(FIX, aIdent, aPos)
44 FGFixList::FGFixList( void ) {
49 FGFixList::~FGFixList( void ) {
53 // load the navaids and build the map
54 bool FGFixList::init( SGPath path ) {
55 fixlist.erase( fixlist.begin(), fixlist.end() );
57 sg_gzifstream in( path.str() );
58 if ( !in.is_open() ) {
59 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
63 // toss the first two lines of the file
67 // read in each remaining line of the file
68 while ( ! in.eof() ) {
71 in >> lat >> lon >> ident;
74 FGFix* fix = new FGFix(ident, SGGeod::fromDeg(lon, lat));
75 fixlist.insert(std::make_pair(fix->ident(), fix));
82 // query the database for the specified fix, lon and lat are in
83 // degrees, elev is in meters
84 bool FGFixList::query( const string& ident, FGFix* &fix ) {
85 fix_map_const_iterator it = fixlist.find(ident);
86 if ( it != fixlist.end() ) {
95 // query the database for the specified fix, lon and lat are in
96 // degrees, elev is in meters
97 bool FGFixList::query_and_offset( const string& ident, double lon, double lat,
98 double elev, FGFix* &fix, double *heading,
101 std::pair<fix_map_const_iterator, fix_map_const_iterator> range = fixlist.equal_range(ident);
103 if (range.first == range.second) {
108 for (fix_map_const_iterator current = range.first; current != range.second; ++current) {
110 geo_inverse_wgs_84( elev, lat, lon,
111 current->second->get_lat(), current->second->get_lon(),
113 // cout << " dist = " << s << endl;
114 if (min_s < 0 || s < min_s) {
118 fix = current->second;
125 const FGFix* FGFixList::search(const string& ident)
127 fix_map_iterator itr = fixlist.find(ident);
128 if (itr == fixlist.end()) {
135 class orderingFunctor
138 orderingFunctor(FGIdentOrdering* aOrder) :
142 bool operator()(const fix_map_type::value_type& aA, const std::string& aB) const
144 return mOrdering->compare(aA.first,aB);
147 bool operator()(const std::string& aA, const fix_map_type::value_type& aB) const
149 return mOrdering->compare(aA, aB.first);
152 bool operator()(const fix_map_type::value_type& aA, const fix_map_type::value_type& aB) const
154 return mOrdering->compare(aA.first, aB.first);
158 FGIdentOrdering* mOrdering;
161 const FGFix* FGFixList::findFirstByIdent( const string& ident, FGIdentOrdering* aOrder)
163 fix_map_iterator itr;
165 orderingFunctor func(aOrder);
166 itr = std::lower_bound(fixlist.begin(),fixlist.end(), ident, func);
168 itr = fixlist.lower_bound(ident);
171 if (itr == fixlist.end()) {