1 // route_mgr.cxx - manage a route (i.e. a collection of waypoints)
3 // Written by Curtis Olson, started January 2004.
7 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 #include <simgear/compiler.h>
36 #include "route_mgr.hxx"
38 #include <boost/algorithm/string/case_conv.hpp>
40 #include <simgear/misc/strutils.hxx>
41 #include <simgear/structure/exception.hxx>
42 #include <simgear/misc/sgstream.hxx>
44 #include <simgear/props/props_io.hxx>
45 #include <simgear/misc/sg_path.hxx>
46 #include <simgear/route/route.hxx>
47 #include <simgear/sg_inlines.h>
49 #include "Main/fg_props.hxx"
50 #include "Navaids/positioned.hxx"
51 #include "Airports/simple.hxx"
52 #include "Airports/runways.hxx"
54 #define RM "/autopilot/route-manager/"
56 FGRouteMgr::FGRouteMgr() :
57 _route( new SGRoute ),
58 input(fgGetNode( RM "input", true )),
59 mirror(fgGetNode( RM "route", true ))
61 listener = new InputListener(this);
62 input->setStringValue("");
63 input->addChangeListener(listener);
67 FGRouteMgr::~FGRouteMgr() {
68 input->removeChangeListener(listener);
75 void FGRouteMgr::init() {
76 SGPropertyNode_ptr rm(fgGetNode(RM));
78 lon = fgGetNode( "/position/longitude-deg", true );
79 lat = fgGetNode( "/position/latitude-deg", true );
80 alt = fgGetNode( "/position/altitude-ft", true );
81 magvar = fgGetNode("/environment/magnetic-variation-deg", true);
83 departure = fgGetNode(RM "departure", true);
84 departure->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this,
85 &FGRouteMgr::getDepartureICAO, &FGRouteMgr::setDepartureICAO));
86 departure->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this,
87 &FGRouteMgr::getDepartureName, NULL));
89 // init departure information from current location
90 SGGeod pos = SGGeod::fromDegFt(lon->getDoubleValue(), lat->getDoubleValue(), alt->getDoubleValue());
91 _departure = FGAirport::findClosest(pos, 20.0);
93 FGRunway* active = _departure->getActiveRunwayForUsage();
94 departure->setStringValue("runway", active->ident().c_str());
96 departure->setStringValue("runway", "");
99 departure->getChild("etd", 0, true);
100 departure->getChild("takeoff-time", 0, true);
102 destination = fgGetNode(RM "destination", true);
103 destination->getChild("airport", 0, true);
105 destination->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this,
106 &FGRouteMgr::getDestinationICAO, &FGRouteMgr::setDestinationICAO));
107 destination->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this,
108 &FGRouteMgr::getDestinationName, NULL));
110 destination->getChild("runway", 0, true);
111 destination->getChild("eta", 0, true);
112 destination->getChild("touchdown-time", 0, true);
114 alternate = fgGetNode(RM "alternate", true);
115 alternate->getChild("airport", 0, true);
116 alternate->getChild("runway", 0, true);
118 cruise = fgGetNode(RM "cruise", true);
119 cruise->getChild("altitude-ft", 0, true);
120 cruise->setDoubleValue("altitude-ft", 10000.0);
121 cruise->getChild("flight-level", 0, true);
122 cruise->getChild("speed-kts", 0, true);
123 cruise->setDoubleValue("speed-kts", 160.0);
125 totalDistance = fgGetNode(RM "total-distance", true);
126 totalDistance->setDoubleValue(0.0);
128 ete = fgGetNode(RM "ete", true);
129 ete->setDoubleValue(0.0);
131 elapsedFlightTime = fgGetNode(RM "flight-time", true);
132 elapsedFlightTime->setDoubleValue(0.0);
134 active = fgGetNode(RM "active", true);
135 active->setBoolValue(false);
137 airborne = fgGetNode(RM "airborne", true);
138 airborne->setBoolValue(false);
140 _edited = fgGetNode(RM "signals/edited", true);
141 _finished = fgGetNode(RM "signals/finished", true);
143 _currentWpt = fgGetNode(RM "current-wp", true);
144 _currentWpt->tie(SGRawValueMethods<FGRouteMgr, int>
145 (*this, &FGRouteMgr::currentWaypoint, &FGRouteMgr::jumpToIndex));
147 // temporary distance / eta calculations, for backward-compatability
148 wp0 = fgGetNode(RM "wp", 0, true);
149 wp0->getChild("id", 0, true);
150 wp0->getChild("dist", 0, true);
151 wp0->getChild("eta", 0, true);
152 wp0->getChild("bearing-deg", 0, true);
154 wp1 = fgGetNode(RM "wp", 1, true);
155 wp1->getChild("id", 0, true);
156 wp1->getChild("dist", 0, true);
157 wp1->getChild("eta", 0, true);
159 wpn = fgGetNode(RM "wp-last", 0, true);
160 wpn->getChild("dist", 0, true);
161 wpn->getChild("eta", 0, true);
164 _route->set_current(0);
167 _pathNode = fgGetNode(RM "file-path", 0, true);
171 void FGRouteMgr::postinit() {
172 string_list *waypoints = globals->get_initial_waypoints();
174 vector<string>::iterator it;
175 for (it = waypoints->begin(); it != waypoints->end(); ++it)
179 weightOnWheels = fgGetNode("/gear/gear[0]/wow", false);
180 // check airbone flag agrees with presets
185 void FGRouteMgr::bind() { }
186 void FGRouteMgr::unbind() { }
188 bool FGRouteMgr::isRouteActive() const
190 return active->getBoolValue();
193 void FGRouteMgr::update( double dt ) {
195 // paused, nothing to do here
199 if (!active->getBoolValue()) {
203 double groundSpeed = fgGetDouble("/velocities/groundspeed-kt", 0.0);
204 if (airborne->getBoolValue()) {
205 time_t now = time(NULL);
206 elapsedFlightTime->setDoubleValue(difftime(now, _takeoffTime));
207 } else { // not airborne
208 if (weightOnWheels->getBoolValue() || (groundSpeed < 40)) {
212 airborne->setBoolValue(true);
213 _takeoffTime = time(NULL); // start the clock
214 departure->setIntValue("takeoff-time", _takeoffTime);
217 // basic course/distance information
218 double wp_course, wp_distance;
219 SGWayPoint wp = _route->get_current();
220 wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
221 alt->getDoubleValue(), &wp_course, &wp_distance );
223 // update wp0 / wp1 / wp-last for legacy users
224 wp0->setDoubleValue("dist", wp_distance * SG_METER_TO_NM);
225 wp_course -= magvar->getDoubleValue(); // expose magnetic bearing
226 wp0->setDoubleValue("bearing-deg", wp_course);
227 setETAPropertyFromDistance(wp0->getChild("eta"), wp_distance);
229 if ((_route->current_index() + 1) < _route->size()) {
230 wp = _route->get_waypoint(_route->current_index() + 1);
231 double wp1_course, wp1_distance;
232 wp.CourseAndDistance(lon->getDoubleValue(), lat->getDoubleValue(),
233 alt->getDoubleValue(), &wp1_course, &wp1_distance);
235 wp1->setDoubleValue("dist", wp1_distance * SG_METER_TO_NM);
236 setETAPropertyFromDistance(wp1->getChild("eta"), wp1_distance);
239 double totalDistanceRemaining = wp_distance; // distance to current waypoint
240 for (int i=_route->current_index() + 1; i<_route->size(); ++i) {
241 totalDistanceRemaining += _route->get_waypoint(i).get_distance();
244 wpn->setDoubleValue("dist", totalDistanceRemaining * SG_METER_TO_NM);
245 ete->setDoubleValue(totalDistanceRemaining * SG_METER_TO_NM / groundSpeed * 3600.0);
246 setETAPropertyFromDistance(wpn->getChild("eta"), totalDistanceRemaining);
248 // get time now at destination tz as tm struct
250 // convert to string ... and stash in property
251 //destination->setDoubleValue("eta", eta);
255 void FGRouteMgr::setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance) {
256 double speed =fgGetDouble("/velocities/groundspeed-kt", 0.0);
258 aProp->setStringValue("--:--");
263 double eta = aDistance * SG_METER_TO_NM / speed;
264 if ( eta >= 100.0 ) {
265 eta = 99.999; // clamp
268 if ( eta < (1.0/6.0) ) {
269 eta *= 60.0; // within 10 minutes, bump up to min/secs
272 int major = (int)eta,
273 minor = (int)((eta - (int)eta) * 60.0);
274 snprintf( eta_str, 64, "%d:%02d", major, minor );
275 aProp->setStringValue( eta_str );
278 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n )
280 _route->add_waypoint( wp, n );
282 if ((n >= 0) && (_route->current_index() > n)) {
283 _route->set_current(_route->current_index() + 1);
289 void FGRouteMgr::waypointsChanged()
291 double routeDistanceNm = _route->total_distance() * SG_METER_TO_NM;
292 totalDistance->setDoubleValue(routeDistanceNm);
293 double cruiseSpeedKts = cruise->getDoubleValue("speed", 0.0);
294 if (cruiseSpeedKts > 1.0) {
295 // very very crude approximation, doesn't allow for climb / descent
296 // performance or anything else at all
297 ete->setDoubleValue(routeDistanceNm / cruiseSpeedKts * (60.0 * 60.0));
301 _edited->fireValueChanged();
305 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
306 if ( _route->size() <= 0 ) {
311 n = _route->size() - 1;
314 if (_route->current_index() > n) {
315 _route->set_current(_route->current_index() - 1);
318 SGWayPoint wp = _route->get_waypoint(n);
319 _route->delete_waypoint(n);
326 bool FGRouteMgr::build() {
331 void FGRouteMgr::new_waypoint( const string& target, int n ) {
332 SGWayPoint* wp = make_waypoint( target );
337 add_waypoint( *wp, n );
342 SGWayPoint* FGRouteMgr::make_waypoint(const string& tgt ) {
343 string target(boost::to_upper_copy(tgt));
346 double alt = -9999.0;
348 size_t pos = target.find( '@' );
349 if ( pos != string::npos ) {
350 alt = atof( target.c_str() + pos + 1 );
351 target = target.substr( 0, pos );
352 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
353 alt *= SG_FEET_TO_METER;
357 pos = target.find( ',' );
358 if ( pos != string::npos ) {
359 double lon = atof( target.substr(0, pos).c_str());
360 double lat = atof( target.c_str() + pos + 1);
362 char ew = (lon < 0.0) ? 'W' : 'E';
363 char ns = (lat < 0.0) ? 'S' : 'N';
364 snprintf(buf, 32, "%c%03d%c%03d", ew, (int) fabs(lon), ns, (int)fabs(lat));
365 return new SGWayPoint( lon, lat, alt, SGWayPoint::WGS84, buf);
369 if (_route->size() > 0) {
370 SGWayPoint wp = get_waypoint(_route->size()-1);
371 basePosition = wp.get_target();
373 // route is empty, use current position
374 basePosition = SGGeod::fromDeg(
375 fgGetNode("/position/longitude-deg")->getDoubleValue(),
376 fgGetNode("/position/latitude-deg")->getDoubleValue());
379 vector<string> pieces(simgear::strutils::split(target, "/"));
382 FGPositionedRef p = FGPositioned::findClosestWithIdent(pieces.front(), basePosition);
384 SG_LOG( SG_AUTOPILOT, SG_INFO, "Unable to find FGPositioned with ident:" << pieces.front());
388 SGGeod geod = SGGeod::fromGeodM(p->geod(), alt);
389 if (pieces.size() == 1) {
391 return new SGWayPoint(geod, target, p->name());
394 if (pieces.size() == 3) {
395 // navaid/radial/distance-nm notation
396 double radial = atof(pieces[1].c_str()),
397 distanceNm = atof(pieces[2].c_str()),
399 radial += magvar->getDoubleValue(); // convert to true bearing
401 SGGeodesy::direct(geod, radial, distanceNm * SG_NM_TO_METER, offsetPos, az2);
402 offsetPos.setElevationM(alt);
404 SG_LOG(SG_AUTOPILOT, SG_INFO, "final offset radial is " << radial);
405 return new SGWayPoint(offsetPos, p->ident() + pieces[2], target);
408 if (pieces.size() == 2) {
409 FGAirport* apt = dynamic_cast<FGAirport*>(p.ptr());
411 SG_LOG(SG_AUTOPILOT, SG_INFO, "Waypoint is not an airport:" << pieces.front());
415 if (!apt->hasRunwayWithIdent(pieces[1])) {
416 SG_LOG(SG_AUTOPILOT, SG_INFO, "No runway: " << pieces[1] << " at " << pieces[0]);
420 FGRunway* runway = apt->getRunwayByIdent(pieces[1]);
421 SGGeod t = runway->threshold();
422 return new SGWayPoint(t.getLongitudeDeg(), t.getLatitudeDeg(), alt, SGWayPoint::WGS84, pieces[1]);
425 SG_LOG(SG_AUTOPILOT, SG_INFO, "Unable to parse waypoint:" << target);
430 // mirror internal route to the property system for inspection by other subsystems
431 void FGRouteMgr::update_mirror() {
432 mirror->removeChildren("wp");
433 for (int i = 0; i < _route->size(); i++) {
434 SGWayPoint wp = _route->get_waypoint(i);
435 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
437 const SGGeod& pos(wp.get_target());
438 prop->setStringValue("id", wp.get_id().c_str());
439 prop->setStringValue("name", wp.get_name().c_str());
440 prop->setDoubleValue("longitude-deg", pos.getLongitudeDeg());
441 prop->setDoubleValue("latitude-deg",pos.getLatitudeDeg());
442 prop->setDoubleValue("altitude-m", pos.getElevationM());
443 prop->setDoubleValue("altitude-ft", pos.getElevationFt());
445 // set number as listener attachment point
446 mirror->setIntValue("num", _route->size());
449 // command interface /autopilot/route-manager/input:
451 // @CLEAR ... clear route
452 // @POP ... remove first entry
453 // @DELETE3 ... delete 4th entry
454 // @INSERT2:KSFO@900 ... insert "KSFO@900" as 3rd entry
455 // KSFO@900 ... append "KSFO@900"
457 void FGRouteMgr::InputListener::valueChanged(SGPropertyNode *prop)
459 const char *s = prop->getStringValue();
460 if (strlen(s) == 0) {
464 if (!strcmp(s, "@CLEAR"))
466 else if (!strcmp(s, "@ACTIVATE"))
468 else if (!strcmp(s, "@LOAD")) {
470 } else if (!strcmp(s, "@SAVE")) {
472 } else if (!strcmp(s, "@POP")) {
473 SG_LOG(SG_AUTOPILOT, SG_WARN, "route-manager @POP command is deprecated");
474 } else if (!strcmp(s, "@NEXT")) {
475 mgr->jumpToIndex(mgr->currentWaypoint() + 1);
476 } else if (!strcmp(s, "@PREVIOUS")) {
477 mgr->jumpToIndex(mgr->currentWaypoint() - 1);
478 } else if (!strncmp(s, "@JUMP", 5)) {
479 mgr->jumpToIndex(atoi(s + 5));
480 } else if (!strncmp(s, "@DELETE", 7))
481 mgr->pop_waypoint(atoi(s + 7));
482 else if (!strncmp(s, "@INSERT", 7)) {
484 int pos = strtol(s + 7, &r, 10);
490 mgr->new_waypoint(r, pos);
492 mgr->new_waypoint(s);
495 // SGWayPoint( const double lon = 0.0, const double lat = 0.0,
496 // const double alt = 0.0, const modetype m = WGS84,
497 // const string& s = "", const string& n = "" );
499 bool FGRouteMgr::activate()
501 if (isRouteActive()) {
502 SG_LOG(SG_AUTOPILOT, SG_WARN, "duplicate route-activation, no-op");
506 // only add departure waypoint if we're not airborne, so that
507 // in-air route activation doesn't confuse matters.
508 if (weightOnWheels->getBoolValue() && _departure) {
509 string runwayId(departure->getStringValue("runway"));
510 FGRunway* runway = NULL;
511 if (_departure->hasRunwayWithIdent(runwayId)) {
512 runway = _departure->getRunwayByIdent(runwayId);
514 SG_LOG(SG_AUTOPILOT, SG_INFO,
515 "route-manager, departure runway not found:" << runwayId);
516 runway = _departure->getActiveRunwayForUsage();
519 SGWayPoint swp(runway->threshold(),
520 _departure->ident() + "-" + runway->ident(), runway->name());
521 add_waypoint(swp, 0);
525 string runwayId = (destination->getStringValue("runway"));
526 if (_destination->hasRunwayWithIdent(runwayId)) {
527 FGRunway* runway = _destination->getRunwayByIdent(runwayId);
528 SGWayPoint swp(runway->end(),
529 _destination->ident() + "-" + runway->ident(), runway->name());
532 // quite likely, since destination runway may not be known until enroute
533 // probably want a listener on the 'destination' node to allow an enroute
535 add_waypoint(SGWayPoint(_destination->geod(), _destination->ident(), _destination->name()));
539 _route->set_current(0);
540 active->setBoolValue(true);
541 SG_LOG(SG_AUTOPILOT, SG_INFO, "route-manager, activate route ok");
546 void FGRouteMgr::sequence()
548 if (!active->getBoolValue()) {
549 SG_LOG(SG_AUTOPILOT, SG_ALERT, "trying to sequence waypoints with no active route");
553 if (checkFinished()) {
557 _route->increment_current();
558 currentWaypointChanged();
559 _currentWpt->fireValueChanged();
562 bool FGRouteMgr::checkFinished()
564 int lastWayptIndex = _route->size() - 1;
565 if (_route->current_index() < lastWayptIndex) {
569 SG_LOG(SG_AUTOPILOT, SG_INFO, "reached end of active route");
570 _finished->fireValueChanged();
571 active->setBoolValue(false);
575 void FGRouteMgr::jumpToIndex(int index)
577 if ((index < 0) || (index >= _route->size())) {
578 SG_LOG(SG_AUTOPILOT, SG_ALERT, "passed invalid index (" <<
579 index << ") to FGRouteMgr::jumpToIndex");
583 if (_route->current_index() == index) {
587 _route->set_current(index);
588 currentWaypointChanged();
589 _currentWpt->fireValueChanged();
592 void FGRouteMgr::currentWaypointChanged()
594 SGWayPoint previous = _route->get_previous();
595 SGWayPoint cur = _route->get_current();
597 wp0->getChild("id")->setStringValue(cur.get_id());
598 if ((_route->current_index() + 1) < _route->size()) {
599 wp1->getChild("id")->setStringValue(_route->get_next().get_id());
601 wp1->getChild("id")->setStringValue("");
604 SG_LOG(SG_AUTOPILOT, SG_INFO, "route manager, current-wp is now " << _route->current_index());
607 int FGRouteMgr::findWaypoint(const SGGeod& aPos) const
609 for (int i=0; i<_route->size(); ++i) {
610 double d = SGGeodesy::distanceM(aPos, _route->get_waypoint(i).get_target());
611 if (d < 200.0) { // 200 metres seems close enough
619 SGWayPoint FGRouteMgr::get_waypoint( int i ) const
621 return _route->get_waypoint(i);
624 int FGRouteMgr::size() const
626 return _route->size();
629 int FGRouteMgr::currentWaypoint() const
631 return _route->current_index();
634 void FGRouteMgr::setWaypointTargetAltitudeFt(unsigned int index, int altFt)
636 SGWayPoint wp = _route->get_waypoint(index);
637 wp.setTargetAltFt(altFt);
638 // simplest way to update a waypoint is to remove and re-add it
639 _route->delete_waypoint(index);
640 _route->add_waypoint(wp, index);
644 void FGRouteMgr::saveRoute()
646 SGPath path(_pathNode->getStringValue());
647 SG_LOG(SG_IO, SG_INFO, "Saving route to " << path.str());
649 SGPropertyNode_ptr d(new SGPropertyNode);
650 SGPath path(_pathNode->getStringValue());
651 d->setIntValue("version", 1);
654 d->setStringValue("departure/airport", _departure->ident());
655 d->setStringValue("departure/sid", departure->getStringValue("sid"));
656 d->setStringValue("departure/runway", departure->getStringValue("runway"));
660 d->setStringValue("destination/airport", _destination->ident());
661 d->setStringValue("destination/star", destination->getStringValue("star"));
662 d->setStringValue("destination/transition", destination->getStringValue("transition"));
663 d->setStringValue("destination/runway", destination->getStringValue("runway"));
667 SGPropertyNode* routeNode = d->getChild("route", 0, true);
668 for (int i=0; i<_route->size(); ++i) {
669 SGPropertyNode* wpNode = routeNode->getChild("wp",i, true);
670 SGWayPoint wp(_route->get_waypoint(i));
672 wpNode->setStringValue("ident", wp.get_id());
673 wpNode->setStringValue("name", wp.get_name());
674 SGGeod geod(wp.get_target());
676 wpNode->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
677 wpNode->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
679 if (geod.getElevationFt() > -9990.0) {
680 wpNode->setDoubleValue("altitude-ft", geod.getElevationFt());
682 } // of waypoint iteration
684 writeProperties(path.str(), d, true /* write-all */);
685 } catch (const sg_exception &e) {
686 SG_LOG(SG_IO, SG_WARN, "Error saving route:" << e.getMessage());
690 void FGRouteMgr::loadRoute()
692 // deactivate route first
693 active->setBoolValue(false);
695 SGPropertyNode_ptr routeData(new SGPropertyNode);
696 SGPath path(_pathNode->getStringValue());
698 SG_LOG(SG_IO, SG_INFO, "going to read flight-plan from:" << path.str());
701 readProperties(path.str(), routeData);
702 } catch (sg_exception& e) {
703 // if XML parsing fails, the file might be simple textual list of waypoints
704 loadPlainTextRoute(path);
710 SGPropertyNode* dep = routeData->getChild("departure");
712 throw sg_io_exception("malformed route file, no departure node");
715 string depIdent = dep->getStringValue("airport");
716 _departure = (FGAirport*) fgFindAirportID(depIdent);
720 SGPropertyNode* dst = routeData->getChild("destination");
722 throw sg_io_exception("malformed route file, no destination node");
725 _destination = (FGAirport*) fgFindAirportID(dst->getStringValue("airport"));
726 destination->setStringValue("runway", dst->getStringValue("runway"));
729 SGPropertyNode* alt = routeData->getChild("alternate");
731 alternate->setStringValue(alt->getStringValue("airport"));
732 } // of cruise data loading
735 SGPropertyNode* crs = routeData->getChild("cruise");
737 cruise->setDoubleValue("speed-kts", crs->getDoubleValue("speed-kts"));
738 cruise->setDoubleValue("mach", crs->getDoubleValue("mach"));
739 cruise->setDoubleValue("altitude-ft", crs->getDoubleValue("altitude-ft"));
740 } // of cruise data loading
744 SGPropertyNode_ptr _route = routeData->getChild("route", 0);
745 SGGeod lastPos = (_departure ? _departure->geod() : SGGeod());
747 for (int i=0; i<_route->nChildren(); ++i) {
748 SGPropertyNode_ptr wp = _route->getChild("wp", i);
749 parseRouteWaypoint(wp);
750 } // of route iteration
751 } catch (sg_exception& e) {
752 SG_LOG(SG_IO, SG_WARN, "failed to load flight-plan (from '" << e.getOrigin()
753 << "'):" << e.getMessage());
757 void FGRouteMgr::parseRouteWaypoint(SGPropertyNode* aWP)
760 if (_route->size() > 0) {
761 lastPos = get_waypoint(_route->size()-1).get_target();
763 // route is empty, use departure airport position
764 const FGAirport* apt = fgFindAirportID(departure->getStringValue("airport"));
765 assert(apt); // shouldn't have got this far with an invalid airport
766 lastPos = apt->geod();
769 SGPropertyNode_ptr altProp = aWP->getChild("altitude-ft");
770 double altM = -9999.0;
772 altM = altProp->getDoubleValue() * SG_FEET_TO_METER;
775 string ident(aWP->getStringValue("ident"));
776 if (aWP->hasChild("longitude-deg")) {
777 // explicit longitude/latitude
778 SGWayPoint swp(aWP->getDoubleValue("longitude-deg"),
779 aWP->getDoubleValue("latitude-deg"), altM,
780 SGWayPoint::WGS84, ident, aWP->getStringValue("name"));
782 } else if (aWP->hasChild("navid")) {
783 // lookup by navid (possibly with offset)
784 string nid(aWP->getStringValue("navid"));
785 FGPositionedRef p = FGPositioned::findClosestWithIdent(nid, lastPos);
787 throw sg_io_exception("bad route file, unknown navid:" + nid);
790 SGGeod pos(p->geod());
791 if (aWP->hasChild("offset-nm") && aWP->hasChild("offset-radial")) {
792 double radialDeg = aWP->getDoubleValue("offset-radial");
793 // convert magnetic radial to a true radial!
794 radialDeg += magvar->getDoubleValue();
795 double offsetNm = aWP->getDoubleValue("offset-nm");
797 SGGeodesy::direct(p->geod(), radialDeg, offsetNm * SG_NM_TO_METER, pos, az2);
800 SGWayPoint swp(pos.getLongitudeDeg(), pos.getLatitudeDeg(), altM,
801 SGWayPoint::WGS84, ident, "");
804 // lookup by ident (symbolic waypoint)
805 FGPositionedRef p = FGPositioned::findClosestWithIdent(ident, lastPos);
807 throw sg_io_exception("bad route file, unknown waypoint:" + ident);
810 SGWayPoint swp(p->longitude(), p->latitude(), altM,
811 SGWayPoint::WGS84, p->ident(), p->name());
816 void FGRouteMgr::loadPlainTextRoute(const SGPath& path)
818 sg_gzifstream in(path.str().c_str());
826 getline(in, line, '\n');
827 // trim CR from end of line, if found
828 if (line[line.size() - 1] == '\r') {
829 line.erase(line.size() - 1, 1);
832 new_waypoint(line, -1);
833 } // of line iteration
836 const char* FGRouteMgr::getDepartureICAO() const
842 return _departure->ident().c_str();
845 const char* FGRouteMgr::getDepartureName() const
851 return _departure->name().c_str();
854 void FGRouteMgr::setDepartureICAO(const char* aIdent)
856 if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
859 _departure = FGAirport::findByIdent(aIdent);
863 const char* FGRouteMgr::getDestinationICAO() const
869 return _destination->ident().c_str();
872 const char* FGRouteMgr::getDestinationName() const
878 return _destination->name().c_str();
881 void FGRouteMgr::setDestinationICAO(const char* aIdent)
883 if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
886 _destination = FGAirport::findByIdent(aIdent);