]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
5a3e509dc10549bc218650f874430fee235868e9
[flightgear.git] / src / Airports / simple.cxx
1 //
2 // simple.cxx -- a really simplistic class to manage airport ID,
3 //               lat, lon of the center of one of it's runways, and 
4 //               elevation in feet.
5 //
6 // Written by Curtis Olson, started April 1998.
7 //
8 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <math.h>
31
32 #include <simgear/compiler.h>
33
34 #include <simgear/debug/logstream.hxx>
35
36 #include STL_STRING
37
38 #include "simple.hxx"
39
40 SG_USING_NAMESPACE(std);
41
42
43 // add an entry to the list
44 void FGAirportList::add( const string id, const double longitude,
45                          const double latitude, const double elevation,
46                          const string name, const bool has_metar )
47 {
48     FGAirport a;
49     a._id = id;
50     a._longitude = longitude;
51     a._latitude = latitude;
52     a._elevation = elevation;
53     a._name = name;
54     a._has_metar = has_metar;
55     airports_by_id[a._id] = a;
56     airports_array.push_back( &airports_by_id[a._id] );
57     SG_LOG( SG_GENERAL, SG_BULK, "Adding " << id << " pos = " << longitude
58             << ", " << latitude << " elev = " << elevation );
59 }
60
61
62 // search for the specified id
63 FGAirport FGAirportList::search( const string& id) {
64     return airports_by_id[id];
65 }
66
67
68 // search for the airport nearest the specified position
69 FGAirport FGAirportList::search( double lon_deg, double lat_deg,
70                                  bool with_metar ) {
71     int closest = 0;
72     double min_dist = 360.0;
73     unsigned int i;
74     for ( i = 0; i < airports_array.size(); ++i ) {
75         // crude manhatten distance based on lat/lon difference
76         double d = fabs(lon_deg - airports_array[i]->_longitude)
77             + fabs(lat_deg - airports_array[i]->_latitude);
78         if ( d < min_dist ) {
79             if ( !with_metar || (with_metar&&airports_array[i]->_has_metar) ) {
80                 closest = i;
81                 min_dist = d;
82             }
83         }
84     }
85
86     return *airports_array[closest];
87 }
88
89
90 // Destructor
91 FGAirportList::~FGAirportList( void ) {
92 }
93
94 int
95 FGAirportList::size () const
96 {
97     return airports_array.size();
98 }
99
100 const FGAirport *FGAirportList::getAirport( int index ) const
101 {
102     return airports_array[index];
103 }
104
105
106 /**
107  * Mark the specified airport record as not having metar
108  */
109 void FGAirportList::no_metar( const string &id ) {
110     airports_by_id[id]._has_metar = false;
111 }
112
113
114 /**
115  * Mark the specified airport record as (yes) having metar
116  */
117 void FGAirportList::has_metar( const string &id ) {
118     airports_by_id[id]._has_metar = true;
119 }