1
0
Fork 0

Maintenance: apt_loader

SPDX tags.
parameter to loadAirportFormFile() changed to const&.
parameter to Line() ctor changed to const&.
parameter to loadAirport() changed to const&.
member variable initialization.
++prefix for complex types.
virtual dtor.
This commit is contained in:
scttgs0 2023-05-23 00:56:19 -05:00
parent 2c40595d3c
commit cda2337041
2 changed files with 691 additions and 718 deletions

View file

@ -1,65 +1,46 @@
// apt_loader.cxx -- a front end loader of the apt.dat file. This loader /*
// populates the runway and basic classes. * SPDX-FileName: apt_loader.cxx
// * SPDX-FileComment: a front end loader of the apt.dat file. This loader populates the runway and basic classes.
// Written by Curtis Olson, started August 2000. * SPDX-FileCopyrightText: Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
// * SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt */
//
// 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
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include <config.h> #include <config.h>
#endif #endif
#include "apt_loader.hxx"
#include <simgear/compiler.h>
#include <algorithm> #include <algorithm>
#include <cerrno>
#include <cstddef> // std::size_t
#include <ctype.h> // isspace()
#include <iostream>
#include <sstream> // std::istringstream
#include <stdlib.h> // atof(), atoi() #include <stdlib.h> // atof(), atoi()
#include <string.h> // memchr() #include <string.h> // memchr()
#include <ctype.h> // isspace() #include <string>
#include <cerrno> #include <utility> // std::pair, std::move()
#include <vector>
#include <simgear/compiler.h>
#include <simgear/constants.h> #include <simgear/constants.h>
#include <simgear/debug/logstream.hxx> #include <simgear/debug/logstream.hxx>
#include <simgear/io/iostreams/sgstream.hxx> #include <simgear/io/iostreams/sgstream.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/misc/strutils.hxx> #include <simgear/misc/strutils.hxx>
#include <simgear/structure/exception.hxx> #include <simgear/structure/exception.hxx>
#include <simgear/misc/sg_path.hxx>
#include <cstddef> // std::size_t #include <ATC/CommStation.hxx>
#include <string>
#include <vector>
#include <utility> // std::pair, std::move()
#include "airport.hxx"
#include "runways.hxx"
#include "pavement.hxx"
#include <Navaids/NavDataCache.hxx> #include <Navaids/NavDataCache.hxx>
#include <Navaids/positioned.hxx> #include <Navaids/positioned.hxx>
#include <ATC/CommStation.hxx>
#include <iostream> #include "airport.hxx"
#include <sstream> // std::istringstream #include "apt_loader.hxx"
#include "pavement.hxx"
#include "runways.hxx"
using std::vector;
using std::string; using std::string;
using std::vector;
namespace strutils = simgear::strutils; namespace strutils = simgear::strutils;
@ -76,28 +57,28 @@ static FGPositioned::Type fptypeFromRobinType(unsigned int aType)
} }
} }
namespace flightgear namespace flightgear {
{
APTLoader::APTLoader() APTLoader::APTLoader()
: last_apt_id(""), : last_apt_id(""),
last_apt_elev(0.0), last_apt_elev(0.0),
currentAirportPosID(0), currentAirportPosID(0),
cache(NavDataCache::instance()) cache(NavDataCache::instance())
{ } {
}
APTLoader::~APTLoader() { } APTLoader::~APTLoader() {}
void APTLoader::readAptDatFile(const SGPath &aptdb_file, void APTLoader::readAptDatFile(const SGPath& aptdb_file,
std::size_t bytesReadSoFar, std::size_t bytesReadSoFar,
std::size_t totalSizeOfAllAptDatFiles) std::size_t totalSizeOfAllAptDatFiles)
{ {
string apt_dat = aptdb_file.utf8Str(); // full path to the file being parsed string apt_dat = aptdb_file.utf8Str(); // full path to the file being parsed
sg_gzifstream in(aptdb_file, std::ios_base::in | std::ios_base::binary, true); sg_gzifstream in(aptdb_file, std::ios_base::in | std::ios_base::binary, true);
if ( !in.is_open() ) { if (!in.is_open()) {
const std::string errMsg = simgear::strutils::error_string(errno); const std::string errMsg = simgear::strutils::error_string(errno);
SG_LOG( SG_GENERAL, SG_ALERT, SG_LOG(SG_GENERAL, SG_ALERT,
"Cannot open file '" << apt_dat << "': " << errMsg ); "Cannot open file '" << apt_dat << "': " << errMsg);
throw sg_io_exception("Cannot open file (" + errMsg + ")", throw sg_io_exception("Cannot open file (" + errMsg + ")",
sg_location(aptdb_file)); sg_location(aptdb_file));
} }
@ -117,17 +98,17 @@ void APTLoader::readAptDatFile(const SGPath &aptdb_file,
bool skipAirport = true; bool skipAirport = true;
// Read the apt.dat header (two lines) // Read the apt.dat header (two lines)
while ( line_num < 2 && std::getline(in, line) ) { while (line_num < 2 && std::getline(in, line)) {
// 'line' may end with an \r character (tested on Linux, only \n was // 'line' may end with an \r character (tested on Linux, only \n was
// stripped: std::getline() only discards the _native_ line terminator) // stripped: std::getline() only discards the _native_ line terminator)
line_num++; line_num++;
if ( line_num == 1 ) { if (line_num == 1) {
std::string stripped_line = simgear::strutils::strip(line); std::string stripped_line = simgear::strutils::strip(line);
// First line indicates IBM ("I") or Macintosh ("A") line endings. // First line indicates IBM ("I") or Macintosh ("A") line endings.
if ( stripped_line != "I" && stripped_line != "A" ) { if (stripped_line != "I" && stripped_line != "A") {
std::string pb = "invalid first line (neither 'I' nor 'A')"; std::string pb = "invalid first line (neither 'I' nor 'A')";
SG_LOG( SG_GENERAL, SG_ALERT, aptdb_file << ": " << pb); SG_LOG(SG_GENERAL, SG_ALERT, aptdb_file << ": " << pb);
throw sg_format_exception("cannot parse '" + apt_dat + "': " + pb, throw sg_format_exception("cannot parse '" + apt_dat + "': " + pb,
stripped_line); stripped_line);
} }
@ -143,25 +124,23 @@ void APTLoader::readAptDatFile(const SGPath &aptdb_file,
unsigned int aptDatFormatVersion = unsigned int aptDatFormatVersion =
strutils::readNonNegativeInt<unsigned int>(fields[0]); strutils::readNonNegativeInt<unsigned int>(fields[0]);
SG_LOG(SG_GENERAL, SG_INFO, SG_LOG(SG_GENERAL, SG_INFO,
"apt.dat format version (" << apt_dat << "): " << "apt.dat format version (" << apt_dat << "): " << aptDatFormatVersion);
aptDatFormatVersion);
} }
} }
} // end of the apt.dat header } // end of the apt.dat header
throwExceptionIfStreamError(in, aptdb_file); throwExceptionIfStreamError(in, aptdb_file);
while ( std::getline(in, line) ) { while (std::getline(in, line)) {
// 'line' may end with an \r character, see above // 'line' may end with an \r character, see above
line_num++; line_num++;
if ( isBlankOrCommentLine(line) ) if (isBlankOrCommentLine(line))
continue; continue;
if ((line_num % 100) == 0) { if ((line_num % 100) == 0) {
// every 100 lines // every 100 lines
unsigned int percent = ((bytesReadSoFar + in.approxOffset()) * 100) unsigned int percent = ((bytesReadSoFar + in.approxOffset()) * 100) / totalSizeOfAllAptDatFiles;
/ totalSizeOfAllAptDatFiles;
cache->setRebuildPhaseProgress( cache->setRebuildPhaseProgress(
NavDataCache::REBUILD_READING_APT_DAT_FILES, percent); NavDataCache::REBUILD_READING_APT_DAT_FILES, percent);
} }
@ -169,14 +148,14 @@ void APTLoader::readAptDatFile(const SGPath &aptdb_file,
// Extract the first field into 'rowCode' // Extract the first field into 'rowCode'
rowCode = atoi(line.c_str()); rowCode = atoi(line.c_str());
if ( rowCode == 1 /* Airport */ || if (rowCode == 1 /* Airport */ ||
rowCode == 16 /* Seaplane base */ || rowCode == 16 /* Seaplane base */ ||
rowCode == 17 /* Heliport */ ) { rowCode == 17 /* Heliport */) {
vector<string> tokens(simgear::strutils::split(line)); vector<string> tokens(simgear::strutils::split(line));
if (tokens.size() < 6) { if (tokens.size() < 6) {
SG_LOG( SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
apt_dat << ":" << line_num << ": invalid airport header " apt_dat << ":" << line_num << ": invalid airport header "
"(at least 6 fields are required)" ); "(at least 6 fields are required)");
skipAirport = true; // discard everything until the next airport header skipAirport = true; // discard everything until the next airport header
continue; continue;
} }
@ -189,10 +168,9 @@ void APTLoader::readAptDatFile(const SGPath &aptdb_file,
AirportInfoMapType::value_type(currentAirportId, RawAirportInfo())); AirportInfoMapType::value_type(currentAirportId, RawAirportInfo()));
skipAirport = !insertRetval.second; skipAirport = !insertRetval.second;
if ( skipAirport ) { if (skipAirport) {
SG_LOG( SG_GENERAL, SG_INFO, SG_LOG(SG_GENERAL, SG_INFO,
apt_dat << ":" << line_num << ": skipping airport " << apt_dat << ":" << line_num << ": skipping airport " << currentAirportId << " (already defined earlier)");
currentAirportId << " (already defined earlier)" );
} else { } else {
// We haven't seen this airport yet in any apt.dat file // We haven't seen this airport yet in any apt.dat file
RawAirportInfo& airportInfo = insertRetval.first->second; RawAirportInfo& airportInfo = insertRetval.first->second;
@ -201,11 +179,11 @@ void APTLoader::readAptDatFile(const SGPath &aptdb_file,
airportInfo.firstLineNum = line_num; airportInfo.firstLineNum = line_num;
airportInfo.firstLineTokens = std::move(tokens); airportInfo.firstLineTokens = std::move(tokens);
} }
} else if ( rowCode == 99 ) { } else if (rowCode == 99) {
SG_LOG( SG_GENERAL, SG_DEBUG, SG_LOG(SG_GENERAL, SG_DEBUG,
apt_dat << ":" << line_num << ": code 99 found " apt_dat << ":" << line_num << ": code 99 found "
"(normally at end of file)" ); "(normally at end of file)");
} else if ( !skipAirport ) { } else if (!skipAirport) {
// Line belonging to an already started, and not skipped airport entry; // Line belonging to an already started, and not skipped airport entry;
// just append it. // just append it.
airportInfoMap[currentAirportId].otherLines.emplace_back( airportInfoMap[currentAirportId].otherLines.emplace_back(
@ -223,7 +201,7 @@ void APTLoader::loadAirports()
// Loop over all airports found in all apt.dat files // Loop over all airports found in all apt.dat files
for (AirportInfoMapType::const_iterator it = airportInfoMap.begin(); for (AirportInfoMapType::const_iterator it = airportInfoMap.begin();
it != airportInfoMap.end(); it++) { it != airportInfoMap.end(); ++it) {
// Full path to the apt.dat file this airport info comes from // Full path to the apt.dat file this airport info comes from
const string aptDat = it->second.file.utf8Str(); const string aptDat = it->second.file.utf8Str();
@ -242,12 +220,12 @@ void APTLoader::loadAirports()
} }
} // of loop over 'airportInfoMap' } // of loop over 'airportInfoMap'
SG_LOG( SG_GENERAL, SG_INFO, SG_LOG(SG_GENERAL, SG_INFO,
"Loaded data for " << nbLoadedAirports << " airports" ); "Loaded data for " << nbLoadedAirports << " airports");
} }
// Parse and return specific apt.dat file containing a single airport. // Parse and return specific apt.dat file containing a single airport.
const FGAirport* APTLoader::loadAirportFromFile(std::string id, const SGPath& aptdb_file) const FGAirport* APTLoader::loadAirportFromFile(const std::string& id, const SGPath& aptdb_file)
{ {
std::size_t bytesReadSoFar = 10; std::size_t bytesReadSoFar = 10;
std::size_t totalSizeOfAllAptDatFiles = 100; std::size_t totalSizeOfAllAptDatFiles = 100;
@ -263,7 +241,7 @@ static bool isCommLine(const int code)
return ((code >= 50) && (code <= 56)) || ((code >= 1050) && (code <= 1056)); return ((code >= 50) && (code <= 56)) || ((code >= 1050) && (code <= 1056));
} }
const FGAirport* APTLoader::loadAirport(const string aptDat, const std::string airportID, RawAirportInfo* airport_info, bool createFGAirport) const FGAirport* APTLoader::loadAirport(const string& aptDat, const std::string& airportID, RawAirportInfo* airport_info, bool createFGAirport)
{ {
// The first line for this airport was already split over whitespace, but // The first line for this airport was already split over whitespace, but
// remains to be parsed for the most part. // remains to be parsed for the most part.
@ -274,36 +252,36 @@ const FGAirport* APTLoader::loadAirport(const string aptDat, const std::string a
// Loop over the second and subsequent lines // Loop over the second and subsequent lines
for (LinesList::const_iterator linesIt = lines.begin(); for (LinesList::const_iterator linesIt = lines.begin();
linesIt != lines.end(); linesIt++) { linesIt != lines.end(); ++linesIt) {
// Beware that linesIt->str may end with an '\r' character, see above! // Beware that linesIt->str may end with an '\r' character, see above!
unsigned int rowCode = linesIt->rowCode; unsigned int rowCode = linesIt->rowCode;
if ( rowCode == 10 ) { // Runway v810 if (rowCode == 10) { // Runway v810
parseRunwayLine810(aptDat, linesIt->number, parseRunwayLine810(aptDat, linesIt->number,
simgear::strutils::split(linesIt->str)); simgear::strutils::split(linesIt->str));
} else if ( rowCode == 100 ) { // Runway v850 } else if (rowCode == 100) { // Runway v850
parseRunwayLine850(aptDat, linesIt->number, parseRunwayLine850(aptDat, linesIt->number,
simgear::strutils::split(linesIt->str)); simgear::strutils::split(linesIt->str));
} else if ( rowCode == 101 ) { // Water Runway v850 } else if (rowCode == 101) { // Water Runway v850
parseWaterRunwayLine850(aptDat, linesIt->number, parseWaterRunwayLine850(aptDat, linesIt->number,
simgear::strutils::split(linesIt->str)); simgear::strutils::split(linesIt->str));
} else if ( rowCode == 102 ) { // Helipad v850 } else if (rowCode == 102) { // Helipad v850
parseHelipadLine850(aptDat, linesIt->number, parseHelipadLine850(aptDat, linesIt->number,
simgear::strutils::split(linesIt->str)); simgear::strutils::split(linesIt->str));
} else if ( rowCode == 18 ) { } else if (rowCode == 18) {
// beacon entry (ignore) // beacon entry (ignore)
} else if ( rowCode == 14 ) { // Viewpoint/control tower } else if (rowCode == 14) { // Viewpoint/control tower
parseViewpointLine(aptDat, linesIt->number, parseViewpointLine(aptDat, linesIt->number,
simgear::strutils::split(linesIt->str)); simgear::strutils::split(linesIt->str));
} else if ( rowCode == 19 ) { } else if (rowCode == 19) {
// windsock entry (ignore) // windsock entry (ignore)
} else if ( rowCode == 20 ) { } else if (rowCode == 20) {
// Taxiway sign (ignore) // Taxiway sign (ignore)
} else if ( rowCode == 21 ) { } else if (rowCode == 21) {
// lighting objects (ignore) // lighting objects (ignore)
} else if ( rowCode == 15 ) { } else if (rowCode == 15) {
// custom startup locations (ignore) // custom startup locations (ignore)
} else if ( rowCode == 0 ) { } else if (rowCode == 0) {
// ?? // ??
} else if (isCommLine(rowCode)) { } else if (isCommLine(rowCode)) {
parseCommLine(aptDat, linesIt->number, rowCode, parseCommLine(aptDat, linesIt->number, rowCode,
@ -313,25 +291,24 @@ const FGAirport* APTLoader::loadAirport(const string aptDat, const std::string a
parsePavementLine850(simgear::strutils::split(linesIt->str, 0, 4)); parsePavementLine850(simgear::strutils::split(linesIt->str, 0, 4));
} else if (rowCode >= 111 && rowCode <= 116) { } else if (rowCode >= 111 && rowCode <= 116) {
switch (current_block) { switch (current_block) {
case Pavement : case Pavement:
parseNodeLine850(&pavements, aptDat, linesIt->number, rowCode, parseNodeLine850(&pavements, aptDat, linesIt->number, rowCode,
simgear::strutils::split(linesIt->str)); simgear::strutils::split(linesIt->str));
break; break;
case AirportBoundary : case AirportBoundary:
parseNodeLine850(&airport_boundary, aptDat, linesIt->number, rowCode, parseNodeLine850(&airport_boundary, aptDat, linesIt->number, rowCode,
simgear::strutils::split(linesIt->str)); simgear::strutils::split(linesIt->str));
break; break;
case LinearFeature : case LinearFeature:
parseNodeLine850(&linear_feature, aptDat, linesIt->number, rowCode, parseNodeLine850(&linear_feature, aptDat, linesIt->number, rowCode,
simgear::strutils::split(linesIt->str)); simgear::strutils::split(linesIt->str));
break; break;
default : default:
case None : case None:
std::ostringstream oss; std::ostringstream oss;
string cleanedLine = cleanLine(linesIt->str); string cleanedLine = cleanLine(linesIt->str);
oss << aptDat << ":" << linesIt->number << ": unexpected row code " << oss << aptDat << ":" << linesIt->number << ": unexpected row code " << rowCode;
rowCode; SG_LOG(SG_GENERAL, SG_ALERT, oss.str() << " (" << cleanedLine << ")");
SG_LOG( SG_GENERAL, SG_ALERT, oss.str() << " (" << cleanedLine << ")" );
throw sg_format_exception(oss.str(), cleanedLine); throw sg_format_exception(oss.str(), cleanedLine);
break; break;
} }
@ -358,17 +335,17 @@ const FGAirport* APTLoader::loadAirport(const string aptDat, const std::string a
std::for_each( std::for_each(
pavements.begin(), pavements.begin(),
pavements.end(), pavements.end(),
[airport] (FGPavementRef p) { airport->addPavement(p); } ); [airport](FGPavementRef p) { airport->addPavement(p); });
std::for_each( std::for_each(
airport_boundary.begin(), airport_boundary.begin(),
airport_boundary.end(), airport_boundary.end(),
[airport] (FGPavementRef p) { airport->addBoundary(p); } ); [airport](FGPavementRef p) { airport->addBoundary(p); });
std::for_each( std::for_each(
linear_feature.begin(), linear_feature.begin(),
linear_feature.end(), linear_feature.end(),
[airport] (FGPavementRef p) { airport->addLineFeature(p); } ); [airport](FGPavementRef p) { airport->addLineFeature(p); });
pavements.clear(); pavements.clear();
airport_boundary.clear(); airport_boundary.clear();
@ -388,9 +365,9 @@ const FGAirport* APTLoader::loadAirport(const string aptDat, const std::string a
bool APTLoader::isBlankOrCommentLine(const std::string& line) bool APTLoader::isBlankOrCommentLine(const std::string& line)
{ {
size_t pos = line.find_first_not_of(" \t"); size_t pos = line.find_first_not_of(" \t");
return ( pos == std::string::npos || return (pos == std::string::npos ||
line[pos] == '\r' || line[pos] == '\r' ||
line.find("##", pos) == pos ); line.find("##", pos) == pos);
} }
std::string APTLoader::cleanLine(const std::string& line) std::string APTLoader::cleanLine(const std::string& line)
@ -400,9 +377,9 @@ std::string APTLoader::cleanLine(const std::string& line)
// Lines obtained from readAptDatFile() may end with \r, which can be quite // Lines obtained from readAptDatFile() may end with \r, which can be quite
// confusing when printed to the terminal. // confusing when printed to the terminal.
for (std::string::reverse_iterator it = res.rbegin(); for (std::string::reverse_iterator it = res.rbegin();
it != res.rend() && *it == '\r'; /* empty */) it != res.rend() && *it == '\r';
{ // The beauty of C++ iterators... /* empty */) { // The beauty of C++ iterators...
it = std::string::reverse_iterator(res.erase( (it+1).base() )); it = std::string::reverse_iterator(res.erase((it + 1).base()));
} }
return res; return res;
@ -414,8 +391,8 @@ void APTLoader::throwExceptionIfStreamError(const sg_gzifstream& input_stream,
if (input_stream.bad()) { if (input_stream.bad()) {
const std::string errMsg = simgear::strutils::error_string(errno); const std::string errMsg = simgear::strutils::error_string(errno);
SG_LOG( SG_NAVAID, SG_ALERT, SG_LOG(SG_NAVAID, SG_ALERT,
"Error while reading '" << path.utf8Str() << "': " << errMsg ); "Error while reading '" << path.utf8Str() << "': " << errMsg);
throw sg_io_exception("APTLoader: error reading file (" + errMsg + ")", throw sg_io_exception("APTLoader: error reading file (" + errMsg + ")",
sg_location(path)); sg_location(path));
} }
@ -429,8 +406,7 @@ void APTLoader::finishAirport(const string& aptDat)
if (!rwy_count) { if (!rwy_count) {
currentAirportPosID = 0; currentAirportPosID = 0;
SG_LOG( SG_GENERAL, SG_ALERT, "Error in '" << aptDat << SG_LOG(SG_GENERAL, SG_ALERT, "Error in '" << aptDat << "': no runways for " << last_apt_id << ", skipping.");
"': no runways for " << last_apt_id << ", skipping." );
return; return;
} }
@ -451,12 +427,12 @@ void APTLoader::parseAirportLine(unsigned int rowCode,
// The algorithm in APTLoader::readAptDatFile() ensures this is at least 5. // The algorithm in APTLoader::readAptDatFile() ensures this is at least 5.
vector<string>::size_type lastIndex = token.size() - 1; vector<string>::size_type lastIndex = token.size() - 1;
const string& id(token[4]); const string& id(token[4]);
double elev = atof( token[1].c_str() ); double elev = atof(token[1].c_str());
last_apt_elev = elev; last_apt_elev = elev;
string name; string name;
// build the name // build the name
for ( vector<string>::size_type i = 5; i < lastIndex; ++i ) { for (vector<string>::size_type i = 5; i < lastIndex; ++i) {
name += token[i] + " "; name += token[i] + " ";
} }
name += token[lastIndex]; name += token[lastIndex];
@ -474,33 +450,33 @@ void APTLoader::parseRunwayLine810(const string& aptDat, unsigned int lineNum,
const vector<string>& token) const vector<string>& token)
{ {
if (token.size() < 11) { if (token.size() < 11) {
SG_LOG( SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v810 runway line " << aptDat << ":" << lineNum << ": invalid v810 runway line "
"(row code 10): at least 11 fields are required" ); << "(row code 10): at least 11 fields are required");
return; return;
} }
double lat = atof( token[1].c_str() ); double lat = atof(token[1].c_str());
double lon = atof( token[2].c_str() ); double lon = atof(token[2].c_str());
rwy_lat_accum += lat; rwy_lat_accum += lat;
rwy_lon_accum += lon; rwy_lon_accum += lon;
rwy_count++; rwy_count++;
const string& rwy_no(token[3]); const string& rwy_no(token[3]);
double heading = atof( token[4].c_str() ); double heading = atof(token[4].c_str());
double length = atoi( token[5].c_str() ); double length = atoi(token[5].c_str());
double width = atoi( token[8].c_str() ); double width = atoi(token[8].c_str());
length *= SG_FEET_TO_METER; length *= SG_FEET_TO_METER;
width *= SG_FEET_TO_METER; width *= SG_FEET_TO_METER;
// adjust lat / lon to the start of the runway/taxiway, not the middle // adjust lat / lon to the start of the runway/taxiway, not the middle
SGGeod pos_1 = SGGeodesy::direct( SGGeod::fromDegFt(lon, lat, last_apt_elev), SGGeod pos_1 = SGGeodesy::direct(SGGeod::fromDegFt(lon, lat, last_apt_elev),
heading, -length/2 ); heading, -length / 2);
last_rwy_heading = heading; last_rwy_heading = heading;
int surface_code = atoi( token[10].c_str() ); int surface_code = atoi(token[10].c_str());
if (rwy_no[0] == 'x') { // Taxiway if (rwy_no[0] == 'x') { // Taxiway
cache->insertRunway( cache->insertRunway(
@ -513,22 +489,20 @@ void APTLoader::parseRunwayLine810(const string& aptDat, unsigned int lineNum,
} else { } else {
// (pair of) runways // (pair of) runways
string rwy_displ_threshold = token[6]; string rwy_displ_threshold = token[6];
vector<string> displ vector<string> displ = simgear::strutils::split(rwy_displ_threshold, ".");
= simgear::strutils::split( rwy_displ_threshold, "." ); double displ_thresh1 = atof(displ[0].c_str());
double displ_thresh1 = atof( displ[0].c_str() ); double displ_thresh2 = atof(displ[1].c_str());
double displ_thresh2 = atof( displ[1].c_str() );
displ_thresh1 *= SG_FEET_TO_METER; displ_thresh1 *= SG_FEET_TO_METER;
displ_thresh2 *= SG_FEET_TO_METER; displ_thresh2 *= SG_FEET_TO_METER;
string rwy_stopway = token[7]; string rwy_stopway = token[7];
vector<string> stop vector<string> stop = simgear::strutils::split(rwy_stopway, ".");
= simgear::strutils::split( rwy_stopway, "." ); double stopway1 = atof(stop[0].c_str());
double stopway1 = atof( stop[0].c_str() ); double stopway2 = atof(stop[1].c_str());
double stopway2 = atof( stop[1].c_str() );
stopway1 *= SG_FEET_TO_METER; stopway1 *= SG_FEET_TO_METER;
stopway2 *= SG_FEET_TO_METER; stopway2 *= SG_FEET_TO_METER;
SGGeod pos_2 = SGGeodesy::direct( pos_1, heading, length ); SGGeod pos_2 = SGGeodesy::direct(pos_1, heading, length);
PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos_1, PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no, pos_1,
currentAirportPosID, heading, length, currentAirportPosID, heading, length,
@ -551,61 +525,61 @@ void APTLoader::parseRunwayLine850(const string& aptDat, unsigned int lineNum,
const vector<string>& token) const vector<string>& token)
{ {
if (token.size() < 26) { if (token.size() < 26) {
SG_LOG( SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v850 runway line " << aptDat << ":" << lineNum << ": invalid v850 runway line "
"(row code 100): at least 26 fields are required" ); << "(row code 100): at least 26 fields are required");
return; return;
} }
double width = atof( token[1].c_str() ); double width = atof(token[1].c_str());
int surface_code = atoi( token[2].c_str() ); int surface_code = atoi(token[2].c_str());
int shoulder_code = atoi( token[3].c_str() ); int shoulder_code = atoi(token[3].c_str());
float smoothness = atof( token[4].c_str() ); float smoothness = atof(token[4].c_str());
int center_lights = atoi( token[5].c_str() ); int center_lights = atoi(token[5].c_str());
int edge_lights = atoi( token[6].c_str() ); int edge_lights = atoi(token[6].c_str());
int distance_remaining = atoi( token[7].c_str() ); int distance_remaining = atoi(token[7].c_str());
double lat_1 = atof( token[9].c_str() ); double lat_1 = atof(token[9].c_str());
double lon_1 = atof( token[10].c_str() ); double lon_1 = atof(token[10].c_str());
SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0)); SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
rwy_lat_accum += lat_1; rwy_lat_accum += lat_1;
rwy_lon_accum += lon_1; rwy_lon_accum += lon_1;
rwy_count++; rwy_count++;
double lat_2 = atof( token[18].c_str() ); double lat_2 = atof(token[18].c_str());
double lon_2 = atof( token[19].c_str() ); double lon_2 = atof(token[19].c_str());
SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0)); SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
rwy_lat_accum += lat_2; rwy_lat_accum += lat_2;
rwy_lon_accum += lon_2; rwy_lon_accum += lon_2;
rwy_count++; rwy_count++;
double length, heading_1, heading_2; double length, heading_1, heading_2;
SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length ); SGGeodesy::inverse(pos_1, pos_2, heading_1, heading_2, length);
last_rwy_heading = heading_1; last_rwy_heading = heading_1;
const string& rwy_no_1(token[8]); const string& rwy_no_1(token[8]);
const string& rwy_no_2(token[17]); const string& rwy_no_2(token[17]);
if ( rwy_no_1.empty() || rwy_no_2.empty() ) // these tests are weird... if (rwy_no_1.empty() || rwy_no_2.empty()) // these tests are weird...
return; return;
double displ_thresh1 = atof( token[11].c_str() ); double displ_thresh1 = atof(token[11].c_str());
double displ_thresh2 = atof( token[20].c_str() ); double displ_thresh2 = atof(token[20].c_str());
double stopway1 = atof( token[12].c_str() ); double stopway1 = atof(token[12].c_str());
double stopway2 = atof( token[21].c_str() ); double stopway2 = atof(token[21].c_str());
int markings1 = atoi( token[13].c_str() ); int markings1 = atoi(token[13].c_str());
int markings2 = atoi( token[22].c_str() ); int markings2 = atoi(token[22].c_str());
int approach1 = atoi( token[14].c_str() ); int approach1 = atoi(token[14].c_str());
int approach2 = atoi( token[23].c_str() ); int approach2 = atoi(token[23].c_str());
int tdz1 = atoi( token[15].c_str() ); int tdz1 = atoi(token[15].c_str());
int tdz2 = atoi( token[24].c_str() ); int tdz2 = atoi(token[24].c_str());
int reil1 = atoi( token[16].c_str() ); int reil1 = atoi(token[16].c_str());
int reil2 = atoi( token[25].c_str() ); int reil2 = atoi(token[25].c_str());
PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1, PositionedID rwy = cache->insertRunway(FGPositioned::RUNWAY, rwy_no_1, pos_1,
currentAirportPosID, heading_1, length, currentAirportPosID, heading_1, length,
@ -631,30 +605,30 @@ void APTLoader::parseWaterRunwayLine850(const string& aptDat,
const vector<string>& token) const vector<string>& token)
{ {
if (token.size() < 9) { if (token.size() < 9) {
SG_LOG( SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v850 water runway line " << aptDat << ":" << lineNum << ": invalid v850 water runway line "
"(row code 101): at least 9 fields are required" ); << "(row code 101): at least 9 fields are required");
return; return;
} }
double width = atof( token[1].c_str() ); double width = atof(token[1].c_str());
double lat_1 = atof( token[4].c_str() ); double lat_1 = atof(token[4].c_str());
double lon_1 = atof( token[5].c_str() ); double lon_1 = atof(token[5].c_str());
SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0)); SGGeod pos_1(SGGeod::fromDegFt(lon_1, lat_1, 0.0));
rwy_lat_accum += lat_1; rwy_lat_accum += lat_1;
rwy_lon_accum += lon_1; rwy_lon_accum += lon_1;
rwy_count++; rwy_count++;
double lat_2 = atof( token[7].c_str() ); double lat_2 = atof(token[7].c_str());
double lon_2 = atof( token[8].c_str() ); double lon_2 = atof(token[8].c_str());
SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0)); SGGeod pos_2(SGGeod::fromDegFt(lon_2, lat_2, 0.0));
rwy_lat_accum += lat_2; rwy_lat_accum += lat_2;
rwy_lon_accum += lon_2; rwy_lon_accum += lon_2;
rwy_count++; rwy_count++;
double length, heading_1, heading_2; double length, heading_1, heading_2;
SGGeodesy::inverse( pos_1, pos_2, heading_1, heading_2, length ); SGGeodesy::inverse(pos_1, pos_2, heading_1, heading_2, length);
last_rwy_heading = heading_1; last_rwy_heading = heading_1;
@ -680,32 +654,32 @@ void APTLoader::parseHelipadLine850(const string& aptDat, unsigned int lineNum,
const vector<string>& token) const vector<string>& token)
{ {
if (token.size() < 12) { if (token.size() < 12) {
SG_LOG( SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v850 helipad line " << aptDat << ":" << lineNum << ": invalid v850 helipad line "
"(row code 102): at least 12 fields are required" ); << "(row code 102): at least 12 fields are required");
return; return;
} }
double length = atof( token[5].c_str() ); double length = atof(token[5].c_str());
double width = atof( token[6].c_str() ); double width = atof(token[6].c_str());
double lat = atof( token[2].c_str() ); double lat = atof(token[2].c_str());
double lon = atof( token[3].c_str() ); double lon = atof(token[3].c_str());
SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0)); SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
rwy_lat_accum += lat; rwy_lat_accum += lat;
rwy_lon_accum += lon; rwy_lon_accum += lon;
rwy_count++; rwy_count++;
double heading = atof( token[4].c_str() ); double heading = atof(token[4].c_str());
last_rwy_heading = heading; last_rwy_heading = heading;
const string& rwy_no(token[1]); const string& rwy_no(token[1]);
int surface_code = atoi( token[7].c_str() ); int surface_code = atoi(token[7].c_str());
int markings = atoi( token[8].c_str() ); int markings = atoi(token[8].c_str());
int shoulder_code = atoi( token[9].c_str() ); int shoulder_code = atoi(token[9].c_str());
float smoothness = atof( token[10].c_str() ); float smoothness = atof(token[10].c_str());
int edge_lights = atoi( token[11].c_str() ); int edge_lights = atoi(token[11].c_str());
cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos, cache->insertRunway(FGPositioned::HELIPAD, rwy_no, pos,
currentAirportPosID, heading, length, currentAirportPosID, heading, length,
@ -717,9 +691,9 @@ void APTLoader::parseViewpointLine(const string& aptDat, unsigned int lineNum,
const vector<string>& token) const vector<string>& token)
{ {
if (token.size() < 5) { if (token.size() < 5) {
SG_LOG( SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid viewpoint line " aptDat << ":" << lineNum << ": invalid viewpoint line "
"(row code 14): at least 5 fields are required" ); "(row code 14): at least 5 fields are required");
} else { } else {
double lat = atof(token[1].c_str()); double lat = atof(token[1].c_str());
double lon = atof(token[2].c_str()); double lon = atof(token[2].c_str());
@ -731,17 +705,17 @@ void APTLoader::parseViewpointLine(const string& aptDat, unsigned int lineNum,
void APTLoader::parsePavementLine850(const vector<string>& token) void APTLoader::parsePavementLine850(const vector<string>& token)
{ {
if ( token.size() >= 5 ) { if (token.size() >= 5) {
pavement_ident = token[4]; pavement_ident = token[4];
if ( !pavement_ident.empty() && if (!pavement_ident.empty() &&
pavement_ident[pavement_ident.size()-1] == '\r' ) pavement_ident[pavement_ident.size() - 1] == '\r')
pavement_ident.erase( pavement_ident.size()-1 ); pavement_ident.erase(pavement_ident.size() - 1);
} else { } else {
pavement_ident = "xx"; pavement_ident = "xx";
} }
} }
void APTLoader::parseNodeLine850(NodeList *nodelist, void APTLoader::parseNodeLine850(NodeList* nodelist,
const string& aptDat, const string& aptDat,
unsigned int lineNum, int rowCode, unsigned int lineNum, int rowCode,
const vector<string>& token) const vector<string>& token)
@ -749,22 +723,21 @@ void APTLoader::parseNodeLine850(NodeList *nodelist,
static const unsigned int minNbTokens[] = {3, 5, 3, 5, 3, 5}; static const unsigned int minNbTokens[] = {3, 5, 3, 5, 3, 5};
assert(111 <= rowCode && rowCode <= 116); assert(111 <= rowCode && rowCode <= 116);
if (token.size() < minNbTokens[rowCode-111]) { if (token.size() < minNbTokens[rowCode - 111]) {
SG_LOG( SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v850 node line " << aptDat << ":" << lineNum << ": invalid v850 node line "
"(row code " << rowCode << "): at least " << << "(row code " << rowCode << "): at least " << minNbTokens[rowCode - 111] << " fields are required");
minNbTokens[rowCode-111] << " fields are required" );
return; return;
} }
double lat = atof( token[1].c_str() ); double lat = atof(token[1].c_str());
double lon = atof( token[2].c_str() ); double lon = atof(token[2].c_str());
SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0)); SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
FGPavement* pvt = 0; FGPavement* pvt = 0;
if (( !pavement_ident.empty() ) || ( nodelist->size() == 0 )) { if ((!pavement_ident.empty()) || (nodelist->size() == 0)) {
pvt = new FGPavement( 0, pavement_ident, pos ); pvt = new FGPavement(0, pavement_ident, pos);
nodelist->push_back( pvt ); nodelist->push_back(pvt);
pavement_ident = ""; pavement_ident = "";
} else { } else {
pvt = nodelist->back(); pvt = nodelist->back();
@ -775,20 +748,20 @@ void APTLoader::parseNodeLine850(NodeList *nodelist,
// Line information. The 2nd last token is the painted line type. Last token // Line information. The 2nd last token is the painted line type. Last token
// is the light type of the segment. Only applicable to codes 111-114. // is the light type of the segment. Only applicable to codes 111-114.
if ((rowCode < 115) && (token.size() == (minNbTokens[rowCode-111] + 1))) { if ((rowCode < 115) && (token.size() == (minNbTokens[rowCode - 111] + 1))) {
// We've got a line paint code but no lighting code // We've got a line paint code but no lighting code
paintCode = atoi(token[minNbTokens[rowCode-111]].c_str()); paintCode = atoi(token[minNbTokens[rowCode - 111]].c_str());
} }
if ((rowCode < 115) && (token.size() == (minNbTokens[rowCode-111] + 2))) { if ((rowCode < 115) && (token.size() == (minNbTokens[rowCode - 111] + 2))) {
// We've got a line paint code and a lighting code // We've got a line paint code and a lighting code
paintCode = atoi(token[minNbTokens[rowCode-111] -1].c_str()); paintCode = atoi(token[minNbTokens[rowCode - 111] - 1].c_str());
lightCode = atoi(token[minNbTokens[rowCode-111]].c_str()); lightCode = atoi(token[minNbTokens[rowCode - 111]].c_str());
} }
if ((rowCode == 112) || (rowCode == 114) || (rowCode == 116)) { if ((rowCode == 112) || (rowCode == 114) || (rowCode == 116)) {
double lat_b = atof( token[3].c_str() ); double lat_b = atof(token[3].c_str());
double lon_b = atof( token[4].c_str() ); double lon_b = atof(token[4].c_str());
SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0)); SGGeod pos_b(SGGeod::fromDegFt(lon_b, lat_b, 0.0));
pvt->addBezierNode(pos, pos_b, (rowCode == 114) || (rowCode == 116), (rowCode == 114), paintCode, lightCode); pvt->addBezierNode(pos, pos_b, (rowCode == 114) || (rowCode == 116), (rowCode == 114), paintCode, lightCode);
} else { } else {
@ -801,14 +774,14 @@ void APTLoader::parseCommLine(const string& aptDat,
const vector<string>& token) const vector<string>& token)
{ {
if (token.size() < 3) { if (token.size() < 3) {
SG_LOG( SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid Comm Frequency line " << aptDat << ":" << lineNum << ": invalid Comm Frequency line "
"(row code " << rowCode << "): at least 3 fields are required" ); << "(row code " << rowCode << "): at least 3 fields are required");
return; return;
} else if (rwy_count <= 0) { } else if (rwy_count <= 0) {
SG_LOG( SG_GENERAL, SG_ALERT, SG_LOG(SG_GENERAL, SG_ALERT,
aptDat << ":" << lineNum << ": no runways, skipping Comm " << aptDat << ":" << lineNum << ": no runways, skipping Comm "
"Frequency line for " << last_apt_id ); << "Frequency line for " << last_apt_id);
// There used to be no 'return' here. Not sure how useful this is, but // There used to be no 'return' here. Not sure how useful this is, but
// clearly this code block doesn't make sense without it, so here it is. // clearly this code block doesn't make sense without it, so here it is.
return; return;
@ -859,10 +832,8 @@ void APTLoader::parseCommLine(const string& aptDat,
switch (rowCode) { switch (rowCode) {
case 50: case 50:
ty = FGPositioned::FREQ_AWOS; ty = FGPositioned::FREQ_AWOS;
for (size_t i = 2; i < token.size(); ++i) for (size_t i = 2; i < token.size(); ++i) {
{ if (token[i] == "ATIS") {
if (token[i] == "ATIS")
{
ty = FGPositioned::FREQ_ATIS; ty = FGPositioned::FREQ_ATIS;
break; break;
} }
@ -892,18 +863,18 @@ void APTLoader::parseCommLine(const string& aptDat,
// The 'metar.dat' file lists the airports that have METAR available. // The 'metar.dat' file lists the airports that have METAR available.
bool metarDataLoad(const SGPath& metar_file) bool metarDataLoad(const SGPath& metar_file)
{ {
sg_gzifstream metar_in( metar_file ); sg_gzifstream metar_in(metar_file);
if ( !metar_in.is_open() ) { if (!metar_in.is_open()) {
SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file ); SG_LOG(SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file);
return false; return false;
} }
NavDataCache* cache = NavDataCache::instance(); NavDataCache* cache = NavDataCache::instance();
string ident; string ident;
while ( metar_in ) { while (metar_in) {
metar_in >> ident; metar_in >> ident;
if ( ident == "#" || ident == "//" ) { if (ident == "#" || ident == "//") {
metar_in >> skipeol; metar_in >> skipeol;
} else { } else {
cache->setAirportMetar(ident, true); cache->setAirportMetar(ident, true);
@ -913,4 +884,4 @@ bool metarDataLoad(const SGPath& metar_file)
return true; return true;
} }
} // of namespace flightgear } // namespace flightgear

View file

@ -7,7 +7,6 @@
#pragma once #pragma once
#include "airport.hxx"
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -18,18 +17,21 @@
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_path.hxx>
#include <simgear/structure/SGSharedPtr.hxx> #include <simgear/structure/SGSharedPtr.hxx>
#include "airport.hxx"
class NavDataCache; class NavDataCache;
class sg_gzifstream; class sg_gzifstream;
class FGPavement; class FGPavement;
namespace flightgear { namespace flightgear {
class APTLoader class APTLoader
{ {
public: public:
APTLoader(); APTLoader();
~APTLoader(); virtual ~APTLoader();
// Read the specified apt.dat file into 'airportInfoMap'. // Read the specified apt.dat file into 'airportInfoMap'.
// 'bytesReadSoFar' and 'totalSizeOfAllAptDatFiles' are used for progress // 'bytesReadSoFar' and 'totalSizeOfAllAptDatFiles' are used for progress
@ -43,11 +45,11 @@ public:
// Load a specific airport defined in aptdb_file, and return a "rich" view // Load a specific airport defined in aptdb_file, and return a "rich" view
// of the airport including taxiways, pavement and line features. // of the airport including taxiways, pavement and line features.
const FGAirport* loadAirportFromFile(std::string id, const SGPath& aptdb_file); const FGAirport* loadAirportFromFile(const std::string& id, const SGPath& aptdb_file);
private: private:
struct Line { struct Line {
Line(unsigned int number_, unsigned int rowCode_, std::string str_) Line(unsigned int number_, unsigned int rowCode_, const std::string& str_)
: number(number_), rowCode(rowCode_), str(str_) {} : number(number_), rowCode(rowCode_), str(str_) {}
unsigned int number; unsigned int number;
@ -78,7 +80,7 @@ private:
APTLoader(const APTLoader&); // disable copy constructor APTLoader(const APTLoader&); // disable copy constructor
APTLoader& operator=(const APTLoader&); // disable copy-assignment operator APTLoader& operator=(const APTLoader&); // disable copy-assignment operator
const FGAirport* loadAirport(const std::string aptDat, const std::string airportID, RawAirportInfo* airport_info, bool createFGAirport = false); const FGAirport* loadAirport(const std::string& aptDat, const std::string& airportID, RawAirportInfo* airport_info, bool createFGAirport = false);
// Tell whether an apt.dat line is blank or a comment line // Tell whether an apt.dat line is blank or a comment line
bool isBlankOrCommentLine(const std::string& line); bool isBlankOrCommentLine(const std::string& line);
@ -111,10 +113,10 @@ private:
std::vector<std::string> token; std::vector<std::string> token;
AirportInfoMapType airportInfoMap; AirportInfoMapType airportInfoMap;
double rwy_lat_accum; double rwy_lat_accum{0.0};
double rwy_lon_accum; double rwy_lon_accum{0.0};
double last_rwy_heading; double last_rwy_heading{0.0};
int rwy_count; int rwy_count{0};
std::string last_apt_id; std::string last_apt_id;
double last_apt_elev; double last_apt_elev;
SGGeod tower; SGGeod tower;