| 1 | |
| 2 | /* |
| 3 | * SPDX-FileCopyrightText: (C) 2012 James Turner <[email protected]> |
| 4 | * SPDX_FileComment: Defines a unified binary cache for navigation data, parsed from text/XMl sources |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <cstddef> // for std::size_t |
| 11 | #include <functional> |
| 12 | #include <memory> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <Navaids/positioned.hxx> |
| 16 | #include <simgear/misc/sg_path.hxx> |
| 17 | #include <simgear/misc/strutils.hxx> // for string_list |
| 18 | |
| 19 | |
| 20 | class SGPath; |
| 21 | class FGRunway; |
| 22 | |
| 23 | namespace flightgear { |
| 24 | |
| 25 | /// a pair of airport ID, runway ID |
| 26 | typedef std::pair<PositionedID, PositionedID> AirportRunwayPair; |
| 27 | |
| 28 | typedef std::pair<FGPositioned::Type, PositionedID> TypedPositioned; |
| 29 | typedef std::vector<TypedPositioned> TypedPositionedVec; |
| 30 | |
| 31 | // pair of airway ID, destination node ID |
| 32 | typedef std::pair<int, PositionedID> AirwayEdge; |
| 33 | typedef std::vector<AirwayEdge> AirwayEdgeVec; |
| 34 | |
| 35 | namespace Octree { |
| 36 | class Node; |
| 37 | class Branch; |
| 38 | } // namespace Octree |
| 39 | |
| 40 | class Airway; |
| 41 | using AirwayRef = SGSharedPtr<Airway>; |
| 42 | |
| 43 | class NavDataCache |
| 44 | { |
| 45 | public: |
| 46 | ~NavDataCache(); |
| 47 | |
| 48 | // singleton accessor |
| 49 | static NavDataCache* instance(); |
| 50 | |
| 51 | // static creator |
| 52 | static NavDataCache* createInstance(); |
| 53 | |
| 54 | static void shutdown(); |
| 55 | |
| 56 | SGPath path() const; |
| 57 | |
| 58 | enum DatFileType { |
| 59 | DATFILETYPE_APT = 0, |
| 60 | DATFILETYPE_METAR, |
| 61 | DATFILETYPE_AWY, |
| 62 | DATFILETYPE_NAV, |
| 63 | DATFILETYPE_FIX, |
| 64 | DATFILETYPE_POI, |
| 65 | DATFILETYPE_CARRIER, |
| 66 | DATFILETYPE_TACAN_FREQ, |
| 67 | DATFILETYPE_LAST |
| 68 | }; |
| 69 | |
| 70 | struct SceneryLocation { |
| 71 | SGPath datPath; |
| 72 | SGPath sceneryPath; // scenery realpath() the dat file belongs to |
| 73 | }; |
| 74 | |
| 75 | using SceneryLocationList = std::vector<SceneryLocation>; |
| 76 | |
| 77 | struct DatFilesGroupInfo { |
| 78 | DatFileType datFileType; // for instance, DATFILETYPE_APT |
| 79 | SceneryLocationList paths; |
| 80 | std::size_t totalSize; // total size of all these files, in bytes |
| 81 | }; |
| 82 | |
| 83 | // datTypeStr[DATFILETYPE_APT] = std::string("apt"), etc. This gives, |
| 84 | // among other things, the subdirectory of $scenery_path/NavData where |
| 85 | // each type of dat file is looked for. |
| 86 | static const std::string datTypeStr[]; |
| 87 | // defaultDatFile[DATFILETYPE_APT] = std::string("Airports/apt.dat.gz"), |
| 88 | // etc. This tells where to find the historical dat files: those under |
| 89 | // $FG_ROOT. |
| 90 | static const std::string defaultDatFile[]; |
| 91 | |
| 92 | // Update d->datFilesInfo and legacy d->metarDatPath, d->poiDatPath, |
| 93 | // etc. by looking into $scenery_path/NavData for each scenery path. |
| 94 | void updateListsOfDatFiles(); |
| 95 | // Returns datFilesInfo for the given type. |
| 96 | const DatFilesGroupInfo& getDatFilesInfo(DatFileType datFileType) const; |
| 97 | |
| 98 | /** |
| 99 | * predicate - check if the cache needs to be rebuilt. |
| 100 | * This can happen is the cache file is missing or damaged, or one of the |
| 101 | ** global input files is changed. |
| 102 | */ |
| 103 | bool isRebuildRequired(); |
| 104 | |
| 105 | static bool isAnotherProcessRebuilding(); |
| 106 | |
| 107 | enum RebuildPhase { |
| 108 | REBUILD_UNKNOWN = 0, |
| 109 | REBUILD_READING_APT_DAT_FILES, |
| 110 | REBUILD_LOADING_AIRPORTS, |
| 111 | REBUILD_NAVAIDS, |
| 112 | REBUILD_FIXES, |
| 113 | REBUILD_POIS, |
| 114 | REBUILD_DONE |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * run the cache rebuild - returns the current phase or 'done' |
| 119 | */ |
| 120 | RebuildPhase rebuild(); |
| 121 | |
| 122 | unsigned int rebuildPhaseCompletionPercentage() const; |
| 123 | void setRebuildPhaseProgress(RebuildPhase ph, unsigned int percent = 0); |
| 124 | |
| 125 | bool isCachedFileModified(const SGPath& path) const; |
| 126 | void stampCacheFile(const SGPath& path, const std::string& sha = {}); |
| 127 | |
| 128 | int readIntProperty(const std::string& key); |
| 129 | double readDoubleProperty(const std::string& key); |
| 130 | std::string readStringProperty(const std::string& key); |
| 131 | |
| 132 | void writeIntProperty(const std::string& key, int value); |
| 133 | void writeStringProperty(const std::string& key, const std::string& value); |
| 134 | void writeDoubleProperty(const std::string& key, const double& value); |
| 135 | |
| 136 | // Warning: order is not necessarily preserved upon write-read cycles! |
| 137 | string_list readStringListProperty(const std::string& key); |
| 138 | void writeStringListProperty(const std::string& key, const string_list& values); |
| 139 | |
| 140 | // These ones guarantee the same order after each write-read cycle. |
| 141 | string_list readOrderedStringListProperty(const std::string& key, |
| 142 | const char* separator); |
| 143 | void writeOrderedStringListProperty(const std::string& key, |
| 144 | const string_list& values, |
| 145 | const char* separator); |
| 146 | |
| 147 | /** |
| 148 | * retrieve an FGPositioned from the cache. |
| 149 | * This may be trivial if the object is previously loaded, or require actual |
| 150 | * disk IO. |
| 151 | */ |
| 152 | FGPositionedRef loadById(PositionedID guid); |
| 153 | |
| 154 | PositionedID insertAirport(FGPositioned::Type ty, const std::string& ident, |
| 155 | const std::string& name, |
| 156 | const SGPath& sceneryPath); |
| 157 | void insertTower(PositionedID airportId, const SGGeod& pos); |
| 158 | |
| 159 | |
| 160 | PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident, |
| 161 | const SGGeod& pos, PositionedID apt, |
| 162 | double heading, double length, double width, double displacedThreshold, |
| 163 | double stopway, int markings, int approach, int tdz, int reil, |
| 164 | int surfaceCode, int shoulder_code, float smoothness, int center_lights, |
| 165 | int edge_lights, int distance_remaining); |
| 166 | |
| 167 | PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident, |
| 168 | const SGGeod& pos, PositionedID apt, |
| 169 | double heading, double length, double width, double displacedThreshold, |
| 170 | double stopway, int surfaceCode); |
| 171 | |
| 172 | void setRunwayReciprocal(PositionedID runway, PositionedID recip); |
| 173 | void setRunwayILS(PositionedID runway, PositionedID ils); |
| 174 | |
| 175 | PositionedID insertNavaid(FGPositioned::Type ty, const std::string& ident, |
| 176 | const std::string& name, const SGGeod& pos, int freq, int range, double multiuse, |
| 177 | PositionedID apt, PositionedID runway); |
| 178 | |
| 179 | // Assign colocated DME to a navaid |
| 180 | void setNavaidColocated(PositionedID navaid, PositionedID colocatedDME); |
| 181 | |
| 182 | PositionedID insertCommStation(FGPositioned::Type ty, |
| 183 | const std::string& name, const SGGeod& pos, int freq, int range, |
| 184 | PositionedID apt); |
| 185 | |
| 186 | PositionedID createPOI(FGPositioned::Type ty, const std::string& ident, const SGGeod& aPos, const std::string& aName, bool transient); |
| 187 | |
| 188 | bool removePOI(FGPositionedRef wpt); |
| 189 | |
| 190 | /// update the metar flag associated with an airport |
| 191 | void setAirportMetar(const std::string& icao, bool hasMetar); |
| 192 | |
| 193 | /** |
| 194 | * Modify the position of an existing item. |
| 195 | */ |
| 196 | void updatePosition(PositionedID item, const SGGeod& pos); |
| 197 | |
| 198 | FGPositionedList findAllWithIdent(const std::string& ident, |
| 199 | FGPositioned::Filter* filter, |
| 200 | bool exact); |
| 201 | FGPositionedList findAllWithName(const std::string& ident, |
| 202 | FGPositioned::Filter* filter, |
| 203 | bool exact); |
| 204 | |
| 205 | FGPositionedRef findClosestWithIdent(const std::string& aIdent, |
| 206 | const SGGeod& aPos, |
| 207 | FGPositioned::Filter* aFilter); |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * Helper to implement the AirportSearch widget. Optimised text search of |
| 212 | * airport names and idents, returning a list suitable for passing directly |
| 213 | * to PLIB. |
| 214 | */ |
| 215 | char** searchAirportNamesAndIdents(const std::string& aFilter); |
| 216 | |
| 217 | /** |
| 218 | * Find the closest matching comm-station on a frequency, to a position. |
| 219 | * The filter with be used for both type ranging and to validate the result |
| 220 | * candidates. |
| 221 | */ |
| 222 | FGPositionedRef findCommByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt); |
| 223 | |
| 224 | /** |
| 225 | * Find all on a frequency, sorted by distance from a position |
| 226 | * The filter with be used for both type ranging and to validate the result |
| 227 | * candidates. |
| 228 | */ |
| 229 | PositionedIDVec findCommsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt); |
| 230 | |
| 231 | /** |
| 232 | * find all items of a specified type (or range of types) at an airport |
| 233 | */ |
| 234 | PositionedIDVec airportItemsOfType(PositionedID apt, FGPositioned::Type ty, |
| 235 | FGPositioned::Type maxTy = FGPositioned::INVALID); |
| 236 | |
| 237 | /** |
| 238 | * find the first match item of the specified type and ident, at an airport |
| 239 | */ |
| 240 | PositionedID airportItemWithIdent(PositionedID apt, FGPositioned::Type ty, const std::string& ident); |
| 241 | |
| 242 | /** |
| 243 | * Find all navaids matching a particular frequency, sorted by range from the |
| 244 | * supplied position. Type-range will be determined from the filter |
| 245 | */ |
| 246 | PositionedIDVec findNavaidsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt); |
| 247 | |
| 248 | /// overload version of the above that does not consider positioned when |
| 249 | /// returning results. Only used by TACAN carrier search |
| 250 | PositionedIDVec findNavaidsByFreq(int freqKhz, FGPositioned::Filter* filt); |
| 251 | |
| 252 | /** |
| 253 | * Given a runway and type, find the corresponding navaid (ILS / GS / OM) |
| 254 | */ |
| 255 | PositionedID findNavaidForRunway(PositionedID runway, FGPositioned::Type ty); |
| 256 | |
| 257 | /** |
| 258 | * given a navaid name (or similar) from apt.dat / nav.dat, find the |
| 259 | * corresponding airport and runway IDs. |
| 260 | * Such names look like: 'LHBP 31L DME-ILS' or 'UEEE 23L MM' |
| 261 | */ |
| 262 | AirportRunwayPair findAirportRunway(const std::string& name); |
| 263 | |
| 264 | /** |
| 265 | * Given an airport, and runway and ILS identifier, find the corresponding cache |
| 266 | * entry. This matches the data we get in the ils.xml files for airports. |
| 267 | */ |
| 268 | PositionedID findILS(PositionedID airport, const std::string& runway, const std::string& navIdent); |
| 269 | |
| 270 | /** |
| 271 | * Given an Octree node ID, return a bit-mask defining which of the child |
| 272 | * nodes exist. In practice this means an 8-bit value be sufficient, but |
| 273 | * an int works fine too. |
| 274 | */ |
| 275 | int getOctreeBranchChildren(int64_t octreeNodeId); |
| 276 | |
| 277 | void defineOctreeNode(Octree::Branch* pr, Octree::Node* nd); |
| 278 | |
| 279 | /** |
| 280 | * given an octree leaf, return all its child positioned items and their types |
| 281 | */ |
| 282 | TypedPositionedVec getOctreeLeafChildren(int64_t octreeNodeId); |
| 283 | |
| 284 | // airways |
| 285 | int findAirway(int network, const std::string& aName, bool create); |
| 286 | |
| 287 | int findAirway(const std::string& aName); |
| 288 | |
| 289 | /** |
| 290 | * insert an edge between two positioned nodes, into the network. |
| 291 | * The airway identifier will be set accordingly. No reverse edge is created |
| 292 | * by this method - edges are directional so a reverses must be explicitly |
| 293 | * created. |
| 294 | */ |
| 295 | void insertEdge(int network, int airwayID, PositionedID from, PositionedID to); |
| 296 | |
| 297 | /// is the specified positioned a node on the network? |
| 298 | bool isInAirwayNetwork(int network, PositionedID pos); |
| 299 | |
| 300 | /** |
| 301 | * retrive all the destination points reachable from a positioned |
| 302 | * in an airway |
| 303 | */ |
| 304 | AirwayEdgeVec airwayEdgesFrom(int network, PositionedID pos); |
| 305 | |
| 306 | AirwayRef loadAirway(int airwayID); |
| 307 | |
| 308 | /** |
| 309 | * Waypoints on the airway |
| 310 | */ |
| 311 | PositionedIDVec airwayWaypts(int id); |
| 312 | |
| 313 | class Transaction |
| 314 | { |
| 315 | public: |
| 316 | Transaction(NavDataCache* cache); |
| 317 | ~Transaction(); |
| 318 | |
| 319 | void commit(); |
| 320 | |
| 321 | private: |
| 322 | NavDataCache* _instance; |
| 323 | bool _committed; |
| 324 | }; |
| 325 | |
| 326 | bool isReadOnly() const; |
| 327 | |
| 328 | class ThreadedGUISearch |
| 329 | { |
| 330 | public: |
| 331 | ThreadedGUISearch(const std::string& term, bool onlyAirports); |
| 332 | ~ThreadedGUISearch(); |
| 333 | |
| 334 | PositionedIDVec results() const; |
| 335 | |
| 336 | bool isComplete() const; |
| 337 | |
| 338 | private: |
| 339 | class ThreadedGUISearchPrivate; |
| 340 | std::unique_ptr<ThreadedGUISearchPrivate> d; |
| 341 | }; |
| 342 | |
| 343 | void clearDynamicPositioneds(); |
| 344 | |
| 345 | PositionedID createTransientID(); |
| 346 | |
| 347 | private: |
| 348 | NavDataCache(); |
| 349 | |
| 350 | friend class RebuildThread; |
| 351 | |
| 352 | // A generic function for loading all navigation data files of the |
| 353 | // specified type (apt/fix/nav etc.) using the passed type-specific loader. |
| 354 | void loadDatFiles(DatFileType type, |
| 355 | std::function<void(const SceneryLocation&, std::size_t, std::size_t)> loader); |
| 356 | |
| 357 | void doRebuild(); |
| 358 | |
| 359 | friend class Transaction; |
| 360 | |
| 361 | void beginTransaction(); |
| 362 | void commitTransaction(); |
| 363 | void abortTransaction(); |
| 364 | |
| 365 | class NavDataCachePrivate; |
| 366 | std::unique_ptr<NavDataCachePrivate> d; |
| 367 | |
| 368 | bool rebuildInProgress = false; |
| 369 | }; |
| 370 | |
| 371 | } // namespace flightgear |