# include <config.h>
#endif
-// #include <sys/types.h> // for gdbm open flags
-// #include <sys/stat.h> // for gdbm open flags
+#include <math.h> // fabs()
+#include <stdio.h> // sprintf()
#include <simgear/compiler.h>
#include <simgear/debug/logstream.hxx>
-#include <simgear/misc/fgstream.hxx>
-
-#include <Main/options.hxx>
+#include <simgear/misc/sgstream.hxx>
#include STL_STRING
#include STL_FUNCTIONAL
#include "runways.hxx"
-FG_USING_NAMESPACE(std);
+SG_USING_NAMESPACE(std);
+#ifndef _MSC_VER
+#define NDEBUG // MSVC needs this
+#endif // !_MSC_VER
-FGRunway::FGRunway() {
-}
+#include <mk4.h>
+#include <mk4str.h>
+
+#ifndef _MSC_VER
+#undef NDEBUG
+#endif // !_MSC_VER
+
+#ifdef SG_HAVE_STD_INCLUDES
+# include <istream>
+#elif defined( SG_HAVE_NATIVE_SGI_COMPILERS )
+# include <iostream.h>
+#elif defined( __BORLANDC__ ) || defined (__APPLE__)
+# include <iostream>
+#else
+# include <istream.h>
+#endif
+#if ! defined( SG_HAVE_NATIVE_SGI_COMPILERS )
+SG_USING_STD(istream);
+#endif
-FGRunway::~FGRunway() {
+inline istream&
+operator >> ( istream& in, FGRunway& a )
+{
+ int tmp;
+
+ return in >> a.rwy_no >> a.lat >> a.lon >> a.heading >> a.length >> a.width
+ >> a.surface_flags >> a.end1_flags >> tmp >> tmp >> a.end2_flags
+ >> tmp >> tmp;
}
storage = new c4_Storage( file.c_str(), false );
if ( !storage->Strategy().IsValid() ) {
- FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
+ SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
exit(-1);
}
vRunway = new c4_View;
*vRunway =
- storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
+ storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
next_index = 0;
}
-// search for the specified id
-bool FGRunways::search( const string& id, FGRunway* r ) {
+// search for the specified apt id
+bool FGRunways::search( const string& aptid, FGRunway* r ) {
c4_StringProp pID ("ID");
c4_StringProp pRwy ("Rwy");
c4_FloatProp pLon ("Longitude");
c4_StringProp pEnd1 ("End1Flags");
c4_StringProp pEnd2 ("End2Flags");
- int index = vRunway->Find(pID[id.c_str()]);
- cout << "index = " << index << endl;
+ int index = vRunway->Find(pID[aptid.c_str()]);
+ // cout << "index = " << index << endl;
if ( index == -1 ) {
return false;
}
-FGRunway FGRunways::search( const string& id ) {
+// search for the specified apt id and runway no
+bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
+{
+ c4_StringProp pID ("ID");
+ c4_StringProp pRwy ("Rwy");
+ c4_FloatProp pLon ("Longitude");
+ c4_FloatProp pLat ("Latitude");
+ c4_FloatProp pHdg ("Heading");
+ c4_FloatProp pLen ("Length");
+ c4_FloatProp pWid ("Width");
+ c4_StringProp pSurf ("SurfaceFlags");
+ c4_StringProp pEnd1 ("End1Flags");
+ c4_StringProp pEnd2 ("End2Flags");
+
+ int index = vRunway->Find(pID[aptid.c_str()]);
+ // cout << "index = " << index << endl;
+
+ if ( index == -1 ) {
+ return false;
+ }
+
+ c4_RowRef row = vRunway->GetAt(index);
+ string rowid = (const char *) pID(row);
+ string rowrwyno = (const char *) pRwy(row);
+ while ( rowid == aptid ) {
+ next_index = index + 1;
+
+ if ( rowrwyno == rwyno ) {
+ r->id = (const char *) pID(row);
+ r->rwy_no = (const char *) pRwy(row);
+ r->lon = (double) pLon(row);
+ r->lat = (double) pLat(row);
+ r->heading = (double) pHdg(row);
+ r->length = (double) pLen(row);
+ r->width = (double) pWid(row);
+ r->surface_flags = (const char *) pSurf(row);
+ r->end1_flags = (const char *) pEnd1(row);
+ r->end2_flags = (const char *) pEnd2(row);
+
+ return true;
+ }
+
+ index++;
+ c4_RowRef row = vRunway->GetAt(index);
+ string rowid = (const char *) pID(row);
+ string rowrwyno = (const char *) pRwy(row);
+ }
+
+ return false;
+}
+
+
+FGRunway FGRunways::search( const string& aptid ) {
FGRunway a;
- search( id, &a );
+ search( aptid, &a );
return a;
}
}
+// Return the runway closest to a given heading
+bool FGRunways::search( const string& aptid, const int tgt_hdg,
+ FGRunway* runway )
+{
+ FGRunway r;
+ double found_dir = 0.0;
+
+ if ( !search( aptid, &r ) ) {
+ SG_LOG( SG_GENERAL, SG_ALERT,
+ "Failed to find " << aptid << " in database." );
+ return false;
+ }
+
+ double diff;
+ double min_diff = 360.0;
+
+ while ( r.id == aptid ) {
+ // forward direction
+ diff = tgt_hdg - r.heading;
+ while ( diff < -180.0 ) { diff += 360.0; }
+ while ( diff > 180.0 ) { diff -= 360.0; }
+ diff = fabs(diff);
+ // SG_LOG( SG_GENERAL, SG_INFO,
+ // "Runway " << r.rwy_no << " heading = " << r.heading <<
+ // " diff = " << diff );
+ if ( diff < min_diff ) {
+ min_diff = diff;
+ runway = &r;
+ found_dir = 0;
+ }
+
+ // reverse direction
+ diff = tgt_hdg - r.heading - 180.0;
+ while ( diff < -180.0 ) { diff += 360.0; }
+ while ( diff > 180.0 ) { diff -= 360.0; }
+ diff = fabs(diff);
+ // SG_LOG( SG_GENERAL, SG_INFO,
+ // "Runway -" << r.rwy_no << " heading = " <<
+ // r.heading + 180.0 <<
+ // " diff = " << diff );
+ if ( diff < min_diff ) {
+ min_diff = diff;
+ runway = &r;
+ found_dir = 180.0;
+ }
+
+ next( &r );
+ }
+
+ // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << runway->rwy_no
+ // << " + " << found_dir );
+
+ double heading = runway->heading + found_dir;
+ while ( heading >= 360.0 ) { heading -= 360.0; }
+ runway->heading = heading;
+
+ return true;
+}
+
+
+// Return the runway number of the runway closest to a given heading
+string FGRunways::search( const string& aptid, const int tgt_hdg ) {
+ FGRunway r;
+ string rn;
+ double found_dir = 0.0;
+
+ if ( !search( aptid, &r ) ) {
+ SG_LOG( SG_GENERAL, SG_ALERT,
+ "Failed to find " << aptid << " in database." );
+ return "NN";
+ }
+
+ double diff;
+ double min_diff = 360.0;
+
+ while ( r.id == aptid ) {
+ // forward direction
+ diff = tgt_hdg - r.heading;
+ while ( diff < -180.0 ) { diff += 360.0; }
+ while ( diff > 180.0 ) { diff -= 360.0; }
+ diff = fabs(diff);
+ // SG_LOG( SG_GENERAL, SG_INFO,
+ // "Runway " << r.rwy_no << " heading = " << r.heading <<
+ // " diff = " << diff );
+ if ( diff < min_diff ) {
+ min_diff = diff;
+ rn = r.rwy_no;
+ found_dir = 0;
+ }
+
+ // reverse direction
+ diff = tgt_hdg - r.heading - 180.0;
+ while ( diff < -180.0 ) { diff += 360.0; }
+ while ( diff > 180.0 ) { diff -= 360.0; }
+ diff = fabs(diff);
+ // SG_LOG( SG_GENERAL, SG_INFO,
+ // "Runway -" << r.rwy_no << " heading = " <<
+ // r.heading + 180.0 <<
+ // " diff = " << diff );
+ if ( diff < min_diff ) {
+ min_diff = diff;
+ rn = r.rwy_no;
+ found_dir = 180.0;
+ }
+
+ next( &r );
+ }
+
+ // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
+ // << " + " << found_dir );
+ // rn = r.rwy_no;
+ // cout << "In search, rn = " << rn << endl;
+ if ( found_dir == 180 ) {
+ int irn = atoi(rn.c_str());
+ irn += 18;
+ if ( irn > 36 ) {
+ irn -= 36;
+ }
+ char buf[4]; // 2 chars + string terminator + 1 for safety
+ sprintf(buf, "%i", irn);
+ rn = buf;
+ }
+
+ return rn;
+}
+
+
// Destructor
FGRunways::~FGRunways( void ) {
- // gdbm_close( dbf );
+ delete storage;
}
runways.erase( runways.begin(), runways.end() );
- fg_gzifstream in( file );
+ sg_gzifstream in( file );
if ( !in.is_open() ) {
- FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
+ SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
exit(-1);
}
// skip first line of file
- char tmp[256];
- in.getline( tmp, 256 );
+ char tmp[2048];
+ in.getline( tmp, 2048 );
// read in each line of the file
#else
- in >> skipws;
+ in >> ::skipws;
while ( ! in.eof() ) {
char c = 0;
in.get(c);
} else {
in >> skipeol;
}
- in >> skipws;
+ in >> ::skipws;
}
#endif
// save the data in gdbm format
bool FGRunwaysUtil::dump_mk4( const string& file ) {
-
// open database for writing
c4_Storage storage( file.c_str(), true );
// Start with an empty view of the proper structure.
c4_View vRunway =
- storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
+ storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
c4_Row row;