]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/route.cxx
Give the FGAirport class a sane filename.
[flightgear.git] / src / Navaids / route.cxx
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 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "route.hxx"
26
27 // std
28 #include <map>
29 #include <fstream>
30
31 // Boost
32 #include <boost/algorithm/string/case_conv.hpp>
33 #include <boost/algorithm/string.hpp>
34 #include <boost/foreach.hpp>
35
36 // SimGear
37 #include <simgear/structure/exception.hxx>
38 #include <simgear/misc/sg_path.hxx>
39 #include <simgear/magvar/magvar.hxx>
40 #include <simgear/timing/sg_time.hxx>
41 #include <simgear/misc/sgstream.hxx>
42 #include <simgear/misc/strutils.hxx>
43 #include <simgear/props/props_io.hxx>
44
45 // FlightGear
46 #include <Main/globals.hxx>
47 #include "Main/fg_props.hxx"
48 #include <Navaids/procedure.hxx>
49 #include <Navaids/waypoint.hxx>
50 #include <Navaids/LevelDXML.hxx>
51 #include <Airports/airport.hxx>
52
53 using std::string;
54 using std::vector;
55 using std::endl;
56 using std::fstream;
57
58 namespace flightgear {
59
60 const double NO_MAG_VAR = -1000.0; // an impossible mag-var value
61
62 bool isMachRestrict(RouteRestriction rr)
63 {
64   return (rr == SPEED_RESTRICT_MACH) || (rr == SPEED_COMPUTED_MACH);
65 }
66   
67 Waypt::Waypt(RouteBase* aOwner) :
68   _altitudeFt(0.0),
69   _speed(0.0),
70   _altRestrict(RESTRICT_NONE),
71   _speedRestrict(RESTRICT_NONE),
72   _owner(aOwner),
73   _flags(0),
74   _magVarDeg(NO_MAG_VAR)
75 {
76 }
77
78 Waypt::~Waypt()
79 {
80 }
81   
82 std::string Waypt::ident() const
83 {
84   return "";
85 }
86   
87 bool Waypt::flag(WayptFlag aFlag) const
88 {
89   return ((_flags & aFlag) != 0);
90 }
91         
92 void Waypt::setFlag(WayptFlag aFlag, bool aV)
93 {
94   _flags = (_flags & ~aFlag);
95   if (aV) _flags |= aFlag;
96 }
97
98 bool Waypt::matches(Waypt* aOther) const
99 {
100   assert(aOther);
101   if (ident() != aOther->ident()) { // cheap check first
102     return false;
103   }
104   
105   return matches(aOther->position());
106 }
107
108
109 bool Waypt::matches(const SGGeod& aPos) const
110 {
111   double d = SGGeodesy::distanceM(position(), aPos);
112   return (d < 100.0); // 100 metres seems plenty
113 }
114
115 void Waypt::setAltitude(double aAlt, RouteRestriction aRestrict)
116 {
117   _altitudeFt = aAlt;
118   _altRestrict = aRestrict;
119 }
120
121 void Waypt::setSpeed(double aSpeed, RouteRestriction aRestrict)
122 {
123   _speed = aSpeed;
124   _speedRestrict = aRestrict;
125 }
126
127 double Waypt::speedKts() const
128 {
129   assert(_speedRestrict != SPEED_RESTRICT_MACH);
130   return speed();
131 }
132   
133 double Waypt::speedMach() const
134 {
135   assert(_speedRestrict == SPEED_RESTRICT_MACH);
136   return speed();
137 }
138   
139 std::pair<double, double>
140 Waypt::courseAndDistanceFrom(const SGGeod& aPos) const
141 {
142   if (flag(WPT_DYNAMIC)) {
143     return std::make_pair(0.0, 0.0);
144   }
145   
146   double course, az2, distance;
147   SGGeodesy::inverse(aPos, position(), course, az2, distance);
148   return std::make_pair(course, distance);
149 }
150
151 double Waypt::magvarDeg() const
152 {
153   if (_magVarDeg == NO_MAG_VAR) {
154     // derived classes with a default pos must override this method
155     assert(!(position() == SGGeod()));
156     
157     double jd = globals->get_time_params()->getJD();
158     _magVarDeg = sgGetMagVar(position(), jd) * SG_RADIANS_TO_DEGREES;
159   }
160   
161   return _magVarDeg;
162 }
163   
164 double Waypt::headingRadialDeg() const
165 {
166   return 0.0;
167 }
168   
169 ///////////////////////////////////////////////////////////////////////////
170 // persistence
171
172 static RouteRestriction restrictionFromString(const char* aStr)
173 {
174   std::string l = boost::to_lower_copy(std::string(aStr));
175
176   if (l == "at") return RESTRICT_AT;
177   if (l == "above") return RESTRICT_ABOVE;
178   if (l == "below") return RESTRICT_BELOW;
179   if (l == "none") return RESTRICT_NONE;
180   if (l == "mach") return SPEED_RESTRICT_MACH;
181   
182   if (l.empty()) return RESTRICT_NONE;
183   throw sg_io_exception("unknown restriction specification:" + l, 
184     "Route restrictFromString");
185 }
186
187 static const char* restrictionToString(RouteRestriction aRestrict)
188 {
189   switch (aRestrict) {
190   case RESTRICT_AT: return "at";
191   case RESTRICT_BELOW: return "below";
192   case RESTRICT_ABOVE: return "above";
193   case RESTRICT_NONE: return "none";
194   case SPEED_RESTRICT_MACH: return "mach";
195   
196   default:
197     throw sg_exception("invalid route restriction",
198       "Route restrictToString");
199   }
200 }
201
202 Waypt* Waypt::createInstance(RouteBase* aOwner, const std::string& aTypeName)
203 {
204   Waypt* r = NULL;
205   if (aTypeName == "basic") {
206     r = new BasicWaypt(aOwner);
207   } else if (aTypeName == "navaid") {
208     r = new NavaidWaypoint(aOwner);
209   } else if (aTypeName == "offset-navaid") {
210     r = new OffsetNavaidWaypoint(aOwner);
211   } else if (aTypeName == "hold") {
212     r = new Hold(aOwner);
213   } else if (aTypeName == "runway") {
214     r = new RunwayWaypt(aOwner);
215   } else if (aTypeName == "hdgToAlt") {
216     r = new HeadingToAltitude(aOwner);
217   } else if (aTypeName == "dmeIntercept") {
218     r = new DMEIntercept(aOwner);
219   } else if (aTypeName == "radialIntercept") {
220     r = new RadialIntercept(aOwner);
221   } else if (aTypeName == "vectors") {
222     r = new ATCVectors(aOwner);
223   } 
224
225   if (!r || (r->type() != aTypeName)) {
226     throw sg_exception("broken factory method for type:" + aTypeName,
227       "Waypt::createInstance");
228   }
229   
230   return r;
231 }
232
233 WayptRef Waypt::createFromProperties(RouteBase* aOwner, SGPropertyNode_ptr aProp)
234 {
235   if (!aProp->hasChild("type")) {
236     throw sg_io_exception("bad props node, no type provided", 
237       "Waypt::createFromProperties");
238   }
239   
240   try {
241     WayptRef nd(createInstance(aOwner, aProp->getStringValue("type")));
242     nd->initFromProperties(aProp);
243     return nd;
244   } catch (sg_exception& e) {
245     SG_LOG(SG_GENERAL, SG_WARN, "failed to create waypoint, trying basic:" << e.getMessage());
246   }
247   
248 // if we failed to make the waypoint, try again making a basic waypoint.
249 // this handles the case where a navaid waypoint is missing, for example
250   WayptRef nd(new BasicWaypt(aOwner));
251   nd->initFromProperties(aProp);
252   return nd;
253 }
254   
255 void Waypt::saveAsNode(SGPropertyNode* n) const
256 {
257   n->setStringValue("type", type());
258   writeToProperties(n);
259 }
260
261 void Waypt::initFromProperties(SGPropertyNode_ptr aProp)
262 {
263   if (aProp->hasChild("generated")) {
264     setFlag(WPT_GENERATED, aProp->getBoolValue("generated")); 
265   }
266   
267   if (aProp->hasChild("overflight")) {
268     setFlag(WPT_OVERFLIGHT, aProp->getBoolValue("overflight")); 
269   }
270   
271   if (aProp->hasChild("arrival")) {
272     setFlag(WPT_ARRIVAL, aProp->getBoolValue("arrival")); 
273   }
274   
275   if (aProp->hasChild("approach")) {
276     setFlag(WPT_APPROACH, aProp->getBoolValue("approach"));
277   }
278   
279   if (aProp->hasChild("departure")) {
280     setFlag(WPT_DEPARTURE, aProp->getBoolValue("departure")); 
281   }
282   
283   if (aProp->hasChild("miss")) {
284     setFlag(WPT_MISS, aProp->getBoolValue("miss")); 
285   }
286   
287   if (aProp->hasChild("alt-restrict")) {
288     _altRestrict = restrictionFromString(aProp->getStringValue("alt-restrict"));
289     _altitudeFt = aProp->getDoubleValue("altitude-ft");
290   }
291   
292   if (aProp->hasChild("speed-restrict")) {
293     _speedRestrict = restrictionFromString(aProp->getStringValue("speed-restrict"));
294     _speed = aProp->getDoubleValue("speed");
295   }
296   
297   
298 }
299
300 void Waypt::writeToProperties(SGPropertyNode_ptr aProp) const
301 {
302   if (flag(WPT_OVERFLIGHT)) {
303     aProp->setBoolValue("overflight", true);
304   }
305
306   if (flag(WPT_DEPARTURE)) {
307     aProp->setBoolValue("departure", true);
308   }
309   
310   if (flag(WPT_ARRIVAL)) {
311     aProp->setBoolValue("arrival", true);
312   }
313   
314   if (flag(WPT_APPROACH)) {
315     aProp->setBoolValue("approach", true);
316   }
317   
318   if (flag(WPT_MISS)) {
319     aProp->setBoolValue("miss", true);
320   }
321   
322   if (flag(WPT_GENERATED)) {
323     aProp->setBoolValue("generated", true);
324   }
325   
326   if (_altRestrict != RESTRICT_NONE) {
327     aProp->setStringValue("alt-restrict", restrictionToString(_altRestrict));
328     aProp->setDoubleValue("altitude-ft", _altitudeFt);
329   }
330   
331   if (_speedRestrict != RESTRICT_NONE) {
332     aProp->setStringValue("speed-restrict", restrictionToString(_speedRestrict));
333     aProp->setDoubleValue("speed", _speed);
334   }
335 }
336
337 void RouteBase::dumpRouteToKML(const WayptVec& aRoute, const std::string& aName)
338 {
339   SGPath p = "/Users/jmt/Desktop/" + aName + ".kml";
340   std::fstream f;
341   f.open(p.str().c_str(), fstream::out | fstream::app);
342   if (!f.is_open()) {
343     SG_LOG(SG_NAVAID, SG_WARN, "unable to open:" << p.str());
344     return;
345   }
346   
347 // pre-amble
348   f << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
349       "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
350     "<Document>\n";
351
352   dumpRouteToKMLLineString(aName, aRoute, f);
353   
354 // post-amble
355   f << "</Document>\n" 
356     "</kml>" << endl;
357   f.close();
358 }
359
360 void RouteBase::dumpRouteToKMLLineString(const std::string& aIdent,
361   const WayptVec& aRoute, std::ostream& aStream)
362 {
363   // preamble
364   aStream << "<Placemark>\n";
365   aStream << "<name>" << aIdent << "</name>\n";
366   aStream << "<LineString>\n";
367   aStream << "<tessellate>1</tessellate>\n";
368   aStream << "<coordinates>\n";
369   
370   // waypoints
371   for (unsigned int i=0; i<aRoute.size(); ++i) {
372     SGGeod pos = aRoute[i]->position();
373     aStream << pos.getLongitudeDeg() << "," << pos.getLatitudeDeg() << " " << endl;
374   }
375   
376   // postable
377   aStream << "</coordinates>\n"
378     "</LineString>\n"
379     "</Placemark>\n" << endl;
380 }
381
382 void RouteBase::loadAirportProcedures(const SGPath& aPath, FGAirport* aApt)
383 {
384   assert(aApt);
385   try {
386     NavdataVisitor visitor(aApt, aPath);
387     readXML(aPath.str(), visitor);
388   } catch (sg_io_exception& ex) {
389     SG_LOG(SG_NAVAID, SG_WARN, "failure parsing procedures: " << aPath.str() <<
390       "\n\t" << ex.getMessage() << "\n\tat:" << ex.getLocation().asString());
391   } catch (sg_exception& ex) {
392     SG_LOG(SG_NAVAID, SG_WARN, "failure parsing procedures: " << aPath.str() <<
393       "\n\t" << ex.getMessage());
394   }
395 }
396   
397 } // of namespace flightgear