FlightGear next
NavDataCache.hxx
Go to the documentation of this file.
1
2/*
3 * SPDX-FileCopyrightText: (C) 2012 James Turner <james@flightgear.org>
4 * SPDX_FileComment: Defines a unified binary cache for navigation data, parsed from text/XMl sources
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#pragma once
9
10#include <cstddef> // for std::size_t
11#include <functional>
12#include <memory>
13#include <vector>
14
16#include <simgear/misc/sg_path.hxx>
17#include <simgear/misc/strutils.hxx> // for string_list
18
19
20class SGPath;
21class FGRunway;
22
23namespace flightgear {
24
26typedef std::pair<PositionedID, PositionedID> AirportRunwayPair;
27
28typedef std::pair<FGPositioned::Type, PositionedID> TypedPositioned;
29typedef std::vector<TypedPositioned> TypedPositionedVec;
30
31// pair of airway ID, destination node ID
32typedef std::pair<int, PositionedID> AirwayEdge;
33typedef std::vector<AirwayEdge> AirwayEdgeVec;
34
35namespace Octree {
36class Node;
37class Branch;
38} // namespace Octree
39
40class Airway;
41using AirwayRef = SGSharedPtr<Airway>;
42
43class NavDataCache
44{
45public:
47
48 // singleton accessor
49 static NavDataCache* instance();
50
51 // static creator
52 static NavDataCache* createInstance();
53
54 static void shutdown();
55
56 SGPath path() const;
57
69
71 SGPath datPath;
72 SGPath sceneryPath; // scenery realpath() the dat file belongs to
73 };
74
75 using SceneryLocationList = std::vector<SceneryLocation>;
76
78 DatFileType datFileType; // for instance, DATFILETYPE_APT
80 std::size_t totalSize; // total size of all these files, in bytes
81 };
82
83 // datTypeStr[DATFILETYPE_APT] = std::string("apt"), etc. This gives,
84 // among other things, the subdirectory of $scenery_path/NavData where
85 // each type of dat file is looked for.
86 static const std::string datTypeStr[];
87 // defaultDatFile[DATFILETYPE_APT] = std::string("Airports/apt.dat.gz"),
88 // etc. This tells where to find the historical dat files: those under
89 // $FG_ROOT.
90 static const std::string defaultDatFile[];
91
92 // Update d->datFilesInfo and legacy d->metarDatPath, d->poiDatPath,
93 // etc. by looking into $scenery_path/NavData for each scenery path.
95 // Returns datFilesInfo for the given type.
96 const DatFilesGroupInfo& getDatFilesInfo(DatFileType datFileType) const;
97
103 bool isRebuildRequired();
104
105 static bool isAnotherProcessRebuilding();
106
116
121
122 unsigned int rebuildPhaseCompletionPercentage() const;
123 void setRebuildPhaseProgress(RebuildPhase ph, unsigned int percent = 0);
124
125 bool isCachedFileModified(const SGPath& path) const;
126 void stampCacheFile(const SGPath& path, const std::string& sha = {});
127
128 int readIntProperty(const std::string& key);
129 double readDoubleProperty(const std::string& key);
130 std::string readStringProperty(const std::string& key);
131
132 void writeIntProperty(const std::string& key, int value);
133 void writeStringProperty(const std::string& key, const std::string& value);
134 void writeDoubleProperty(const std::string& key, const double& value);
135
136 // Warning: order is not necessarily preserved upon write-read cycles!
137 string_list readStringListProperty(const std::string& key);
138 void writeStringListProperty(const std::string& key, const string_list& values);
139
140 // These ones guarantee the same order after each write-read cycle.
141 string_list readOrderedStringListProperty(const std::string& key,
142 const char* separator);
143 void writeOrderedStringListProperty(const std::string& key,
144 const string_list& values,
145 const char* separator);
146
153
154 PositionedID insertAirport(FGPositioned::Type ty, const std::string& ident,
155 const std::string& name,
156 const SGPath& sceneryPath);
157 void insertTower(PositionedID airportId, const SGGeod& pos);
158
159
160 PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident,
161 const SGGeod& pos, PositionedID apt,
162 double heading, double length, double width, double displacedThreshold,
163 double stopway, int markings, int approach, int tdz, int reil,
164 int surfaceCode, int shoulder_code, float smoothness, int center_lights,
165 int edge_lights, int distance_remaining);
166
167 PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident,
168 const SGGeod& pos, PositionedID apt,
169 double heading, double length, double width, double displacedThreshold,
170 double stopway, int surfaceCode);
171
173 void setRunwayILS(PositionedID runway, PositionedID ils);
174
175 PositionedID insertNavaid(FGPositioned::Type ty, const std::string& ident,
176 const std::string& name, const SGGeod& pos, int freq, int range, double multiuse,
177 PositionedID apt, PositionedID runway);
178
179 // Assign colocated DME to a navaid
180 void setNavaidColocated(PositionedID navaid, PositionedID colocatedDME);
181
183 const std::string& name, const SGGeod& pos, int freq, int range,
184 PositionedID apt);
185
186 PositionedID createPOI(FGPositioned::Type ty, const std::string& ident, const SGGeod& aPos, const std::string& aName, bool transient);
187
188 bool removePOI(FGPositionedRef wpt);
189
191 void setAirportMetar(const std::string& icao, bool hasMetar);
192
196 void updatePosition(PositionedID item, const SGGeod& pos);
197
198 FGPositionedList findAllWithIdent(const std::string& ident,
199 FGPositioned::Filter* filter,
200 bool exact);
201 FGPositionedList findAllWithName(const std::string& ident,
202 FGPositioned::Filter* filter,
203 bool exact);
204
205 FGPositionedRef findClosestWithIdent(const std::string& aIdent,
206 const SGGeod& aPos,
207 FGPositioned::Filter* aFilter);
208
209
215 char** searchAirportNamesAndIdents(const std::string& aFilter);
216
222 FGPositionedRef findCommByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
223
229 PositionedIDVec findCommsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
230
236
240 PositionedID airportItemWithIdent(PositionedID apt, FGPositioned::Type ty, const std::string& ident);
241
246 PositionedIDVec findNavaidsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
247
251
256
262 AirportRunwayPair findAirportRunway(const std::string& name);
263
268 PositionedID findILS(PositionedID airport, const std::string& runway, const std::string& navIdent);
269
275 int getOctreeBranchChildren(int64_t octreeNodeId);
276
278
282 TypedPositionedVec getOctreeLeafChildren(int64_t octreeNodeId);
283
284 // airways
285 int findAirway(int network, const std::string& aName, bool create);
286
287 int findAirway(const std::string& aName);
288
295 void insertEdge(int network, int airwayID, PositionedID from, PositionedID to);
296
298 bool isInAirwayNetwork(int network, PositionedID pos);
299
305
306 AirwayRef loadAirway(int airwayID);
307
312
314 {
315 public:
316 Transaction(NavDataCache* cache);
317 ~Transaction();
318
319 void commit();
320
321 private:
322 NavDataCache* _instance;
323 bool _committed;
324 };
325
326 bool isReadOnly() const;
327
329 {
330 public:
331 ThreadedGUISearch(const std::string& term, bool onlyAirports);
333
334 PositionedIDVec results() const;
335
336 bool isComplete() const;
337
338 private:
340 std::unique_ptr<ThreadedGUISearchPrivate> d;
341 };
342
344
346
347private:
348 NavDataCache();
349
350 friend class RebuildThread;
351
352 // A generic function for loading all navigation data files of the
353 // specified type (apt/fix/nav etc.) using the passed type-specific loader.
354 void loadDatFiles(DatFileType type,
355 std::function<void(const SceneryLocation&, std::size_t, std::size_t)> loader);
356
357 void doRebuild();
358
359 friend class Transaction;
360
361 void beginTransaction();
362 void commitTransaction();
363 void abortTransaction();
364
366 std::unique_ptr<NavDataCachePrivate> d;
367
368 bool rebuildInProgress = false;
369};
370
371} // namespace flightgear
SGSharedPtr< FGPositioned > FGPositionedRef
Definition airways.hxx:30
Predicate class to support custom filtering of FGPositioned queries Default implementation of this pa...
ThreadedGUISearch(const std::string &term, bool onlyAirports)
PositionedID findILS(PositionedID airport, const std::string &runway, const std::string &navIdent)
Given an airport, and runway and ILS identifier, find the corresponding cache entry.
PositionedID insertNavaid(FGPositioned::Type ty, const std::string &ident, const std::string &name, const SGGeod &pos, int freq, int range, double multiuse, PositionedID apt, PositionedID runway)
void stampCacheFile(const SGPath &path, const std::string &sha={})
void writeStringProperty(const std::string &key, const std::string &value)
PositionedID insertRunway(FGPositioned::Type ty, const std::string &ident, const SGGeod &pos, PositionedID apt, double heading, double length, double width, double displacedThreshold, double stopway, int markings, int approach, int tdz, int reil, int surfaceCode, int shoulder_code, float smoothness, int center_lights, int edge_lights, int distance_remaining)
PositionedID createPOI(FGPositioned::Type ty, const std::string &ident, const SGGeod &aPos, const std::string &aName, bool transient)
static const std::string defaultDatFile[]
PositionedID insertCommStation(FGPositioned::Type ty, const std::string &name, const SGGeod &pos, int freq, int range, PositionedID apt)
const DatFilesGroupInfo & getDatFilesInfo(DatFileType datFileType) const
void writeIntProperty(const std::string &key, int value)
std::string readStringProperty(const std::string &key)
void defineOctreeNode(Octree::Branch *pr, Octree::Node *nd)
int readIntProperty(const std::string &key)
void writeDoubleProperty(const std::string &key, const double &value)
static bool isAnotherProcessRebuilding()
string_list readStringListProperty(const std::string &key)
FGPositionedRef findCommByFreq(int freqKhz, const SGGeod &pos, FGPositioned::Filter *filt)
Find the closest matching comm-station on a frequency, to a position.
static NavDataCache * createInstance()
PositionedID createTransientID()
void writeOrderedStringListProperty(const std::string &key, const string_list &values, const char *separator)
int findAirway(const std::string &aName)
TypedPositionedVec getOctreeLeafChildren(int64_t octreeNodeId)
given an octree leaf, return all its child positioned items and their types
void setRunwayReciprocal(PositionedID runway, PositionedID recip)
unsigned int rebuildPhaseCompletionPercentage() const
PositionedIDVec findCommsByFreq(int freqKhz, const SGGeod &pos, FGPositioned::Filter *filt)
Find all on a frequency, sorted by distance from a position The filter with be used for both type ran...
PositionedID airportItemWithIdent(PositionedID apt, FGPositioned::Type ty, const std::string &ident)
find the first match item of the specified type and ident, at an airport
FGPositionedRef findClosestWithIdent(const std::string &aIdent, const SGGeod &aPos, FGPositioned::Filter *aFilter)
double readDoubleProperty(const std::string &key)
FGPositionedList findAllWithIdent(const std::string &ident, FGPositioned::Filter *filter, bool exact)
PositionedIDVec airwayWaypts(int id)
Waypoints on the airway.
void setAirportMetar(const std::string &icao, bool hasMetar)
update the metar flag associated with an airport
void writeStringListProperty(const std::string &key, const string_list &values)
static const std::string datTypeStr[]
PositionedID insertAirport(FGPositioned::Type ty, const std::string &ident, const std::string &name, const SGPath &sceneryPath)
int getOctreeBranchChildren(int64_t octreeNodeId)
Given an Octree node ID, return a bit-mask defining which of the child nodes exist.
PositionedID findNavaidForRunway(PositionedID runway, FGPositioned::Type ty)
Given a runway and type, find the corresponding navaid (ILS / GS / OM)
void updatePosition(PositionedID item, const SGGeod &pos)
Modify the position of an existing item.
PositionedID insertRunway(FGPositioned::Type ty, const std::string &ident, const SGGeod &pos, PositionedID apt, double heading, double length, double width, double displacedThreshold, double stopway, int surfaceCode)
AirwayRef loadAirway(int airwayID)
bool removePOI(FGPositionedRef wpt)
FGPositionedRef loadById(PositionedID guid)
retrieve an FGPositioned from the cache.
void setRunwayILS(PositionedID runway, PositionedID ils)
static NavDataCache * instance()
void insertTower(PositionedID airportId, const SGGeod &pos)
char ** searchAirportNamesAndIdents(const std::string &aFilter)
Helper to implement the AirportSearch widget.
bool isCachedFileModified(const SGPath &path) const
void insertEdge(int network, int airwayID, PositionedID from, PositionedID to)
insert an edge between two positioned nodes, into the network.
string_list readOrderedStringListProperty(const std::string &key, const char *separator)
bool isRebuildRequired()
predicate - check if the cache needs to be rebuilt.
RebuildPhase rebuild()
run the cache rebuild - returns the current phase or 'done'
FGPositionedList findAllWithName(const std::string &ident, FGPositioned::Filter *filter, bool exact)
PositionedIDVec findNavaidsByFreq(int freqKhz, const SGGeod &pos, FGPositioned::Filter *filt)
Find all navaids matching a particular frequency, sorted by range from the supplied position.
void setRebuildPhaseProgress(RebuildPhase ph, unsigned int percent=0)
bool isInAirwayNetwork(int network, PositionedID pos)
is the specified positioned a node on the network?
void setNavaidColocated(PositionedID navaid, PositionedID colocatedDME)
AirwayEdgeVec airwayEdgesFrom(int network, PositionedID pos)
retrive all the destination points reachable from a positioned in an airway
AirportRunwayPair findAirportRunway(const std::string &name)
given a navaid name (or similar) from apt.dat / nav.dat, find the corresponding airport and runway ID...
std::vector< SceneryLocation > SceneryLocationList
PositionedIDVec airportItemsOfType(PositionedID apt, FGPositioned::Type ty, FGPositioned::Type maxTy=FGPositioned::INVALID)
find all items of a specified type (or range of types) at an airport
int findAirway(int network, const std::string &aName, bool create)
Octree node base class, tracks its bounding box and provides various queries relating to it.
const char * name
std::vector< std::string > string_list
Definition globals.hxx:36
FlightPlan.hxx - defines a full flight-plan object, including departure, cruise, arrival information ...
Definition Addon.cxx:53
std::vector< AirwayEdge > AirwayEdgeVec
std::pair< FGPositioned::Type, PositionedID > TypedPositioned
SGSharedPtr< FGPositioned > FGPositionedRef
Definition airways.cxx:49
SGSharedPtr< Airway > AirwayRef
Definition airways.hxx:40
const char * name
std::pair< int, PositionedID > AirwayEdge
std::pair< PositionedID, PositionedID > AirportRunwayPair
a pair of airport ID, runway ID
std::vector< TypedPositioned > TypedPositionedVec
std::vector< PositionedID > PositionedIDVec
std::vector< FGPositionedRef > FGPositionedList
int64_t PositionedID