OpenCPN Partial API docs
Loading...
Searching...
No Matches
chartdbs.h
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2010 by David S. Register *
3 * Copyright (C) 2010 by Mark A Sikes *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
25#ifndef __CHARTDBS_H__
26#define __CHARTDBS_H__
27
28#include <map>
29#include <memory>
30#include <vector>
31#include <atomic>
32
33#include <wx/progdlg.h>
34#include <wx/thread.h>
35
36#include "model/ocpn_types.h"
37#include "bbox.h"
38#include "LLRegion.h"
39#include "chartdb_thread.h"
40
41class ChartGroupArray; // forward
42extern ChartGroupArray *g_pGroupArray;
43class ChartBase; // Circular if including chcanv.h in gui...
44
45// A small class used in an array to describe chart directories
47public:
48 wxString fullpath;
49 wxString magic_number;
50};
51
52WX_DECLARE_OBJARRAY(ChartDirInfo, ArrayOfCDI);
53WX_DECLARE_STRING_HASH_MAP(int, ChartCollisionsHashMap);
54
56
57static const int DB_VERSION_PREVIOUS = 17;
58static const int DB_VERSION_CURRENT = 18;
59
60class ChartDatabase;
61class ChartGroupArray;
62
64 int EntryOffset;
65 int ChartType;
66 int ChartFamily;
67 float LatMax;
68 float LatMin;
69 float LonMax;
70 float LonMin;
71
72 int Scale;
73 int edition_date;
74 int file_date;
75
76 int nPlyEntries;
77 int nAuxPlyEntries;
78
79 float skew;
80 int ProjectionType;
81 bool bValid;
82
83 int nNoCovrPlyEntries;
84};
85
87 int EntryOffset;
88 int ChartType;
89 float LatMax;
90 float LatMin;
91 float LonMax;
92 float LonMin;
93
94 int Scale;
95 int edition_date;
96 int file_date;
97
98 int nPlyEntries;
99 int nAuxPlyEntries;
100
101 float skew;
102 int ProjectionType;
103 bool bValid;
104
105 int nNoCovrPlyEntries;
106};
107
109 int EntryOffset;
110 int ChartType;
111 float LatMax;
112 float LatMin;
113 float LonMax;
114 float LonMin;
115
116 int Scale;
117 int edition_date;
118 int file_date;
119
120 int nPlyEntries;
121 int nAuxPlyEntries;
122
123 float skew;
124 int ProjectionType;
125 bool bValid;
126};
127
129 int EntryOffset;
130 int ChartType;
131 float LatMax;
132 float LatMin;
133 float LonMax;
134 float LonMin;
135
136 int Scale;
137 time_t edition_date;
138 time_t file_date;
139
140 int nPlyEntries;
141 int nAuxPlyEntries;
142
143 bool bValid;
144};
145
147 int EntryOffset;
148 int ChartType;
149 char ChartID[16];
150 float LatMax;
151 float LatMin;
152 float LonMax;
153 float LonMin;
154 char *pFullPath;
155 int Scale;
156 time_t edition_date;
157 float *pPlyTable;
158 int nPlyEntries;
159 int nAuxPlyEntries;
160 float **pAuxPlyTable;
161 int *pAuxCntTable;
162 bool bValid;
163};
164
167 ChartTableHeader(int dirEntries, int tableEntries)
168 : nTableEntries(tableEntries), nDirEntries(dirEntries) {}
169
170 void Read(wxInputStream &is);
171 void Write(wxOutputStream &os);
172 bool CheckValid();
173 int GetDirEntries() const { return nDirEntries; }
174 int GetTableEntries() const { return nTableEntries; }
175 char *GetDBVersionString() { return dbVersion; }
176
177private:
178 // NOTE: on-disk structure - cannot add, remove, or reorder!
179 char dbVersion[4];
180 int nTableEntries;
181 int nDirEntries;
182};
188 ChartTableEntry() { Clear(); }
189 ChartTableEntry(ChartBase &theChart, wxString &utf8Path);
191
192 bool IsEqualTo(const ChartTableEntry &cte) const;
193 bool IsEarlierThan(const ChartTableEntry &cte) const;
194 bool Read(const ChartDatabase *pDb, wxInputStream &is);
195 bool Write(const ChartDatabase *pDb, wxOutputStream &os);
196 void Clear();
197 void Disable();
198 void ReEnable();
199
200 void SetValid(bool valid) { bValid = valid; }
201 time_t GetFileTime() const { return file_date; }
202 time_t GetChartEditionDate() const { return edition_date; }
203
204 int GetnPlyEntries() const { return nPlyEntries; }
205 float *GetpPlyTable() const { return pPlyTable; }
206
207 int GetnAuxPlyEntries() const { return nAuxPlyEntries; }
208 float *GetpAuxPlyTableEntry(int index) const { return pAuxPlyTable[index]; }
209 int GetAuxCntTableEntry(int index) const { return pAuxCntTable[index]; }
210
211 int GetnNoCovrPlyEntries() const { return nNoCovrPlyEntries; }
212 float *GetpNoCovrPlyTableEntry(int index) const {
213 return pNoCovrPlyTable[index];
214 }
215 int GetNoCovrCntTableEntry(int index) const { return pNoCovrCntTable[index]; }
216
217 const LLBBox &GetBBox() const { return m_bbox; }
218
219 char *GetpFullPath() const { return pFullPath; }
220 float GetLonMax() const { return LonMax; }
221 float GetLonMin() const { return LonMin; }
222 float GetLatMax() const { return LatMax; }
223 float GetLatMin() const { return LatMin; }
224 int GetScale() const { return Scale; }
225 int GetChartType() const { return ChartType; }
226 int GetChartFamily() const { return ChartFamily; }
227 int GetChartProjectionType() const { return ProjectionType; }
228 float GetChartSkew() const { return Skew; }
229
230 bool GetbValid() { return bValid; }
231 void SetEntryOffset(int n) { EntryOffset = n; }
232 const wxString *GetpFileName(void) const { return m_pfilename; }
233 wxString *GetpsFullPath(void) const { return m_psFullPath; }
234 wxString GetFullSystemPath() const { return m_fullSystemPath; }
235 const std::string &GetFullPath() const { return m_FullPath; }
236
237 const std::vector<int> &GetGroupArray(void) const { return m_GroupArray; }
238 void ClearGroupArray(void) { m_GroupArray.clear(); }
239 void AddIntToGroupArray(int val) { m_GroupArray.push_back(val); }
240 void SetAvailable(bool avail) { m_bavail = avail; }
241
242 std::vector<float> GetReducedPlyPoints();
243 std::vector<float> GetReducedAuxPlyPoints(int iTable);
244
245 LLRegion quilt_candidate_region;
246
247 void SetScale(int scale);
248 bool Scale_eq(int b) const { return abs(Scale - b) <= rounding; }
249 bool Scale_ge(int b) const { return Scale_eq(b) || Scale > b; }
250 bool Scale_gt(int b) const { return Scale > b && !Scale_eq(b); }
251 bool IsBasemap() const;
252
253private:
254 int EntryOffset;
255 int ChartType;
256 int ChartFamily;
257 float LatMax;
258 float LatMin;
259 float LonMax;
260 float LonMin;
261 char *pFullPath;
262 int rounding;
263 int Scale;
264 time_t edition_date;
265 time_t file_date;
266 float *pPlyTable;
267 int nPlyEntries;
268 int nAuxPlyEntries;
269 float **pAuxPlyTable;
270 int *pAuxCntTable;
271 float Skew;
272 int ProjectionType;
273 bool bValid;
274 int nNoCovrPlyEntries;
275 int *pNoCovrCntTable;
276 float **pNoCovrPlyTable;
277
278 std::vector<int> m_GroupArray;
279 wxString *m_pfilename; // a helper member, not on disk
280 wxString *m_psFullPath;
281 wxString m_fullSystemPath;
282 std::string m_FullPath;
283
284 LLBBox m_bbox;
285 bool m_bavail;
286
287 std::vector<float> m_reducedPlyPoints;
288
289 std::vector<std::vector<float>> m_reducedAuxPlyPointsVector;
290};
291
293// Chart Database
295
296WX_DECLARE_OBJARRAY(ChartTableEntry, ChartTable);
297
298#define CTE_THREAD_MAX 1
299
304class ChartDatabase : public wxEvtHandler {
305public:
307 virtual ~ChartDatabase() {};
308
309 void OnEvtThread(OCPN_ChartTableEntryThreadEvent &event);
310 void OnAnyEvent(wxEvent &event);
311
317 bool Create(ArrayOfCDI &dir_array, wxGenericProgressDialog *pprog);
318
325 bool Update(ArrayOfCDI &dir_array, bool bForce,
326 wxGenericProgressDialog *pprog);
327 void FinalizeChartUpdate();
328
329 bool Read(const wxString &filePath);
330 bool Write(const wxString &filePath);
331
332 bool AddSingleChart(wxString &fullpath, bool b_force_full_search = true);
333 bool RemoveSingleChart(wxString &ChartFullPath);
334
335 const wxString &GetDBFileName() const { return m_DBFileName; }
336 ArrayOfCDI &GetChartDirArray() { return m_dir_array; }
337 wxArrayString &GetChartDirArrayString() { return m_chartDirs; }
338 void SetChartDirArray(ArrayOfCDI array) { m_dir_array = array; }
339 bool CompareChartDirArray(ArrayOfCDI &test_array);
340 wxString GetMagicNumberCached(wxString dir);
341
342 void UpdateChartClassDescriptorArray(void);
343 bool UpdateChartDatabaseInplace(ArrayOfCDI &DirArray, bool b_force,
344 wxGenericProgressDialog *_prog);
345
346 inline std::vector<std::shared_ptr<ChartTableEntry>> &GetChartTable() {
347 return active_chartTable;
348 }
349 int GetChartTableEntries() const { return active_chartTable.size(); }
350 ChartTableEntry &GetChartTableEntry(int index) const;
351
352 bool IsValid() const { return bValid; }
353 int DisableChart(wxString &PathToDisable);
354 bool GetCentroidOfLargestScaleChart(double *clat, double *clon,
355 ChartFamilyEnum family);
356 int GetDBChartType(int dbIndex);
357 int GetDBChartFamily(int dbIndex);
358 float GetDBChartSkew(int dbIndex);
359 int GetDBChartProj(int dbIndex);
360 int GetDBChartScale(int dbIndex);
361
362 bool GetDBBoundingBox(int dbindex, LLBBox &box);
363 const LLBBox &GetDBBoundingBox(int dbIndex);
364
365 int GetnAuxPlyEntries(int dbIndex);
366 int GetDBPlyPoint(int dbIndex, int plyindex, float *lat, float *lon);
367 int GetDBAuxPlyPoint(int dbIndex, int plyindex, int iAuxPly, float *lat,
368 float *lon);
369 int GetVersion() { return m_dbversion; }
370 wxString GetFullChartInfo(ChartBase *pc, int dbIndex, int *char_width,
371 int *line_count);
372 int FinddbIndex(wxString PathToFind);
373 wxString GetDBChartFileName(int dbIndex);
374 void ApplyGroupArray(ChartGroupArray *pGroupArray);
375 bool IsChartAvailable(int dbIndex);
376 std::map<wxString, int> active_chartTable_pathindex;
377
378 std::vector<float> GetReducedPlyPoints(int dbIndex);
379 std::vector<float> GetReducedAuxPlyPoints(int dbIndex, int iTable);
380
381 bool IsBusy() { return m_b_busy; }
382 void SetBusy(bool _busy) { m_b_busy = _busy; }
383 bool ScrubGroupArray();
384
385 ChartTableEntry *CreateChartTableEntry(const wxString &filePath,
386 wxString &utf8Path,
387 ChartClassDescriptor &chart_desc);
388
389 std::vector<std::shared_ptr<ChartTableEntry>> active_chartTable;
390 void OnDBSProgressUpdate(wxCommandEvent &evt);
391
392protected:
393 virtual ChartBase *GetChart(const wxChar *theFilePath,
394 ChartClassDescriptor &chart_desc) const;
395 int AddChartDirectory(const wxString &theDir, bool bshow_prog);
396 void SetValid(bool valid) { bValid = valid; }
397
398 std::vector<ChartClassDescriptor> m_ChartClassDescriptorArray;
399 ArrayOfCDI m_dir_array;
400 bool m_b_busy;
401
402private:
403 bool IsChartDirUsed(const wxString &theDir);
404
405 int SearchDirAndAddCharts(wxString &dir_name_base,
406 ChartClassDescriptor &chart_desc,
407 wxGenericProgressDialog *pprog);
408
409 int TraverseDirAndAddCharts(ChartDirInfo &dir_info,
410 wxGenericProgressDialog *pprog,
411 wxString &dir_magic, bool bForce);
412 bool DetectDirChange(const wxString &dir_path, const wxString &prog_label,
413 const wxString &magic, wxString &new_magic,
414 wxGenericProgressDialog *pprog);
415
416 bool AddChart(wxString &chartfilename, ChartClassDescriptor &chart_desc,
417 wxGenericProgressDialog *pprog, int isearch,
418 bool bthis_dir_in_dB);
419
420 bool Check_CM93_Structure(wxString dir_name);
421
422 bool bValid;
423 wxArrayString m_chartDirs;
424 int m_dbversion;
425
427 m_ChartTableEntryDummy; // used for return value if database is not valid
428 wxString m_DBFileName;
429
430 int m_pdifile;
431 int m_pdnFile;
432
433 int m_nentries;
434
435 LLBBox m_dummy_bbox;
436 std::atomic<int> m_jobsRemaining{0};
437 JobQueueCTE m_pool;
438 JobQueueCTE m_pool_deferred;
439 std::vector<std::shared_ptr<ChartTableEntryJobTicket>> m_ticket_vector;
440 std::vector<std::shared_ptr<ChartTableEntryJobTicket>>
441 m_deferred_ticket_vector;
442 ChartCollisionsHashMap m_full_collision_map;
443 int m_progcount;
444 int m_ticketcount;
445 wxGenericProgressDialog *m_pprog;
446 int m_progint;
447 int m_nFileProgressQuantum;
448 wxString m_gshhg_chart_loc;
449};
450
451//-------------------------------------------------------------------------------------------
452// Chart Group Structure Definitions
453//-------------------------------------------------------------------------------------------
455class ChartGroup;
456
457WX_DEFINE_ARRAY_PTR(ChartGroup *, ChartGroupArray);
458
466 // ChartGroupElements need nothing special to delete since
467 // m_missing_name_array is a wxArrayString which manages
468 // memory for the strings cleanly without need for a .Clear.
469public:
470 wxString m_element_name;
471 wxArrayString m_missing_name_array;
472};
473
486public:
487 wxString m_group_name;
488 std::vector<ChartGroupElement> m_element_array;
489};
490
491#endif
Define threaded chart database classes.
ChartGroupArray * g_pGroupArray
Global instance.
Definition chartdbs.cpp:61
Base class for all chart types.
Definition chartbase.h:126
Manages a database of charts, including reading, writing, and querying chart information.
Definition chartdbs.h:304
bool Create(ArrayOfCDI &dir_array, wxGenericProgressDialog *pprog)
Creates a new chart database from a list of directories.
bool Update(ArrayOfCDI &dir_array, bool bForce, wxGenericProgressDialog *pprog)
Updates the chart database.
Represents an individual component within a ChartGroup.
Definition chartdbs.h:465
Represents a user-defined collection of logically related charts.
Definition chartdbs.h:485
Navigation data types.
Represents an entry in the chart table, containing information about a single chart.
Definition chartdbs.h:187