| 1 | /* |
| 2 | * SPDX-FileName: airport.hxx |
| 3 | * SPDX-FileComment: a really simplistic class to manage airport ID, lat, lon of the center of one of it's runways, and elevation in feet. |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt |
| 5 | * SPDX-FileContributor: Updated by Durk Talsma, started December 2004. |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include <map> |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include <simgear/compiler.h> |
| 17 | #include <simgear/misc/sg_path.hxx> |
| 18 | |
| 19 | #include <Navaids/positioned.hxx> |
| 20 | #include <Navaids/procedure.hxx> |
| 21 | |
| 22 | #include "airports_fwd.hxx" |
| 23 | #include "runways.hxx" |
| 24 | |
| 25 | class FGGroundNetwork; |
| 26 | |
| 27 | |
| 28 | /*************************************************************************************** |
| 29 | * |
| 30 | **************************************************************************************/ |
| 31 | class FGAirport : public FGPositioned |
| 32 | { |
| 33 | public: |
| 34 | // 'sceneryPath' is the scenery path that provided the apt.dat file for |
| 35 | // the airport (except for the “default dat file” under $FG_ROOT and the |
| 36 | // null SGPath special case, 'sceneryPath' should be an element of |
| 37 | // globals->get_fg_scenery()). Knowing this allows one to stop looking for |
| 38 | // files such as ils.xml or threshold.xml in scenery paths that come later |
| 39 | // in globals->get_fg_scenery() order (cf. XMLLoader::findAirportData()). |
| 40 | FGAirport(PositionedID aGuid, const std::string& id, const SGGeod& location, |
| 41 | const std::string& name, bool has_metar, Type aType, |
| 42 | SGPath sceneryPath = SGPath()); |
| 43 | ~FGAirport(); |
| 44 | |
| 45 | static bool isType(FGPositioned::Type ty) |
| 46 | { |
| 47 | return (ty >= FGPositioned::AIRPORT) && (ty <= FGPositioned::SEAPORT); |
| 48 | } |
| 49 | |
| 50 | // Return the realpath() of the scenery folder under which we found the |
| 51 | // apt.dat file for this airport. |
| 52 | SGPath sceneryPath() const; |
| 53 | const std::string& getId() const { return ident(); } |
| 54 | const std::string& getName() const { return _name; } |
| 55 | std::string toString() const { return "an airport " + ident(); } |
| 56 | |
| 57 | double getLongitude() const { return longitude(); } |
| 58 | // Returns degrees |
| 59 | double getLatitude() const { return latitude(); } |
| 60 | // Returns ft |
| 61 | double getElevation() const { return elevation(); } |
| 62 | bool getMetar() const { return _has_metar; } |
| 63 | bool isAirport() const; |
| 64 | bool isSeaport() const; |
| 65 | bool isHeliport() const; |
| 66 | |
| 67 | /// is the airport closed (disused)? |
| 68 | /// note at present we look for an [x] in the name, ideally the database |
| 69 | /// would explicitly include this |
| 70 | bool isClosed() const |
| 71 | { |
| 72 | return mIsClosed; |
| 73 | } |
| 74 | |
| 75 | virtual const std::string& name() const |
| 76 | { |
| 77 | return _name; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * reload the ILS data from XML if required. |
| 82 | */ |
| 83 | void validateILSData(); |
| 84 | |
| 85 | bool hasTower() const; |
| 86 | |
| 87 | SGGeod getTowerLocation() const; |
| 88 | |
| 89 | void setMetar(bool value) { _has_metar = value; } |
| 90 | |
| 91 | FGRunwayRef getActiveRunwayForUsage() const; |
| 92 | |
| 93 | FGAirportDynamicsRef getDynamics() const; |
| 94 | |
| 95 | FGGroundNetwork* groundNetwork() const; |
| 96 | |
| 97 | unsigned int numRunways() const; |
| 98 | unsigned int numHelipads() const; |
| 99 | FGRunwayRef getRunwayByIndex(unsigned int aIndex) const; |
| 100 | FGHelipadRef getHelipadByIndex(unsigned int aIndex) const; |
| 101 | FGRunwayMap getRunwayMap() const; |
| 102 | FGHelipadMap getHelipadMap() const; |
| 103 | |
| 104 | bool hasRunwayWithIdent(const std::string& aIdent) const; |
| 105 | bool hasHelipadWithIdent(const std::string& aIdent) const; |
| 106 | FGRunwayRef getRunwayByIdent(const std::string& aIdent) const; |
| 107 | FGHelipadRef getHelipadByIdent(const std::string& aIdent) const; |
| 108 | |
| 109 | struct FindBestRunwayForHeadingParams { |
| 110 | FindBestRunwayForHeadingParams() |
| 111 | { |
| 112 | lengthWeight = 0.01; |
| 113 | widthWeight = 0.01; |
| 114 | surfaceWeight = 10; |
| 115 | deviationWeight = 1; |
| 116 | ilsWeight = 0; |
| 117 | } |
| 118 | double lengthWeight; |
| 119 | double widthWeight; |
| 120 | double surfaceWeight; |
| 121 | double deviationWeight; |
| 122 | double ilsWeight; |
| 123 | }; |
| 124 | FGRunwayRef findBestRunwayForHeading(double aHeading, struct FindBestRunwayForHeadingParams* parms = NULL) const; |
| 125 | |
| 126 | /** |
| 127 | * return the most likely target runway based on a position. |
| 128 | * Specifically, return the runway for which the course from aPos |
| 129 | * to the runway end, mostly closely matches the runway heading. |
| 130 | * This is a good approximation of which runway the position is on or |
| 131 | * aiming towards. |
| 132 | */ |
| 133 | FGRunwayRef findBestRunwayForPos(const SGGeod& aPos) const; |
| 134 | |
| 135 | /** |
| 136 | * Retrieve all runways at the airport, but excluding the reciprocal |
| 137 | * runways. For example at KSFO this might return 1L, 1R, 28L and 28R, |
| 138 | * but would not then include 19L/R or 10L/R. |
| 139 | * |
| 140 | * Exactly which runways you get, is undefined (i.e, dont assumes it's |
| 141 | * runways with heading < 180 degrees) - it depends on order in apt.dat. |
| 142 | * |
| 143 | * This is useful for code that wants to process each piece of tarmac at |
| 144 | * an airport *once*, not *twice* - eg mapping and nav-display code. |
| 145 | */ |
| 146 | FGRunwayList getRunwaysWithoutReciprocals() const; |
| 147 | |
| 148 | /** |
| 149 | * Retrieve all runways at the airport |
| 150 | */ |
| 151 | FGRunwayList getRunways() const; |
| 152 | |
| 153 | std::string findAPTRunwayForNewName(const std::string& newIdent) const; |
| 154 | |
| 155 | /** |
| 156 | * Useful predicate for FMS/GPS/NAV displays and similar - check if this |
| 157 | * airport has a hard-surfaced runway of at least the specified length. |
| 158 | */ |
| 159 | bool hasHardRunwayOfLengthFt(double aLengthFt) const; |
| 160 | |
| 161 | FGRunwayRef longestRunway() const; |
| 162 | |
| 163 | unsigned int numTaxiways() const; |
| 164 | FGTaxiwayRef getTaxiwayByIndex(unsigned int aIndex) const; |
| 165 | FGTaxiwayList getTaxiways() const; |
| 166 | |
| 167 | unsigned int numPavements() const; |
| 168 | FGPavementRef getPavementByIndex(unsigned int aIndex) const; |
| 169 | FGPavementList getPavements() const; |
| 170 | void addPavement(FGPavementRef pavement); |
| 171 | |
| 172 | unsigned int numBoundary() const; |
| 173 | FGPavementRef getBoundaryIndex(unsigned int aIndex) const; |
| 174 | FGPavementList getBoundary() const; |
| 175 | void addBoundary(FGPavementRef boundary); |
| 176 | |
| 177 | unsigned int numLineFeatures() const; |
| 178 | FGPavementList getLineFeatures() const; |
| 179 | void addLineFeature(FGPavementRef linefeature); |
| 180 | |
| 181 | class AirportFilter : public Filter |
| 182 | { |
| 183 | public: |
| 184 | virtual bool pass(FGPositioned* aPos) const |
| 185 | { |
| 186 | return passAirport(static_cast<FGAirport*>(aPos)); |
| 187 | } |
| 188 | |
| 189 | virtual Type minType() const |
| 190 | { |
| 191 | return AIRPORT; |
| 192 | } |
| 193 | |
| 194 | virtual Type maxType() const |
| 195 | { |
| 196 | return AIRPORT; |
| 197 | } |
| 198 | |
| 199 | virtual bool passAirport(FGAirport* aApt) const |
| 200 | { |
| 201 | return true; |
| 202 | } |
| 203 | }; |
| 204 | |
| 205 | /** |
| 206 | * Filter which passes heliports and seaports in addition to airports |
| 207 | */ |
| 208 | class PortsFilter : public AirportFilter |
| 209 | { |
| 210 | public: |
| 211 | virtual Type maxType() const override |
| 212 | { |
| 213 | return SEAPORT; |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | class HardSurfaceFilter : public AirportFilter |
| 218 | { |
| 219 | public: |
| 220 | explicit HardSurfaceFilter(double minLengthFt = -1); |
| 221 | |
| 222 | virtual bool passAirport(FGAirport* aApt) const override; |
| 223 | |
| 224 | private: |
| 225 | double mMinLengthFt; |
| 226 | }; |
| 227 | |
| 228 | /** |
| 229 | * Filter which passes specified port type and in case of airport checks |
| 230 | * if a runway larger the /sim/navdb/min-runway-length-ft exists. |
| 231 | */ |
| 232 | class TypeRunwayFilter : public AirportFilter |
| 233 | { |
| 234 | public: |
| 235 | TypeRunwayFilter(); |
| 236 | |
| 237 | /** |
| 238 | * Construct from string containing type (airport, seaport or heliport) |
| 239 | */ |
| 240 | bool fromTypeString(const std::string& type); |
| 241 | |
| 242 | virtual FGPositioned::Type minType() const override { return _type; } |
| 243 | virtual FGPositioned::Type maxType() const override { return _type; } |
| 244 | virtual bool pass(FGPositioned* pos) const override; |
| 245 | |
| 246 | protected: |
| 247 | FGPositioned::Type _type; |
| 248 | double _min_runway_length_ft; |
| 249 | }; |
| 250 | |
| 251 | |
| 252 | void setProcedures(const std::vector<flightgear::SID*>& aSids, |
| 253 | const std::vector<flightgear::STAR*>& aStars, |
| 254 | const std::vector<flightgear::Approach*>& aApproaches); |
| 255 | |
| 256 | void addSID(flightgear::SID* aSid); |
| 257 | void addSTAR(flightgear::STAR* aStar); |
| 258 | void addApproach(flightgear::Approach* aApp); |
| 259 | |
| 260 | unsigned int numSIDs() const; |
| 261 | flightgear::SID* getSIDByIndex(unsigned int aIndex) const; |
| 262 | flightgear::SID* findSIDWithIdent(const std::string& aIdent) const; |
| 263 | flightgear::SIDList getSIDs() const; |
| 264 | |
| 265 | flightgear::Transition* selectSIDByEnrouteTransition(FGPositioned* enroute) const; |
| 266 | flightgear::Transition* selectSIDByTransition(const FGRunway* runway, const std::string& aIdent) const; |
| 267 | |
| 268 | unsigned int numSTARs() const; |
| 269 | flightgear::STAR* getSTARByIndex(unsigned int aIndex) const; |
| 270 | flightgear::STAR* findSTARWithIdent(const std::string& aIdent) const; |
| 271 | flightgear::STARList getSTARs() const; |
| 272 | |
| 273 | flightgear::Transition* selectSTARByEnrouteTransition(FGPositioned* enroute) const; |
| 274 | flightgear::Transition* selectSTARByTransition(const FGRunway* runway, const std::string& aIdent) const; |
| 275 | |
| 276 | unsigned int numApproaches() const; |
| 277 | flightgear::Approach* getApproachByIndex(unsigned int aIndex) const; |
| 278 | flightgear::Approach* findApproachWithIdent(const std::string& aIdent) const; |
| 279 | flightgear::ApproachList getApproaches( |
| 280 | flightgear::ProcedureType type = flightgear::PROCEDURE_INVALID) const; |
| 281 | |
| 282 | /** |
| 283 | * Syntactic wrapper around FGPositioned::findClosest - find the closest |
| 284 | * match for filter, and return it cast to FGAirport. The default filter |
| 285 | * passes airports, but not seaports or heliports |
| 286 | */ |
| 287 | static FGAirportRef findClosest(const SGGeod& aPos, double aCuttofNm, Filter* filter = NULL); |
| 288 | |
| 289 | /** |
| 290 | * Helper to look up an FGAirport instance by unique ident. Throws an |
| 291 | * exception if the airport could not be found - so callers can assume |
| 292 | * the result is non-NULL. |
| 293 | */ |
| 294 | static FGAirportRef getByIdent(const std::string& aIdent); |
| 295 | |
| 296 | /** |
| 297 | * Helper to look up an FGAirport instance by unique ident. Returns NULL |
| 298 | * if the airport could not be found. |
| 299 | */ |
| 300 | static FGAirportRef findByIdent(const std::string& aIdent); |
| 301 | |
| 302 | /** |
| 303 | * Specialised helper to implement the AirportList dialog. Performs a |
| 304 | * case-insensitive search on airport names and ICAO codes, and returns |
| 305 | * matches in a format suitable for use by a puaList. |
| 306 | */ |
| 307 | static char** searchNamesAndIdents(const std::string& aFilter); |
| 308 | |
| 309 | |
| 310 | /** |
| 311 | * Sort an FGPositionedList of airports by size (number of runways + length) |
| 312 | * this is meant to prioritise more important airports. |
| 313 | */ |
| 314 | static void sortBySize(FGPositionedList&); |
| 315 | |
| 316 | flightgear::CommStationList commStationsOfType(FGPositioned::Type aTy) const; |
| 317 | |
| 318 | flightgear::CommStationList commStations() const; |
| 319 | |
| 320 | static void clearAirportsCache(); |
| 321 | |
| 322 | // helper to allow testing without needing a full Airports hierarchy |
| 323 | // only for use by the test-suite, not available outside of it. |
| 324 | void testSuiteInjectGroundnetXML(const SGPath& path); |
| 325 | |
| 326 | void testSuiteInjectProceduresXML(const SGPath& path); |
| 327 | |
| 328 | private: |
| 329 | static flightgear::AirportCache airportCache; |
| 330 | |
| 331 | // disable these |
| 332 | FGAirport operator=(FGAirport& other); |
| 333 | FGAirport(const FGAirport&); |
| 334 | |
| 335 | /** |
| 336 | * helper to read airport data from the scenery XML files. |
| 337 | */ |
| 338 | void loadSceneryDefinitions() const; |
| 339 | |
| 340 | /** |
| 341 | * Helpers to process property data loaded from an ICAO.threshold.xml file |
| 342 | */ |
| 343 | void readThresholdData(SGPropertyNode* aRoot); |
| 344 | void processThreshold(SGPropertyNode* aThreshold); |
| 345 | |
| 346 | void readILSData(SGPropertyNode* aRoot); |
| 347 | |
| 348 | void validateTowerData() const; |
| 349 | |
| 350 | /** |
| 351 | * Helpers to parse property data loaded from an ICAO.twr.xml file |
| 352 | */ |
| 353 | void readTowerData(SGPropertyNode* aRoot); |
| 354 | |
| 355 | /** |
| 356 | * Helpers to parse property data loaded from an ICAO.runway_rename.xml file |
| 357 | */ |
| 358 | void parseRunwayRenameData(SGPropertyNode* aRoot); |
| 359 | |
| 360 | PositionedIDVec itemsOfType(FGPositioned::Type ty) const; |
| 361 | |
| 362 | std::string _name; |
| 363 | bool _has_metar = false; |
| 364 | const SGPath _sceneryPath; |
| 365 | |
| 366 | void loadRunways() const; |
| 367 | void loadHelipads() const; |
| 368 | void loadTaxiways() const; |
| 369 | void loadProcedures() const; |
| 370 | void loadRunwayRenames() const; |
| 371 | |
| 372 | mutable bool mTowerDataLoaded; |
| 373 | mutable bool mHasTower; |
| 374 | mutable SGGeod mTowerPosition; |
| 375 | |
| 376 | mutable bool mRunwaysLoaded; |
| 377 | mutable bool mHelipadsLoaded; |
| 378 | mutable bool mTaxiwaysLoaded; |
| 379 | mutable bool mProceduresLoaded; |
| 380 | mutable bool mRunwayRenamesLoaded = false; |
| 381 | bool mIsClosed; |
| 382 | mutable bool mThresholdDataLoaded; |
| 383 | bool mILSDataLoaded; |
| 384 | |
| 385 | mutable std::vector<FGRunwayRef> mRunways; |
| 386 | |
| 387 | mutable PositionedIDVec mHelipads; |
| 388 | mutable PositionedIDVec mTaxiways; |
| 389 | std::vector<FGPavementRef> mPavements; |
| 390 | std::vector<FGPavementRef> mBoundary; |
| 391 | std::vector<FGPavementRef> mLineFeatures; |
| 392 | |
| 393 | typedef SGSharedPtr<flightgear::SID> SIDRef; |
| 394 | typedef SGSharedPtr<flightgear::STAR> STARRef; |
| 395 | typedef SGSharedPtr<flightgear::Approach> ApproachRef; |
| 396 | |
| 397 | std::vector<SIDRef> mSIDs; |
| 398 | std::vector<STARRef> mSTARs; |
| 399 | std::vector<ApproachRef> mApproaches; |
| 400 | |
| 401 | mutable std::unique_ptr<FGGroundNetwork> _groundNetwork; |
| 402 | |
| 403 | using RunwayRenameMap = std::map<std::string, std::string>; |
| 404 | // map from new name (eg in Navigraph) to old name (in apt.dat) |
| 405 | RunwayRenameMap _renamedRunways; |
| 406 | }; |
| 407 | |
| 408 | // find basic airport location info from airport database |
| 409 | const FGAirport* fgFindAirportID(const std::string& id); |
| 410 | |
| 411 | // get airport elevation |
| 412 | double fgGetAirportElev(const std::string& id); |