]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/NavDataCache.hxx
Whoops, work-around for #926 correctly.
[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
29 #include <simgear/misc/strutils.hxx> // for string_list
30 #include <Navaids/positioned.hxx>
31     
32 class SGPath;
33 class FGRunway;
34
35 namespace flightgear
36 {
37   
38 /// a pair of airport ID, runway ID
39 typedef std::pair<PositionedID, PositionedID> AirportRunwayPair;
40   
41 typedef std::pair<FGPositioned::Type, PositionedID> TypedPositioned;
42 typedef std::vector<TypedPositioned> TypedPositionedVec;
43
44 // pair of airway ID, destination node ID
45 typedef std::pair<int, PositionedID> AirwayEdge;
46 typedef std::vector<AirwayEdge> AirwayEdgeVec;
47   
48 namespace Octree {
49   class Node;
50   class Branch;
51 }
52   
53 class NavDataCache
54 {
55 public:
56     ~NavDataCache();
57     
58 // singleton accessor
59     static NavDataCache* instance();
60           
61   /**
62    * predicate - check if the cache needs to be rebuilt.
63    * This can happen is the cache file is missing or damaged, or one of the
64    ** global input files is changed.
65    */
66   bool isRebuildRequired();
67   
68   /**
69    * run the cache rebuild - returns true if rebuild is complete,
70    * otherwise keep going.
71    */
72   bool rebuild();
73   
74   bool isCachedFileModified(const SGPath& path) const;
75   void stampCacheFile(const SGPath& path);
76   
77   int readIntProperty(const std::string& key);
78   double readDoubleProperty(const std::string& key);
79   std::string readStringProperty(const std::string& key);
80   
81   void writeIntProperty(const std::string& key, int value);
82   void writeStringProperty(const std::string& key, const std::string& value);
83   void writeDoubleProperty(const std::string& key, const double& value);
84   
85   string_list readStringListProperty(const std::string& key);
86   void writeStringListProperty(const std::string& key, const string_list& values);
87   
88 // transaction API wrappers
89   void beginTransaction();
90   void commitTransaction();
91   void abortTransaction();
92   
93   /**
94    * retrieve an FGPositioned from the cache.
95    * This may be trivial if the object is previously loaded, or require actual
96    * disk IO.
97    */
98   FGPositioned* loadById(PositionedID guid);
99   
100   PositionedID insertAirport(FGPositioned::Type ty, const std::string& ident,
101                              const std::string& name);
102   void insertTower(PositionedID airportId, const SGGeod& pos);
103   PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident,
104                           const SGGeod& pos, PositionedID apt,
105                           double heading, double length, double width, double displacedThreshold,
106                           double stopway, int surfaceCode);
107   void setRunwayReciprocal(PositionedID runway, PositionedID recip);
108   void setRunwayILS(PositionedID runway, PositionedID ils);
109   
110   void updateRunwayThreshold(PositionedID runwayID, const SGGeod &aThreshold,
111                     double aHeading, double aDisplacedThreshold,
112                     double aStopway);
113   
114   PositionedID insertNavaid(FGPositioned::Type ty, const std::string& ident,
115                             const std::string& name, const SGGeod& pos, int freq, int range, double multiuse,
116                             PositionedID apt, PositionedID runway);
117   void updateILS(PositionedID ils, const SGGeod& newPos, double aHdg);
118   
119   PositionedID insertCommStation(FGPositioned::Type ty,
120                                  const std::string& name, const SGGeod& pos, int freq, int range,
121                                 PositionedID apt);
122   PositionedID insertFix(const std::string& ident, const SGGeod& aPos);
123   
124   PositionedID createUserWaypoint(const std::string& ident, const SGGeod& aPos);
125   
126   void dropGroundnetFor(PositionedID aAirport);
127   
128   PositionedID insertParking(const std::string& name, const SGGeod& aPos,
129                              PositionedID aAirport,
130                              double aHeading, int aRadius, const std::string& aAircraftType,
131                              const std::string& aAirlines);
132   
133   void setParkingPushBackRoute(PositionedID parking, PositionedID pushBackNode);
134   
135   PositionedID insertTaxiNode(const SGGeod& aPos, PositionedID aAirport, int aHoldType, bool aOnRunway);
136   
137   void insertGroundnetEdge(PositionedID aAirport, PositionedID from, PositionedID to);
138   
139   /// update the metar flag associated with an airport
140   void setAirportMetar(const std::string& icao, bool hasMetar);
141   
142   /**
143    * Modify the position of an existing item.
144    */
145   void updatePosition(PositionedID item, const SGGeod &pos);
146   
147   FGPositioned::List findAllWithIdent(const std::string& ident,
148                                       FGPositioned::Filter* filter, bool exact);
149   FGPositioned::List findAllWithName(const std::string& ident,
150                                       FGPositioned::Filter* filter, bool exact);
151   
152   FGPositionedRef findClosestWithIdent(const std::string& aIdent,
153                                       const SGGeod& aPos, FGPositioned::Filter* aFilter);
154   
155
156   /**
157    * Helper to implement the AirportSearch widget. Optimised text search of
158    * airport names and idents, returning a list suitable for passing directly
159    * to PLIB.
160    */
161   char** searchAirportNamesAndIdents(const std::string& aFilter);
162   
163   /**
164    * Find the closest matching comm-station on a frequency, to a position.
165    * The filter with be used for both type ranging and to validate the result
166    * candidates.
167    */
168   FGPositionedRef findCommByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
169   
170   /**
171    * find all items of a specified type (or range of types) at an airport
172    */
173   PositionedIDVec airportItemsOfType(PositionedID apt, FGPositioned::Type ty,
174                                      FGPositioned::Type maxTy = FGPositioned::INVALID);
175     
176   /**
177    * find the first match item of the specified type and ident, at an airport
178    */
179   PositionedID airportItemWithIdent(PositionedID apt, FGPositioned::Type ty, const std::string& ident);
180     
181   /**
182    * Find all navaids matching a particular frequency, sorted by range from the
183    * supplied position. Type-range will be determined from the filter
184    */
185   PositionedIDVec findNavaidsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
186   
187   /// overload version of the above that does not consider positioned when
188   /// returning results. Only used by TACAN carrier search
189   PositionedIDVec findNavaidsByFreq(int freqKhz, FGPositioned::Filter* filt);
190   
191   /**
192    * Given a runway and type, find the corresponding navaid (ILS / GS / OM)
193    */
194   PositionedID findNavaidForRunway(PositionedID runway, FGPositioned::Type ty);
195   
196   /**
197    * given a navaid name (or similar) from apt.dat / nav.dat, find the
198    * corresponding airport and runway IDs.
199    * Such names look like: 'LHBP 31L DME-ILS' or 'UEEE 23L MM'
200    */
201   AirportRunwayPair findAirportRunway(const std::string& name);
202   
203   /**
204    * Given an aiport, and runway and ILS identifier, find the corresponding cache
205    * entry. This matches the data we get in the ils.xml files for airports.
206    */
207   PositionedID findILS(PositionedID airport, const std::string& runway, const std::string& navIdent);
208   
209   /**
210    * Given an Octree node ID, return a bit-mask defining which of the child
211    * nodes exist. In practice this means an 8-bit value be sufficent, but
212    * an int works fine too.
213    */
214   int getOctreeBranchChildren(int64_t octreeNodeId);
215   
216   void defineOctreeNode(Octree::Branch* pr, Octree::Node* nd);
217     
218   /**
219    * given an octree leaf, return all its child positioned items and their types
220    */
221   TypedPositionedVec getOctreeLeafChildren(int64_t octreeNodeId);
222   
223 // airways
224   int findAirway(int network, const std::string& aName);
225   
226   /**
227    * insert an edge between two positioned nodes, into the network.
228    * The airway identifier will be set accordingly. No reverse edge is created
229    * by this method - edges are directional so a reverses must be explicitly
230    * created.
231    */
232   void insertEdge(int network, int airwayID, PositionedID from, PositionedID to);
233   
234   /// is the specified positioned a node on the network?
235   bool isInAirwayNetwork(int network, PositionedID pos);
236   
237   /**
238    * retrive all the destination points reachcable from a positioned
239    * in an airway
240    */
241   AirwayEdgeVec airwayEdgesFrom(int network, PositionedID pos);
242   
243 // ground-network
244   PositionedIDVec groundNetNodes(PositionedID aAirport, bool onlyPushback);
245   void markGroundnetAsPushback(PositionedID nodeId);
246   
247   PositionedID findGroundNetNode(PositionedID airport, const SGGeod& aPos,
248                                  bool onRunway, FGRunway* aRunway = NULL);
249   PositionedIDVec groundNetEdgesFrom(PositionedID pos, bool onlyPushback);
250   
251   PositionedIDVec findAirportParking(PositionedID airport, const std::string& flightType,
252                                      int radius);
253 private:
254   NavDataCache();
255   
256   friend class RebuildThread;
257   void doRebuild();
258   
259   class NavDataCachePrivate;
260   std::auto_ptr<NavDataCachePrivate> d;      
261 };
262   
263 } // of namespace flightgear
264
265 #endif // of FG_NAVDATACACHE_HXX
266