OpenCPN Partial API docs
Loading...
Searching...
No Matches
o_senc.h
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2015 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#ifndef OSENC_H
25#define OSENC_H
26
27#include <string.h>
28#include <stdint.h>
29#include <vector>
30#include <mutex>
31#include <unordered_map>
32
33// For compilers that support precompilation, includes "wx.h".
34#include "wx/wxprec.h"
35
36#ifndef WX_PRECOMP
37#include <wx/wx.h>
38#endif // precompiled headers
39
40#include <wx/filename.h>
41#include <wx/progdlg.h>
42#include <wx/string.h>
43#include <wx/wfstream.h>
44
45#include "gdal/cpl_csv.h"
46
47#include "chartbase.h"
48#include "mygeom.h"
49#include "ogr_s57.h"
50#include "s52s57.h"
51
52// Various error return enums
53#define SENC_NO_ERROR 0
54#define ERROR_SENCFILE_NOT_FOUND 1
55#define ERROR_SENC_VERSION_MISMATCH 2
56#define ERROR_CANNOT_CREATE_SENC_DIR 3
57#define ERROR_CANNOT_CREATE_TEMP_SENC_FILE 4
58#define ERROR_INGESTING000 5
59#define ERROR_REGISTRAR_NOT_SET 6
60#define ERROR_BASEFILE_ATTRIBUTES 7
61#define ERROR_SENCFILE_ABORT 8
62
63// OSENC V2 record definitions
64#define HEADER_SENC_VERSION 1
65#define HEADER_CELL_NAME 2
66#define HEADER_CELL_PUBLISHDATE 3
67#define HEADER_CELL_EDITION 4
68#define HEADER_CELL_UPDATEDATE 5
69#define HEADER_CELL_UPDATE 6
70#define HEADER_CELL_NATIVESCALE 7
71#define HEADER_CELL_SENCCREATEDATE 8
72
73#define FEATURE_ID_RECORD 64
74#define FEATURE_ATTRIBUTE_RECORD 65
75
76#define FEATURE_GEOMETRY_RECORD_POINT 80
77#define FEATURE_GEOMETRY_RECORD_LINE 81
78#define FEATURE_GEOMETRY_RECORD_AREA 82
79#define FEATURE_GEOMETRY_RECORD_MULTIPOINT 83
80
81#define VECTOR_EDGE_NODE_TABLE_RECORD 96
82#define VECTOR_CONNECTED_NODE_TABLE_RECORD 97
83
84#define CELL_COVR_RECORD 98
85#define CELL_NOCOVR_RECORD 99
86#define CELL_EXTENT_RECORD 100
87
88#define ATTRIBUTE_ID_PRIM 50000
89
90WX_DEFINE_ARRAY_PTR(float *, SENCFloatPtrArray);
91
92//--------------------------------------------------------------------------
93// Utility Structures
94//--------------------------------------------------------------------------
95#pragma pack(push, 1)
96
97typedef struct _OSENC_Record_Base {
98 uint16_t record_type;
99 uint32_t record_length;
101
102typedef struct _OSENC_Record {
103 uint16_t record_type;
104 uint32_t record_length;
105 unsigned char payload;
107
109 uint16_t record_type;
110 uint32_t record_length;
111 uint16_t feature_type_code;
112 uint16_t feature_ID;
113 uint8_t feature_primitive;
115
117 uint16_t feature_type_code;
118 uint16_t feature_ID;
119 uint8_t feature_primitive;
121
123 uint16_t record_type;
124 uint32_t record_length;
125 uint16_t attribute_type;
126 unsigned char attribute_value_type;
128
130 uint16_t record_type;
131 uint32_t record_length;
132 uint16_t attribute_type;
133 unsigned char attribute_value_type;
134 void *payload;
136
138 uint16_t attribute_type_code;
139 unsigned char attribute_value_type;
140
141 union {
142 uint32_t attribute_value_int;
143 double attribute_value_double;
144 char *attribute_value_char_ptr;
145 };
147
149 uint16_t record_type;
150 uint32_t record_length;
151 double lat;
152 double lon;
154
156 double lat;
157 double lon;
159
161 uint16_t record_type;
162 uint32_t record_length;
163 double extent_s_lat;
164 double extent_n_lat;
165 double extent_w_lon;
166 double extent_e_lon;
167 uint32_t point_count;
169
171 double extent_s_lat;
172 double extent_n_lat;
173 double extent_w_lon;
174 double extent_e_lon;
175 uint32_t point_count;
176 void *payLoad;
178
180 uint16_t record_type;
181 uint32_t record_length;
182 double extent_s_lat;
183 double extent_n_lat;
184 double extent_w_lon;
185 double extent_e_lon;
186 uint32_t edgeVector_count;
188
190 double extent_s_lat;
191 double extent_n_lat;
192 double extent_w_lon;
193 double extent_e_lon;
194 uint32_t edgeVector_count;
195 void *payLoad;
197
199 uint16_t record_type;
200 uint32_t record_length;
201 double extent_s_lat;
202 double extent_n_lat;
203 double extent_w_lon;
204 double extent_e_lon;
205 uint32_t contour_count;
206 uint32_t triprim_count;
207 uint32_t edgeVector_count;
209
211 double extent_s_lat;
212 double extent_n_lat;
213 double extent_w_lon;
214 double extent_e_lon;
215 uint32_t contour_count;
216 uint32_t triprim_count;
217 uint32_t edgeVector_count;
218 void *payLoad;
220
221typedef struct _OSENC_VET_Record {
222 uint16_t record_type;
223 uint32_t record_length;
224 unsigned char payload;
226
228 uint16_t record_type;
229 uint32_t record_length;
231
232typedef struct _OSENC_VCT_Record {
233 uint16_t record_type;
234 uint32_t record_length;
235 unsigned char payload;
237
239 uint16_t record_type;
240 uint32_t record_length;
242
243typedef struct _OSENC_COVR_Record {
244 uint16_t record_type;
245 uint32_t record_length;
246 unsigned char payload;
248
250 uint16_t record_type;
251 uint32_t record_length;
253
255 uint32_t point_count;
256 float point_array;
258
259typedef struct _OSENC_NOCOVR_Record {
260 uint16_t record_type;
261 uint32_t record_length;
262 unsigned char payload;
264
266 uint16_t record_type;
267 uint32_t record_length;
269
271 uint32_t point_count;
272 float point_array;
274
275typedef struct _OSENC_EXTENT_Record {
276 uint16_t record_type;
277 uint32_t record_length;
278 double extent_sw_lat;
279 double extent_sw_lon;
280 double extent_nw_lat;
281 double extent_nw_lon;
282 double extent_ne_lat;
283 double extent_ne_lon;
284 double extent_se_lat;
285 double extent_se_lon;
287
289 double extent_sw_lat;
290 double extent_sw_lon;
291 double extent_nw_lat;
292 double extent_nw_lon;
293 double extent_ne_lat;
294 double extent_ne_lon;
295 double extent_se_lat;
296 double extent_se_lon;
298
299#pragma pack(pop)
300
301typedef std::vector<S57Obj *> S57ObjVector;
302typedef std::vector<VE_Element *> VE_ElementVector;
303typedef std::vector<VC_Element *> VC_ElementVector;
304
305class s57RegistrarMgr; // forward
306extern s57RegistrarMgr *m_pRegistrarMan;
308const char *MyCSVGetField(const char *pszFilename, const char *pszKeyFieldName,
309 const char *pszKeyFieldValue,
310 CSVCompareCriteria eCriteria,
311 const char *pszTargetField);
312
313//--------------------------------------------------------------------------
314// Osenc_instream definition
315//--------------------------------------------------------------------------
317public:
318 Osenc_instream() {};
319 virtual ~Osenc_instream() {};
320
321 virtual bool Open(const wxString &senc_file_name) = 0;
322 virtual void Close() = 0;
323
324 virtual Osenc_instream &Read(void *buffer, size_t size) = 0;
325 virtual bool IsOk() = 0;
326 virtual bool isAvailable() = 0;
327 virtual void Shutdown() = 0;
328};
329
330//--------------------------------------------------------------------------
331// Osenc_instreamFile definition
332// A simple file stream implementation based on wxFFileInputStream
333//--------------------------------------------------------------------------
335public:
338
339 bool Open(const wxString &senc_file_name);
340 void Close();
341
342 Osenc_instream &Read(void *buffer, size_t size);
343 bool IsOk();
344 bool isAvailable();
345 void Shutdown();
346
347private:
348 void Init();
349
350 wxFFileInputStream *m_instream;
351 bool m_ok;
352};
353
354//--------------------------------------------------------------------------
355// Osenc_outstream definition
356//--------------------------------------------------------------------------
358public:
359 Osenc_outstream() {};
360 virtual ~Osenc_outstream() {};
361
362 virtual bool Open(const wxString &ofileName) = 0;
363
364 virtual Osenc_outstream &Write(const void *buffer, size_t size) = 0;
365 virtual void Close() = 0;
366 virtual bool IsOk() = 0;
367};
368
369//--------------------------------------------------------------------------
370// Osenc_outstreamFile definition
371// A simple file stream implementation based on wxFFileInputStream
372//--------------------------------------------------------------------------
374public:
377
378 bool Open(const wxString &ofileName);
379
380 Osenc_outstream &Write(const void *buffer, size_t size);
381 void Close();
382 bool IsOk();
383
384private:
385 void Init();
386
387 wxFFileOutputStream *m_outstream;
388 bool m_ok;
389};
390
391//--------------------------------------------------------------------------
392// Osenc definition
393//--------------------------------------------------------------------------
394
395class Osenc {
396public:
397 Osenc();
398 ~Osenc();
399
400 wxString getLastError() { return errorMessage; }
401 void setVerbose(bool verbose);
402 void setNoErrDialog(bool val) { m_NoErrDialog = val; }
403
404 int ingestHeader(const wxString &senc_file_name);
405 int ingest(const wxString &senc_file_name, S57ObjVector *pObjectVector,
406 VE_ElementVector *pVEArray, VC_ElementVector *pVCArray);
407
408 int ingest200(const wxString &senc_file_name, S57ObjVector *pObjectVector,
409 VE_ElementVector *pVEArray, VC_ElementVector *pVCArray);
410
411 // SENC creation, by Version desired...
412 void SetLODMeters(double meters) { m_LOD_meters = meters; }
413 void setRegistrar(S57ClassRegistrar *registrar) { m_poRegistrar = registrar; }
414 void setRefLocn(double lat, double lon) {
415 m_ref_lat = lat;
416 m_ref_lon = lon;
417 }
418 void setOutstream(Osenc_outstream *stream) { m_pauxOutstream = stream; }
419 void setInstream(Osenc_instream *stream) { m_pauxInstream = stream; }
420
421 wxString getUpdateDate() { return m_LastUpdateDate; }
422 wxString getBaseDate() { return m_sdate000; }
423
424 wxString getSENCFileCreateDate() { return m_readFileCreateDate; }
425
426 int getSencReadVersion() { return m_senc_file_read_version; }
427 wxString getSENCReadBaseEdition() { return m_read_base_edtn; }
428 int getSENCReadLastUpdate() { return m_read_last_applied_update; }
429 int getSENCReadScale() { return m_Chart_Scale; }
430 wxString getReadName() { return m_Name; }
431 wxString getReadID() { return m_ID; }
432 Extent &getReadExtent() { return m_extent; }
433
434 SENCFloatPtrArray &getSENCReadAuxPointArray() { return m_AuxPtrArray; }
435 std::vector<int> &getSENCReadAuxPointCountArray() { return m_AuxCntArray; }
436 SENCFloatPtrArray &getSENCReadNOCOVRPointArray() { return m_NoCovrPtrArray; }
437 std::vector<int> &getSENCReadNOCOVRPointCountArray() {
438 return m_NoCovrCntArray;
439 }
440
441 int createSenc200(const wxString &FullPath000, const wxString &SENCFileName,
442 bool b_showProg = true);
443
444 void CreateSENCVectorEdgeTableRecord200(Osenc_outstream *stream,
445 S57Reader *poReader);
446 void CreateSENCVectorConnectedTableRecord200(Osenc_outstream *stream,
447 S57Reader *poReader);
448
449 void InitializePersistentBuffer(void);
450 unsigned char *getBuffer(size_t length);
451
452 int getNativeScale() { return m_native_scale; }
453 int GetBaseFileInfo(const wxString &FullPath000,
454 const wxString &SENCFileName);
455
456 std::unique_lock<std::mutex> lockCR;
457
458private:
459 void init();
460
461 int ingestCell(OGRS57DataSource *poS57DS, const wxString &FullPath000,
462 const wxString &working_dir);
463 int ValidateAndCountUpdates(const wxFileName file000, const wxString CopyDir,
464 wxString &LastUpdateDate, bool b_copyfiles);
465 int GetUpdateFileArray(const wxFileName file000, wxArrayString *UpFiles);
466 bool GetBaseFileAttr(const wxString &FullPath000);
467 unsigned char *getObjectVectorIndexTable(S57Reader *poReader,
468 OGRFeature *poFeature,
469 int &nEntries);
470
471 OGRFeature *GetChartFirstM_COVR(int &catcov, S57Reader *pENCReader,
472 S57ClassRegistrar *poRegistrar);
473 OGRFeature *GetChartNextM_COVR(int &catcov, S57Reader *pENCReader);
474 bool CreateCOVRTables(S57Reader *pENCReader, S57ClassRegistrar *poRegistrar);
475 bool CreateCovrRecords(Osenc_outstream *stream);
476
477 void CreateSENCRecord124(OGRFeature *pFeature, Osenc_outstream *stream,
478 int mode, S57Reader *poReader);
479 void CreateSENCVectorEdgeTable(Osenc_outstream *stream, S57Reader *poReader);
480 void CreateSENCConnNodeTable(Osenc_outstream *stream, S57Reader *poReader);
481
482 bool CreateSENCRecord200(OGRFeature *pFeature, Osenc_outstream *stream,
483 int mode, S57Reader *poReader);
484 bool WriteFIDRecord200(Osenc_outstream *stream, int nOBJL, int featureID,
485 int prim);
486 bool WriteHeaderRecord200(Osenc_outstream *stream, int recordType,
487 std::string payload);
488 bool WriteHeaderRecord200(Osenc_outstream *stream, int recordType,
489 uint16_t value);
490 bool WriteHeaderRecord200(Osenc_outstream *stream, int recordType,
491 uint32_t value);
492 bool CreateAreaFeatureGeometryRecord200(S57Reader *poReader,
493 OGRFeature *pFeature,
494 Osenc_outstream *stream);
495 bool CreateLineFeatureGeometryRecord200(S57Reader *poReader,
496 OGRFeature *pFeature,
497 Osenc_outstream *stream);
498 bool CreateMultiPointFeatureGeometryRecord200(OGRFeature *pFeature,
499 Osenc_outstream *stream);
500
501 std::string GetFeatureAcronymFromTypecode(int typeCode);
502 std::string GetAttributeAcronymFromTypecode(int typeCode);
503
504 PolyTessGeo *BuildPolyTessGeo(_OSENC_AreaGeometry_Record_Payload *record,
505 unsigned char **bytes_consumed);
506 bool CalculateExtent(S57Reader *poReader, S57ClassRegistrar *poRegistrar);
507
508 wxString errorMessage;
509
510 wxString m_Name;
511 wxString m_ID;
512 wxString m_FullPath000;
513
514 int m_Chart_Scale;
515 int m_senc_file_read_version;
516 int m_senc_file_create_version;
517 wxString m_read_base_edtn;
518 int m_read_last_applied_update;
519
520 S57Reader *poReader;
521
522 wxDateTime m_date000;
523 wxString m_sdate000;
524
525 wxString m_edtn000;
526 int m_UPDN;
527
528 int m_nGeoRecords;
529 int m_last_applied_update;
530 wxString m_LastUpdateDate;
531 int m_native_scale;
532 wxString m_readFileCreateDate;
533
534 double m_ref_lat,
535 m_ref_lon; // Common reference point, derived from FullExtent
536 std::unordered_map<int, int> m_vector_helper_hash;
537 double m_LOD_meters;
538 S57ClassRegistrar *m_poRegistrar;
539 wxArrayString m_tmpup_array;
540
541 wxGenericProgressDialog *m_ProgDialog;
542
543 unsigned char *pBuffer;
544 size_t bufferSize;
545
546 Extent m_extent;
547
548 // Clone of Chartbase structures of the same name and purpose
549 // Use mainly for SENC creation for ENC(.000) file
550 int m_nCOVREntries; // number of coverage table entries
551 int *m_pCOVRTablePoints; // int table of number of points in each coverage
552 // table entry
553 float **m_pCOVRTable; // table of pointers to list of floats describing valid
554 // COVR
555
556 int m_nNoCOVREntries; // number of NoCoverage table entries
557 int *m_pNoCOVRTablePoints; // int table of number of points in each
558 // NoCoverage table entry
559 float **m_pNoCOVRTable; // table of pointers to list of floats describing
560 // valid NOCOVR
561
562 // Arrays used to accumulate coverage regions on oSENC load
563 SENCFloatPtrArray m_AuxPtrArray;
564 std::vector<int> m_AuxCntArray;
565 SENCFloatPtrArray m_NoCovrPtrArray;
566 std::vector<int> m_NoCovrCntArray;
567
568 Osenc_outstream *m_pauxOutstream;
569 Osenc_instream *m_pauxInstream;
570
571 Osenc_outstream *m_pOutstream;
572 Osenc_instream *m_pInstream;
573
574 bool m_bVerbose;
575 wxArrayString *m_UpFiles;
576 bool m_bPrivateRegistrar;
577 bool m_NoErrDialog;
578};
579
580#endif // Guard
General chart base definitions.
s57RegistrarMgr * m_pRegistrarMan
Global instance.
Definition o_senc.cpp:62