]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.hxx
Add a lower-bound type navaid lookup, and the ability to specify navaid type in the...
[flightgear.git] / src / Airports / simple.hxx
1 // simple.hxx -- a really simplistic class to manage airport ID,
2 //                 lat, lon of the center of one of it's runways, and 
3 //                 elevation in feet.
4 //
5 // Written by Curtis Olson, started April 1998.
6 // Updated by Durk Talsma, started December 2004.
7 //
8 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #ifndef _FG_SIMPLE_HXX
28 #define _FG_SIMPLE_HXX
29
30
31 #ifndef __cplusplus                                                          
32 # error This library requires C++
33 #endif                                   
34
35
36 #ifdef HAVE_CONFIG_H
37 #  include <config.h>
38 #endif
39 #include <simgear/math/point3d.hxx>
40 #include <simgear/route/waypoint.hxx>
41 #include <simgear/compiler.h>
42 #include <simgear/xml/easyxml.hxx>
43
44 #include STL_STRING
45 #include <map>
46 #include <set>
47 #include <vector>
48
49 SG_USING_STD(string);
50 SG_USING_STD(map);
51 SG_USING_STD(set);
52 SG_USING_STD(vector);
53
54 typedef vector<string> stringVec;
55 typedef vector<string>::iterator stringVecIterator;
56 typedef vector<string>::const_iterator stringVecConstIterator;
57
58 typedef vector<time_t> timeVec;
59 typedef vector<time_t>::const_iterator timeVecConstIterator;
60
61
62 /***************************************************************************/
63 class ScheduleTime {
64 private:
65   timeVec   start;
66   timeVec   end;
67   stringVec scheduleNames;
68   double tailWind;
69   double crssWind;
70 public:
71   ScheduleTime() {};
72   ScheduleTime(const ScheduleTime &other);
73   ScheduleTime &operator= (const ScheduleTime &other);
74   string getName(time_t dayStart);
75
76   void clear();
77   void addStartTime(time_t time)     { start.push_back(time);            };
78   void addEndTime  (time_t time)     { end.  push_back(time);            };
79   void addScheduleName(const string& sched) { scheduleNames.push_back(sched);   };
80   void setTailWind(double wnd)  { tailWind = wnd;                        };
81   void setCrossWind(double wnd) { tailWind = wnd;                        };
82
83   double getTailWind()  { return tailWind;                               };
84   double getCrossWind() { return crssWind;                               };
85 };
86
87 //typedef vector<ScheduleTime> ScheduleTimes;
88 /*****************************************************************************/
89
90 class RunwayList
91 {
92 private:
93   string type;
94   stringVec preferredRunways;
95 public:
96   RunwayList() {};
97   RunwayList(const RunwayList &other);
98   RunwayList& operator= (const RunwayList &other);
99
100   void set(const string&, const string&);
101   void clear();
102
103   string getType() { return type; };
104   stringVec *getRwyList() { return &preferredRunways;    };
105   string getRwyList(int j) { return preferredRunways[j]; };
106 };
107
108 typedef vector<RunwayList> RunwayListVec;
109 typedef vector<RunwayList>::iterator RunwayListVectorIterator;
110 typedef vector<RunwayList>::const_iterator RunwayListVecConstIterator;
111
112
113 /*****************************************************************************/
114
115 class RunwayGroup
116 {
117 private:
118   string name;
119   RunwayListVec rwyList;
120   int active;
121   //stringVec runwayNames;
122   int choice[2];
123   int nrActive;
124 public:
125   RunwayGroup() {};
126   RunwayGroup(const RunwayGroup &other);
127   RunwayGroup &operator= (const RunwayGroup &other);
128
129   void setName(string nm) { name = nm;                };
130   void add(RunwayList list) { rwyList.push_back(list);};
131   void setActive(const string& aptId, double windSpeed, double windHeading, double maxTail, double maxCross);
132
133   int getNrActiveRunways() { return nrActive;};
134   void getActive(int i, string& name, string& type);
135
136   string getName() { return name; };
137   void clear() { rwyList.clear(); }; 
138   //void add(string, string);
139 };
140
141 typedef vector<RunwayGroup> PreferenceList;
142 typedef vector<RunwayGroup>::iterator PreferenceListIterator;
143 typedef vector<RunwayGroup>::const_iterator PreferenceListConstIterator;
144 /******************************************************************************/
145
146 class FGRunwayPreference  : public XMLVisitor {
147 private:
148   string value;
149   string scheduleName;
150
151   ScheduleTime comTimes; // Commercial Traffic;
152   ScheduleTime genTimes; // General Aviation;
153   ScheduleTime milTimes; // Military Traffic;
154   ScheduleTime currTimes; // Needed for parsing;
155
156   RunwayList  rwyList;
157   RunwayGroup rwyGroup;
158   PreferenceList preferences;
159
160   time_t processTime(const string&);
161   bool initialized;
162
163 public:
164   FGRunwayPreference();
165   FGRunwayPreference(const FGRunwayPreference &other);
166   
167   FGRunwayPreference & operator= (const FGRunwayPreference &other);
168   ScheduleTime *getSchedule(const char *trafficType);
169   RunwayGroup *getGroup(const string& groupName);
170   bool available() { return initialized; };
171
172  // Some overloaded virtual XMLVisitor members
173   virtual void startXML (); 
174   virtual void endXML   ();
175   virtual void startElement (const char * name, const XMLAttributes &atts);
176   virtual void endElement (const char * name);
177   virtual void data (const char * s, int len);
178   virtual void pi (const char * target, const char * data);
179   virtual void warning (const char * message, int line, int column);
180   virtual void error (const char * message, int line, int column);
181 };
182
183 double processPosition(const string& pos);
184
185 class FGParking {
186 private:
187   double latitude;
188   double longitude;
189   double heading;
190   double radius;
191   int index;
192   string parkingName;
193   string type;
194   string airlineCodes;
195  
196   bool available;
197
198   
199
200 public:
201   FGParking() { available = true;};
202   //FGParking(FGParking &other);
203   FGParking(double lat,
204             double lon,
205             double hdg,
206             double rad,
207             int idx,
208             const string& name,
209             const string& tpe,
210             const string& codes);
211   void setLatitude (const string& lat)  { latitude    = processPosition(lat);  };
212   void setLongitude(const string& lon)  { longitude   = processPosition(lon);  };
213   void setHeading  (double hdg)  { heading     = hdg;  };
214   void setRadius   (double rad)  { radius      = rad;  };
215   void setIndex    (int    idx)  { index       = idx;  };
216   void setName     (const string& name) { parkingName = name; };
217   void setType     (const string& tpe)  { type        = tpe;  };
218   void setCodes    (const string& codes){ airlineCodes= codes;};
219
220   bool isAvailable ()         { return available;};
221   void setAvailable(bool val) { available = val; };
222   
223   double getLatitude () { return latitude;    };
224   double getLongitude() { return longitude;   };
225   double getHeading  () { return heading;     };
226   double getRadius   () { return radius;      };
227   int    getIndex    () { return index;       };
228   string getType     () { return type;        };
229   string getCodes    () { return airlineCodes;};
230   string getName     () { return parkingName; };
231
232   bool operator< (const FGParking &other) const {return radius < other.radius; };
233 };
234
235 typedef vector<FGParking> FGParkingVec;
236 typedef vector<FGParking>::iterator FGParkingVecIterator;
237 typedef vector<FGParking>::const_iterator FGParkingVecConstIterator;
238
239 class FGTaxiSegment; // forward reference
240
241 typedef vector<FGTaxiSegment>  FGTaxiSegmentVector;
242 typedef vector<FGTaxiSegment*> FGTaxiSegmentPointerVector;
243 typedef vector<FGTaxiSegment>::iterator FGTaxiSegmentVectorIterator;
244 typedef vector<FGTaxiSegment*>::iterator FGTaxiSegmentPointerVectorIterator;
245
246 /**************************************************************************************
247  * class FGTaxiNode
248  *************************************************************************************/
249 class FGTaxiNode 
250 {
251 private:
252   double lat;
253   double lon;
254   int index;
255   FGTaxiSegmentPointerVector next; // a vector to all the segments leaving from this node
256   
257 public:
258   FGTaxiNode();
259   FGTaxiNode(double, double, int);
260
261   void setIndex(int idx)                  { index = idx;};
262   void setLatitude (double val)           { lat = val;};
263   void setLongitude(double val)           { lon = val;};
264   void setLatitude (const string& val)           { lat = processPosition(val);  };
265   void setLongitude(const string& val)           { lon = processPosition(val);  };
266   void addSegment(FGTaxiSegment *segment) { next.push_back(segment); };
267   
268   double getLatitude() { return lat;};
269   double getLongitude(){ return lon;};
270
271   int getIndex() { return index; };
272   FGTaxiNode *getAddress() { return this;};
273   FGTaxiSegmentPointerVectorIterator getBeginRoute() { return next.begin(); };
274   FGTaxiSegmentPointerVectorIterator getEndRoute()   { return next.end();   }; 
275 };
276
277 typedef vector<FGTaxiNode> FGTaxiNodeVector;
278 typedef vector<FGTaxiNode>::iterator FGTaxiNodeVectorIterator;
279
280 /***************************************************************************************
281  * class FGTaxiSegment
282  **************************************************************************************/
283 class FGTaxiSegment
284 {
285 private:
286   int startNode;
287   int endNode;
288   double length;
289   FGTaxiNode *start;
290   FGTaxiNode *end;
291   int index;
292
293 public:
294   FGTaxiSegment();
295   FGTaxiSegment(FGTaxiNode *, FGTaxiNode *, int);
296
297   void setIndex        (int val) { index     = val; };
298   void setStartNodeRef (int val) { startNode = val; };
299   void setEndNodeRef   (int val) { endNode   = val; };
300
301   void setStart(FGTaxiNodeVector *nodes);
302   void setEnd  (FGTaxiNodeVector *nodes);
303   void setTrackDistance();
304
305   FGTaxiNode * getEnd() { return end;};
306   double getLength() { return length; };
307   int getIndex() { return index; };
308
309   
310 };
311
312
313 typedef vector<int> intVec;
314 typedef vector<int>::iterator intVecIterator;
315
316 class FGTaxiRoute
317 {
318 private:
319   intVec nodes;
320   double distance;
321   intVecIterator currNode;
322
323 public:
324   FGTaxiRoute() { distance = 0; currNode = nodes.begin(); };
325   FGTaxiRoute(intVec nds, double dist) { nodes = nds; distance = dist; currNode = nodes.begin();};
326   bool operator< (const FGTaxiRoute &other) const {return distance < other.distance; };
327   bool empty () { return nodes.begin() == nodes.end(); };
328   bool next(int *val); 
329   
330   void first() { currNode = nodes.begin(); };
331 };
332
333 typedef vector<FGTaxiRoute> TaxiRouteVector;
334 typedef vector<FGTaxiRoute>::iterator TaxiRouteVectorIterator;
335
336 /**************************************************************************************
337  * class FGGroundNetWork
338  *************************************************************************************/
339 class FGGroundNetwork
340 {
341 private:
342   bool hasNetwork;
343   FGTaxiNodeVector    nodes;
344   FGTaxiSegmentVector segments;
345   //intVec route;
346   intVec traceStack;
347   TaxiRouteVector routes;
348   
349   bool foundRoute;
350   double totalDistance, maxDistance;
351   
352 public:
353   FGGroundNetwork();
354
355   void addNode   (const FGTaxiNode& node);
356   void addNodes  (FGParkingVec *parkings);
357   void addSegment(const FGTaxiSegment& seg); 
358
359   void init();
360   bool exists() { return hasNetwork; };
361   int findNearestNode(double lat, double lon);
362   FGTaxiNode *findNode(int idx);
363   FGTaxiRoute findShortestRoute(int start, int end);
364   void trace(FGTaxiNode *, int, int, double dist);
365  
366 };
367
368 /***************************************************************************************
369  *
370  **************************************************************************************/
371 class FGAirport : public XMLVisitor{
372 private:
373   string _id;
374   double _longitude;    // degrees
375   double _latitude;     // degrees
376   double _elevation;    // ft
377   string _code;               // depricated and can be removed
378   string _name;
379   bool _has_metar;
380   FGParkingVec parkings;
381   FGRunwayPreference rwyPrefs;
382   FGGroundNetwork groundNetwork;
383
384   time_t lastUpdate;
385   string prevTrafficType;
386   stringVec landing;
387   stringVec takeoff;
388
389   // Experimental keep a running average of wind dir and speed to prevent
390   // Erratic runway changes. 
391   // Note: I should add these to the copy constructor and assigment operator to be
392   // constistent
393   double avWindHeading [10];
394   double avWindSpeed   [10];
395
396   string chooseRunwayFallback();
397
398 public:
399   FGAirport();
400   FGAirport(const FGAirport &other);
401   //operator= (FGAirport &other);
402   FGAirport(const string& id, double lon, double lat, double elev, const string& name, bool has_metar);
403
404   void init();
405   void getActiveRunway(const string& trafficType, int action, string& runway);
406   bool getAvailableParking(double *lat, double *lon, double *heading, int *gate, double rad, const string& fltype, 
407                            const string& acType, const string& airline);
408   void getParking         (int id, double *lat, double* lon, double *heading);
409   FGParking *getParking(int i); // { if (i < parkings.size()) return parkings[i]; else return 0;};
410   void releaseParking(int id);
411   string getParkingName(int i); 
412   string getId() const { return _id;};
413   const string &getName() const { return _name;};
414   //FGAirport *getAddress() { return this; };
415   //const string &getName() const { return _name;};
416   // Returns degrees
417   double getLongitude() const { return _longitude;};
418   // Returns degrees
419   double getLatitude()  const { return _latitude; };
420   // Returns ft
421   double getElevation() const { return _elevation;};
422   bool   getMetar()     const { return _has_metar;};
423  FGGroundNetwork* getGroundNetwork() { return &groundNetwork; };
424   
425
426   void setId(const string& id) { _id = id;};
427   void setMetar(bool value) { _has_metar = value; };
428
429   void setRwyUse(const FGRunwayPreference& ref);
430
431  // Some overloaded virtual XMLVisitor members
432   virtual void startXML (); 
433   virtual void endXML   ();
434   virtual void startElement (const char * name, const XMLAttributes &atts);
435   virtual void endElement (const char * name);
436   virtual void data (const char * s, int len);
437   virtual void pi (const char * target, const char * data);
438   virtual void warning (const char * message, int line, int column);
439   virtual void error (const char * message, int line, int column);
440 };
441
442 typedef map < string, FGAirport* > airport_map;
443 typedef airport_map::iterator airport_map_iterator;
444 typedef airport_map::const_iterator const_airport_map_iterator;
445
446 typedef vector < FGAirport * > airport_list;
447
448
449 class FGAirportList {
450
451 private:
452
453     airport_map airports_by_id;
454     airport_list airports_array;
455     set < string > ai_dirs;
456
457 public:
458
459     // Constructor (new)
460     FGAirportList();
461
462     // Destructor
463     ~FGAirportList();
464
465     // add an entry to the list
466     void add( const string& id, const double longitude, const double latitude,
467               const double elevation, const string& name, const bool has_metar );
468
469     // search for the specified id.
470       // Returns NULL if unsucessfull.
471     FGAirport* search( const string& id );
472         
473         // Search for the next airport in ASCII sequence to the supplied id.
474         // eg. id = "KDC" or "KDCA" would both return "KDCA".
475         // If exact = true then only exact matches are returned.
476         // NOTE: Numbers come prior to A-Z in ASCII sequence so id = "LD" would return "LD57", not "LDDP"
477         // Implementation assumes airport codes are unique.
478         // Returns NULL if unsucessfull.
479         const FGAirport* findFirstById( const string& id, bool exact = false );
480
481     // search for the airport closest to the specified position
482     // (currently a linear inefficient search so it's probably not
483     // best to use this at runtime.)  If with_metar is true, then only
484     // return station id's marked as having metar data.
485         // Returns NULL if fails (unlikely unless none have metar and with_metar spec'd!)
486     FGAirport* search( double lon_deg, double lat_deg, bool with_metar );
487
488     /**
489      * Return the number of airports in the list.
490      */
491     int size() const;
492
493     /**
494      * Return a specific airport, by position.
495      */
496     const FGAirport *getAirport( unsigned int index ) const;
497
498     /**
499      * Mark the specified airport record as not having metar
500      */
501     void no_metar( const string &id );
502
503     /**
504      * Mark the specified airport record as (yes) having metar
505      */
506     void has_metar( const string &id );
507
508 };
509
510
511 #endif // _FG_SIMPLE_HXX
512
513