From 4606f96e139204d5b2d76ee93c3b0c66885b5f5f Mon Sep 17 00:00:00 2001 From: curt Date: Mon, 23 Feb 2004 01:37:26 +0000 Subject: [PATCH] Various mods to allow querying for nearest airport (with optional ability to only query those stations with metar weather available.) Metar availability is determined on the fly for now. --- src/Airports/simple.cxx | 49 ++++++++++++++++++++++++++++++++++------- src/Airports/simple.hxx | 23 ++++++++++++++----- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/src/Airports/simple.cxx b/src/Airports/simple.cxx index ab3b47baf..04a528f95 100644 --- a/src/Airports/simple.cxx +++ b/src/Airports/simple.cxx @@ -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; } diff --git a/src/Airports/simple.hxx b/src/Airports/simple.hxx index 881f3b365..2d25e16d5 100644 --- a/src/Airports/simple.hxx +++ b/src/Airports/simple.hxx @@ -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 ); }; -- 2.39.5