]> git.mxchange.org Git - flightgear.git/commitdiff
Various mods to allow querying for nearest airport (with optional ability to
authorcurt <curt>
Mon, 23 Feb 2004 01:37:26 +0000 (01:37 +0000)
committercurt <curt>
Mon, 23 Feb 2004 01:37:26 +0000 (01:37 +0000)
only query those stations with metar weather available.)  Metar availability
is determined on the fly for now.

src/Airports/simple.cxx
src/Airports/simple.hxx

index ab3b47baf97de74471856feaf2eb147b50c9ddba..04a528f956cf27454f0b25d3cd8370bf3c01b0b2 100644 (file)
@@ -52,6 +52,8 @@ operator >> ( istream& in, FGAirport& a )
     in.getline( name, 256 );
     a.name = name;
 
+    a.has_metar = true;         // assume true
+
     return in;
 }
 
@@ -70,18 +72,42 @@ FGAirportList::FGAirportList( const string& file ) {
     in >> skipeol;
 
     FGAirport a;
+    int size = 0;
     while ( in ) {
         in >> a;
-        airports[a.id] = a;
-        airports2.push_back(&airports[a.id]);
+        airports_by_id[a.id] = a;
+        airports_array.push_back( &airports_by_id[a.id] );
+        size++;
     }
-
 }
 
 
 // search for the specified id
 FGAirport FGAirportList::search( const string& id) {
-    return airports[id];
+    
+    return airports_by_id[id];
+}
+
+
+// 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];
 }
 
 
@@ -92,11 +118,18 @@ FGAirportList::~FGAirportList( void ) {
 int
 FGAirportList::size () const
 {
-    return airports2.size();
+    return airports_array.size();
 }
 
-const FGAirport *
-FGAirportList::getAirport (int index) const
+const FGAirport FGAirportList::getAirport( int index ) const
 {
-    return airports2[index];
+    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;
 }
index 881f3b3651633f48a344b7ac858746f2432bf429..2d25e16d5d74dfdc1f1186250044936fe855a8fa 100644 (file)
@@ -55,7 +55,7 @@ struct FGAirport {
     double elevation;
     string code;
     string name;
-
+    bool has_metar;
 };
 
 typedef map < string, FGAirport > airport_map;
@@ -69,8 +69,8 @@ class FGAirportList {
 
 private:
 
-    airport_map airports;
-    airport_list airports2;
+    airport_map airports_by_id;
+    airport_list airports_array;
 
 public:
 
@@ -86,16 +86,29 @@ public:
     // "airport" is not changed if "apt" is not found.
     FGAirport search( const string& id );
 
+    // search for the airport closest to the specified position
+    // (currently a linear inefficient search so it's probably not
+    // best to use this at runtime.)  If with_metar is true, then only
+    // return station id's marked as having metar data.
+    FGAirport search( double lon_deg, double lat_deg, bool with_metar );
+
+
     /**
      * Return the number of airports in the list.
      */
-    int size () const;
+    int size() const;
 
 
     /**
      * Return a specific airport, by position.
      */
-    const FGAirport * getAirport (int index) const;
+    const FGAirport getAirport( int index ) const;
+
+
+    /**
+     * Mark the specified airport record as not having metar
+     */
+    void no_metar( const string &id );
 
 };