]> git.mxchange.org Git - flightgear.git/blobdiff - src/Airports/simple.cxx
Set the format default to float instead of int.
[flightgear.git] / src / Airports / simple.cxx
index f9411a198979d34005cc48ea2bb8dee7521699bd..cfbb61ac24092b841883d75331581a575aa9d343 100644 (file)
@@ -1,7 +1,7 @@
 //
 // simple.cxx -- a really simplistic class to manage airport ID,
-//                 lat, lon of the center of one of it's runways, and 
-//                 elevation in feet.
+//               lat, lon of the center of one of it's runways, and 
+//               elevation in feet.
 //
 // Written by Curtis Olson, started April 1998.
 //
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <math.h>
 
-#include <Include/compiler.h>
+#include <simgear/compiler.h>
 
-#include <Debug/logstream.hxx>
-#include <Misc/fgpath.hxx>
-#include <Misc/fgstream.hxx>
-#include <Main/options.hxx>
+#include <simgear/debug/logstream.hxx>
+#include <simgear/misc/sgstream.hxx>
 
 #include STL_STRING
-#include STL_FUNCTIONAL
-#include STL_ALGORITHM
+#include STL_IOSTREAM
 
 #include "simple.hxx"
 
+SG_USING_NAMESPACE(std);
+SG_USING_STD(istream);
 
-fgAIRPORTS::fgAIRPORTS() {
-}
 
+inline istream&
+operator >> ( istream& in, FGAirport& a )
+{
+    string junk;
+    in >> junk >> a.id >> a.latitude >> a.longitude >> a.elevation
+       >> a.code;
+
+    getline( in,a.name );
 
-// load the data
-int fgAIRPORTS::load( const string& file ) {
-    fgAIRPORT a;
+    // Remove the space before the name
+    if ( a.name.substr(0,1) == " " ) {
+        a.name = a.name.erase(0,1);
+    }
 
-    // build the path name to the airport file
-    FGPath path( current_options.get_fg_root() );
-    path.append( "Airports" );
-    path.append( file );
+    a.has_metar = false;
 
-    airports.erase( airports.begin(), airports.end() );
+#if 0
+    // As a quick seed for the has_metar value, only airports with
+    // four-letter codes can have metar stations
+    a.has_metar = (isalpha(a.id[0]) && isalpha(a.id[1]) && isalpha(a.id[2])
+        && isalpha(a.id[3]) && !a.id[4]);
+#endif
 
-    fg_gzifstream in( path.str() );
-    if ( !in.is_open() ) {
-       FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << path.str() );
+    return in;
+}
+
+
+FGAirportList::FGAirportList( const string &airport_file,
+                              const string &metar_file ) {
+    SG_LOG( SG_GENERAL, SG_INFO, "Reading simple airport list: "
+            << airport_file );
+
+    // open the specified file for reading
+    sg_gzifstream apt_in( airport_file );
+    if ( !apt_in.is_open() ) {
+        SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << airport_file );
        exit(-1);
     }
 
-    /*
-    // We can use the STL copy algorithm because the input
-    // file doesn't contain and comments or blank lines.
-    copy( istream_iterator<fgAIRPORT,ptrdiff_t>(in.stream()),
-         istream_iterator<fgAIRPORT,ptrdiff_t>(),
-         inserter( airports, airports.begin() ) );
-    */
-
-    // read in each line of the file
-
-#ifdef __MWERKS__
-
-    in >> skipcomment;
-    char c = 0;
-    while ( in.get(c) && c != '\0' ) {
-       in.putback(c);
-       in >> a;
-       airports.insert(a);
-       in >> skipcomment;
+    // skip header line
+    apt_in >> skipeol;
+
+    FGAirport a;
+    while ( apt_in ) {
+        apt_in >> a;
+        airports_by_id[a.id] = a;
+        airports_array.push_back( &airports_by_id[a.id] );
     }
 
-#else
 
-    in >> skipcomment;
-    while ( ! in.eof() ) {
-       in >> a;
-       airports.insert(a);
-       in >> skipcomment;
-    }
+    SG_LOG( SG_GENERAL, SG_INFO, "Reading simple metar station list: "
+            << metar_file );
 
-#endif
+    // open the specified file for reading
+    sg_gzifstream metar_in( metar_file );
+    if ( !metar_in.is_open() ) {
+        SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
+    }
 
-    return 1;
+    string ident;
+    while ( metar_in ) {
+        metar_in >> ident;
+        if ( ident == "#" || ident == "//" ) {
+            metar_in >> skipeol;
+        } else {
+            airport_map_iterator apt = airports_by_id.find( ident );
+            if ( apt == airports_by_id.end() ) {
+                SG_LOG( SG_GENERAL, SG_DEBUG, "no apt = " << ident );
+            } else {
+                SG_LOG( SG_GENERAL, SG_DEBUG, "metar = " << ident );
+                airports_by_id[ident].has_metar = true;
+            }
+        }
+    }
 }
 
 
 // search for the specified id
-bool
-fgAIRPORTS::search( const string& id, fgAIRPORT* a ) const
-{
-    const_iterator it = airports.find( fgAIRPORT(id) );
-    if ( it != airports.end() )
-    {
-       *a = *it;
-       return true;
-    }
-    else
-    {
-       return false;
-    }
+FGAirport FGAirportList::search( const string& id) {
+    return airports_by_id[id];
 }
 
 
-fgAIRPORT
-fgAIRPORTS::search( const string& id ) const
-{
-    fgAIRPORT a;
-    this->search( id, &a );
-    return a;
+// search for the airport nearest the specified position
+FGAirport FGAirportList::search( double lon_deg, double lat_deg,
+                                 bool with_metar ) {
+    int closest = 0;
+    double min_dist = 360.0;
+    unsigned int i;
+    for ( i = 0; i < airports_array.size(); ++i ) {
+        // crude manhatten distance based on lat/lon difference
+        double d = fabs(lon_deg - airports_array[i]->longitude)
+            + fabs(lat_deg - airports_array[i]->latitude);
+        if ( d < min_dist ) {
+            if ( !with_metar || (with_metar && airports_array[i]->has_metar) ) {
+                closest = i;
+                min_dist = d;
+            }
+        }
+    }
+
+    return *airports_array[closest];
 }
 
 
 // Destructor
-fgAIRPORTS::~fgAIRPORTS( void ) {
+FGAirportList::~FGAirportList( void ) {
 }
 
+int
+FGAirportList::size () const
+{
+    return airports_array.size();
+}
 
+const FGAirport *FGAirportList::getAirport( int index ) const
+{
+    return airports_array[index];
+}
+
+
+/**
+ * Mark the specified airport record as not having metar
+ */
+void FGAirportList::no_metar( const string &id ) {
+    airports_by_id[id].has_metar = false;
+}