]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/NavDataCache.hxx
Implement a persistent cache for navigation data.
[flightgear.git] / src / Navaids / NavDataCache.hxx
1 /**
2  * NavDataCache.hxx - defines a unified binary cache for navigation
3  * data, parsed from various text / XML sources.
4  */
5  
6 // Written by James Turner, started 2012.
7 //
8 // Copyright (C) 2012 James Turner
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23
24 #ifndef FG_NAVDATACACHE_HXX
25 #define FG_NAVDATACACHE_HXX
26
27 #include <memory>
28 #include <Navaids/positioned.hxx>
29     
30 class SGPath;
31
32 namespace flightgear
33 {
34   
35 /// a pair of airport ID, runway ID
36 typedef std::pair<PositionedID, PositionedID> AirportRunwayPair;
37   
38 typedef std::pair<FGPositioned::Type, PositionedID> TypedPositioned;
39 typedef std::vector<TypedPositioned> TypedPositionedVec;
40
41 // pair of airway ID, destination node ID
42 typedef std::pair<int, PositionedID> AirwayEdge;
43 typedef std::vector<AirwayEdge> AirwayEdgeVec;
44   
45 namespace Octree {
46   class Node;
47   class Branch;
48 }
49   
50 class NavDataCache
51 {
52 public:
53     ~NavDataCache();
54     
55 // singleton accessor
56     static NavDataCache* instance();
57           
58   /**
59    * predicate - check if the cache needs to be rebuilt.
60    * This can happen is the cache file is missing or damaged, or one of the
61    ** global input files is changed.
62    */
63   bool isRebuildRequired();
64   
65   /**
66    * run the cache rebuild
67    */
68   void rebuild();
69   
70   bool isCachedFileModified(const SGPath& path) const;
71   void stampCacheFile(const SGPath& path);
72   
73   int readIntProperty(const std::string& key);
74   double readDoubleProperty(const std::string& key);
75   std::string readStringProperty(const std::string& key);
76   
77   void writeIntProperty(const std::string& key, int value);
78   void writeStringProperty(const std::string& key, const std::string& value);
79   void writeDoubleProperty(const std::string& key, const double& value);
80   
81   FGPositioned* loadById(PositionedID guid);
82   
83   PositionedID insertAirport(FGPositioned::Type ty, const std::string& ident,
84                              const std::string& name);
85   void insertTower(PositionedID airportId, const SGGeod& pos);
86   PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident,
87                           const SGGeod& pos, PositionedID apt,
88                           double heading, double length, double width, double displacedThreshold,
89                           double stopway, int surfaceCode);
90   void setRunwayReciprocal(PositionedID runway, PositionedID recip);
91   void setRunwayILS(PositionedID runway, PositionedID ils);
92   
93   void updateRunwayThreshold(PositionedID runwayID, const SGGeod &aThreshold,
94                     double aHeading, double aDisplacedThreshold,
95                     double aStopway);
96   
97   PositionedID insertNavaid(FGPositioned::Type ty, const std::string& ident,
98                             const std::string& name, const SGGeod& pos, int freq, int range, double multiuse,
99                             PositionedID apt, PositionedID runway);
100   void updateILS(PositionedID ils, const SGGeod& newPos, double aHdg);
101   
102   PositionedID insertCommStation(FGPositioned::Type ty,
103                                  const std::string& name, const SGGeod& pos, int freq, int range,
104                                 PositionedID apt);
105   PositionedID insertFix(const std::string& ident, const SGGeod& aPos);
106   
107   PositionedID createUserWaypoint(const std::string& ident, const SGGeod& aPos);
108   
109   void setAirportMetar(const std::string& icao, bool hasMetar);
110   
111   void updatePosition(PositionedID item, const SGGeod &pos);
112   
113   FGPositioned::List findAllWithIdent(const std::string& ident,
114                                       FGPositioned::Filter* filter, bool exact);
115   FGPositioned::List findAllWithName(const std::string& ident,
116                                       FGPositioned::Filter* filter, bool exact);
117   
118   FGPositionedRef findClosestWithIdent(const std::string& aIdent,
119                                       const SGGeod& aPos, FGPositioned::Filter* aFilter);
120   
121
122   char** searchAirportNamesAndIdents(const std::string& aFilter);
123   
124   FGPositionedRef findCommByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
125   
126   PositionedIDVec airportItemsOfType(PositionedID apt, FGPositioned::Type ty,
127                                      FGPositioned::Type maxTy = FGPositioned::INVALID);
128     
129   PositionedID airportItemWithIdent(PositionedID apt, FGPositioned::Type ty, const std::string& ident);
130   
131   PositionedIDVec findNavaidsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
132   
133   /// overload version of the above that does not consider positioned when
134   /// returning results. Only used by TACAN carrier search
135   PositionedIDVec findNavaidsByFreq(int freqKhz, FGPositioned::Filter* filt);
136   
137   /**
138    * given a navaid name (or similar) from apt.dat / nav.dat, find the
139    * corresponding airport and runway IDs.
140    * Such names look like: 'LHBP 31L DME-ILS' or 'UEEE 23L MM'
141    */
142   AirportRunwayPair findAirportRunway(const std::string& name);
143   
144   /**
145    * Given an aiport, and runway and ILS identifier, find the corresponding cache
146    * entry. This matches the data we get in the ils.xml files for airports.
147    */
148   PositionedID findILS(PositionedID airport, const std::string& runway, const std::string& navIdent);
149   
150   /**
151    * Given an Octree node ID, return a bit-mask defining which of the child
152    * nodes exist. In practice this means an 8-bit value be sufficent, but
153    * an int works fine too.
154    */
155   int getOctreeBranchChildren(int64_t octreeNodeId);
156   
157   void defineOctreeNode(Octree::Branch* pr, Octree::Node* nd);
158     
159   /**
160    * given an octree leaf, return all its child positioned items and their types
161    */
162   TypedPositionedVec getOctreeLeafChildren(int64_t octreeNodeId);
163   
164 // airways
165   int findAirway(int network, const std::string& aName);
166   
167   void insertEdge(int network, int airwayID, PositionedID from, PositionedID to);
168   
169   bool isInAirwayNetwork(int network, PositionedID pos);
170   
171   /**
172    * retrive all the destination points reachcable from a positioned
173    * in an airway
174    */
175   AirwayEdgeVec airwayEdgesFrom(int network, PositionedID pos);
176 private:
177   NavDataCache();
178     
179   class NavDataCachePrivate;
180   std::auto_ptr<NavDataCachePrivate> d;      
181 };
182   
183 } // of namespace flightgear
184
185 #endif // of FG_NAVDATACACHE_HXX
186