// search for the airport nearest the specified position
-FGAirport* FGAirportList::search( double lon_deg, double lat_deg,
- bool with_metar )
+FGAirport* FGAirportList::search(double lon_deg, double lat_deg)
+{
+ static FGAirportSearchFilter accept_any;
+ return search(lon_deg, lat_deg, accept_any);
+}
+
+
+// search for the airport nearest the specified position and
+// passing the filter
+FGAirport* FGAirportList::search(double lon_deg, double lat_deg,
+ FGAirportSearchFilter& search)
{
- int closest = -1;
double min_dist = 360.0;
- unsigned int i;
- for ( i = 0; i < airports_array.size(); ++i ) {
+ airport_list_iterator it = airports_array.begin();
+ airport_list_iterator end = airports_array.end();
+ airport_list_iterator closest = end;
+ for (; it != end; ++it) {
+ if (!search.acceptable(*it))
+ continue;
+
// crude manhatten distance based on lat/lon difference
- double d = fabs(lon_deg - airports_array[i]->getLongitude())
- + fabs(lat_deg - airports_array[i]->getLatitude());
- if ( d < min_dist ) {
- if ( !with_metar || (with_metar&&airports_array[i]->getMetar()) ) {
- closest = i;
- min_dist = d;
- }
+ double d = fabs(lon_deg - (*it)->getLongitude())
+ + fabs(lat_deg - (*it)->getLatitude());
+ if (d < min_dist) {
+ closest = it;
+ min_dist = d;
}
}
-
- return ( closest > -1 ? airports_array[closest] : NULL );
+ return closest != end ? *closest : 0;
}
};
+class FGAirportSearchFilter {
+public:
+ virtual ~FGAirportSearchFilter() {}
+ virtual bool acceptable(FGAirport*) { return true; }
+};
+
typedef map < string, FGAirport* > airport_map;
typedef airport_map::iterator airport_map_iterator;
airport_map airports_by_id;
airport_list airports_array;
- //set < string > ai_dirs;
public:
// Constructor (new)
// 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.
- // Returns NULL if fails (unlikely unless none have metar and with_metar spec'd!)
- FGAirport* search( double lon_deg, double lat_deg, bool with_metar );
+ // best to use this at runtime.) An FGAirportSearchFilter class
+ // can be used to restrict the possible choices to a subtype.
+ FGAirport* search( double lon_deg, double lat_deg );
+ FGAirport* search( double lon_deg, double lat_deg, FGAirportSearchFilter& );
/**
* Return the number of airports in the list.
SG_USING_STD(sort);
+class metar_filter : public FGAirportSearchFilter {
+ virtual bool acceptable(FGAirport *a) { return a->getMetar(); }
+} metar_only;
\f
////////////////////////////////////////////////////////////////////////
const FGAirport* a = globals->get_airports()
->search( longitude->getDoubleValue(),
latitude->getDoubleValue(),
- true );
+ metar_only );
if ( a ) {
FGMetarResult result = fetch_data( a->getId() );
if ( result.m != NULL ) {
const FGAirport* a = globals->get_airports()
->search( longitude->getDoubleValue(),
latitude->getDoubleValue(),
- true );
+ metar_only );
if ( a ) {
if ( !last_apt || last_apt->getId() != a->getId()
|| fetch_elapsed > same_station_interval_sec )
// If the get-nearest-airport-node is true.
// Get the nearest airport, and set it as waypoint 1.
if (_get_nearest_airport_node->getBoolValue()) {
- const FGAirport* a = globals->get_airports()->search(longitude_deg, latitude_deg, false);
+ const FGAirport* a = globals->get_airports()->search(longitude_deg, latitude_deg);
if(a) {
_wp1_ID_node->setStringValue(a->getId().c_str());
wp1_longitude_deg = a->getLongitude();
FGAirportList *aptlst = globals->get_airports();
FGAirport *apt;
if(argc == 0)
- apt = aptlst->search(lon->getDoubleValue(), lat->getDoubleValue(), false);
+ apt = aptlst->search(lon->getDoubleValue(), lat->getDoubleValue());
else if(argc == 1 && naIsString(args[0]))
apt = aptlst->search(naStr_data(args[0]));
else if(argc == 2 && naIsNum(args[0]) && naIsNum(args[1]))
- apt = aptlst->search(args[1].num, args[0].num, false);
+ apt = aptlst->search(args[1].num, args[0].num);
else {
naRuntimeError(c, "airportinfo() with invalid function arguments");
return naNil();