2004-05-28 05:24:54 +00:00
|
|
|
// navdb.cxx -- top level navaids management routines
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started May 2004.
|
|
|
|
//
|
2004-11-19 22:10:41 +00:00
|
|
|
// Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
|
2004-05-28 05:24:54 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-05-28 05:24:54 +00:00
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
2006-02-18 13:58:09 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2004-05-28 05:24:54 +00:00
|
|
|
|
2012-08-28 00:26:36 +01:00
|
|
|
#include "navdb.hxx"
|
2004-05-28 16:24:43 +00:00
|
|
|
|
2012-08-28 00:26:36 +01:00
|
|
|
#include <simgear/compiler.h>
|
2004-05-28 05:24:54 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
2008-08-03 14:34:42 +00:00
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
2008-08-14 18:13:39 +00:00
|
|
|
#include <simgear/misc/strutils.hxx>
|
2008-12-25 23:11:43 +00:00
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2008-09-12 08:46:15 +00:00
|
|
|
#include <simgear/structure/exception.hxx>
|
|
|
|
#include <simgear/misc/sgstream.hxx>
|
2010-12-07 17:55:45 +00:00
|
|
|
#include <simgear/props/props_io.hxx>
|
2012-08-28 00:26:36 +01:00
|
|
|
#include <simgear/sg_inlines.h>
|
2004-05-28 05:24:54 +00:00
|
|
|
|
|
|
|
#include "navrecord.hxx"
|
2008-12-25 23:11:43 +00:00
|
|
|
#include "navlist.hxx"
|
2009-05-14 20:55:09 +00:00
|
|
|
#include <Main/globals.hxx>
|
|
|
|
#include <Navaids/markerbeacon.hxx>
|
|
|
|
#include <Airports/simple.hxx>
|
2010-12-07 17:55:45 +00:00
|
|
|
#include <Airports/runways.hxx>
|
|
|
|
#include <Airports/xmlloader.hxx>
|
|
|
|
#include <Main/fg_props.hxx>
|
2012-08-28 00:26:36 +01:00
|
|
|
#include <Navaids/NavDataCache.hxx>
|
2004-05-28 05:24:54 +00:00
|
|
|
|
2008-07-29 08:27:48 +00:00
|
|
|
using std::string;
|
2011-10-17 17:41:59 +01:00
|
|
|
using std::vector;
|
2004-05-28 16:24:43 +00:00
|
|
|
|
2009-01-03 15:54:03 +00:00
|
|
|
static FGPositioned::Type
|
|
|
|
mapRobinTypeToFGPType(int aTy)
|
|
|
|
{
|
|
|
|
switch (aTy) {
|
|
|
|
// case 1:
|
|
|
|
case 2: return FGPositioned::NDB;
|
|
|
|
case 3: return FGPositioned::VOR;
|
2009-06-11 22:54:51 +00:00
|
|
|
case 4: return FGPositioned::ILS;
|
|
|
|
case 5: return FGPositioned::LOC;
|
2009-01-03 15:54:03 +00:00
|
|
|
case 6: return FGPositioned::GS;
|
2012-08-28 00:26:36 +01:00
|
|
|
case 7: return FGPositioned::OM;
|
|
|
|
case 8: return FGPositioned::MM;
|
|
|
|
case 9: return FGPositioned::IM;
|
2009-01-03 15:54:03 +00:00
|
|
|
case 12:
|
|
|
|
case 13: return FGPositioned::DME;
|
|
|
|
case 99: return FGPositioned::INVALID; // end-of-file code
|
|
|
|
default:
|
|
|
|
throw sg_range_exception("Got a nav.dat type we don't recognize", "FGNavRecord::createFromStream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 00:26:36 +01:00
|
|
|
static bool autoAlignLocalizers = false;
|
|
|
|
static double autoAlignThreshold = 0.0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a runway, and proposed localiser data (ident, positioned and heading),
|
|
|
|
* precisely align the localiser with the actual runway heading, providing the
|
|
|
|
* difference between the localiser course and runway heading is less than a
|
|
|
|
* threshold. (To allow for localizers such as Kai-Tek requiring a turn on final).
|
|
|
|
*
|
|
|
|
* The positioned and heading argument are modified if changes are made.
|
|
|
|
*/
|
|
|
|
void alignLocaliserWithRunway(FGRunway* rwy, const string& ident, SGGeod& pos, double& heading)
|
|
|
|
{
|
|
|
|
assert(rwy);
|
|
|
|
// find the distance from the threshold to the localizer
|
|
|
|
double dist = SGGeodesy::distanceM(pos, rwy->threshold());
|
|
|
|
|
|
|
|
// back project that distance along the runway center line
|
|
|
|
SGGeod newPos = rwy->pointOnCenterline(dist);
|
|
|
|
|
|
|
|
double hdg_diff = heading - rwy->headingDeg();
|
|
|
|
SG_NORMALIZE_RANGE(hdg_diff, -180.0, 180.0);
|
|
|
|
|
|
|
|
if ( fabs(hdg_diff) <= autoAlignThreshold ) {
|
|
|
|
pos = SGGeod::fromGeodFt(newPos, pos.getElevationFt());
|
|
|
|
heading = rwy->headingDeg();
|
|
|
|
} else {
|
2012-11-23 21:14:40 +01:00
|
|
|
SG_LOG(SG_NAVAID, SG_DEBUG, "localizer:" << ident << ", aligning with runway "
|
2012-08-28 00:26:36 +01:00
|
|
|
<< rwy->ident() << " exceeded heading threshold");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static double defaultNavRange(const string& ident, FGPositioned::Type type)
|
|
|
|
{
|
|
|
|
// Ranges are included with the latest data format, no need to
|
|
|
|
// assign our own defaults, unless the range is not set for some
|
|
|
|
// reason.
|
2012-11-23 21:14:40 +01:00
|
|
|
SG_LOG(SG_NAVAID, SG_DEBUG, "navaid " << ident << " has no range set, using defaults");
|
2012-08-28 00:26:36 +01:00
|
|
|
switch (type) {
|
|
|
|
case FGPositioned::NDB:
|
|
|
|
case FGPositioned::VOR:
|
|
|
|
return FG_NAV_DEFAULT_RANGE;
|
|
|
|
|
|
|
|
case FGPositioned::LOC:
|
|
|
|
case FGPositioned::ILS:
|
|
|
|
case FGPositioned::GS:
|
|
|
|
return FG_LOC_DEFAULT_RANGE;
|
|
|
|
|
|
|
|
case FGPositioned::DME: return FG_DME_DEFAULT_RANGE;
|
|
|
|
default: return FG_LOC_DEFAULT_RANGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace flightgear
|
|
|
|
{
|
|
|
|
|
|
|
|
static PositionedID readNavFromStream(std::istream& aStream,
|
|
|
|
FGPositioned::Type type = FGPositioned::INVALID)
|
2009-01-03 15:54:03 +00:00
|
|
|
{
|
2012-08-28 00:26:36 +01:00
|
|
|
NavDataCache* cache = NavDataCache::instance();
|
|
|
|
|
2009-01-03 15:54:03 +00:00
|
|
|
int rawType;
|
|
|
|
aStream >> rawType;
|
2009-01-04 09:41:52 +00:00
|
|
|
if (aStream.eof() || (rawType == 99)) {
|
2012-08-28 00:26:36 +01:00
|
|
|
return 0; // happens with, eg, carrier_nav.dat
|
2009-01-03 15:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double lat, lon, elev_ft, multiuse;
|
|
|
|
int freq, range;
|
|
|
|
std::string name, ident;
|
|
|
|
aStream >> lat >> lon >> elev_ft >> freq >> range >> multiuse >> ident;
|
|
|
|
getline(aStream, name);
|
|
|
|
|
|
|
|
SGGeod pos(SGGeod::fromDegFt(lon, lat, elev_ft));
|
|
|
|
name = simgear::strutils::strip(name);
|
|
|
|
|
2012-08-28 00:26:36 +01:00
|
|
|
// the type can be forced by our caller, but normally we use th value
|
|
|
|
// supplied in the .dat file
|
|
|
|
if (type == FGPositioned::INVALID) {
|
|
|
|
type = mapRobinTypeToFGPType(rawType);
|
2009-01-03 15:54:03 +00:00
|
|
|
}
|
|
|
|
if (type == FGPositioned::INVALID) {
|
2012-08-28 00:26:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((type >= FGPositioned::OM) && (type <= FGPositioned::IM)) {
|
|
|
|
AirportRunwayPair arp(cache->findAirportRunway(name));
|
2012-11-28 13:41:58 +00:00
|
|
|
#if 0
|
|
|
|
// code is disabled since it's causing some problems, see
|
|
|
|
// http://code.google.com/p/flightgear-bugs/issues/detail?id=926
|
2012-08-28 00:26:36 +01:00
|
|
|
if (arp.second && (elev_ft < 0.01)) {
|
|
|
|
// snap to runway elevation
|
|
|
|
FGPositioned* runway = cache->loadById(arp.second);
|
|
|
|
assert(runway);
|
|
|
|
pos.setElevationFt(runway->geod().getElevationFt());
|
|
|
|
}
|
2012-11-28 13:41:58 +00:00
|
|
|
#endif
|
2012-08-28 00:26:36 +01:00
|
|
|
return cache->insertNavaid(type, string(), name, pos, 0, 0, 0,
|
|
|
|
arp.first, arp.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range < 0.01) {
|
|
|
|
range = defaultNavRange(ident, type);
|
2009-01-03 15:54:03 +00:00
|
|
|
}
|
|
|
|
|
2012-08-28 00:26:36 +01:00
|
|
|
AirportRunwayPair arp;
|
|
|
|
FGRunway* runway = NULL;
|
|
|
|
|
|
|
|
// FIXME - also relate DMEs, but only ILS/LOC DMEs - need a heuristic
|
|
|
|
// on the DME naming string
|
|
|
|
if ((type >= FGPositioned::ILS) && (type <= FGPositioned::GS)) {
|
|
|
|
arp = cache->findAirportRunway(name);
|
|
|
|
if (arp.second) {
|
|
|
|
runway = static_cast<FGRunway*>(cache->loadById(arp.second));
|
|
|
|
assert(runway);
|
|
|
|
if (elev_ft < 0.01) {
|
|
|
|
// snap to runway elevation
|
|
|
|
pos.setElevationFt(runway->geod().getElevationFt());
|
|
|
|
}
|
|
|
|
} // of found runway in the DB
|
|
|
|
} // of type is runway-related
|
|
|
|
|
|
|
|
bool isLoc = (type == FGPositioned::ILS) || (type == FGPositioned::LOC);
|
|
|
|
if (runway && autoAlignLocalizers && isLoc) {
|
|
|
|
alignLocaliserWithRunway(runway, ident, pos, multiuse);
|
|
|
|
}
|
|
|
|
|
2009-01-03 15:54:03 +00:00
|
|
|
// silently multiply adf frequencies by 100 so that adf
|
|
|
|
// vs. nav/loc frequency lookups can use the same code.
|
|
|
|
if (type == FGPositioned::NDB) {
|
|
|
|
freq *= 100;
|
|
|
|
}
|
|
|
|
|
2012-08-28 00:26:36 +01:00
|
|
|
PositionedID r = cache->insertNavaid(type, ident, name, pos, freq, range, multiuse,
|
|
|
|
arp.first, arp.second);
|
|
|
|
|
|
|
|
if (isLoc) {
|
|
|
|
cache->setRunwayILS(arp.second, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2009-01-03 15:54:03 +00:00
|
|
|
}
|
2012-08-28 00:26:36 +01:00
|
|
|
|
2004-05-28 05:24:54 +00:00
|
|
|
// load and initialize the navigational databases
|
2012-08-28 00:26:36 +01:00
|
|
|
bool navDBInit(const SGPath& path)
|
2004-05-28 05:24:54 +00:00
|
|
|
{
|
|
|
|
sg_gzifstream in( path.str() );
|
|
|
|
if ( !in.is_open() ) {
|
2012-11-23 21:14:40 +01:00
|
|
|
SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
|
2012-08-28 00:26:36 +01:00
|
|
|
return false;
|
2004-05-28 05:24:54 +00:00
|
|
|
}
|
2012-08-28 00:26:36 +01:00
|
|
|
|
|
|
|
autoAlignLocalizers = fgGetBool("/sim/navdb/localizers/auto-align", true);
|
|
|
|
autoAlignThreshold = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg", 5.0 );
|
2004-05-28 05:24:54 +00:00
|
|
|
|
|
|
|
// skip first two lines
|
|
|
|
in >> skipeol;
|
|
|
|
in >> skipeol;
|
|
|
|
|
2008-09-12 08:46:15 +00:00
|
|
|
while (!in.eof()) {
|
2012-08-28 00:26:36 +01:00
|
|
|
readNavFromStream(in);
|
2008-09-12 08:46:15 +00:00
|
|
|
in >> skipcomment;
|
|
|
|
} // of stream data loop
|
2012-08-28 00:26:36 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool loadCarrierNav(const SGPath& path)
|
|
|
|
{
|
2012-11-23 21:14:40 +01:00
|
|
|
SG_LOG( SG_NAVAID, SG_INFO, "opening file: " << path.str() );
|
2005-10-01 09:56:53 +00:00
|
|
|
sg_gzifstream incarrier( path.str() );
|
|
|
|
|
|
|
|
if ( !incarrier.is_open() ) {
|
2012-11-23 21:14:40 +01:00
|
|
|
SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
|
2012-08-28 00:26:36 +01:00
|
|
|
return false;
|
2005-10-01 09:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while ( ! incarrier.eof() ) {
|
2012-08-28 00:26:36 +01:00
|
|
|
// force the type to be MOBILE_TACAN
|
|
|
|
readNavFromStream(incarrier, FGPositioned::MOBILE_TACAN);
|
2005-10-01 09:56:53 +00:00
|
|
|
} // end while
|
|
|
|
|
2012-08-28 00:26:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadTacan(const SGPath& path, FGTACANList *channellist)
|
|
|
|
{
|
2012-11-23 21:14:40 +01:00
|
|
|
SG_LOG( SG_NAVAID, SG_INFO, "opening file: " << path.str() );
|
2005-10-01 09:56:53 +00:00
|
|
|
sg_gzifstream inchannel( path.str() );
|
|
|
|
|
|
|
|
if ( !inchannel.is_open() ) {
|
2012-11-23 21:14:40 +01:00
|
|
|
SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
|
2012-08-28 00:26:36 +01:00
|
|
|
return false;
|
2005-10-01 09:56:53 +00:00
|
|
|
}
|
|
|
|
|
2005-10-02 17:57:16 +00:00
|
|
|
// skip first line
|
2005-10-01 09:56:53 +00:00
|
|
|
inchannel >> skipeol;
|
|
|
|
while ( ! inchannel.eof() ) {
|
|
|
|
FGTACANRecord *r = new FGTACANRecord;
|
|
|
|
inchannel >> (*r);
|
|
|
|
channellist->add ( r );
|
|
|
|
|
|
|
|
} // end while
|
|
|
|
|
2004-05-28 05:24:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-07 17:55:45 +00:00
|
|
|
|
2012-08-28 00:26:36 +01:00
|
|
|
} // of namespace flightgear
|