| 1 | /* |
| 2 | * SPDX-FileName: apt_loader.hxx |
| 3 | * SPDX-FileComment: a front end loader of the apt.dat file. This loader populates the runway and basic classes. |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <string> |
| 11 | #include <unordered_map> |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <Navaids/positioned.hxx> |
| 16 | #include <Navaids/NavDataCache.hxx> |
| 17 | #include <simgear/compiler.h> |
| 18 | #include <simgear/math/SGGeod.hxx> |
| 19 | #include <simgear/misc/sg_path.hxx> |
| 20 | #include <simgear/structure/SGSharedPtr.hxx> |
| 21 | |
| 22 | #include "airport.hxx" |
| 23 | |
| 24 | |
| 25 | class sg_gzifstream; |
| 26 | class FGPavement; |
| 27 | |
| 28 | |
| 29 | namespace flightgear { |
| 30 | |
| 31 | class APTLoader |
| 32 | { |
| 33 | public: |
| 34 | APTLoader(); |
| 35 | virtual ~APTLoader(); |
| 36 | |
| 37 | // Read the specified apt.dat file into 'airportInfoMap'. |
| 38 | // 'bytesReadSoFar' and 'totalSizeOfAllAptDatFiles' are used for progress |
| 39 | // information. |
| 40 | void readAptDatFile(const NavDataCache::SceneryLocation& sceneryLocation, |
| 41 | std::size_t bytesReadSoFar, |
| 42 | std::size_t totalSizeOfAllAptDatFiles); |
| 43 | // Read all airports gathered in 'airportInfoMap' and load them into the |
| 44 | // navdata cache (even in case of overlapping apt.dat files, |
| 45 | // 'airportInfoMap' has only one entry per airport). |
| 46 | void loadAirports(); |
| 47 | |
| 48 | // Load a specific airport defined in aptdb_file, and return a "rich" view |
| 49 | // of the airport including taxiways, pavement and line features. |
| 50 | const FGAirport* loadAirportFromFile(const std::string& id, const NavDataCache::SceneryLocation& sceneryLocation); |
| 51 | |
| 52 | private: |
| 53 | struct Line { |
| 54 | Line(unsigned int number_, unsigned int rowCode_, const std::string& str_) |
| 55 | : number(number_), rowCode(rowCode_), str(str_) {} |
| 56 | |
| 57 | unsigned int number; |
| 58 | unsigned int rowCode; // Terminology of the apt.dat spec |
| 59 | std::string str; |
| 60 | }; |
| 61 | |
| 62 | typedef std::vector<Line> LinesList; |
| 63 | |
| 64 | struct RawAirportInfo { |
| 65 | // apt.dat file where the airport was defined |
| 66 | SGPath file; |
| 67 | // Base path of the corresponding scenery |
| 68 | SGPath sceneryPath; |
| 69 | // Row code for the airport (1, 16 or 17) |
| 70 | unsigned int rowCode; |
| 71 | // Line number in the apt.dat file where the airport definition starts |
| 72 | unsigned int firstLineNum; |
| 73 | // The whitespace-separated strings comprising the first line of the airport |
| 74 | // definition |
| 75 | std::vector<std::string> firstLineTokens; |
| 76 | // Subsequent lines of the airport definition (one element per line) |
| 77 | LinesList otherLines; |
| 78 | }; |
| 79 | |
| 80 | typedef std::unordered_map<std::string, RawAirportInfo> AirportInfoMapType; |
| 81 | typedef SGSharedPtr<FGPavement> FGPavementPtr; |
| 82 | typedef std::vector<FGPavementPtr> NodeList; |
| 83 | |
| 84 | APTLoader(const APTLoader&); // disable copy constructor |
| 85 | APTLoader& operator=(const APTLoader&); // disable copy-assignment operator |
| 86 | |
| 87 | const FGAirport* loadAirport(const SGPath& aptDat, const std::string& airportID, RawAirportInfo* airport_info, bool createFGAirport = false); |
| 88 | |
| 89 | // Tell whether an apt.dat line is blank or a comment line |
| 90 | bool isBlankOrCommentLine(const std::string& line); |
| 91 | // Return a copy of 'line' with trailing '\r' char(s) removed |
| 92 | std::string cleanLine(const std::string& line); |
| 93 | void throwExceptionIfStreamError(const sg_gzifstream& input_stream, |
| 94 | const SGPath& path); |
| 95 | void parseAirportLine(unsigned int rowCode, |
| 96 | const std::vector<std::string>& token, |
| 97 | const SGPath& sceneryPath); |
| 98 | void finishAirport(const std::string& aptDat); |
| 99 | void parseRunwayLine810(const std::string& aptDat, unsigned int lineNum, |
| 100 | const std::vector<std::string>& token); |
| 101 | void parseRunwayLine850(const std::string& aptDat, unsigned int lineNum, |
| 102 | const std::vector<std::string>& token); |
| 103 | void parseWaterRunwayLine850(const std::string& aptDat, unsigned int lineNum, |
| 104 | const std::vector<std::string>& token); |
| 105 | void parseHelipadLine850(const std::string& aptDat, unsigned int lineNum, |
| 106 | const std::vector<std::string>& token); |
| 107 | void parseViewpointLine(const std::string& aptDat, unsigned int lineNum, |
| 108 | const std::vector<std::string>& token); |
| 109 | void parsePavementLine850(const std::vector<std::string>& token); |
| 110 | void parseNodeLine850( |
| 111 | NodeList* nodelist, |
| 112 | const std::string& aptDat, unsigned int lineNum, int rowCode, |
| 113 | const std::vector<std::string>& token); |
| 114 | |
| 115 | void parseCommLine( |
| 116 | const std::string& aptDat, unsigned int lineNum, unsigned int rowCode, |
| 117 | const std::vector<std::string>& token); |
| 118 | |
| 119 | std::vector<std::string> token; |
| 120 | AirportInfoMapType airportInfoMap; |
| 121 | double rwy_lat_accum{0.0}; |
| 122 | double rwy_lon_accum{0.0}; |
| 123 | double last_rwy_heading{0.0}; |
| 124 | int rwy_count{0}; |
| 125 | std::string last_apt_id; |
| 126 | double last_apt_elev; |
| 127 | SGGeod tower; |
| 128 | |
| 129 | std::string pavement_ident; |
| 130 | NodeList pavements; |
| 131 | NodeList airport_boundary; |
| 132 | NodeList linear_feature; |
| 133 | |
| 134 | // Not an airport identifier in the sense of the apt.dat spec! |
| 135 | PositionedID currentAirportPosID; |
| 136 | NavDataCache* cache; |
| 137 | |
| 138 | // Enum to keep track of whether we are tracking a pavement, airport boundary |
| 139 | // or linear feature when parsing the file. |
| 140 | enum NodeBlock { None, |
| 141 | Pavement, |
| 142 | AirportBoundary, |
| 143 | LinearFeature }; |
| 144 | }; |
| 145 | |
| 146 | bool metarDataLoad(const SGPath& path); |
| 147 | |
| 148 | } // namespace flightgear |