]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/NavDataCache.hxx
ICAO.ils.xml data works read-only.
[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   /**
89    * retrieve an FGPositioned from the cache.
90    * This may be trivial if the object is previously loaded, or require actual
91    * disk IO.
92    */
93   FGPositionedRef loadById(PositionedID guid);
94   
95   PositionedID insertAirport(FGPositioned::Type ty, const std::string& ident,
96                              const std::string& name);
97   void insertTower(PositionedID airportId, const SGGeod& pos);
98   PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident,
99                           const SGGeod& pos, PositionedID apt,
100                           double heading, double length, double width, double displacedThreshold,
101                           double stopway, int surfaceCode);
102   void setRunwayReciprocal(PositionedID runway, PositionedID recip);
103   void setRunwayILS(PositionedID runway, PositionedID ils);
104   
105   PositionedID insertNavaid(FGPositioned::Type ty, const std::string& ident,
106                             const std::string& name, const SGGeod& pos, int freq, int range, double multiuse,
107                             PositionedID apt, PositionedID runway);
108
109   // Assign colocated DME to a navaid
110   void setNavaidColocated(PositionedID navaid, PositionedID colocatedDME);
111   
112   PositionedID insertCommStation(FGPositioned::Type ty,
113                                  const std::string& name, const SGGeod& pos, int freq, int range,
114                                 PositionedID apt);
115   PositionedID insertFix(const std::string& ident, const SGGeod& aPos);
116   
117   PositionedID createPOI(FGPositioned::Type ty, const std::string& ident, const SGGeod& aPos);
118   
119   bool removePOI(FGPositioned::Type ty, const std::string& aIdent);
120     
121   void dropGroundnetFor(PositionedID aAirport);
122   
123   PositionedID insertParking(const std::string& name, const SGGeod& aPos,
124                              PositionedID aAirport,
125                              double aHeading, int aRadius, const std::string& aAircraftType,
126                              const std::string& aAirlines);
127   
128   void setParkingPushBackRoute(PositionedID parking, PositionedID pushBackNode);
129   
130   PositionedID insertTaxiNode(const SGGeod& aPos, PositionedID aAirport, int aHoldType, bool aOnRunway);
131   
132   void insertGroundnetEdge(PositionedID aAirport, PositionedID from, PositionedID to);
133   
134   /// update the metar flag associated with an airport
135   void setAirportMetar(const std::string& icao, bool hasMetar);
136   
137   /**
138    * Modify the position of an existing item.
139    */
140   void updatePosition(PositionedID item, const SGGeod &pos);
141   
142   FGPositionedList findAllWithIdent( const std::string& ident,
143                                      FGPositioned::Filter* filter,
144                                      bool exact );
145   FGPositionedList findAllWithName( const std::string& ident,
146                                     FGPositioned::Filter* filter,
147                                     bool exact );
148   
149   FGPositionedRef findClosestWithIdent( const std::string& aIdent,
150                                         const SGGeod& aPos,
151                                         FGPositioned::Filter* aFilter );
152   
153
154   /**
155    * Helper to implement the AirportSearch widget. Optimised text search of
156    * airport names and idents, returning a list suitable for passing directly
157    * to PLIB.
158    */
159   char** searchAirportNamesAndIdents(const std::string& aFilter);
160   
161   /**
162    * Find the closest matching comm-station on a frequency, to a position.
163    * The filter with be used for both type ranging and to validate the result
164    * candidates.
165    */
166   FGPositionedRef findCommByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
167   
168   /**
169    * find all items of a specified type (or range of types) at an airport
170    */
171   PositionedIDVec airportItemsOfType(PositionedID apt, FGPositioned::Type ty,
172                                      FGPositioned::Type maxTy = FGPositioned::INVALID);
173     
174   /**
175    * find the first match item of the specified type and ident, at an airport
176    */
177   PositionedID airportItemWithIdent(PositionedID apt, FGPositioned::Type ty, const std::string& ident);
178     
179   /**
180    * Find all navaids matching a particular frequency, sorted by range from the
181    * supplied position. Type-range will be determined from the filter
182    */
183   PositionedIDVec findNavaidsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
184   
185   /// overload version of the above that does not consider positioned when
186   /// returning results. Only used by TACAN carrier search
187   PositionedIDVec findNavaidsByFreq(int freqKhz, FGPositioned::Filter* filt);
188   
189   /**
190    * Given a runway and type, find the corresponding navaid (ILS / GS / OM)
191    */
192   PositionedID findNavaidForRunway(PositionedID runway, FGPositioned::Type ty);
193
194   /**
195    * given a navaid name (or similar) from apt.dat / nav.dat, find the
196    * corresponding airport and runway IDs.
197    * Such names look like: 'LHBP 31L DME-ILS' or 'UEEE 23L MM'
198    */
199   AirportRunwayPair findAirportRunway(const std::string& name);
200   
201   /**
202    * Given an aiport, and runway and ILS identifier, find the corresponding cache
203    * entry. This matches the data we get in the ils.xml files for airports.
204    */
205   PositionedID findILS(PositionedID airport, const std::string& runway, const std::string& navIdent);
206   
207   /**
208    * Given an Octree node ID, return a bit-mask defining which of the child
209    * nodes exist. In practice this means an 8-bit value be sufficent, but
210    * an int works fine too.
211    */
212   int getOctreeBranchChildren(int64_t octreeNodeId);
213   
214   void defineOctreeNode(Octree::Branch* pr, Octree::Node* nd);
215     
216   /**
217    * given an octree leaf, return all its child positioned items and their types
218    */
219   TypedPositionedVec getOctreeLeafChildren(int64_t octreeNodeId);
220   
221 // airways
222   int findAirway(int network, const std::string& aName);
223   
224   /**
225    * insert an edge between two positioned nodes, into the network.
226    * The airway identifier will be set accordingly. No reverse edge is created
227    * by this method - edges are directional so a reverses must be explicitly
228    * created.
229    */
230   void insertEdge(int network, int airwayID, PositionedID from, PositionedID to);
231   
232   /// is the specified positioned a node on the network?
233   bool isInAirwayNetwork(int network, PositionedID pos);
234   
235   /**
236    * retrive all the destination points reachcable from a positioned
237    * in an airway
238    */
239   AirwayEdgeVec airwayEdgesFrom(int network, PositionedID pos);
240   
241 // ground-network
242   PositionedIDVec groundNetNodes(PositionedID aAirport, bool onlyPushback);
243   void markGroundnetAsPushback(PositionedID nodeId);
244   
245   PositionedID findGroundNetNode(PositionedID airport, const SGGeod& aPos,
246                                  bool onRunway, FGRunway* aRunway = NULL);
247   PositionedIDVec groundNetEdgesFrom(PositionedID pos, bool onlyPushback);
248   
249   PositionedIDVec findAirportParking(PositionedID airport, const std::string& flightType,
250                                      int radius);
251
252
253     class Transaction
254     {
255     public:
256         Transaction(NavDataCache* cache);
257         ~Transaction();
258         
259         void commit();
260     private:
261         NavDataCache* _instance;
262         bool _committed;
263     };
264     
265     bool isReadOnly() const;
266 private:
267   NavDataCache();
268   
269   friend class RebuildThread;
270   void doRebuild();
271   
272   friend class Transaction;
273   
274     void beginTransaction();
275     void commitTransaction();
276     void abortTransaction();
277     
278   class NavDataCachePrivate;
279   std::auto_ptr<NavDataCachePrivate> d;      
280 };
281   
282 } // of namespace flightgear
283
284 #endif // of FG_NAVDATACACHE_HXX
285