Debian Codegraph @

src/Airports/airport.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: airport.cxx
3 * SPDX-FileComment: Classes representing airports, seaports and helipads
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 #include <config.h>
10
11 #include "airport.hxx"
12 #include "simgear/debug/debug_types.h"
13
14 #include <algorithm>
15 #include <cassert>
16 #include <utility>
17
18 #include <simgear/debug/ErrorReportingCallback.hxx>
19 #include <simgear/debug/logstream.hxx>
20 #include <simgear/misc/sg_path.hxx>
21 #include <simgear/props/props.hxx>
22 #include <simgear/props/props_io.hxx>
23 #include <simgear/sg_inlines.h>
24 #include <simgear/structure/exception.hxx>
25
26 #include <Environment/environment_mgr.hxx>
27 #include <Environment/environment.hxx>
28 #include <Main/fg_props.hxx>
29 #include <Airports/runways.hxx>
30 #include <Airports/pavement.hxx>
31 #include <Airports/xmlloader.hxx>
32 #include <Airports/dynamics.hxx>
33 #include <Airports/airportdynamicsmanager.hxx>
34 #include <Navaids/procedure.hxx>
35 #include <Navaids/waypoint.hxx>
36 #include <ATC/CommStation.hxx>
37 #include <Navaids/NavDataCache.hxx>
38 #include <Navaids/navrecord.hxx>
39 #include <Navaids/positioned.hxx>
40 #include <Airports/groundnetwork.hxx>
41 #include <Airports/xmlloader.hxx>
42
43 using std::vector;
44 using std::pair;
45
46 using namespace flightgear;
47
48 /***************************************************************************
49 * FGAirport
50 ***************************************************************************/
51
52 AirportCache FGAirport::airportCache;
53
54 FGAirport::FGAirport( PositionedID aGuid,
55 const std::string &id,
56 const SGGeod& location,
57 const std::string &name,
58 bool has_metar,
59 Type aType,
60 SGPath sceneryPath):
61 FGPositioned(aGuid, aType, id, location),
62 _name(name),
63 _has_metar(has_metar),
64 _sceneryPath(std::move(sceneryPath)),
65 mTowerDataLoaded(false),
66 mHasTower(false),
67 mRunwaysLoaded(false),
68 mHelipadsLoaded(false),
69 mTaxiwaysLoaded(false),
70 mProceduresLoaded(false),
71 mThresholdDataLoaded(false),
72 mILSDataLoaded(false)
73 {
74 mIsClosed = (name.find("[x]") != std::string::npos);
75 }
76
77
78 FGAirport::~FGAirport()
79 {
80 }
81
82 bool FGAirport::isAirport() const
83 {
84 return type() == AIRPORT;
85 }
86
87 bool FGAirport::isSeaport() const
88 {
89 return type() == SEAPORT;
90 }
91
92 bool FGAirport::isHeliport() const
93 {
94 return type() == HELIPORT;
95 }
96
97 SGPath FGAirport::sceneryPath() const
98 {
99 return _sceneryPath;
100 }
101
102 //------------------------------------------------------------------------------
103 unsigned int FGAirport::numRunways() const
104 {
105 loadRunways();
106 return mRunways.size();
107 }
108
109 //------------------------------------------------------------------------------
110 unsigned int FGAirport::numHelipads() const
111 {
112 loadHelipads();
113 return mHelipads.size();
114 }
115
116 //------------------------------------------------------------------------------
117 FGRunwayRef FGAirport::getRunwayByIndex(unsigned int aIndex) const
118 {
119 loadRunways();
120 return mRunways.at(aIndex);
121 }
122
123 //------------------------------------------------------------------------------
124 FGHelipadRef FGAirport::getHelipadByIndex(unsigned int aIndex) const
125 {
126 loadHelipads();
127 return loadById<FGHelipad>(mHelipads, aIndex);
128 }
129
130 //------------------------------------------------------------------------------
131 FGRunwayMap FGAirport::getRunwayMap() const
132 {
133 loadRunways();
134 FGRunwayMap map;
135
136 double minLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft");
137
138 for (auto rwy : mRunways) {
139 // ignore unusably short runways
140 // TODO: other methods don't check this...
141 if( rwy->lengthFt() >= minLengthFt )
142 map[ rwy->ident() ] = rwy;
143 }
144
145 return map;
146 }
147
148 //------------------------------------------------------------------------------
149 FGHelipadMap FGAirport::getHelipadMap() const
150 {
151 loadHelipads();
152 FGHelipadMap map;
153
154 for (auto id : mHelipads) {
155 FGHelipad* rwy = loadById<FGHelipad>(id);
156 map[ rwy->ident() ] = rwy;
157 }
158
159 return map;
160 }
161
162 //------------------------------------------------------------------------------
163 bool FGAirport::hasRunwayWithIdent(const std::string& aIdent) const
164 {
165 loadRunways();
166 for (auto rwy : mRunways) {
167 if (rwy->ident() == aIdent) {
168 return true;
169 }
170 }
171
172 return false;
173 }
174
175 //------------------------------------------------------------------------------
176 bool FGAirport::hasHelipadWithIdent(const std::string& aIdent) const
177 {
178 return flightgear::NavDataCache::instance()
179 ->airportItemWithIdent(guid(), FGPositioned::HELIPAD, aIdent) != 0;
180 }
181
182 //------------------------------------------------------------------------------
183 FGRunwayRef FGAirport::getRunwayByIdent(const std::string& aIdent) const
184 {
185 if (aIdent.empty())
186 return {};
187
188 loadRunways();
189 for (auto rwy : mRunways) {
190 if (rwy->ident() == aIdent) {
191 return rwy;
192 }
193 }
194
195 SG_LOG(SG_GENERAL, SG_ALERT, "no such runway '" << aIdent << "' at airport " << ident());
196 throw sg_range_exception("unknown runway " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
197 }
198
199 //------------------------------------------------------------------------------
200 FGHelipadRef FGAirport::getHelipadByIdent(const std::string& aIdent) const
201 {
202 PositionedID id = flightgear::NavDataCache::instance()->airportItemWithIdent(guid(), FGPositioned::HELIPAD, aIdent);
203 if (id == 0) {
204 SG_LOG(SG_GENERAL, SG_ALERT, "no such helipad '" << aIdent << "' at airport " << ident());
205 throw sg_range_exception("unknown helipad " + aIdent + " at airport:" + ident(), "FGAirport::getRunwayByIdent");
206 }
207
208 return loadById<FGHelipad>(id);
209 }
210
211 //------------------------------------------------------------------------------
212 FGRunwayRef FGAirport::findBestRunwayForHeading(double aHeading, struct FindBestRunwayForHeadingParams * parms ) const
213 {
214 loadRunways();
215
216 FGRunway* result = NULL;
217 double currentBestQuality = 0.0;
218
219 struct FindBestRunwayForHeadingParams fbrfhp;
220 if( NULL != parms ) fbrfhp = *parms;
221
222 SGPropertyNode_ptr searchNode = fgGetNode("/sim/airport/runways/search");
223 if( searchNode.valid() ) {
224 fbrfhp.lengthWeight = searchNode->getDoubleValue("length-weight", fbrfhp.lengthWeight );
225 fbrfhp.widthWeight = searchNode->getDoubleValue("width-weight", fbrfhp.widthWeight );
226 fbrfhp.surfaceWeight = searchNode->getDoubleValue("surface-weight", fbrfhp.surfaceWeight );
227 fbrfhp.deviationWeight = searchNode->getDoubleValue("deviation-weight", fbrfhp.deviationWeight );
228 fbrfhp.ilsWeight = searchNode->getDoubleValue("ils-weight", fbrfhp.ilsWeight );
229 }
230
231 for (auto rwy : mRunways) {
232 double good = rwy->score( fbrfhp.lengthWeight, fbrfhp.widthWeight, fbrfhp.surfaceWeight, fbrfhp.ilsWeight );
233 double dev = aHeading - rwy->headingDeg();
234 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
235 double bad = fabs( fbrfhp.deviationWeight * dev) + 1e-20;
236 double quality = good / bad;
237
238 if (quality > currentBestQuality) {
239 currentBestQuality = quality;
240 result = rwy;
241 }
242 }
243
244 return result;
245 }
246
247 //------------------------------------------------------------------------------
248 FGRunwayRef FGAirport::findBestRunwayForPos(const SGGeod& aPos) const
249 {
250 loadRunways();
251
252 FGRunway* result = NULL;
253 double currentLowestDev = 180.0;
254
255 for (auto rwy : mRunways) {
256 double inboundCourse = SGGeodesy::courseDeg(aPos, rwy->end());
257 double dev = inboundCourse - rwy->headingDeg();
258 SG_NORMALIZE_RANGE(dev, -180.0, 180.0);
259
260 dev = fabs(dev);
261 if (dev < currentLowestDev) { // new best match
262 currentLowestDev = dev;
263 result = rwy;
264 }
265 } // of runway iteration
266
267 return result;
268
269 }
270
271 //------------------------------------------------------------------------------
272 bool FGAirport::hasHardRunwayOfLengthFt(double aLengthFt) const
273 {
274 loadRunways();
275
276 for (auto rwy : mRunways) {
277 if (rwy->isHardSurface() && (rwy->lengthFt() >= aLengthFt)) {
278 return true; // we're done!
279 }
280 } // of runways iteration
281
282 return false;
283 }
284
285 FGRunwayRef FGAirport::longestRunway() const
286 {
287 FGRunwayRef r;
288 loadRunways();
289
290 for (auto rwy : mRunways) {
291 if (!r || (r->lengthFt() < rwy->lengthFt())) {
292 r = rwy;
293 }
294 } // of runways iteration
295
296 return r;
297 }
298
299 //------------------------------------------------------------------------------
300 FGRunwayList FGAirport::getRunways() const
301 {
302 loadRunways();
303
304 return mRunways;
305 }
306
307 //------------------------------------------------------------------------------
308 FGRunwayList FGAirport::getRunwaysWithoutReciprocals() const
309 {
310 loadRunways();
311
312 FGRunwayList r;
313
314 for (auto rwy : mRunways) {
315 FGRunway* recip = rwy->reciprocalRunway();
316 if (recip) {
317 FGRunwayList::iterator it = std::find(r.begin(), r.end(), recip);
318 if (it != r.end()) {
319 continue; // reciprocal already in result set, don't include us
320 }
321 }
322
323 r.push_back(rwy);
324 }
325
326 return r;
327 }
328
329 //------------------------------------------------------------------------------
330 unsigned int FGAirport::numTaxiways() const
331 {
332 loadTaxiways();
333 return mTaxiways.size();
334 }
335
336 //------------------------------------------------------------------------------
337 FGTaxiwayRef FGAirport::getTaxiwayByIndex(unsigned int aIndex) const
338 {
339 loadTaxiways();
340 return loadById<FGTaxiway>(mTaxiways, aIndex);
341 }
342
343 //------------------------------------------------------------------------------
344 FGTaxiwayList FGAirport::getTaxiways() const
345 {
346 loadTaxiways();
347 return loadAllById<FGTaxiway>(mTaxiways);
348 }
349
350 //------------------------------------------------------------------------------
351 unsigned int FGAirport::numPavements() const
352 {
353 return mPavements.size();
354 }
355
356 //------------------------------------------------------------------------------
357 FGPavementList FGAirport::getPavements() const
358 {
359 return mPavements;
360 }
361
362 void FGAirport::addPavement(FGPavementRef pavement)
363 {
364 mPavements.push_back(pavement);
365 }
366
367 //------------------------------------------------------------------------------
368 unsigned int FGAirport::numBoundary() const
369 {
370 return mBoundary.size();
371 }
372
373 //------------------------------------------------------------------------------
374 FGPavementList FGAirport::getBoundary() const
375 {
376 return mBoundary;
377 }
378
379 void FGAirport::addBoundary(FGPavementRef boundary)
380 {
381 mBoundary.push_back(boundary);
382 }
383
384 //------------------------------------------------------------------------------
385 unsigned int FGAirport::numLineFeatures() const
386 {
387 return mLineFeatures.size();
388 }
389
390 //------------------------------------------------------------------------------
391 FGPavementList FGAirport::getLineFeatures() const
392 {
393 return mLineFeatures;
394 }
395
396 void FGAirport::addLineFeature(FGPavementRef linefeature)
397 {
398 mLineFeatures.push_back(linefeature);
399 }
400
401 //------------------------------------------------------------------------------
402 FGRunwayRef FGAirport::getActiveRunwayForUsage() const
403 {
404 auto envMgr = globals->get_subsystem<FGEnvironmentMgr>();
405
406 // This forces West-facing rwys to be used in no-wind situations
407 // which is consistent with Flightgear's initial setup.
408 double hdg = 270;
409
410 if (envMgr) {
411 // FIXME : this should use the weather at the airport, not the player's
412 // location.
413 const auto stationWeather = envMgr->getAircraftEnvironment();
414
415 double windSpeed = stationWeather->get_wind_speed_kt();
416 if (windSpeed > 0.0) {
417 hdg = stationWeather->get_wind_from_heading_deg();
418 }
419 }
420
421 return findBestRunwayForHeading(hdg);
422 }
423
424 //------------------------------------------------------------------------------
425 FGAirportRef FGAirport::findClosest( const SGGeod& aPos,
426 double aCuttofNm,
427 Filter* filter )
428 {
429 AirportFilter aptFilter;
430 if( !filter )
431 filter = &aptFilter;
432
433 return static_pointer_cast<FGAirport>
434 (
435 FGPositioned::findClosest(aPos, aCuttofNm, filter)
436 );
437 }
438
439 FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) :
440 mMinLengthFt(minLengthFt)
441 {
442 if (minLengthFt < 0.0) {
443 mMinLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
444 }
445 }
446
447 bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
448 {
449 return aApt->hasHardRunwayOfLengthFt(mMinLengthFt);
450 }
451
452 //------------------------------------------------------------------------------
453 FGAirport::TypeRunwayFilter::TypeRunwayFilter():
454 _type(FGPositioned::AIRPORT),
455 _min_runway_length_ft( fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0) )
456 {
457
458 }
459
460 //------------------------------------------------------------------------------
461 bool FGAirport::TypeRunwayFilter::fromTypeString(const std::string& type)
462 {
463 if( type == "heliport" ) _type = FGPositioned::HELIPORT;
464 else if( type == "seaport" ) _type = FGPositioned::SEAPORT;
465 else if( type == "airport" ) _type = FGPositioned::AIRPORT;
466 else return false;
467
468 return true;
469 }
470
471 //------------------------------------------------------------------------------
472 bool FGAirport::TypeRunwayFilter::pass(FGPositioned* pos) const
473 {
474 FGAirport* apt = static_cast<FGAirport*>(pos);
475 if( (apt->type() == FGPositioned::AIRPORT)
476 && !apt->hasHardRunwayOfLengthFt(_min_runway_length_ft)
477 )
478 return false;
479
480 return true;
481 }
482
483 void FGAirport::clearAirportsCache()
484 {
485 airportCache.clear();
486 }
487
488 //------------------------------------------------------------------------------
489 FGAirportRef FGAirport::findByIdent(const std::string& aIdent)
490 {
491 AirportCache::iterator it = airportCache.find(aIdent);
492 if (it != airportCache.end())
493 return it->second;
494
495 PortsFilter filter;
496 FGAirportRef r = static_pointer_cast<FGAirport>
497 (
498 FGPositioned::findFirstWithIdent(aIdent, &filter)
499 );
500
501 // add airport to the cache (even when it's NULL, so we don't need to search in vain again)
502 airportCache[aIdent] = r;
503
504 // we don't warn here when r==NULL, let the caller do that
505 return r;
506 }
507
508 //------------------------------------------------------------------------------
509 FGAirportRef FGAirport::getByIdent(const std::string& aIdent)
510 {
511 FGAirportRef r = findByIdent(aIdent);
512 if (!r)
513 throw sg_range_exception("No such airport with ident: " + aIdent);
514 return r;
515 }
516
517 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
518 {
519 return NavDataCache::instance()->searchAirportNamesAndIdents(aFilter);
520 }
521
522 // find basic airport location info from airport database
523 const FGAirport *fgFindAirportID( const std::string& id)
524 {
525 if ( id.empty() ) {
526 return NULL;
527 }
528
529 return FGAirport::findByIdent(id);
530 }
531
532 PositionedIDVec FGAirport::itemsOfType(FGPositioned::Type ty) const
533 {
534 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
535 return cache->airportItemsOfType(guid(), ty);
536 }
537
538 void FGAirport::loadRunways() const
539 {
540 if (mRunwaysLoaded) {
541 return; // already loaded, great
542 }
543
544 loadSceneryDefinitions();
545
546 mRunwaysLoaded = true;
547 PositionedIDVec rwys(itemsOfType(FGPositioned::RUNWAY));
548 for (auto id : rwys) {
549 mRunways.push_back(loadById<FGRunway>(id));
550 }
551 }
552
553 void FGAirport::loadHelipads() const
554 {
555 if (mHelipadsLoaded) {
556 return; // already loaded, great
557 }
558
559 mHelipadsLoaded = true;
560 mHelipads = itemsOfType(FGPositioned::HELIPAD);
561 }
562
563 void FGAirport::loadTaxiways() const
564 {
565 if (mTaxiwaysLoaded) {
566 return; // already loaded, great
567 }
568
569 mTaxiwaysLoaded = true;
570 mTaxiways = itemsOfType(FGPositioned::TAXIWAY);
571 }
572
573 void FGAirport::loadProcedures() const
574 {
575 if (mProceduresLoaded) {
576 return;
577 }
578
579 mProceduresLoaded = true;
580 SGPath path;
581 if (!XMLLoader::findAirportData(ident(), "procedures", path)) {
582 SG_LOG(SG_GENERAL, SG_INFO, "no procedures data available for " << ident());
583 return;
584 }
585
586 SG_LOG(SG_GENERAL, SG_INFO, ident() << ": loading procedures from " << path);
587 RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
588 }
589
590 void FGAirport::loadRunwayRenames() const
591 {
592 if (mRunwayRenamesLoaded) {
593 return;
594 }
595
596 SGPath path;
597 if (!XMLLoader::findAirportData(ident(), "runway_rename", path)) {
598 // No rename for airport; ignore
599 mRunwayRenamesLoaded = true;
600 return;
601 }
602
603 try {
604 SGPropertyNode_ptr rootNode = new SGPropertyNode;
605 readProperties(path, rootNode);
606 const_cast<FGAirport*>(this)->parseRunwayRenameData(rootNode);
607 mRunwayRenamesLoaded = true;
608 } catch (sg_exception& e) {
609 SG_LOG(SG_NAVAID, SG_WARN, ident() << "loading runway rename XML failed:" << e.getFormattedMessage());
610 }
611 }
612
613 void FGAirport::loadSceneryDefinitions() const
614 {
615 if (mThresholdDataLoaded) {
616 return;
617 }
618
619 mThresholdDataLoaded = true;
620
621 SGPath path;
622 if (!XMLLoader::findAirportData(ident(), "threshold", path)) {
623 return; // no XML threshold data
624 }
625
626 try {
627 SGPropertyNode_ptr rootNode = new SGPropertyNode;
628 readProperties(path, rootNode);
629 const_cast<FGAirport*>(this)->readThresholdData(rootNode);
630 } catch (sg_exception& e) {
631 simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::TerraSync,
632 "Airport threshold data could not be loaded:" + e.getFormattedMessage(),
633 path);
634 }
635 }
636
637 void FGAirport::readThresholdData(SGPropertyNode* aRoot)
638 {
639 SGPropertyNode* runway;
640 int runwayIndex = 0;
641 for (; (runway = aRoot->getChild("runway", runwayIndex)) != NULL; ++runwayIndex) {
642 SGPropertyNode* t0 = runway->getChild("threshold", 0),
643 *t1 = runway->getChild("threshold", 1);
644 if (!t0 || !t1) {
645 throw sg_io_exception("Mis-configured runway threshold data: exactly two thresholds must be defined",
646 sg_location{runway}, "FGAirport::readThresholdData", false);
647 }
648
649 processThreshold(t0);
650 processThreshold(t1);
651 } // of runways iteration
652 }
653
654 void FGAirport::processThreshold(SGPropertyNode* aThreshold)
655 {
656 // first, let's identify the current runway
657 std::string rwyIdent(aThreshold->getStringValue("rwy"));
658 NavDataCache* cache = NavDataCache::instance();
659 PositionedID id = cache->airportItemWithIdent(guid(), FGPositioned::RUNWAY, rwyIdent);
660
661 double lon = aThreshold->getDoubleValue("lon"),
662 lat = aThreshold->getDoubleValue("lat");
663 SGGeod newThreshold(SGGeod::fromDegM(lon, lat, elevationM()));
664
665 double newHeading = aThreshold->getDoubleValue("hdg-deg");
666 double newDisplacedThreshold = aThreshold->getDoubleValue("displ-m");
667 double newStopway = aThreshold->getDoubleValue("stopw-m");
668
669 if (id == 0) {
670 const auto runwayIdent = ident() + "/" + rwyIdent;
671 SG_LOG(SG_NAVAID, SG_DEV_WARN, "Threshold.xml contains runway not included in apt.dat:" <<
672 sg_location{aThreshold}.asString() << ": runway=" << runwayIdent);
673 #if 0
674 // enable this code when threshold.xml contains sufficient data to
675 // fully specify a new runway, *and* we figure out how to assign runtime
676 // Positioned IDs and insert temporary items into the spatial map.
677
678 double newLength = 0.0, newWidth = 0.0;
679 int surfaceCode = 0;
680 FGRunway* rwy = new FGRunway(id, guid(), rwyIdent, newThreshold,
681 newHeading,
682 newLength, newWidth,
683 newDisplacedThreshold, newStopway,
684 surfaceCode);
685 // insert into the spatial map too
686 mRunways.push_back(rwy);
687 #endif
688 } else {
689 FGRunway* rwy = loadById<FGRunway>(id);
690 rwy->updateThreshold(newThreshold, newHeading,
691 newDisplacedThreshold, newStopway);
692
693 }
694 }
695
696 SGGeod FGAirport::getTowerLocation() const
697 {
698 validateTowerData();
699 return mTowerPosition;
700 }
701
702 void FGAirport::validateTowerData() const
703 {
704 if (mTowerDataLoaded) {
705 return;
706 }
707
708 mTowerDataLoaded = true;
709
710 // first, load data from the cache (apt.dat)
711 NavDataCache* cache = NavDataCache::instance();
712 PositionedIDVec towers = cache->airportItemsOfType(guid(), FGPositioned::TOWER);
713 if (towers.empty()) {
714 mHasTower = false;
715 mTowerPosition = geod(); // use airport position
716
717 // offset the tower position away from the runway centerline, if
718 // airport has a single runway. Offset by eight times the runway width,
719 // an entirely guessed figure.
720 int runwayCount = numRunways();
721 if ((runwayCount > 0) && (runwayCount <= 2)) {
722 FGRunway* runway = getRunwayByIndex(0);
723 double hdg = runway->headingDeg() + 90;
724 mTowerPosition = SGGeodesy::direct(geod(), hdg, runway->widthM() * 8);
725 }
726
727 // increase tower elevation by 20 metres above the field elevation
728 mTowerPosition.setElevationM(geod().getElevationM() + 20.0);
729 } else {
730 FGPositionedRef tower = cache->loadById(towers.front());
731 mTowerPosition = tower->geod();
732 mHasTower = true;
733 }
734
735 SGPath path;
736 if (!XMLLoader::findAirportData(ident(), "twr", path)) {
737 return; // no XML tower data, base position is fine
738 }
739
740 try {
741 SGPropertyNode_ptr rootNode = new SGPropertyNode;
742 readProperties(path, rootNode);
743 const_cast<FGAirport*>(this)->readTowerData(rootNode);
744 mHasTower = true;
745 } catch (sg_exception& e){
746 SG_LOG(SG_NAVAID, SG_WARN, ident() << "loading twr XML failed:" << e.getFormattedMessage());
747 }
748 }
749
750 void FGAirport::readTowerData(SGPropertyNode* aRoot)
751 {
752 SGPropertyNode* twrNode = aRoot->getChild("tower")->getChild("twr");
753 double lat = twrNode->getDoubleValue("lat"),
754 lon = twrNode->getDoubleValue("lon"),
755 elevM = twrNode->getDoubleValue("elev-m");
756 // tower elevation is AGL, not AMSL. Since we don't want to depend on the
757 // scenery for a precise terrain elevation, we use the field elevation
758 // (this is also what the apt.dat code does)
759 double fieldElevationM = geod().getElevationM();
760 mTowerPosition = SGGeod::fromDegM(lon, lat, fieldElevationM + elevM);
761 }
762
763 void FGAirport::parseRunwayRenameData(SGPropertyNode* aRoot)
764 {
765 SGPropertyNode* overrideNode = aRoot->getChild("runway-rename");
766 for (auto rnm : overrideNode->getChildren("runway")) {
767 const std::string oldIdent = rnm->getStringValue("old-ident");
768 const std::string newIdent = rnm->getStringValue("new-ident");
769 if (oldIdent.empty() || newIdent.empty()) {
770 SG_LOG(SG_NAVAID, SG_WARN, ident() << ": runway rename: Skipping bad runway rename entry");
771 continue;
772 }
773
774 if (!hasRunwayWithIdent(oldIdent)) {
775 SG_LOG(SG_NAVAID, SG_WARN, ident() << ": no old runway with ident:" << oldIdent);
776 continue;
777 }
778
779 _renamedRunways[newIdent] = oldIdent;
780 }
781 }
782
783 std::string FGAirport::findAPTRunwayForNewName(const std::string& newIdent) const
784 {
785 loadRunwayRenames();
786
787 auto it = _renamedRunways.find(newIdent);
788 if (it == _renamedRunways.end())
789 return {};
790
791 return it->second;
792 }
793
794
795 void FGAirport::validateILSData()
796 {
797 if (mILSDataLoaded) {
798 return;
799 }
800
801 // to avoid re-entrancy on this code-path, ensure we set loaded
802 // immediately.
803 mILSDataLoaded = true;
804
805 SGPath path;
806 if (!XMLLoader::findAirportData(ident(), "ils", path)) {
807 return; // no XML ILS data
808 }
809
810 try {
811 SGPropertyNode_ptr rootNode = new SGPropertyNode;
812 readProperties(path, rootNode);
813 readILSData(rootNode);
814 } catch (sg_exception& e){
815 SG_LOG(SG_NAVAID, SG_WARN, ident() << "loading ils XML failed:" << e.getFormattedMessage());
816 }
817 }
818
819 bool FGAirport::hasTower() const
820 {
821 validateTowerData();
822 return mHasTower;
823 }
824
825 void FGAirport::readILSData(SGPropertyNode* aRoot)
826 {
827 NavDataCache* cache = NavDataCache::instance();
828 // find the entry matching the runway
829 SGPropertyNode* runwayNode, *ilsNode;
830 for (int i=0; (runwayNode = aRoot->getChild("runway", i)) != NULL; ++i) {
831 for (int j=0; (ilsNode = runwayNode->getChild("ils", j)) != NULL; ++j) {
832 // must match on both nav-ident and runway ident, to support the following:
833 // - runways with multiple distinct ILS installations (KEWD, for example)
834 // - runways where both ends share the same nav ident (LFAT, for example)
835 PositionedID ils = cache->findILS(guid(), ilsNode->getStringValue("rwy"),
836 ilsNode->getStringValue("nav-id"));
837 if (ils == 0) {
838 SG_LOG(SG_GENERAL, SG_INFO, "reading ILS data for " << ident() <<
839 ", couldn't find runway/navaid for:" <<
840 ilsNode->getStringValue("rwy") << "/" <<
841 ilsNode->getStringValue("nav-id"));
842 continue;
843 }
844
845 double hdgDeg = ilsNode->getDoubleValue("hdg-deg"),
846 lon = ilsNode->getDoubleValue("lon"),
847 lat = ilsNode->getDoubleValue("lat"),
848 elevM = ilsNode->getDoubleValue("elev-m");
849
850 FGNavRecordRef nav(FGPositioned::loadById<FGNavRecord>(ils));
851 assert(nav.valid());
852 nav->updateFromXML(SGGeod::fromDegM(lon, lat, elevM), hdgDeg);
853 } // of ILS iteration
854 } // of runway iteration
855 }
856
857 void FGAirport::addSID(flightgear::SID* aSid)
858 {
859 mSIDs.push_back(aSid);
860 }
861
862 void FGAirport::addSTAR(STAR* aStar)
863 {
864 mSTARs.push_back(aStar);
865 }
866
867 void FGAirport::addApproach(Approach* aApp)
868 {
869 mApproaches.push_back(aApp);
870 }
871
872 //------------------------------------------------------------------------------
873 unsigned int FGAirport::numSIDs() const
874 {
875 loadProcedures();
876 return mSIDs.size();
877 }
878
879 //------------------------------------------------------------------------------
880 flightgear::SID* FGAirport::getSIDByIndex(unsigned int aIndex) const
881 {
882 loadProcedures();
883 return mSIDs[aIndex];
884 }
885
886 //------------------------------------------------------------------------------
887 flightgear::SID* FGAirport::findSIDWithIdent(const std::string& aIdent) const
888 {
889 loadProcedures();
890 for (unsigned int i=0; i<mSIDs.size(); ++i) {
891 if (mSIDs[i]->ident() == aIdent) {
892 return mSIDs[i];
893 }
894 }
895
896 return NULL;
897 }
898
899 //------------------------------------------------------------------------------
900 flightgear::SIDList FGAirport::getSIDs() const
901 {
902 loadProcedures();
903 return flightgear::SIDList(mSIDs.begin(), mSIDs.end());
904 }
905
906 //------------------------------------------------------------------------------
907 unsigned int FGAirport::numSTARs() const
908 {
909 loadProcedures();
910 return mSTARs.size();
911 }
912
913 //------------------------------------------------------------------------------
914 STAR* FGAirport::getSTARByIndex(unsigned int aIndex) const
915 {
916 loadProcedures();
917 return mSTARs[aIndex];
918 }
919
920 //------------------------------------------------------------------------------
921 STAR* FGAirport::findSTARWithIdent(const std::string& aIdent) const
922 {
923 loadProcedures();
924 for (unsigned int i=0; i<mSTARs.size(); ++i) {
925 if (mSTARs[i]->ident() == aIdent) {
926 return mSTARs[i];
927 }
928 }
929
930 return NULL;
931 }
932
933 //------------------------------------------------------------------------------
934 STARList FGAirport::getSTARs() const
935 {
936 loadProcedures();
937 return STARList(mSTARs.begin(), mSTARs.end());
938 }
939
940 unsigned int FGAirport::numApproaches() const
941 {
942 loadProcedures();
943 return mApproaches.size();
944 }
945
946 //------------------------------------------------------------------------------
947 Approach* FGAirport::getApproachByIndex(unsigned int aIndex) const
948 {
949 loadProcedures();
950 return mApproaches[aIndex];
951 }
952
953 //------------------------------------------------------------------------------
954 Approach* FGAirport::findApproachWithIdent(const std::string& aIdent) const
955 {
956 loadProcedures();
957 for (unsigned int i=0; i<mApproaches.size(); ++i) {
958 if (mApproaches[i]->ident() == aIdent) {
959 return mApproaches[i];
960 }
961 }
962
963 return NULL;
964 }
965
966 //------------------------------------------------------------------------------
967 ApproachList FGAirport::getApproaches(ProcedureType type) const
968 {
969 loadProcedures();
970 if( type == PROCEDURE_INVALID )
971 return ApproachList(mApproaches.begin(), mApproaches.end());
972
973 ApproachList ret;
974 for(size_t i = 0; i < mApproaches.size(); ++i)
975 {
976 if( mApproaches[i]->type() == type )
977 ret.push_back(mApproaches[i]);
978 }
979 return ret;
980 }
981
982 CommStationList
983 FGAirport::commStations() const
984 {
985 NavDataCache* cache = NavDataCache::instance();
986 CommStationList result;
987 for (auto pos : cache->airportItemsOfType(guid(),
988 FGPositioned::FREQ_GROUND,
989 FGPositioned::FREQ_UNICOM)) {
990 result.push_back( loadById<CommStation>(pos) );
991 }
992
993 return result;
994 }
995
996 CommStationList
997 FGAirport::commStationsOfType(FGPositioned::Type aTy) const
998 {
999 NavDataCache* cache = NavDataCache::instance();
1000 CommStationList result;
1001 for (auto pos : cache->airportItemsOfType(guid(), aTy)) {
1002 result.push_back( loadById<CommStation>(pos) );
1003 }
1004
1005 return result;
1006 }
1007
1008 class AirportWithSize
1009 {
1010 public:
1011 explicit AirportWithSize(FGPositionedRef pos) :
1012 _pos(pos),
1013 _sizeMetric(0)
1014 {
1015 assert(pos->type() == FGPositioned::AIRPORT);
1016 FGAirport* apt = static_cast<FGAirport*>(pos.get());
1017 for (auto rwy : apt->getRunwaysWithoutReciprocals()) {
1018 _sizeMetric += static_cast<int>(rwy->lengthFt());
1019 }
1020 }
1021
1022 bool operator<(const AirportWithSize& other) const
1023 {
1024 return _sizeMetric < other._sizeMetric;
1025 }
1026
1027 FGPositionedRef pos() const
1028 { return _pos; }
1029 private:
1030 FGPositionedRef _pos;
1031 unsigned int _sizeMetric;
1032
1033 };
1034
1035 void FGAirport::sortBySize(FGPositionedList& airportList)
1036 {
1037 std::vector<AirportWithSize> annotated;
1038 for (auto p : airportList) {
1039 annotated.push_back(AirportWithSize(p));
1040 }
1041 std::sort(annotated.begin(), annotated.end());
1042
1043 for (unsigned int i=0; i<annotated.size(); ++i) {
1044 airportList[i] = annotated[i].pos();
1045 }
1046 }
1047
1048 FGAirportDynamicsRef FGAirport::getDynamics() const
1049 {
1050 return flightgear::AirportDynamicsManager::find(const_cast<FGAirport*>(this));
1051 }
1052
1053 FGGroundNetwork *FGAirport::groundNetwork() const
1054 {
1055 if (!_groundNetwork.get()) {
1056 _groundNetwork.reset(new FGGroundNetwork(const_cast<FGAirport*>(this)));
1057 XMLLoader::load(_groundNetwork.get());
1058 _groundNetwork->init();
1059 }
1060
1061 return _groundNetwork.get();
1062 }
1063
1064 flightgear::Transition* FGAirport::selectSIDByEnrouteTransition(FGPositioned* enroute) const
1065 {
1066 loadProcedures();
1067 for (auto sid : mSIDs) {
1068 auto trans = sid->findTransitionByEnroute(enroute);
1069 if (trans) {
1070 return trans;
1071 }
1072 }
1073 return nullptr;
1074 }
1075
1076 Transition *FGAirport::selectSIDByTransition(const FGRunway* runway, const std::string &aIdent) const
1077 {
1078 loadProcedures();
1079 for (auto sid : mSIDs) {
1080 if (runway && !sid->isForRunway(runway))
1081 continue;
1082
1083 auto trans = sid->findTransitionByName(aIdent);
1084 if (trans) {
1085 return trans;
1086 }
1087 }
1088 return nullptr;
1089 }
1090
1091 flightgear::Transition* FGAirport::selectSTARByEnrouteTransition(FGPositioned* enroute) const
1092 {
1093 loadProcedures();
1094 for (auto star : mSTARs) {
1095 auto trans = star->findTransitionByEnroute(enroute);
1096 if (trans) {
1097 return trans;
1098 }
1099 }
1100 return nullptr;
1101 }
1102
1103 Transition *FGAirport::selectSTARByTransition(const FGRunway* runway, const std::string &aIdent) const
1104 {
1105 loadProcedures();
1106 for (auto star : mSTARs) {
1107 if (runway && !star->isForRunway(runway))
1108 continue;
1109
1110 auto trans = star->findTransitionByName(aIdent);
1111 if (trans) {
1112 return trans;
1113 }
1114 }
1115 return nullptr;
1116 }
1117
1118 // get airport elevation
1119 double fgGetAirportElev( const std::string& id )
1120 {
1121 const FGAirport *a=fgFindAirportID( id);
1122 if (a) {
1123 return a->getElevation();
1124 } else {
1125 return -9999.0;
1126 }
1127 }
1128
1129
1130 // get airport position
1131 SGGeod fgGetAirportPos( const std::string& id )
1132 {
1133 const FGAirport *a = fgFindAirportID( id);
1134
1135 if (a) {
1136 return SGGeod::fromDegM(a->getLongitude(), a->getLatitude(), a->getElevation());
1137 } else {
1138 return SGGeod::fromDegM(0.0, 0.0, -9999.0);
1139 }
1140 }