Debian Codegraph @

src/Navaids/waypoint.cxx

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // waypoint.cxx - waypoints that can occur in routes/procedures
2 // Written by James Turner, started 2009.
3 //
4 // Copyright (C) 2009 Curtis L. Olson
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include "waypoint.hxx"
25
26 #include <simgear/structure/exception.hxx>
27 #include <simgear/misc/strutils.hxx>
28
29 #include <Airports/airport.hxx>
30 #include <Airports/runways.hxx>
31 #include <Navaids/airways.hxx>
32
33 using std::string;
34
35 namespace flightgear
36 {
37
38 BasicWaypt::BasicWaypt(const SGGeod& aPos, const string& aIdent, RouteBase* aOwner) :
39 Waypt(aOwner),
40 _pos(aPos),
41 _ident(aIdent)
42 {
43 }
44
45 BasicWaypt::BasicWaypt(RouteBase* aOwner) :
46 Waypt(aOwner)
47 {
48 }
49
50 bool BasicWaypt::initFromProperties(SGPropertyNode_ptr aProp)
51 {
52 if (!aProp->hasChild("lon") || !aProp->hasChild("lat")) {
53 SG_LOG(SG_AUTOPILOT, SG_WARN, "missing lon/lat properties");
54 return false;
55 }
56
57 if (!Waypt::initFromProperties(aProp))
58 return false;
59
60 _pos = SGGeod::fromDeg(aProp->getDoubleValue("lon"),
61 aProp->getDoubleValue("lat"));
62 _ident = aProp->getStringValue("ident");
63 return true;
64 }
65
66 void BasicWaypt::writeToProperties(SGPropertyNode_ptr aProp) const
67 {
68 Waypt::writeToProperties(aProp);
69
70 aProp->setStringValue("ident", _ident);
71 aProp->setDoubleValue("lon", _pos.getLongitudeDeg());
72 aProp->setDoubleValue("lat", _pos.getLatitudeDeg());
73 }
74
75 std::string BasicWaypt::icaoDescription() const
76 {
77 return simgear::strutils::formatGeodAsString(_pos, simgear::strutils::LatLonFormat::ICAO_ROUTE_DEGREES);
78 }
79
80 //////////////////////////////////////////////////////////////////////////////
81
82 NavaidWaypoint::NavaidWaypoint(FGPositioned* aPos, RouteBase* aOwner) :
83 Waypt(aOwner),
84 _navaid(aPos)
85 {
86 if (aPos->type() == FGPositioned::RUNWAY) {
87 SG_LOG(SG_NAVAID, SG_WARN, "sure you don't want to be building a runway waypt here?");
88 }
89 }
90
91 NavaidWaypoint::NavaidWaypoint(RouteBase* aOwner) :
92 Waypt(aOwner)
93 {
94 }
95
96
97 SGGeod NavaidWaypoint::position() const
98 {
99 return SGGeod::fromGeodFt(_navaid->geod(), altitudeFt());
100 }
101
102 std::string NavaidWaypoint::ident() const
103 {
104 return _navaid->ident();
105 }
106
107 bool NavaidWaypoint::initFromProperties(SGPropertyNode_ptr aProp)
108 {
109 if (!aProp->hasChild("ident")) {
110 SG_LOG(SG_AUTOPILOT, SG_WARN, "missing navaid ident");
111 return false;
112 }
113
114 if (!Waypt::initFromProperties(aProp))
115 return false;
116
117 std::string idn(aProp->getStringValue("ident"));
118 SGGeod p;
119 if (aProp->hasChild("lon")) {
120 p = SGGeod::fromDeg(aProp->getDoubleValue("lon"), aProp->getDoubleValue("lat"));
121 }
122
123 // FIXME - resolve co-located DME, etc
124 // is it sufficent just to ignore DMEs, actually?
125 FGPositionedRef nav = FGPositioned::findClosestWithIdent(idn, p, nullptr);
126 if (!nav) {
127 SG_LOG(SG_AUTOPILOT, SG_WARN, "unknown navdaid ident:" << idn);
128 return false;
129 }
130
131 if (p.isValid() && (SGGeodesy::distanceM(nav->geod(), p) > 4000)) {
132 // the looked up navaid was more than 4000 metres from the lat/lon.
133 // in this case, throw an exception here so we fall back to using
134 // a basic waypoint
135 // see https://sourceforge.net/p/flightgear/codetickets/1814/
136 SG_LOG(SG_AUTOPILOT, SG_WARN, "Waypoint navaid for ident:" << idn << " is too far from the specified lat/lon");
137 return false;
138 }
139
140 _navaid = nav;
141 return true;
142 }
143
144 void NavaidWaypoint::writeToProperties(SGPropertyNode_ptr aProp) const
145 {
146 Waypt::writeToProperties(aProp);
147
148 aProp->setStringValue("ident", _navaid->ident());
149 // write lon/lat to disambiguate
150 aProp->setDoubleValue("lon", _navaid->geod().getLongitudeDeg());
151 aProp->setDoubleValue("lat", _navaid->geod().getLatitudeDeg());
152 }
153
154 OffsetNavaidWaypoint::OffsetNavaidWaypoint(FGPositioned* aPos, RouteBase* aOwner,
155 double aRadial, double aDistNm) :
156 NavaidWaypoint(aPos, aOwner),
157 _radial(aRadial),
158 _distanceNm(aDistNm)
159 {
160 init();
161 }
162
163 OffsetNavaidWaypoint::OffsetNavaidWaypoint(RouteBase* aOwner) :
164 NavaidWaypoint(aOwner)
165 {
166 }
167
168 void OffsetNavaidWaypoint::init()
169 {
170 SGGeod offset;
171 double az2;
172 SGGeodesy::direct(_navaid->geod(), _radial, _distanceNm * SG_NM_TO_METER, offset, az2);
173 _geod = SGGeod::fromGeodFt(offset, altitude(ALTITUDE_FEET));
174 }
175
176 bool OffsetNavaidWaypoint::initFromProperties(SGPropertyNode_ptr aProp)
177 {
178 if (!aProp->hasChild("radial-deg") || !aProp->hasChild("distance-nm")) {
179 SG_LOG(SG_AUTOPILOT, SG_WARN, "missing radial/offset distance creating offset waypoint");
180 return false;
181 }
182
183 if (!NavaidWaypoint::initFromProperties(aProp))
184 return false;
185
186 _radial = aProp->getDoubleValue("radial-deg");
187 _distanceNm = aProp->getDoubleValue("distance-nm");
188 init();
189 return true;
190 }
191
192 void OffsetNavaidWaypoint::writeToProperties(SGPropertyNode_ptr aProp) const
193 {
194 NavaidWaypoint::writeToProperties(aProp);
195 aProp->setDoubleValue("radial-deg", _radial);
196 aProp->setDoubleValue("distance-nm", _distanceNm);
197 }
198
199 /////////////////////////////////////////////////////////////////////////////
200
201 RunwayWaypt::RunwayWaypt(FGRunway* aPos, RouteBase* aOwner) :
202 Waypt(aOwner),
203 _runway(aPos)
204 {
205 assert(aPos != nullptr);
206 }
207
208 RunwayWaypt::RunwayWaypt(RouteBase* aOwner) :
209 Waypt(aOwner)
210 {
211 }
212
213 SGGeod RunwayWaypt::position() const
214 {
215 return _runway->threshold();
216 }
217
218 std::string RunwayWaypt::ident() const
219 {
220 return _runway->airport()->ident() + "-" + _runway->ident();
221 }
222
223 FGPositioned* RunwayWaypt::source() const
224 {
225 return _runway;
226 }
227
228 double RunwayWaypt::headingRadialDeg() const
229 {
230 return _runway->headingDeg();
231 }
232
233 bool RunwayWaypt::initFromProperties(SGPropertyNode_ptr aProp)
234 {
235 if (!aProp->hasChild("icao") || !aProp->hasChild("ident")) {
236 SG_LOG(SG_AUTOPILOT, SG_WARN, "missing ICAO/ident on runway waypoint");
237 return false;
238 }
239
240 if (!Waypt::initFromProperties(aProp))
241 return false;
242
243 std::string idn(aProp->getStringValue("ident"));
244 const FGAirport* apt = FGAirport::getByIdent(aProp->getStringValue("icao"));
245 if (!apt) {
246 SG_LOG(SG_AUTOPILOT, SG_WARN, "Unknown airport:" << aProp->getStringValue("icao"));
247 return false;
248 }
249
250 const std::string ident = aProp->getStringValue("ident");
251 if (!apt->hasRunwayWithIdent(ident)) {
252 SG_LOG(SG_AUTOPILOT, SG_WARN, "Unknown runway " << ident << " at " << aProp->getStringValue("icao"));
253 return false;
254 }
255
256 _runway = apt->getRunwayByIdent(ident);
257 return true;
258 }
259
260 void RunwayWaypt::writeToProperties(SGPropertyNode_ptr aProp) const
261 {
262 Waypt::writeToProperties(aProp);
263 aProp->setStringValue("ident", _runway->ident());
264 aProp->setStringValue("icao", _runway->airport()->ident());
265 }
266
267 /////////////////////////////////////////////////////////////////////////////
268
269 Hold::Hold(const SGGeod& aPos, const string& aIdent, RouteBase* aOwner) :
270 BasicWaypt(aPos, aIdent, aOwner),
271 _righthanded(true),
272 _isDistance(false)
273 {
274 }
275
276 Hold::Hold(RouteBase* aOwner) :
277 BasicWaypt(aOwner),
278 _righthanded(true),
279 _isDistance(false)
280 {
281 }
282
283 void Hold::setHoldRadial(double aInboundRadial)
284 {
285 _bearing = aInboundRadial;
286 }
287
288 void Hold::setHoldDistance(double aDistanceNm)
289 {
290 _isDistance = true;
291 _holdTD = aDistanceNm;
292 }
293
294 void Hold::setHoldTime(double aTimeSec)
295 {
296 _isDistance = false;
297 _holdTD = aTimeSec;
298 }
299
300 void Hold::setRightHanded()
301 {
302 _righthanded = true;
303 }
304
305 void Hold::setLeftHanded()
306 {
307 _righthanded = false;
308 }
309
310 bool Hold::initFromProperties(SGPropertyNode_ptr aProp)
311 {
312 if (!BasicWaypt::initFromProperties(aProp))
313 return false;
314
315 _righthanded = aProp->getBoolValue("right-handed");
316 _isDistance = aProp->getBoolValue("is-distance");
317 _bearing = aProp->getDoubleValue("inbound-radial-deg");
318 _holdTD = aProp->getDoubleValue("td");
319
320 return true;
321 }
322
323 void Hold::writeToProperties(SGPropertyNode_ptr aProp) const
324 {
325 BasicWaypt::writeToProperties(aProp);
326
327 aProp->setBoolValue("right-handed", _righthanded);
328 aProp->setBoolValue("is-distance", _isDistance);
329 aProp->setDoubleValue("inbound-radial-deg", _bearing);
330 aProp->setDoubleValue("td", _holdTD);
331 }
332
333 /////////////////////////////////////////////////////////////////////////////
334
335 HeadingToAltitude::HeadingToAltitude(RouteBase* aOwner, const string& aIdent,
336 double aMagHdg) :
337 Waypt(aOwner),
338 _ident(aIdent),
339 _magHeading(aMagHdg)
340 {
341 setFlag(WPT_DYNAMIC);
342 }
343
344 HeadingToAltitude::HeadingToAltitude(RouteBase* aOwner) :
345 Waypt(aOwner)
346 {
347 }
348
349 bool HeadingToAltitude::initFromProperties(SGPropertyNode_ptr aProp)
350 {
351 if (!aProp->hasChild("heading-deg")) {
352 SG_LOG(SG_AUTOPILOT, SG_WARN, "Missing heading property creating HdgToAlt waypoint");
353 return false;
354 }
355
356 if (!Waypt::initFromProperties(aProp))
357 return false;
358
359 _magHeading = aProp->getDoubleValue("heading-deg");
360 _ident = aProp->getStringValue("ident");
361 return true;
362 }
363
364 void HeadingToAltitude::writeToProperties(SGPropertyNode_ptr aProp) const
365 {
366 Waypt::writeToProperties(aProp);
367 aProp->setStringValue("ident", _ident);
368 aProp->setDoubleValue("heading-deg", _magHeading);
369 }
370
371 /////////////////////////////////////////////////////////////////////////////
372
373 DMEIntercept::DMEIntercept(RouteBase* aOwner, const string& aIdent, const SGGeod& aPos,
374 double aCourseDeg, double aDistanceNm) :
375 Waypt(aOwner),
376 _ident(aIdent),
377 _pos(aPos),
378 _magCourse(aCourseDeg),
379 _dmeDistanceNm(aDistanceNm)
380 {
381 setFlag(WPT_DYNAMIC);
382 }
383
384 DMEIntercept::DMEIntercept(RouteBase* aOwner) :
385 Waypt(aOwner)
386 {
387 }
388
389 bool DMEIntercept::initFromProperties(SGPropertyNode_ptr aProp)
390 {
391 if (!aProp->hasChild("lon") || !aProp->hasChild("lat")) {
392 SG_LOG(SG_AUTOPILOT, SG_WARN, "Missing lat/lom properties creating DMEIntc waypoint");
393 return false;
394 }
395
396 if (!Waypt::initFromProperties(aProp))
397 return false;
398
399 _pos = SGGeod::fromDeg(aProp->getDoubleValue("lon"), aProp->getDoubleValue("lat"));
400 _ident = aProp->getStringValue("ident");
401 // check it's a real DME?
402 _magCourse = aProp->getDoubleValue("course-deg");
403 _dmeDistanceNm = aProp->getDoubleValue("dme-distance-nm");
404
405 return true;
406 }
407
408 void DMEIntercept::writeToProperties(SGPropertyNode_ptr aProp) const
409 {
410 Waypt::writeToProperties(aProp);
411
412 aProp->setStringValue("ident", _ident);
413 aProp->setDoubleValue("lon", _pos.getLongitudeDeg());
414 aProp->setDoubleValue("lat", _pos.getLatitudeDeg());
415 aProp->setDoubleValue("course-deg", _magCourse);
416 aProp->setDoubleValue("dme-distance-nm", _dmeDistanceNm);
417 }
418
419 /////////////////////////////////////////////////////////////////////////////
420
421 RadialIntercept::RadialIntercept(RouteBase* aOwner, const string& aIdent, const SGGeod& aPos,
422 double aCourseDeg, double aRadial) :
423 Waypt(aOwner),
424 _ident(aIdent),
425 _pos(aPos),
426 _magCourse(aCourseDeg),
427 _radial(aRadial)
428 {
429 setFlag(WPT_DYNAMIC);
430 }
431
432 RadialIntercept::RadialIntercept(RouteBase* aOwner) :
433 Waypt(aOwner)
434 {
435 }
436
437 bool RadialIntercept::initFromProperties(SGPropertyNode_ptr aProp)
438 {
439 if (!aProp->hasChild("lon") || !aProp->hasChild("lat")) {
440 SG_LOG(SG_AUTOPILOT, SG_WARN, "Missing lat/lom properties creating RadialIntercept waypoint");
441 return false;
442 }
443
444 if (!Waypt::initFromProperties(aProp))
445 return false;
446
447 _pos = SGGeod::fromDeg(aProp->getDoubleValue("lon"), aProp->getDoubleValue("lat"));
448 _ident = aProp->getStringValue("ident");
449 // check it's a real VOR?
450 _magCourse = aProp->getDoubleValue("course-deg");
451 _radial = aProp->getDoubleValue("radial-deg");
452
453 return true;
454 }
455
456 void RadialIntercept::writeToProperties(SGPropertyNode_ptr aProp) const
457 {
458 Waypt::writeToProperties(aProp);
459
460 aProp->setStringValue("ident", _ident);
461 aProp->setDoubleValue("lon", _pos.getLongitudeDeg());
462 aProp->setDoubleValue("lat", _pos.getLatitudeDeg());
463 aProp->setDoubleValue("course-deg", _magCourse);
464 aProp->setDoubleValue("radial-deg", _radial);
465 }
466
467 /////////////////////////////////////////////////////////////////////////////
468
469 ATCVectors::ATCVectors(RouteBase* aOwner, FGAirport* aFacility) :
470 Waypt(aOwner),
471 _facility(aFacility)
472 {
473 setFlag(WPT_DYNAMIC);
474 }
475
476 ATCVectors::~ATCVectors()
477 {
478 }
479
480 ATCVectors::ATCVectors(RouteBase* aOwner) :
481 Waypt(aOwner)
482 {
483 }
484
485 SGGeod ATCVectors::position() const
486 {
487 return _facility->geod();
488 }
489
490 string ATCVectors::ident() const
491 {
492 return "VECTORS-" + _facility->ident();
493 }
494
495 bool ATCVectors::initFromProperties(SGPropertyNode_ptr aProp)
496 {
497 if (!Waypt::initFromProperties(aProp))
498 return false;
499
500 _facility = FGAirport::getByIdent(aProp->getStringValue("icao"));
501 return true;
502 }
503
504 void ATCVectors::writeToProperties(SGPropertyNode_ptr aProp) const
505 {
506 Waypt::writeToProperties(aProp);
507 if (_facility) {
508 aProp->setStringValue("icao", _facility->ident());
509 }
510 }
511
512 /////////////////////////////////////////////////////////////////////////////
513
514 Discontinuity::Discontinuity(RouteBase* aOwner) :
515 Waypt(aOwner)
516 {
517 setFlag(WPT_DYNAMIC);
518 setFlag(WPT_GENERATED); // prevent drag, delete, etc
519 }
520
521 Discontinuity::~Discontinuity()
522 {
523 }
524
525 SGGeod Discontinuity::position() const
526 {
527 return SGGeod(); // deliberately invalid of course
528 }
529
530 string Discontinuity::ident() const
531 {
532 return "DISCONTINUITY";
533 }
534
535 bool Discontinuity::initFromProperties(SGPropertyNode_ptr aProp)
536 {
537 return true;
538 }
539
540 void Discontinuity::writeToProperties(SGPropertyNode_ptr aProp) const
541 {
542 Waypt::writeToProperties(aProp);
543 }
544
545 /////////////////////////////////////////////////////////////////////////////
546
547 SGGeod Via::position() const
548 {
549 return _to->geod();
550 }
551
552 string Via::ident() const
553 {
554 return "VIA " + _airway->ident() + " TO " + _to->ident();
555 }
556
557 Via::Via(RouteBase *aOwner) :
558 Waypt(aOwner)
559 {
560 }
561
562 Via::Via(RouteBase *aOwner, AirwayRef awy, FGPositionedRef to) :
563 Waypt(aOwner),
564 _airway(awy),
565 _to(to)
566 {
567 }
568
569 Via::~Via()
570 {
571 }
572
573 bool Via::initFromProperties(SGPropertyNode_ptr aProp)
574 {
575 if (!aProp->hasChild("airway") || !aProp->hasChild("to")) {
576 SG_LOG(SG_AUTOPILOT, SG_WARN, "Missingairway/to properties on Via wp");
577 return false;
578 }
579
580 if (!Waypt::initFromProperties(aProp))
581 return false;
582
583 const std::string ident = aProp->getStringValue("airway");
584 const Airway::Level level = static_cast<Airway::Level>(aProp->getIntValue("level", Airway::Both));
585 _airway = Airway::findByIdent(ident, level);
586 if (!_airway) {
587 SG_LOG(SG_AUTOPILOT, SG_WARN, "VIA: unknown airway:" << ident);
588 return false;
589 }
590
591 std::string idn(aProp->getStringValue("to"));
592 SGGeod p;
593 if (aProp->hasChild("lon")) {
594 p = SGGeod::fromDeg(aProp->getDoubleValue("lon"),
595 aProp->getDoubleValue("lat"));
596 }
597
598 FGPositionedRef nav = FGPositioned::findClosestWithIdent(idn, p, nullptr);
599 if (!nav) {
600 SG_LOG(SG_AUTOPILOT, SG_WARN, "VIA TO navaid: " << idn << " not found");
601 return false;
602 }
603
604 if (!_airway->containsNavaid(nav)) {
605 // warn but don't block this
606 SG_LOG(SG_AUTOPILOT, SG_WARN, "VIA TO navaid: " << idn << " not found on airway " << _airway);
607 }
608
609 _to = nav;
610 return true;
611 }
612
613 void Via::writeToProperties(SGPropertyNode_ptr aProp) const
614 {
615 Waypt::writeToProperties(aProp);
616 aProp->setStringValue("airway", _airway->ident());
617 aProp->setIntValue("level", _airway->level());
618
619 aProp->setStringValue("to", _to->ident());
620 // write lon/lat to disambiguate
621 aProp->setDoubleValue("lon", _to->geod().getLongitudeDeg());
622 aProp->setDoubleValue("lat", _to->geod().getLatitudeDeg());
623 }
624
625 WayptVec Via::expandToWaypoints(WayptRef aPreceeding) const
626 {
627 if (!aPreceeding) {
628 throw sg_exception("invalid preceeding waypoint");
629 }
630
631 // this waypoint is noly used for the search, it's not part
632 // of the result, so we don't need to set the owner
633 WayptRef toWp = new NavaidWaypoint(_to, nullptr);
634 return _airway->via(aPreceeding, toWp);
635 }
636
637 } // of namespace