1 // waypoint.cxx - waypoints that can occur in routes/procedures
2 // Written by James Turner, started 2009.
4 // Copyright (C) 2009 Curtis L. Olson
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.
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.
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.
24 #include "waypoint.hxx"
26 #include <simgear/structure/exception.hxx>
27 #include <simgear/route/waypoint.hxx>
29 #include <Airports/simple.hxx>
30 #include <Airports/runways.hxx>
37 BasicWaypt::BasicWaypt(const SGGeod& aPos, const string& aIdent, Route* aOwner) :
42 if (aPos.getElevationFt() > -999.0) {
43 setAltitude(aPos.getElevationFt(), RESTRICT_AT);
47 BasicWaypt::BasicWaypt(const SGWayPoint& aWP, Route* aOwner) :
49 _pos(aWP.get_target()),
54 BasicWaypt::BasicWaypt(Route* aOwner) :
59 void BasicWaypt::initFromProperties(SGPropertyNode_ptr aProp)
61 if (!aProp->hasChild("lon") || !aProp->hasChild("lat")) {
62 throw sg_io_exception("missing lon/lat properties",
63 "BasicWaypt::initFromProperties");
66 Waypt::initFromProperties(aProp);
68 _pos = SGGeod::fromDeg(aProp->getDoubleValue("lon"),
69 aProp->getDoubleValue("lat"));
70 _ident = aProp->getStringValue("ident");
73 void BasicWaypt::writeToProperties(SGPropertyNode_ptr aProp) const
75 Waypt::writeToProperties(aProp);
77 aProp->setStringValue("ident", _ident);
78 aProp->setDoubleValue("lon", _pos.getLongitudeDeg());
79 aProp->setDoubleValue("lat", _pos.getLatitudeDeg());
82 //////////////////////////////////////////////////////////////////////////////
84 NavaidWaypoint::NavaidWaypoint(FGPositioned* aPos, Route* aOwner) :
88 if (aPos->type() == FGPositioned::RUNWAY) {
89 SG_LOG(SG_GENERAL, SG_WARN, "sure you don't want to be building a runway waypt here?");
93 NavaidWaypoint::NavaidWaypoint(Route* aOwner) :
99 SGGeod NavaidWaypoint::position() const
101 return SGGeod::fromGeodFt(_navaid->geod(), _altitudeFt);
104 std::string NavaidWaypoint::ident() const
106 return _navaid->ident();
109 void NavaidWaypoint::initFromProperties(SGPropertyNode_ptr aProp)
111 if (!aProp->hasChild("ident")) {
112 throw sg_io_exception("missing navaid value",
113 "NavaidWaypoint::initFromProperties");
116 Waypt::initFromProperties(aProp);
118 std::string idn(aProp->getStringValue("ident"));
120 if (aProp->hasChild("lon")) {
121 p = SGGeod::fromDeg(aProp->getDoubleValue("lon"), aProp->getDoubleValue("lat"));
124 // FIXME - resolve co-located DME, etc
125 // is it sufficent just to ignore DMEs, actually?
126 FGPositionedRef nav = FGPositioned::findClosestWithIdent(idn, p, NULL);
128 throw sg_io_exception("unknown navaid ident:" + idn,
129 "NavaidWaypoint::initFromProperties");
135 void NavaidWaypoint::writeToProperties(SGPropertyNode_ptr aProp) const
137 Waypt::writeToProperties(aProp);
139 aProp->setStringValue("ident", _navaid->ident());
140 // write lon/lat to disambiguate
141 aProp->setDoubleValue("lon", _navaid->geod().getLongitudeDeg());
142 aProp->setDoubleValue("lat", _navaid->geod().getLatitudeDeg());
145 OffsetNavaidWaypoint::OffsetNavaidWaypoint(FGPositioned* aPos, Route* aOwner,
146 double aRadial, double aDistNm) :
147 NavaidWaypoint(aPos, aOwner),
154 OffsetNavaidWaypoint::OffsetNavaidWaypoint(Route* aOwner) :
155 NavaidWaypoint(aOwner)
159 void OffsetNavaidWaypoint::init()
163 SGGeodesy::direct(_navaid->geod(), _radial, _distanceNm * SG_NM_TO_METER, offset, az2);
164 _geod = SGGeod::fromGeodFt(offset, _altitudeFt);
167 void OffsetNavaidWaypoint::initFromProperties(SGPropertyNode_ptr aProp)
169 if (!aProp->hasChild("radial-deg") || !aProp->hasChild("distance-nm")) {
170 throw sg_io_exception("missing radial/offset distance",
171 "OffsetNavaidWaypoint::initFromProperties");
174 NavaidWaypoint::initFromProperties(aProp);
175 _radial = aProp->getDoubleValue("radial-deg");
176 _distanceNm = aProp->getDoubleValue("distance-nm");
180 void OffsetNavaidWaypoint::writeToProperties(SGPropertyNode_ptr aProp) const
182 NavaidWaypoint::writeToProperties(aProp);
183 aProp->setDoubleValue("radial-deg", _radial);
184 aProp->setDoubleValue("distance-nm", _distanceNm);
187 /////////////////////////////////////////////////////////////////////////////
189 RunwayWaypt::RunwayWaypt(FGRunway* aPos, Route* aOwner) :
195 RunwayWaypt::RunwayWaypt(Route* aOwner) :
200 SGGeod RunwayWaypt::position() const
202 return _runway->threshold();
205 std::string RunwayWaypt::ident() const
207 return _runway->airport()->ident() + "-" + _runway->ident();
210 FGPositioned* RunwayWaypt::source() const
215 void RunwayWaypt::initFromProperties(SGPropertyNode_ptr aProp)
217 if (!aProp->hasChild("icao") || !aProp->hasChild("ident")) {
218 throw sg_io_exception("missing values: icao or ident",
219 "RunwayWaypoint::initFromProperties");
222 Waypt::initFromProperties(aProp);
223 std::string idn(aProp->getStringValue("ident"));
224 const FGAirport* apt = FGAirport::getByIdent(aProp->getStringValue("icao"));
225 _runway = apt->getRunwayByIdent(aProp->getStringValue("ident"));
228 void RunwayWaypt::writeToProperties(SGPropertyNode_ptr aProp) const
230 Waypt::writeToProperties(aProp);
231 aProp->setStringValue("ident", _runway->ident());
232 aProp->setStringValue("icao", _runway->airport()->ident());
235 /////////////////////////////////////////////////////////////////////////////
237 Hold::Hold(const SGGeod& aPos, const string& aIdent, Route* aOwner) :
238 BasicWaypt(aPos, aIdent, aOwner),
242 setFlag(WPT_DYNAMIC);
245 Hold::Hold(Route* aOwner) :
252 void Hold::setHoldRadial(double aInboundRadial)
254 _bearing = aInboundRadial;
257 void Hold::setHoldDistance(double aDistanceNm)
260 _holdTD = aDistanceNm;
263 void Hold::setHoldTime(double aTimeSec)
269 void Hold::setRightHanded()
274 void Hold::setLeftHanded()
276 _righthanded = false;
279 void Hold::initFromProperties(SGPropertyNode_ptr aProp)
281 BasicWaypt::initFromProperties(aProp);
283 if (!aProp->hasChild("lon") || !aProp->hasChild("lat")) {
284 throw sg_io_exception("missing lon/lat properties",
285 "Hold::initFromProperties");
288 _righthanded = aProp->getBoolValue("right-handed");
289 _isDistance = aProp->getBoolValue("is-distance");
290 _bearing = aProp->getDoubleValue("inbound-radial-deg");
291 _holdTD = aProp->getDoubleValue("td");
294 void Hold::writeToProperties(SGPropertyNode_ptr aProp) const
296 BasicWaypt::writeToProperties(aProp);
298 aProp->setBoolValue("right-handed", _righthanded);
299 aProp->setBoolValue("is-distance", _isDistance);
300 aProp->setDoubleValue("inbound-radial-deg", _bearing);
301 aProp->setDoubleValue("td", _holdTD);
304 /////////////////////////////////////////////////////////////////////////////
306 HeadingToAltitude::HeadingToAltitude(Route* aOwner, const string& aIdent,
312 setFlag(WPT_DYNAMIC);
315 HeadingToAltitude::HeadingToAltitude(Route* aOwner) :
320 void HeadingToAltitude::initFromProperties(SGPropertyNode_ptr aProp)
322 if (!aProp->hasChild("heading-deg")) {
323 throw sg_io_exception("missing heading/alt properties",
324 "HeadingToAltitude::initFromProperties");
327 Waypt::initFromProperties(aProp);
328 _magHeading = aProp->getDoubleValue("heading-deg");
329 _ident = aProp->getStringValue("ident");
332 void HeadingToAltitude::writeToProperties(SGPropertyNode_ptr aProp) const
334 Waypt::writeToProperties(aProp);
335 aProp->setStringValue("ident", _ident);
336 aProp->setDoubleValue("heading-deg", _magHeading);
339 /////////////////////////////////////////////////////////////////////////////
341 DMEIntercept::DMEIntercept(Route* aOwner, const string& aIdent, const SGGeod& aPos,
342 double aCourseDeg, double aDistanceNm) :
346 _magCourse(aCourseDeg),
347 _dmeDistanceNm(aDistanceNm)
349 setFlag(WPT_DYNAMIC);
352 DMEIntercept::DMEIntercept(Route* aOwner) :
357 void DMEIntercept::initFromProperties(SGPropertyNode_ptr aProp)
359 if (!aProp->hasChild("lon") || !aProp->hasChild("lat")) {
360 throw sg_io_exception("missing lon/lat properties",
361 "DMEIntercept::initFromProperties");
364 Waypt::initFromProperties(aProp);
365 _pos = SGGeod::fromDeg(aProp->getDoubleValue("lon"), aProp->getDoubleValue("lat"));
366 _ident = aProp->getStringValue("ident");
367 // check it's a real DME?
368 _magCourse = aProp->getDoubleValue("course-deg");
369 _dmeDistanceNm = aProp->getDoubleValue("dme-distance-nm");
373 void DMEIntercept::writeToProperties(SGPropertyNode_ptr aProp) const
375 Waypt::writeToProperties(aProp);
377 aProp->setStringValue("ident", _ident);
378 aProp->setDoubleValue("lon", _pos.getLongitudeDeg());
379 aProp->setDoubleValue("lat", _pos.getLatitudeDeg());
380 aProp->setDoubleValue("course-deg", _magCourse);
381 aProp->setDoubleValue("dme-distance-nm", _dmeDistanceNm);
384 /////////////////////////////////////////////////////////////////////////////
386 RadialIntercept::RadialIntercept(Route* aOwner, const string& aIdent, const SGGeod& aPos,
387 double aCourseDeg, double aRadial) :
391 _magCourse(aCourseDeg),
394 setFlag(WPT_DYNAMIC);
397 RadialIntercept::RadialIntercept(Route* aOwner) :
402 void RadialIntercept::initFromProperties(SGPropertyNode_ptr aProp)
404 if (!aProp->hasChild("lon") || !aProp->hasChild("lat")) {
405 throw sg_io_exception("missing lon/lat properties",
406 "RadialIntercept::initFromProperties");
409 Waypt::initFromProperties(aProp);
410 _pos = SGGeod::fromDeg(aProp->getDoubleValue("lon"), aProp->getDoubleValue("lat"));
411 _ident = aProp->getStringValue("ident");
412 // check it's a real VOR?
413 _magCourse = aProp->getDoubleValue("course-deg");
414 _radial = aProp->getDoubleValue("radial-deg");
418 void RadialIntercept::writeToProperties(SGPropertyNode_ptr aProp) const
420 Waypt::writeToProperties(aProp);
422 aProp->setStringValue("ident", _ident);
423 aProp->setDoubleValue("lon", _pos.getLongitudeDeg());
424 aProp->setDoubleValue("lat", _pos.getLatitudeDeg());
425 aProp->setDoubleValue("course-deg", _magCourse);
426 aProp->setDoubleValue("radial-deg", _radial);
429 /////////////////////////////////////////////////////////////////////////////
431 ATCVectors::ATCVectors(Route* aOwner, FGAirport* aFacility) :
435 setFlag(WPT_DYNAMIC);
438 ATCVectors::~ATCVectors()
442 ATCVectors::ATCVectors(Route* aOwner) :
447 SGGeod ATCVectors::position() const
449 return _facility->geod();
452 string ATCVectors::ident() const
454 return "VECTORS-" + _facility->ident();
457 void ATCVectors::initFromProperties(SGPropertyNode_ptr aProp)
459 if (!aProp->hasChild("icao")) {
460 throw sg_io_exception("missing icao propertie",
461 "ATCVectors::initFromProperties");
464 Waypt::initFromProperties(aProp);
465 _facility = FGAirport::getByIdent(aProp->getStringValue("icao"));
468 void ATCVectors::writeToProperties(SGPropertyNode_ptr aProp) const
470 Waypt::writeToProperties(aProp);
471 aProp->setStringValue("icao", _facility->ident());