Index: Instrumentation/dclgps.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/dclgps.cxx,v
retrieving revision 1.14
diff -u -3 -r1.14 dclgps.cxx
--- Instrumentation/dclgps.cxx	12 Sep 2008 08:46:15 -0000	1.14
+++ Instrumentation/dclgps.cxx	8 Dec 2008 21:38:30 -0000
@@ -32,11 +32,12 @@
 
 #include <Main/fg_props.hxx>
 #include <Navaids/fix.hxx>
+#include <Navaids/navrecord.hxx>
+#include <Airports/simple.hxx>
 
 #include <iostream>
-using std::cout;
 
-//using namespace std;
+using namespace std;
 
 // Command callbacks for FlightGear
 
@@ -140,31 +141,43 @@
 	else return(id);
 }
 
-GPSWaypoint* GPSWaypoint::createFromFix(const FGFix* aFix)
+static GPSWpType
+GPSWpTypeFromFGPosType(FGPositioned::Type aType)
 {
-  assert(aFix);
-  return new GPSWaypoint(aFix->get_ident(), 
-    aFix->get_lat() * SG_DEGREES_TO_RADIANS,
-    aFix->get_lon() * SG_DEGREES_TO_RADIANS,
-    GPS_WP_INT);
-}
-
-GPSWaypoint* GPSWaypoint::createFromNav(const FGNavRecord* aNav)
-{
-  assert(aNav);
-  return new GPSWaypoint(aNav->get_ident(), 
-    aNav->get_lat() * SG_DEGREES_TO_RADIANS,
-    aNav->get_lon() * SG_DEGREES_TO_RADIANS,
-    (aNav->get_fg_type() == FG_NAV_VOR ? GPS_WP_VOR : GPS_WP_NDB));
+  switch (aType) {
+  case FGPositioned::AIRPORT:
+  case FGPositioned::SEAPORT:
+  case FGPositioned::HELIPORT:
+    return GPS_WP_APT;
+  
+  case FGPositioned::VOR:
+    return GPS_WP_VOR;
+  
+  case FGPositioned::NDB:
+    return GPS_WP_NDB;
+  
+  case FGPositioned::WAYPOINT:
+    return GPS_WP_USR;
+  
+  case FGPositioned::FIX:
+    return GPS_WP_INT;
+  
+  default:
+    return GPS_WP_USR;
+  }
 }
 
-GPSWaypoint* GPSWaypoint::createFromAirport(const FGAirport* aApt)
+GPSWaypoint* GPSWaypoint::createFromPositioned(const FGPositioned* aPos)
 {
-  assert(aApt);
-  return new GPSWaypoint(aApt->getId(), 
-    aApt->getLatitude() * SG_DEGREES_TO_RADIANS,
-    aApt->getLongitude() * SG_DEGREES_TO_RADIANS,
-    GPS_WP_APT);
+  if (!aPos) {
+    return NULL; // happens if find returns no match
+  }
+  
+  return new GPSWaypoint(aPos->ident(), 
+    aPos->latitude() * SG_DEGREES_TO_RADIANS,
+    aPos->longitude() * SG_DEGREES_TO_RADIANS,
+    GPSWpTypeFromFGPosType(aPos->type())
+  );
 }
 
 ostream& operator << (ostream& os, GPSAppWpType type) {
@@ -1071,132 +1084,83 @@
   }
 };
 
-GPSWaypoint* DCLGPS::FindFirstById(const string& id) const
+class DCLGPSFilter : public FGPositioned::Filter
 {
-  stringOrderKLN89 ordering;
-  nav_list_type vors = globals->get_navlist()->findFirstByIdent(id, FG_NAV_VOR, false);
-  nav_list_type ndbs = globals->get_navlist()->findFirstByIdent(id, FG_NAV_NDB, false);
-  const FGFix* fix = globals->get_fixlist()->findFirstByIdent(id, &ordering);
-  const FGAirport* apt = globals->get_airports()->findFirstById(id, &ordering);
-  // search local gps waypoints (USR)
-
-// pick the best - ugly logic, sorry. This is a temporary fix to getting rid
-// of the huge local waypoint table, it'll die when there's a way to query
-// this stuff centrally.
-// what we're doing is using map inserts to order the result, then using
-// the first entry (begin()) as the lowest, hence best, match
-  map<string, GPSWpType, stringOrderKLN89> sorter;
-  if (fix) sorter[fix->get_ident()] = GPS_WP_INT;
-  if (apt) sorter[apt->getId()] = GPS_WP_APT;
-  if (!vors.empty()) sorter[vors.front()->get_ident()] = GPS_WP_VOR;
-  if (!ndbs.empty()) sorter[ndbs.front()->get_ident()] = GPS_WP_NDB;
-
-  if (sorter.empty()) return NULL; // no results at all
-  GPSWpType ty = sorter.begin()->second;
-  
-  switch (ty) {
-  case GPS_WP_INT: 
-    return GPSWaypoint::createFromFix(fix);
-  
-  case GPS_WP_APT:
-    return GPSWaypoint::createFromAirport(apt);
-  
-  case GPS_WP_VOR:
-    return GPSWaypoint::createFromNav(vors.front());
-    
-  case GPS_WP_NDB:
-    return GPSWaypoint::createFromNav(ndbs.front());
-  default:
-    return NULL; // can't happen
+public:
+  virtual bool pass(const FGPositioned* aPos) const {
+    switch (aPos->type()) {
+    case FGPositioned::AIRPORT:
+    // how about heliports and seaports?
+    case FGPositioned::NDB:
+    case FGPositioned::VOR:
+    case FGPositioned::WAYPOINT:
+    case FGPositioned::FIX:
+      break;
+    default: return false; // reject all other types
+    }
+    return true;
   }
+};
+
+GPSWaypoint* DCLGPS::FindFirstById(const string& id) const
+{
+  DCLGPSFilter filter;
+  FGPositionedRef result = FGPositioned::findNextWithPartialId(NULL, id, &filter);
+  return GPSWaypoint::createFromPositioned(result);
 }
 
 GPSWaypoint* DCLGPS::FindFirstByExactId(const string& id) const
 {
-  if (const FGAirport* apt = globals->get_airports()->search(id)) {
-    return GPSWaypoint::createFromAirport(apt);
-  }
-  
-  if (const FGFix* fix = globals->get_fixlist()->search(id)) {
-    return GPSWaypoint::createFromFix(fix);
-  }
-  
-  nav_list_type vors = globals->get_navlist()->findFirstByIdent(id, FG_NAV_VOR, true);
-  if (!vors.empty()) {
-    return GPSWaypoint::createFromNav(vors.front());
-  }
+  SGGeod pos(SGGeod::fromRad(_lon, _lat));
+  FGPositionedRef result = FGPositioned::findClosestWithIdent(id, pos);
+  return GPSWaypoint::createFromPositioned(result);
+}
+
+// TODO - add the ASCII / alphabetical stuff from the Atlas version
+FGPositioned* DCLGPS::FindTypedFirstById(const string& id, FGPositioned::Type ty, bool &multi, bool exact)
+{
+  multi = false;
+  FGPositioned::TypeFilter filter(ty);
   
-  nav_list_type ndbs = globals->get_navlist()->findFirstByIdent(id, FG_NAV_NDB, true);
-  if (!ndbs.empty()) {
-    return GPSWaypoint::createFromNav(ndbs.front());
+  if (exact) {
+    FGPositioned::List matches = 
+      FGPositioned::findAllWithIdentSortedByRange(id, SGGeod::fromRad(_lon, _lat), &filter);
+    multi = (matches.size() > 1);
+    return matches.empty() ? NULL : matches.front().ptr();
   }
   
-  return NULL;
+  return FGPositioned::findNextWithPartialId(NULL, id, &filter);
 }
 
-// Host specific lookup functions
-// TODO - add the ASCII / alphabetical stuff from the Atlas version
-FGNavRecord* DCLGPS::FindFirstVorById(const string& id, bool &multi, bool exact) {
-	// NOTE - at the moment multi is never set.
-	multi = false;
-	//if(exact) return(_overlays->FindFirstVorById(id, exact));
-	
-	nav_list_type nav = globals->get_navlist()->findFirstByIdent(id, FG_NAV_VOR, exact);
-	
-	if(nav.size() > 1) multi = true;
-	//return(nav.empty() ? NULL : *(nav.begin()));
-	
-	// The above is sort of what we want - unfortunately we can't guarantee no NDB/ILS at the moment
-	if(nav.empty()) return(NULL);
-	
-	for(nav_list_iterator it = nav.begin(); it != nav.end(); ++it) {
-		if((*it)->type() == FGPositioned::VOR) return(*it);
-	}
-	return(NULL);	// Shouldn't get here!
+FGNavRecord* DCLGPS::FindFirstVorById(const string& id, bool &multi, bool exact)
+{
+  return dynamic_cast<FGNavRecord*>(FindTypedFirstById(id, FGPositioned::VOR, multi, exact));
 }
 
-// TODO - add the ASCII / alphabetical stuff from the Atlas version
-FGNavRecord* DCLGPS::FindFirstNDBById(const string& id, bool &multi, bool exact) {
-	// NOTE - at the moment multi is never set.
-	multi = false;
-	//if(exact) return(_overlays->FindFirstVorById(id, exact));
-	
-	nav_list_type nav = globals->get_navlist()->findFirstByIdent(id, FG_NAV_NDB, exact);
-	
-	if(nav.size() > 1) multi = true;
-	//return(nav.empty() ? NULL : *(nav.begin()));
-	
-	// The above is sort of what we want - unfortunately we can't guarantee no NDB/ILS at the moment
-	if(nav.empty()) return(NULL);
-	
-	for(nav_list_iterator it = nav.begin(); it != nav.end(); ++it) {
-		if((*it)->type() == FGPositioned::NDB) return(*it);
-	}
-	return(NULL);	// Shouldn't get here!
+FGNavRecord* DCLGPS::FindFirstNDBById(const string& id, bool &multi, bool exact)
+{
+  return dynamic_cast<FGNavRecord*>(FindTypedFirstById(id, FGPositioned::NDB, multi, exact));
 }
 
-const FGFix* DCLGPS::FindFirstIntById(const string& id, bool &multi, bool exact) {
-	// NOTE - at the moment multi is never set, and indeed can't be
-	// since FG can only map one Fix per ID at the moment.
-	multi = false;
-	if (exact) return globals->get_fixlist()->search(id);
-	
-  stringOrderKLN89 ordering;
-  return globals->get_fixlist()->findFirstByIdent(id, &ordering);
+const FGFix* DCLGPS::FindFirstIntById(const string& id, bool &multi, bool exact)
+{
+  return dynamic_cast<FGFix*>(FindTypedFirstById(id, FGPositioned::FIX, multi, exact));
 }
 
-const FGAirport* DCLGPS::FindFirstAptById(const string& id, bool &multi, bool exact) {
-	// NOTE - at the moment multi is never set.
-	//cout << "FindFirstAptById, id = " << id << '\n';
-	multi = false;
-	if(exact) return(globals->get_airports()->search(id));
-	
-  stringOrderKLN89 ordering;
-  return globals->get_airports()->findFirstById(id, &ordering);
+const FGAirport* DCLGPS::FindFirstAptById(const string& id, bool &multi, bool exact)
+{
+  return dynamic_cast<FGAirport*>(FindTypedFirstById(id, FGPositioned::AIRPORT, multi, exact));
 }
 
-FGNavRecord* DCLGPS::FindClosestVor(double lat_rad, double lon_rad) {
-	return(globals->get_navlist()->findClosest(lon_rad, lat_rad, 0.0, FG_NAV_VOR));
+FGNavRecord* DCLGPS::FindClosestVor(double lat_rad, double lon_rad) {  
+  FGPositioned::TypeFilter filter(FGPositioned::VOR);
+  double cutoff = 1000; // nautical miles
+  FGPositionedRef v = FGPositioned::findClosest(SGGeod::fromRad(lon_rad, lat_rad), cutoff, &filter);
+  if (!v) {
+    return NULL;
+  }
+  
+  return dynamic_cast<FGNavRecord*>(v.ptr());
 }
 
 //----------------------------------------------------------------------------------------------------------
Index: Instrumentation/dclgps.hxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/dclgps.hxx,v
retrieving revision 1.7
diff -u -3 -r1.7 dclgps.hxx
--- Instrumentation/dclgps.hxx	22 Aug 2008 11:22:30 -0000	1.7
+++ Instrumentation/dclgps.hxx	8 Dec 2008 21:38:30 -0000
@@ -32,15 +32,16 @@
 #include <vector>
 #include <map>
 
-#include <Navaids/navrecord.hxx>
-#include <Navaids/navlist.hxx>
-#include <Navaids/fixlist.hxx>
-#include <Airports/simple.hxx>
 #include <simgear/structure/subsystem_mgr.hxx>
-
-using namespace std;
+#include <Navaids/positioned.hxx>
 
 class SGTime;
+class FGPositioned;
+
+// XXX fix me
+class FGNavRecord;
+class FGAirport;
+class FGFix;
 
 enum GPSDistanceUnits {
 	GPS_DIST_UNITS_NM = 0,
@@ -94,9 +95,7 @@
   
   GPSWaypoint(const std::string& aIdent, float lat, float lon, GPSWpType aType);
   
-  static GPSWaypoint* createFromFix(const FGFix* aFix);
-  static GPSWaypoint* createFromNav(const FGNavRecord* aNav);
-  static GPSWaypoint* createFromAirport(const FGAirport* aApt);
+  static GPSWaypoint* createFromPositioned(const FGPositioned* aFix);
   
     ~GPSWaypoint();
 	string GetAprId();	// Returns the id with i, f, m or h added if appropriate. (Initial approach fix, final approach fix, etc)
@@ -428,6 +427,9 @@
 	// Find the closest VOR to a position in RADIANS.
 	FGNavRecord* FindClosestVor(double lat_rad, double lon_rad);
 
+  // helper to implement the above FindFirstXXX methods
+  FGPositioned* FindTypedFirstById(const std::string& id, FGPositioned::Type ty, bool &multi, bool exact);
+
 	// Position, orientation and velocity.
 	// These should be read from FG's built-in GPS logic if possible.
 	// Use the property node pointers below to do this.
Index: Instrumentation/KLN89/kln89.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/KLN89/kln89.cxx,v
retrieving revision 1.7
diff -u -3 -r1.7 kln89.cxx
--- Instrumentation/KLN89/kln89.cxx	27 Jul 2008 16:25:15 -0000	1.7
+++ Instrumentation/KLN89/kln89.cxx	8 Dec 2008 21:38:32 -0000
@@ -46,6 +46,7 @@
 #include <ATCDCL/ATCProjection.hxx>
 #include <Main/fg_props.hxx>
 #include <simgear/math/point3d.hxx>
+#include "Airports/simple.hxx"
 
 using std::cout;
 
Index: Instrumentation/KLN89/kln89_page_apt.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/KLN89/kln89_page_apt.cxx,v
retrieving revision 1.9
diff -u -3 -r1.9 kln89_page_apt.cxx
--- Instrumentation/KLN89/kln89_page_apt.cxx	11 Sep 2008 08:38:11 -0000	1.9
+++ Instrumentation/KLN89/kln89_page_apt.cxx	8 Dec 2008 21:38:33 -0000
@@ -26,10 +26,10 @@
 #endif
 
 #include "kln89_page_apt.hxx"
-#include <ATCDCL/commlist.hxx>
-#include <Main/globals.hxx>
-#include <Airports/runways.hxx>
-
+#include "ATCDCL/commlist.hxx"
+#include "Main/globals.hxx"
+#include "Airports/runways.hxx"
+#include "Airports/simple.hxx"
 
 // This function is copied from Airports/runways.cxx
 // TODO - Make the original properly available and remove this instance!!!!
Index: Instrumentation/KLN89/kln89_page_int.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/KLN89/kln89_page_int.cxx,v
retrieving revision 1.5
diff -u -3 -r1.5 kln89_page_int.cxx
--- Instrumentation/KLN89/kln89_page_int.cxx	10 Sep 2008 08:54:49 -0000	1.5
+++ Instrumentation/KLN89/kln89_page_int.cxx	8 Dec 2008 21:38:33 -0000
@@ -27,6 +27,7 @@
 
 #include "kln89_page_int.hxx"
 #include "Navaids/fix.hxx"
+#include "Navaids/navrecord.hxx"
 
 KLN89IntPage::KLN89IntPage(KLN89* parent) 
 : KLN89Page(parent) {
Index: Instrumentation/KLN89/kln89_page_ndb.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/KLN89/kln89_page_ndb.cxx,v
retrieving revision 1.4
diff -u -3 -r1.4 kln89_page_ndb.cxx
--- Instrumentation/KLN89/kln89_page_ndb.cxx	21 Feb 2006 01:19:03 -0000	1.4
+++ Instrumentation/KLN89/kln89_page_ndb.cxx	8 Dec 2008 21:38:33 -0000
@@ -26,6 +26,7 @@
 #endif
 
 #include "kln89_page_ndb.hxx"
+#include "Navaids/navrecord.hxx"
 
 KLN89NDBPage::KLN89NDBPage(KLN89* parent) 
 : KLN89Page(parent) {
Index: Instrumentation/KLN89/kln89_page_vor.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/KLN89/kln89_page_vor.cxx,v
retrieving revision 1.4
diff -u -3 -r1.4 kln89_page_vor.cxx
--- Instrumentation/KLN89/kln89_page_vor.cxx	21 Feb 2006 01:19:03 -0000	1.4
+++ Instrumentation/KLN89/kln89_page_vor.cxx	8 Dec 2008 21:38:33 -0000
@@ -26,6 +26,7 @@
 #endif
 
 #include "kln89_page_vor.hxx"
+#include "Navaids/navrecord.hxx"
 
 KLN89VorPage::KLN89VorPage(KLN89* parent) 
 : KLN89Page(parent) {
