| 1 | // route.cxx - classes supporting waypoints and route structures |
| 2 | |
| 3 | // Written by James Turner, started 2009. |
| 4 | // |
| 5 | // Copyright (C) 2009 Curtis L. Olson |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include "route.hxx" |
| 24 | |
| 25 | // std |
| 26 | #include <map> |
| 27 | #include <fstream> |
| 28 | |
| 29 | |
| 30 | // SimGear |
| 31 | #include <simgear/structure/exception.hxx> |
| 32 | #include <simgear/misc/sg_path.hxx> |
| 33 | #include <simgear/magvar/magvar.hxx> |
| 34 | #include <simgear/timing/sg_time.hxx> |
| 35 | #include <simgear/io/iostreams/sgstream.hxx> |
| 36 | #include <simgear/misc/strutils.hxx> |
| 37 | #include <simgear/props/props_io.hxx> |
| 38 | |
| 39 | // FlightGear |
| 40 | #include <Main/globals.hxx> |
| 41 | #include "Main/fg_props.hxx" |
| 42 | #include <Navaids/procedure.hxx> |
| 43 | #include <Navaids/waypoint.hxx> |
| 44 | #include <Navaids/LevelDXML.hxx> |
| 45 | #include <Airports/airport.hxx> |
| 46 | #include <Navaids/airways.hxx> |
| 47 | #include <Environment/atmosphere.hxx> // for Mach conversions |
| 48 | |
| 49 | using std::string; |
| 50 | using std::vector; |
| 51 | using std::endl; |
| 52 | using std::fstream; |
| 53 | |
| 54 | |
| 55 | namespace flightgear { |
| 56 | |
| 57 | const double NO_MAG_VAR = -1000.0; // an impossible mag-var value |
| 58 | |
| 59 | bool isMachRestrict(RouteRestriction rr) |
| 60 | { |
| 61 | return (rr == SPEED_RESTRICT_MACH) || (rr == SPEED_COMPUTED_MACH); |
| 62 | } |
| 63 | |
| 64 | namespace { |
| 65 | |
| 66 | double magvarDegAt(const SGGeod& pos) |
| 67 | { |
| 68 | double jd = globals->get_time_params()->getJD(); |
| 69 | return sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES; |
| 70 | } |
| 71 | |
| 72 | WayptRef intersectionFromString(FGPositionedRef p1, |
| 73 | const SGGeod& basePosition, |
| 74 | const double magvar, |
| 75 | const string_list& pieces) |
| 76 | { |
| 77 | assert(pieces.size() == 4); |
| 78 | // navid/radial/navid/radial notation |
| 79 | FGPositionedRef p2 = FGPositioned::findClosestWithIdent(pieces[2], basePosition); |
| 80 | if (!p2) { |
| 81 | SG_LOG(SG_NAVAID, SG_INFO, "Unable to find FGPositioned with ident:" << pieces[2]); |
| 82 | return {}; |
| 83 | } |
| 84 | |
| 85 | double r1 = atof(pieces[1].c_str()), |
| 86 | r2 = atof(pieces[3].c_str()); |
| 87 | r1 += magvar; |
| 88 | r2 += magvar; |
| 89 | |
| 90 | SGGeod intersection; |
| 91 | bool ok = SGGeodesy::radialIntersection(p1->geod(), r1, p2->geod(), r2, intersection); |
| 92 | if (!ok) { |
| 93 | SG_LOG(SG_NAVAID, SG_INFO, "no valid intersection for:" << pieces[0] << "/" << pieces[2]); |
| 94 | return {}; |
| 95 | } |
| 96 | |
| 97 | std::string name = p1->ident() + "-" + p2->ident(); |
| 98 | return new BasicWaypt(intersection, name, nullptr); |
| 99 | } |
| 100 | |
| 101 | WayptRef viaFromString(const SGGeod& basePosition, const std::string& target) |
| 102 | { |
| 103 | assert(target.find("VIA-") == 0); |
| 104 | string_list pieces(simgear::strutils::split(target.substr(4), "/")); |
| 105 | if (pieces.size() != 2) { |
| 106 | SG_LOG(SG_NAVAID, SG_WARN, "Malformed VIA specification string:" << target); |
| 107 | return {}; |
| 108 | } |
| 109 | |
| 110 | // TO navaid is pieces[1] |
| 111 | FGPositionedRef nav = FGPositioned::findClosestWithIdent(pieces[1], basePosition, nullptr); |
| 112 | if (!nav) { |
| 113 | SG_LOG(SG_NAVAID, SG_WARN, "TO navaid:" << pieces[3] << " unknown"); |
| 114 | return {}; |
| 115 | } |
| 116 | |
| 117 | // airway ident is pieces[1] |
| 118 | AirwayRef airway = Airway::findByIdentAndNavaid(pieces[0], nav); |
| 119 | if (!airway) { |
| 120 | SG_LOG(SG_NAVAID, SG_WARN, "Unknown airway:" << pieces[0]); |
| 121 | return {}; |
| 122 | } |
| 123 | |
| 124 | return new Via(nullptr, airway, nav); |
| 125 | } |
| 126 | |
| 127 | static double convertSpeedToKnots(RouteUnits aUnits, double aAltitudeFt, double aValue) |
| 128 | { |
| 129 | switch (aUnits) { |
| 130 | case SPEED_KNOTS: return aValue; |
| 131 | case SPEED_KPH: return aValue * SG_KMH_TO_MPS * SG_MPS_TO_KT; |
| 132 | case SPEED_MACH: return FGAtmo::knotsFromMachAtAltitudeFt(aValue, aAltitudeFt); |
| 133 | |
| 134 | default: |
| 135 | throw sg_format_exception("Can't convert unit to Knots", "convertSpeedToKnots"); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static double convertSpeedFromKnots(RouteUnits aUnits, double aAltitudeFt, double aValue) |
| 140 | { |
| 141 | if (aUnits == DEFAULT_UNITS) { |
| 142 | // TODO : use KPH is simulator is in metric |
| 143 | aUnits = SPEED_KNOTS; |
| 144 | } |
| 145 | |
| 146 | switch (aUnits) { |
| 147 | case SPEED_KNOTS: return aValue; |
| 148 | case SPEED_KPH: return aValue * SG_KT_TO_MPS * SG_MPS_TO_KMH; |
| 149 | case SPEED_MACH: return FGAtmo::machFromKnotsAtAltitudeFt(aValue, aAltitudeFt); |
| 150 | |
| 151 | default: |
| 152 | throw sg_format_exception("Can't convert to unit", "convertSpeedFromKnots"); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } // anonymous namespace |
| 157 | |
| 158 | double convertSpeedUnits(RouteUnits aSrc, RouteUnits aDest, double aAltitudeFt, double aValue) |
| 159 | { |
| 160 | const double valueKnots = convertSpeedToKnots(aSrc, aAltitudeFt, aValue); |
| 161 | return convertSpeedFromKnots(aDest, aAltitudeFt, valueKnots); |
| 162 | } |
| 163 | |
| 164 | double convertAltitudeUnits(RouteUnits aSrc, RouteUnits aDest, double aValue) |
| 165 | { |
| 166 | if (aDest == DEFAULT_UNITS) { |
| 167 | // TODO : use meters if sim is in metric |
| 168 | aDest = ALTITUDE_FEET; |
| 169 | } |
| 170 | |
| 171 | double altFt = 0.0; |
| 172 | switch (aSrc) { |
| 173 | case ALTITUDE_FEET: altFt = aValue; break; |
| 174 | case ALTITUDE_METER: altFt = aValue * SG_METER_TO_FEET; break; |
| 175 | case ALTITUDE_FLIGHTLEVEL: altFt = aValue * 100; break; |
| 176 | default: |
| 177 | throw sg_format_exception("Unsupported source altitude units", "convertAltitudeUnits"); |
| 178 | } |
| 179 | |
| 180 | switch (aDest) { |
| 181 | case ALTITUDE_FEET: return altFt; |
| 182 | case ALTITUDE_METER: return altFt * SG_FEET_TO_METER; |
| 183 | case ALTITUDE_FLIGHTLEVEL: return round(altFt / 100); |
| 184 | default: |
| 185 | throw sg_format_exception("Unsupported destination altitude units", "convertAltitudeUnits"); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | Waypt::Waypt(RouteBase* aOwner) : |
| 190 | _owner(aOwner), |
| 191 | _magVarDeg(NO_MAG_VAR) |
| 192 | { |
| 193 | } |
| 194 | |
| 195 | Waypt::~Waypt() |
| 196 | { |
| 197 | } |
| 198 | |
| 199 | std::string Waypt::ident() const |
| 200 | { |
| 201 | return {}; |
| 202 | } |
| 203 | |
| 204 | bool Waypt::flag(WayptFlag aFlag) const |
| 205 | { |
| 206 | return ((_flags & aFlag) != 0); |
| 207 | } |
| 208 | |
| 209 | void Waypt::setFlag(WayptFlag aFlag, bool aV) |
| 210 | { |
| 211 | if (aFlag == 0) { |
| 212 | throw sg_range_exception("invalid waypoint flag set"); |
| 213 | } |
| 214 | |
| 215 | _flags = (_flags & ~aFlag); |
| 216 | if (aV) _flags |= aFlag; |
| 217 | } |
| 218 | |
| 219 | bool Waypt::matches(Waypt* aOther) const |
| 220 | { |
| 221 | assert(aOther); |
| 222 | if (ident() != aOther->ident()) { // cheap check first |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | return matches(aOther->position()); |
| 227 | } |
| 228 | |
| 229 | bool Waypt::matches(FGPositioned* aPos) const |
| 230 | { |
| 231 | if (!aPos) |
| 232 | return false; |
| 233 | |
| 234 | // if w ehave no source, match on position and ident |
| 235 | if (!source()) { |
| 236 | return (ident() == aPos->ident()) && matches(aPos->geod()); |
| 237 | } |
| 238 | |
| 239 | return (aPos == source()); |
| 240 | } |
| 241 | |
| 242 | bool Waypt::matches(const SGGeod& aPos) const |
| 243 | { |
| 244 | double d = SGGeodesy::distanceM(position(), aPos); |
| 245 | return (d < 100.0); // 100 metres seems plenty |
| 246 | } |
| 247 | |
| 248 | void Waypt::setAltitude(double aAlt, RouteRestriction aRestrict, RouteUnits aUnit) |
| 249 | { |
| 250 | if (aUnit == DEFAULT_UNITS) { |
| 251 | aUnit = ALTITUDE_FEET; |
| 252 | } |
| 253 | |
| 254 | _altitude = aAlt; |
| 255 | _altitudeUnits = aUnit; |
| 256 | _altRestrict = aRestrict; |
| 257 | } |
| 258 | |
| 259 | void Waypt::setConstraintAltitude(double aAlt) |
| 260 | { |
| 261 | _constraintAltitude = aAlt; |
| 262 | } |
| 263 | |
| 264 | void Waypt::setSpeed(double aSpeed, RouteRestriction aRestrict, RouteUnits aUnit) |
| 265 | { |
| 266 | if (aUnit == DEFAULT_UNITS) { |
| 267 | if ((aRestrict == SPEED_RESTRICT_MACH) || (aRestrict == SPEED_RESTRICT_MACH)) { |
| 268 | aUnit = SPEED_MACH; |
| 269 | } else { |
| 270 | aUnit = SPEED_KNOTS; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | _speed = aSpeed; |
| 275 | _speedUnits = aUnit; |
| 276 | _speedRestrict = aRestrict; |
| 277 | } |
| 278 | |
| 279 | double Waypt::speedKts() const |
| 280 | { |
| 281 | return speed(SPEED_KNOTS); |
| 282 | } |
| 283 | |
| 284 | double Waypt::speedMach() const |
| 285 | { |
| 286 | return speed(SPEED_MACH); |
| 287 | } |
| 288 | |
| 289 | double Waypt::altitudeFt() const |
| 290 | { |
| 291 | return altitude(ALTITUDE_FEET); |
| 292 | } |
| 293 | |
| 294 | double Waypt::speed(RouteUnits aUnits) const |
| 295 | { |
| 296 | if (aUnits == _speedUnits) { |
| 297 | return _speed; |
| 298 | } |
| 299 | |
| 300 | return convertSpeedUnits(_speedUnits, aUnits, altitudeFt(), _speed); |
| 301 | } |
| 302 | |
| 303 | double Waypt::altitude(RouteUnits aUnits) const |
| 304 | { |
| 305 | if (aUnits == _altitudeUnits) { |
| 306 | return _altitude; |
| 307 | } |
| 308 | |
| 309 | return convertAltitudeUnits(_altitudeUnits, aUnits, _altitude); |
| 310 | } |
| 311 | |
| 312 | double Waypt::constraintAltitude(RouteUnits aUnits) const |
| 313 | { |
| 314 | if (!_constraintAltitude.has_value()) |
| 315 | return 0.0; |
| 316 | |
| 317 | if (aUnits == _altitudeUnits) { |
| 318 | return _constraintAltitude.value_or(0.0); |
| 319 | } |
| 320 | |
| 321 | return convertAltitudeUnits(_altitudeUnits, aUnits, _constraintAltitude.value_or(0.0)); |
| 322 | } |
| 323 | |
| 324 | double Waypt::magvarDeg() const |
| 325 | { |
| 326 | if (_magVarDeg == NO_MAG_VAR) { |
| 327 | // derived classes with a default pos must override this method |
| 328 | assert(!(position() == SGGeod())); |
| 329 | |
| 330 | double jd = globals->get_time_params()->getJD(); |
| 331 | _magVarDeg = sgGetMagVar(position(), jd) * SG_RADIANS_TO_DEGREES; |
| 332 | } |
| 333 | |
| 334 | return _magVarDeg; |
| 335 | } |
| 336 | |
| 337 | double Waypt::headingRadialDeg() const |
| 338 | { |
| 339 | return 0.0; |
| 340 | } |
| 341 | |
| 342 | std::string Waypt::icaoDescription() const |
| 343 | { |
| 344 | return ident(); |
| 345 | } |
| 346 | |
| 347 | /////////////////////////////////////////////////////////////////////////// |
| 348 | // persistence |
| 349 | |
| 350 | RouteRestriction restrictionFromString(const std::string& aStr) |
| 351 | { |
| 352 | std::string l = simgear::strutils::lowercase(aStr); |
| 353 | |
| 354 | if (l == "at") return RESTRICT_AT; |
| 355 | if (l == "above") return RESTRICT_ABOVE; |
| 356 | if (l == "below") return RESTRICT_BELOW; |
| 357 | if (l == "between") return RESTRICT_BETWEEN; |
| 358 | if (l == "none") return RESTRICT_NONE; |
| 359 | if (l == "mach") return SPEED_RESTRICT_MACH; |
| 360 | |
| 361 | if (l.empty()) return RESTRICT_NONE; |
| 362 | throw sg_io_exception("unknown restriction specification:" + l, |
| 363 | "Route restrictFromString"); |
| 364 | } |
| 365 | |
| 366 | const char* restrictionToString(RouteRestriction aRestrict) |
| 367 | { |
| 368 | switch (aRestrict) { |
| 369 | case RESTRICT_AT: return "at"; |
| 370 | case RESTRICT_BELOW: return "below"; |
| 371 | case RESTRICT_ABOVE: return "above"; |
| 372 | case RESTRICT_NONE: return "none"; |
| 373 | case RESTRICT_BETWEEN: return "between"; |
| 374 | case SPEED_RESTRICT_MACH: return "mach"; |
| 375 | |
| 376 | default: |
| 377 | throw sg_exception("invalid route restriction", |
| 378 | "Route restrictToString"); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | WayptRef Waypt::createInstance(RouteBase* aOwner, const std::string& aTypeName) |
| 383 | { |
| 384 | WayptRef r; |
| 385 | if (aTypeName == "basic") { |
| 386 | r = new BasicWaypt(aOwner); |
| 387 | } else if (aTypeName == "navaid") { |
| 388 | r = new NavaidWaypoint(aOwner); |
| 389 | } else if (aTypeName == "offset-navaid") { |
| 390 | r = new OffsetNavaidWaypoint(aOwner); |
| 391 | } else if (aTypeName == "hold") { |
| 392 | r = new Hold(aOwner); |
| 393 | } else if (aTypeName == "runway") { |
| 394 | r = new RunwayWaypt(aOwner); |
| 395 | } else if (aTypeName == "hdgToAlt") { |
| 396 | r = new HeadingToAltitude(aOwner); |
| 397 | } else if (aTypeName == "dmeIntercept") { |
| 398 | r = new DMEIntercept(aOwner); |
| 399 | } else if (aTypeName == "radialIntercept") { |
| 400 | r = new RadialIntercept(aOwner); |
| 401 | } else if (aTypeName == "vectors") { |
| 402 | r = new ATCVectors(aOwner); |
| 403 | } else if (aTypeName == "discontinuity") { |
| 404 | r = new Discontinuity(aOwner); |
| 405 | } else if (aTypeName == "via") { |
| 406 | r = new Via(aOwner); |
| 407 | } |
| 408 | |
| 409 | if (!r || (r->type() != aTypeName)) { |
| 410 | throw sg_exception("broken factory method for type:" + aTypeName, |
| 411 | "Waypt::createInstance"); |
| 412 | } |
| 413 | |
| 414 | return r; |
| 415 | } |
| 416 | |
| 417 | WayptRef Waypt::createFromProperties(RouteBase* aOwner, SGPropertyNode_ptr aProp) |
| 418 | { |
| 419 | if (!aProp->hasChild("type")) { |
| 420 | SG_LOG(SG_GENERAL, SG_WARN, "Bad waypoint node: missing type"); |
| 421 | return {}; |
| 422 | } |
| 423 | |
| 424 | flightgear::AirwayRef via; |
| 425 | if (aProp->hasChild("airway")) { |
| 426 | auto level = Airway::Both; |
| 427 | if (aProp->hasValue("network")) { |
| 428 | level = static_cast<flightgear::Airway::Level>(aProp->getIntValue("network")); |
| 429 | } |
| 430 | |
| 431 | via = flightgear::Airway::findByIdent(aProp->getStringValue("airway"), level); |
| 432 | if (via) { |
| 433 | // override owner if we are from an airway |
| 434 | aOwner = via.get(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | WayptRef nd(createInstance(aOwner, aProp->getStringValue("type"))); |
| 439 | if (nd->initFromProperties(aProp)) { |
| 440 | return nd; |
| 441 | } |
| 442 | SG_LOG(SG_GENERAL, SG_WARN, "failed to create waypoint, trying basic"); |
| 443 | |
| 444 | |
| 445 | // if we failed to make the waypoint, try again making a basic waypoint. |
| 446 | // this handles the case where a navaid waypoint is missing, for example |
| 447 | // we also reject navaids that don't look correct (too far form the specified |
| 448 | // lat-lon, eg see https://sourceforge.net/p/flightgear/codetickets/1814/ ) |
| 449 | // and again fallback to here. |
| 450 | WayptRef bw(new BasicWaypt(aOwner)); |
| 451 | if (bw->initFromProperties(aProp)) { |
| 452 | return bw; |
| 453 | } |
| 454 | |
| 455 | return {}; // total failure |
| 456 | } |
| 457 | |
| 458 | WayptRef Waypt::fromLatLonString(RouteBase* aOwner, const std::string& target) |
| 459 | { |
| 460 | // permit lat,lon/radial/offset format |
| 461 | string_list pieces(simgear::strutils::split(target, "/")); |
| 462 | if ((pieces.size() != 1) && (pieces.size() != 3)) { |
| 463 | return {}; |
| 464 | } |
| 465 | |
| 466 | SGGeod g; |
| 467 | const bool defaultToLonLat = true; // parseStringAsGeod would otherwise default to lat,lon |
| 468 | if (!simgear::strutils::parseStringAsGeod(pieces[0], &g, defaultToLonLat)) { |
| 469 | return {}; |
| 470 | } |
| 471 | |
| 472 | if (pieces.size() == 3) { |
| 473 | // process offset |
| 474 | const double bearing = std::stod(pieces[1]); |
| 475 | const double distanceNm = std::stod(pieces[2]); |
| 476 | g = SGGeodesy::direct(g, bearing, distanceNm * SG_NM_TO_METER); |
| 477 | } |
| 478 | |
| 479 | // build a short name |
| 480 | const int lonDeg = static_cast<int>(g.getLongitudeDeg()); |
| 481 | const int latDeg = static_cast<int>(g.getLatitudeDeg()); |
| 482 | |
| 483 | char buf[32]; |
| 484 | char ew = (lonDeg < 0) ? 'W' : 'E'; |
| 485 | char ns = (latDeg < 0) ? 'S' : 'N'; |
| 486 | snprintf(buf, 32, "%c%03d%c%03d", ew, std::abs(lonDeg), ns, std::abs(latDeg)); |
| 487 | |
| 488 | return new BasicWaypt(g, buf, aOwner); |
| 489 | } |
| 490 | |
| 491 | WayptRef Waypt::createFromString(RouteBase* aOwner, const std::string& s, const SGGeod& aVicinity) |
| 492 | { |
| 493 | auto vicinity = aVicinity; |
| 494 | if (!vicinity.isValid()) { |
| 495 | vicinity = globals->get_aircraft_position(); |
| 496 | } |
| 497 | |
| 498 | auto target = simgear::strutils::uppercase(s); |
| 499 | // extract altitude |
| 500 | double alt = 0.0; |
| 501 | RouteRestriction altSetting = RESTRICT_NONE; |
| 502 | RouteUnits altitudeUnits = ALTITUDE_FEET; |
| 503 | |
| 504 | size_t pos = target.find('@'); |
| 505 | if (pos != string::npos) { |
| 506 | auto altStr = simgear::strutils::uppercase(target.substr(pos + 1)); |
| 507 | if (simgear::strutils::starts_with(altStr, "FL")) { |
| 508 | altitudeUnits = ALTITUDE_FLIGHTLEVEL; |
| 509 | altStr = altStr.substr(2); // trim leading 'FL' |
| 510 | } else if (fgGetString("/sim/startup/units") == "meter") { |
| 511 | altitudeUnits = ALTITUDE_METER; |
| 512 | } |
| 513 | |
| 514 | alt = std::stof(altStr); |
| 515 | target = target.substr(0, pos); |
| 516 | altSetting = RESTRICT_AT; |
| 517 | } |
| 518 | |
| 519 | // check for lon,lat |
| 520 | WayptRef wpt = fromLatLonString(aOwner, target); |
| 521 | |
| 522 | const double magvar = magvarDegAt(vicinity); |
| 523 | |
| 524 | if (wpt) { |
| 525 | // already handled in the lat/lon test above |
| 526 | } else if (target.find("VIA-") == 0) { |
| 527 | wpt = viaFromString(vicinity, target); |
| 528 | } else { |
| 529 | FGPositioned::TypeFilter filter({FGPositioned::Type::AIRPORT, FGPositioned::Type::HELIPORT, |
| 530 | FGPositioned::Type::SEAPORT, FGPositioned::Type::NDB, FGPositioned::Type::VOR, |
| 531 | FGPositioned::Type::FIX, FGPositioned::Type::WAYPOINT}); |
| 532 | string_list pieces(simgear::strutils::split(target, "/")); |
| 533 | FGPositionedRef p = FGPositioned::findClosestWithIdent(pieces.front(), vicinity, &filter); |
| 534 | if (!p) { |
| 535 | SG_LOG(SG_NAVAID, SG_INFO, "Unable to find FGPositioned with ident:" << pieces.front()); |
| 536 | return {}; |
| 537 | } |
| 538 | |
| 539 | if (pieces.size() == 1) { |
| 540 | wpt = new NavaidWaypoint(p, aOwner); |
| 541 | } else if (pieces.size() == 3) { |
| 542 | // navaid/radial/distance-nm notation |
| 543 | double radial = atof(pieces[1].c_str()), |
| 544 | distanceNm = atof(pieces[2].c_str()); |
| 545 | radial += magvar; |
| 546 | wpt = new OffsetNavaidWaypoint(p, aOwner, radial, distanceNm); |
| 547 | } else if (pieces.size() == 2) { |
| 548 | FGAirport* apt = dynamic_cast<FGAirport*>(p.ptr()); |
| 549 | if (!apt) { |
| 550 | SG_LOG(SG_NAVAID, SG_INFO, "Waypoint is not an airport:" << pieces.front()); |
| 551 | return {}; |
| 552 | } |
| 553 | |
| 554 | if (!apt->hasRunwayWithIdent(pieces[1])) { |
| 555 | SG_LOG(SG_NAVAID, SG_INFO, "No runway: " << pieces[1] << " at " << pieces[0]); |
| 556 | return {}; |
| 557 | } |
| 558 | |
| 559 | FGRunway* runway = apt->getRunwayByIdent(pieces[1]); |
| 560 | wpt = new NavaidWaypoint(runway, aOwner); |
| 561 | } else if (pieces.size() == 4) { |
| 562 | wpt = intersectionFromString(p, vicinity, magvar, pieces); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | if (!wpt) { |
| 567 | SG_LOG(SG_NAVAID, SG_INFO, "Unable to parse waypoint:" << target); |
| 568 | return {}; |
| 569 | } |
| 570 | |
| 571 | if (altSetting != RESTRICT_NONE) { |
| 572 | wpt->setAltitude(alt, altSetting, altitudeUnits); |
| 573 | } |
| 574 | return wpt; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | void Waypt::saveAsNode(SGPropertyNode* n) const |
| 579 | { |
| 580 | n->setStringValue("type", type()); |
| 581 | writeToProperties(n); |
| 582 | } |
| 583 | |
| 584 | bool Waypt::initFromProperties(SGPropertyNode_ptr aProp) |
| 585 | { |
| 586 | if (aProp->hasChild("generated")) { |
| 587 | setFlag(WPT_GENERATED, aProp->getBoolValue("generated")); |
| 588 | } |
| 589 | |
| 590 | if (aProp->hasChild("overflight")) { |
| 591 | setFlag(WPT_OVERFLIGHT, aProp->getBoolValue("overflight")); |
| 592 | } |
| 593 | |
| 594 | if (aProp->hasChild("arrival")) { |
| 595 | setFlag(WPT_ARRIVAL, aProp->getBoolValue("arrival")); |
| 596 | } |
| 597 | |
| 598 | if (aProp->hasChild("approach")) { |
| 599 | setFlag(WPT_APPROACH, aProp->getBoolValue("approach")); |
| 600 | } |
| 601 | |
| 602 | if (aProp->hasChild("departure")) { |
| 603 | setFlag(WPT_DEPARTURE, aProp->getBoolValue("departure")); |
| 604 | } |
| 605 | |
| 606 | if (aProp->hasChild("miss")) { |
| 607 | setFlag(WPT_MISS, aProp->getBoolValue("miss")); |
| 608 | } |
| 609 | |
| 610 | if (aProp->hasChild("airway")) { |
| 611 | setFlag(WPT_VIA, true); |
| 612 | } |
| 613 | |
| 614 | if (aProp->hasChild("alt-restrict")) { |
| 615 | _altRestrict = restrictionFromString(aProp->getStringValue("alt-restrict")); |
| 616 | if (aProp->hasChild("altitude-ft")) { |
| 617 | _altitude = aProp->getDoubleValue("altitude-ft"); |
| 618 | _altitudeUnits = ALTITUDE_FEET; |
| 619 | } else if (aProp->hasChild("altitude-m")) { |
| 620 | _altitude = aProp->getDoubleValue("altitude-m"); |
| 621 | _altitudeUnits = ALTITUDE_METER; |
| 622 | } else if (aProp->hasChild("flight-level")) { |
| 623 | _altitude = aProp->getIntValue("flight-level"); |
| 624 | _altitudeUnits = ALTITUDE_FLIGHTLEVEL; |
| 625 | } |
| 626 | |
| 627 | if (aProp->hasChild("constraint-altitude")) { |
| 628 | _constraintAltitude = aProp->getDoubleValue("constraint-altitude"); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | if (aProp->hasChild("speed-restrict")) { |
| 633 | _speedRestrict = restrictionFromString(aProp->getStringValue("speed-restrict")); |
| 634 | RouteUnits units = SPEED_KNOTS; |
| 635 | if (_speedRestrict == SPEED_RESTRICT_MACH) { |
| 636 | units = SPEED_MACH; |
| 637 | } |
| 638 | |
| 639 | if (aProp->hasChild("speed-mach")) { |
| 640 | units = SPEED_MACH; |
| 641 | _speed = aProp->getDoubleValue("speed-mach"); |
| 642 | } else if (aProp->hasChild("speed-kph")) { |
| 643 | units = SPEED_KPH; |
| 644 | _speed = aProp->getDoubleValue("speed-kph"); |
| 645 | } else { |
| 646 | _speed = aProp->getDoubleValue("speed"); |
| 647 | } |
| 648 | |
| 649 | _speedUnits = units; |
| 650 | } |
| 651 | |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | void Waypt::writeToProperties(SGPropertyNode_ptr aProp) const |
| 656 | { |
| 657 | if (flag(WPT_OVERFLIGHT)) { |
| 658 | aProp->setBoolValue("overflight", true); |
| 659 | } |
| 660 | |
| 661 | if (flag(WPT_DEPARTURE)) { |
| 662 | aProp->setBoolValue("departure", true); |
| 663 | } |
| 664 | |
| 665 | if (flag(WPT_ARRIVAL)) { |
| 666 | aProp->setBoolValue("arrival", true); |
| 667 | } |
| 668 | |
| 669 | if (flag(WPT_APPROACH)) { |
| 670 | aProp->setBoolValue("approach", true); |
| 671 | } |
| 672 | |
| 673 | if (flag(WPT_VIA) && _owner) { |
| 674 | flightgear::AirwayRef awy = (flightgear::Airway*) _owner; |
| 675 | aProp->setStringValue("airway", awy->ident()); |
| 676 | aProp->setIntValue("network", awy->level()); |
| 677 | } |
| 678 | |
| 679 | if (flag(WPT_MISS)) { |
| 680 | aProp->setBoolValue("miss", true); |
| 681 | } |
| 682 | |
| 683 | if (flag(WPT_GENERATED)) { |
| 684 | aProp->setBoolValue("generated", true); |
| 685 | } |
| 686 | |
| 687 | if (_altRestrict != RESTRICT_NONE) { |
| 688 | aProp->setStringValue("alt-restrict", restrictionToString(_altRestrict)); |
| 689 | if (_altitudeUnits == ALTITUDE_METER) { |
| 690 | aProp->setDoubleValue("altitude-m", _altitude); |
| 691 | } else if (_altitudeUnits == ALTITUDE_FLIGHTLEVEL) { |
| 692 | aProp->setDoubleValue("flight-level", _altitude); |
| 693 | |
| 694 | } else { |
| 695 | aProp->setDoubleValue("altitude-ft", _altitude); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | if (_constraintAltitude.has_value()) { |
| 700 | aProp->setDoubleValue("constraint-altitude", _constraintAltitude.value_or(0.0)); |
| 701 | } |
| 702 | |
| 703 | if (_speedRestrict != RESTRICT_NONE) { |
| 704 | aProp->setStringValue("speed-restrict", restrictionToString(_speedRestrict)); |
| 705 | aProp->setDoubleValue("speed", _speed); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | RouteBase::~RouteBase() |
| 710 | { |
| 711 | } |
| 712 | |
| 713 | void RouteBase::dumpRouteToKML(const WayptVec& aRoute, const std::string& aName) |
| 714 | { |
| 715 | SGPath p = SGPath::desktop() / (aName + ".kml"); |
| 716 | sg_ofstream f(p); |
| 717 | if (!f.is_open()) { |
| 718 | SG_LOG(SG_NAVAID, SG_WARN, "unable to open:" << p); |
| 719 | return; |
| 720 | } |
| 721 | |
| 722 | // pre-amble |
| 723 | f << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 724 | "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" |
| 725 | "<Document>\n"; |
| 726 | |
| 727 | dumpRouteToKMLLineString(aName, aRoute, f); |
| 728 | |
| 729 | // post-amble |
| 730 | f << "</Document>\n" |
| 731 | "</kml>" << endl; |
| 732 | f.close(); |
| 733 | } |
| 734 | |
| 735 | void RouteBase::dumpRouteToKMLLineString(const std::string& aIdent, |
| 736 | const WayptVec& aRoute, std::ostream& aStream) |
| 737 | { |
| 738 | // preamble |
| 739 | aStream << "<Placemark>\n"; |
| 740 | aStream << "<name>" << aIdent << "</name>\n"; |
| 741 | aStream << "<LineString>\n"; |
| 742 | aStream << "<tessellate>1</tessellate>\n"; |
| 743 | aStream << "<coordinates>\n"; |
| 744 | |
| 745 | // waypoints |
| 746 | for (unsigned int i=0; i<aRoute.size(); ++i) { |
| 747 | SGGeod pos = aRoute[i]->position(); |
| 748 | aStream << pos.getLongitudeDeg() << "," << pos.getLatitudeDeg() << " " << endl; |
| 749 | } |
| 750 | |
| 751 | // postable |
| 752 | aStream << "</coordinates>\n" |
| 753 | "</LineString>\n" |
| 754 | "</Placemark>\n" << endl; |
| 755 | } |
| 756 | |
| 757 | void RouteBase::loadAirportProcedures(const SGPath& aPath, FGAirport* aApt) |
| 758 | { |
| 759 | assert(aApt); |
| 760 | try { |
| 761 | NavdataVisitor visitor(aApt, aPath); |
| 762 | readXML(aPath, visitor); |
| 763 | } catch (sg_io_exception& ex) { |
| 764 | SG_LOG(SG_NAVAID, SG_WARN, "failure parsing procedures: " << aPath << |
| 765 | "\n\t" << ex.getMessage() << "\n\tat:" << ex.getLocation().asString()); |
| 766 | } catch (sg_exception& ex) { |
| 767 | SG_LOG(SG_NAVAID, SG_WARN, "failure parsing procedures: " << aPath << |
| 768 | "\n\t" << ex.getMessage()); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | } // of namespace flightgear |