OpenCPN Partial API docs
Loading...
Searching...
No Matches
Osenc.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: S57 SENC File Object
5 * Author: David Register
6 *
7 ***************************************************************************
8 * Copyright (C) 2015 by David S. Register *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU 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 *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 **************************************************************************/
25
26// For compilers that support precompilation, includes "wx.h".
27#include "wx/wxprec.h"
28
29#ifndef WX_PRECOMP
30#include "wx/wx.h"
31#endif // precompiled headers
32
33#include <setjmp.h>
34
35#include <wx/wfstream.h>
36#include <wx/filename.h>
37#include <wx/progdlg.h>
38
39#include "Osenc.h"
40#include "s52s57.h"
41#include "s57chart.h" // for one static method
42#include "model/cutil.h"
43#include "s57registrar_mgr.h"
44#include "gdal/cpl_csv.h"
45#include "ogr_s57.h"
46#include "gdal/cpl_string.h"
47#include "LOD_reduce.h"
48
49#include "mygeom.h"
50#include "model/georef.h"
51#include "gui_lib.h"
52#include <mutex>
53
54extern s57RegistrarMgr *m_pRegistrarMan;
55extern wxString g_csv_locn;
56extern bool g_bGDAL_Debug;
57
58bool chain_broken_mssage_shown = false;
59
60using namespace std;
61
62#include <wx/arrimpl.cpp>
63WX_DEFINE_ARRAY(float *, MyFloatPtrArray);
64
65#define MAX_VECTOR_POINTS 1000
66
67#ifndef __WXMSW__
68sigjmp_buf env_osenc_ogrf; // the context saved by sigsetjmp();
69#endif
70
71std::mutex m;
72
73/************************************************************************/
74/* OpenCPN_OGRErrorHandler() */
75/* Use Global wxLog Class */
76/************************************************************************/
77bool g_OsencVerbose;
78
79void OpenCPN_OGR_OSENC_ErrorHandler(CPLErr eErrClass, int nError,
80 const char *pszErrorMsg) {
81#define ERR_BUF_LEN 2000
82
83 char buf[ERR_BUF_LEN + 1];
84
85 if (eErrClass == CE_Debug) {
86 if (g_OsencVerbose) sprintf(buf, " %s", pszErrorMsg);
87 } else if (eErrClass == CE_Warning)
88 sprintf(buf, " Warning %d: %s\n", nError, pszErrorMsg);
89 else
90 sprintf(buf, " ERROR %d: %s\n", nError, pszErrorMsg);
91
92 if (g_bGDAL_Debug || (CE_Debug != eErrClass)) { // log every warning or error
93 wxString msg(buf, wxConvUTF8);
94 wxLogMessage(msg);
95 }
96
97 // Do not simply return on CE_Fatal errors, as we don't want to abort()
98
99#ifndef __WXMSW__
100 if (eErrClass == CE_Fatal) {
101 longjmp(env_osenc_ogrf, 1); // jump back to the setjmp() point
102 }
103#endif
104}
105
106//--------------------------------------------------------------------------
107// Osenc_instreamFile implementation
108// A simple file stream implementation based on wxFFileInputStream
109//--------------------------------------------------------------------------
110Osenc_instreamFile::Osenc_instreamFile() { Init(); }
111
112Osenc_instreamFile::~Osenc_instreamFile() { delete m_instream; }
113
114bool Osenc_instreamFile::Open(const wxString &senc_file_name) {
115 m_instream = new wxFFileInputStream(senc_file_name);
116 return m_instream->IsOk();
117}
118
119void Osenc_instreamFile::Close() {}
120
121Osenc_instream &Osenc_instreamFile::Read(void *buffer, size_t size) {
122 if (m_instream) m_ok = m_instream->Read(buffer, size).IsOk();
123
124 return *this;
125}
126
127bool Osenc_instreamFile::IsOk() {
128 if (m_instream) m_ok = m_instream->IsOk();
129
130 return m_ok;
131}
132
133bool Osenc_instreamFile::isAvailable() { return true; }
134
135void Osenc_instreamFile::Shutdown() {}
136
137void Osenc_instreamFile::Init() {
138 m_instream = NULL;
139 m_ok = false;
140}
141
142//--------------------------------------------------------------------------
143// Osenc_outstreamFile implementation
144// A simple file stream implementation based on wxFFileOutStream
145//--------------------------------------------------------------------------
146Osenc_outstreamFile::Osenc_outstreamFile() { Init(); }
147
148Osenc_outstreamFile::~Osenc_outstreamFile() { delete m_outstream; }
149
150bool Osenc_outstreamFile::Open(const wxString &file) {
151 Init();
152 m_outstream = new wxFFileOutputStream(file);
153 if (m_outstream) m_ok = m_outstream->IsOk();
154
155 return m_ok;
156}
157
158void Osenc_outstreamFile::Close() {
159 if (m_outstream) m_ok = m_outstream->Close();
160}
161
162Osenc_outstream &Osenc_outstreamFile::Write(const void *buffer, size_t size) {
163 if (m_outstream) m_ok = m_outstream->Write(buffer, size).IsOk();
164
165 return *this;
166}
167
168bool Osenc_outstreamFile::IsOk() {
169 if (m_outstream) m_ok = m_outstream->IsOk();
170
171 return m_ok;
172}
173
174void Osenc_outstreamFile::Init() {
175 m_outstream = NULL;
176 m_ok = false;
177}
178
179//--------------------------------------------------------------------------
180// Osenc implementation
181//--------------------------------------------------------------------------
182
183Osenc::Osenc() { init(); }
184
185Osenc::~Osenc() {
186 if (m_bPrivateRegistrar) delete m_poRegistrar;
187
188 // Free the coverage arrays, if they exist
189 SENCFloatPtrArray &AuxPtrArray = getSENCReadAuxPointArray();
190 std::vector<int> &AuxCntArray = getSENCReadAuxPointCountArray();
191 int nCOVREntries = AuxCntArray.size();
192 for (unsigned int j = 0; j < (unsigned int)nCOVREntries; j++) {
193 free(AuxPtrArray[j]);
194 }
195
196 SENCFloatPtrArray &AuxNoPtrArray = getSENCReadNOCOVRPointArray();
197 std::vector<int> &AuxNoCntArray = getSENCReadNOCOVRPointCountArray();
198 int nNoCOVREntries = AuxNoCntArray.size();
199 for (unsigned int j = 0; j < (unsigned int)nNoCOVREntries; j++) {
200 free(AuxNoPtrArray[j]);
201 }
202
203 free(pBuffer);
204
205 for (unsigned int j = 0; j < (unsigned int)m_nNoCOVREntries; j++)
206 free(m_pNoCOVRTable[j]);
207
208 for (unsigned int j = 0; j < (unsigned int)m_nCOVREntries; j++)
209 free(m_pCOVRTable[j]);
210
211 free(m_pCOVRTablePoints);
212 free(m_pCOVRTable);
213 free(m_pNoCOVRTablePoints);
214 free(m_pNoCOVRTable);
215 delete m_UpFiles;
216 CPLPopErrorHandler();
217}
218
219void Osenc::init(void) {
220 m_LOD_meters = 0;
221 m_poRegistrar = NULL;
222 m_bPrivateRegistrar = false;
223 m_senc_file_read_version = 0;
224 m_ProgDialog = NULL;
225 InitializePersistentBuffer();
226
227 m_ref_lat = 0;
228 m_ref_lon = 0;
229
230 m_read_base_edtn = "-1";
231
232 m_nNoCOVREntries = 0;
233 m_nCOVREntries = 0;
234 m_pCOVRTablePoints = NULL;
235 m_pCOVRTable = NULL;
236 m_pNoCOVRTablePoints = NULL;
237 m_pNoCOVRTable = NULL;
238
239 m_pauxOutstream = NULL;
240 m_pauxInstream = NULL;
241 m_pOutstream = NULL;
242 m_pInstream = NULL;
243 m_UpFiles = nullptr;
244
245 m_bVerbose = true;
246 g_OsencVerbose = true;
247 m_NoErrDialog = false;
248
249 // Insert my local error handler to catch OGR errors,
250 // Especially CE_Fatal type errors
251 // Discovered/debugged on US5MD11M.017. VI 548 geometry deleted
252 CPLPushErrorHandler(OpenCPN_OGR_OSENC_ErrorHandler);
253
254 lockCR = std::unique_lock<std::mutex>(m, std::defer_lock);
255}
256
257void Osenc::setVerbose(bool verbose) {
258 m_bVerbose = verbose;
259 g_OsencVerbose = verbose;
260}
261
262int Osenc::ingestHeader(const wxString &senc_file_name) {
263 // Read oSENC header records, stopping at the first Feature_ID record
264 // Then check to see if everything is defined as required.
265
266 int ret_val = SENC_NO_ERROR; // default is OK
267
268 wxFileName fn(senc_file_name);
269
270 // Sanity check for existence of file
271
272 // int nProg = 0;
273
274 // wxString ifs( senc_file_name );
275 //
276 // wxFFileInputStream fpx_u( ifs );
277 // if (!fpx_u.IsOk()) {
278 // return ERROR_SENCFILE_NOT_FOUND;
279 // }
280 // wxBufferedInputStream fpx( fpx_u );
281
282 // Sanity check for existence of file
284 fpx.Open(senc_file_name);
285 if (!fpx.IsOk()) return ERROR_SENCFILE_NOT_FOUND;
286
287 // For identification purposes, the very first record must be the OSENC
288 // Version Number Record
289 OSENC_Record_Base record;
290
291 fpx.Read(&record, sizeof(OSENC_Record_Base));
292 if (!fpx.IsOk()) {
293 return ERROR_SENCFILE_NOT_FOUND;
294 }
295
296 // Check Record
297 if (HEADER_SENC_VERSION != record.record_type) {
298 return ERROR_SENCFILE_NOT_FOUND;
299 }
300
301 // This is the correct record type (OSENC Version Number Record), so read it
302 unsigned char *buf =
303 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
304 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base)).IsOk()) {
305 return ERROR_SENCFILE_NOT_FOUND;
306 }
307 uint16_t *pint = (uint16_t *)buf;
308 m_senc_file_read_version = *pint;
309
310 // Read the rest of the records in the header
311 int dun = 0;
312
313 while (!dun) {
314 // Read a record Header
315 OSENC_Record_Base record;
316
317 // off = fpx.TellI();
318
319 fpx.Read(&record, sizeof(OSENC_Record_Base));
320 if (!fpx.IsOk()) {
321 dun = 1;
322 break;
323 }
324
325 // Process Records
326 switch (record.record_type) {
327 case HEADER_SENC_VERSION: {
328 unsigned char *buf =
329 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
330 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
331 .IsOk()) {
332 dun = 1;
333 break;
334 }
335 uint16_t *pint = (uint16_t *)buf;
336 m_senc_file_read_version = *pint;
337 break;
338 }
339 case HEADER_CELL_NAME: {
340 unsigned char *buf =
341 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
342 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
343 .IsOk()) {
344 dun = 1;
345 break;
346 }
347 m_Name = wxString(buf, wxConvUTF8);
348 break;
349 }
350 case HEADER_CELL_PUBLISHDATE: {
351 unsigned char *buf =
352 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
353 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
354 .IsOk()) {
355 dun = 1;
356 break;
357 }
358 break;
359 }
360
361 case HEADER_CELL_EDITION: {
362 unsigned char *buf =
363 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
364 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
365 .IsOk()) {
366 dun = 1;
367 break;
368 }
369 uint16_t *pint = (uint16_t *)buf;
370 m_read_base_edtn.Printf("%d", *pint);
371 break;
372 }
373
374 case HEADER_CELL_UPDATEDATE: {
375 unsigned char *buf =
376 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
377 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
378 .IsOk()) {
379 dun = 1;
380 break;
381 }
382 break;
383 }
384
385 case HEADER_CELL_UPDATE: {
386 unsigned char *buf =
387 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
388 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
389 .IsOk()) {
390 dun = 1;
391 break;
392 }
393
394 uint16_t *pint = (uint16_t *)buf;
395 m_read_last_applied_update = *pint;
396 break;
397 }
398
399 case HEADER_CELL_NATIVESCALE: {
400 unsigned char *buf =
401 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
402 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
403 .IsOk()) {
404 dun = 1;
405 break;
406 }
407 uint32_t *pint = (uint32_t *)buf;
408 m_Chart_Scale = *pint;
409
410 break;
411 }
412
413 case HEADER_CELL_SENCCREATEDATE: {
414 unsigned char *buf =
415 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
416 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
417 .IsOk()) {
418 dun = 1;
419 break;
420 }
421 m_readFileCreateDate = wxString(buf, wxConvUTF8);
422
423 break;
424 }
425
426 case CELL_EXTENT_RECORD: {
427 unsigned char *buf =
428 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
429 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
430 .IsOk()) {
431 dun = 1;
432 break;
433 }
436 m_extent.NLAT = pPayload->extent_nw_lat;
437 m_extent.SLAT = pPayload->extent_se_lat;
438 m_extent.WLON = pPayload->extent_nw_lon;
439 m_extent.ELON = pPayload->extent_se_lon;
440
441 break;
442 }
443
444 case CELL_COVR_RECORD: {
445 unsigned char *buf =
446 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
447 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
448 .IsOk()) {
449 dun = 1;
450 break;
451 }
452
455
456 int point_count = pPayload->point_count;
457 m_AuxCntArray.push_back(point_count);
458
459 float *pf = (float *)malloc(point_count * 2 * sizeof(float));
460 memcpy(pf, &pPayload->point_array, point_count * 2 * sizeof(float));
461 m_AuxPtrArray.Add(pf);
462
463 break;
464 }
465
466 case CELL_NOCOVR_RECORD: {
467 unsigned char *buf =
468 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
469 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
470 .IsOk()) {
471 dun = 1;
472 break;
473 }
474
477
478 int point_count = pPayload->point_count;
479 m_NoCovrCntArray.push_back(point_count);
480
481 float *pf = (float *)malloc(point_count * 2 * sizeof(float));
482 memcpy(pf, &pPayload->point_array, point_count * 2 * sizeof(float));
483 m_NoCovrPtrArray.Add(pf);
484
485 break;
486 }
487
488 case FEATURE_ID_RECORD: {
489 dun = 1;
490 break;
491 }
492
493 default: {
494 dun = 1;
495 break;
496 }
497
498 } // switch
499
500 } // while
501
502 return ret_val;
503}
504
505std::string Osenc::GetFeatureAcronymFromTypecode(int typeCode) {
506 if (m_pRegistrarMan) {
507 std::string acronym = m_pRegistrarMan->getFeatureAcronym(typeCode);
508 return acronym.c_str();
509 } else
510 return "";
511}
512
513std::string Osenc::GetAttributeAcronymFromTypecode(int typeCode) {
514 if (m_pRegistrarMan)
515 return m_pRegistrarMan->getAttributeAcronym(typeCode);
516 else
517 return "";
518}
519
520int Osenc::ingest200(const wxString &senc_file_name,
521 S57ObjVector *pObjectVector, VE_ElementVector *pVEArray,
522 VC_ElementVector *pVCArray) {
523 int ret_val = SENC_NO_ERROR; // default is OK
524
525 // wxFileName fn(senc_file_name);
526 // m_ID = fn.GetName(); // This will be the NOAA
527 // File name, usually
528
529 // int nProg = 0;
530
531 // wxString ifs( senc_file_name );
532 //
533 // wxFFileInputStream fpx_u( ifs );
534 // if (!fpx_u.IsOk()) {
535 // return ERROR_SENCFILE_NOT_FOUND;
536 // }
537 // wxBufferedInputStream fpx( fpx_u );
538
539 // Sanity check for existence of file
541 fpx.Open(senc_file_name);
542 if (!fpx.IsOk()) return ERROR_SENCFILE_NOT_FOUND;
543
544 S57Obj *obj = 0;
545 int featureID;
546
547 int dun = 0;
548
549 while (!dun) {
550 // Read a record Header
551 OSENC_Record_Base record;
552
553 // long off = fpx.TellI();
554
555 fpx.Read(&record, sizeof(OSENC_Record_Base));
556 if (!fpx.IsOk()) {
557 dun = 1;
558 break;
559 }
560
561 // Process Records
562 switch (record.record_type) {
563 case HEADER_SENC_VERSION: {
564 unsigned char *buf =
565 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
566 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
567 .IsOk()) {
568 dun = 1;
569 break;
570 }
571 uint16_t *pint = (uint16_t *)buf;
572 m_senc_file_read_version = *pint;
573 break;
574 }
575 case HEADER_CELL_NAME: {
576 unsigned char *buf =
577 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
578 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
579 .IsOk()) {
580 dun = 1;
581 break;
582 }
583 m_Name = wxString(buf, wxConvUTF8);
584 break;
585 }
586 case HEADER_CELL_PUBLISHDATE: {
587 unsigned char *buf =
588 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
589 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
590 .IsOk()) {
591 dun = 1;
592 break;
593 }
594 m_sdate000 = wxString(buf, wxConvUTF8);
595 break;
596 }
597
598 case HEADER_CELL_EDITION: {
599 unsigned char *buf =
600 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
601 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
602 .IsOk()) {
603 dun = 1;
604 break;
605 }
606 uint16_t *pint = (uint16_t *)buf;
607 m_read_base_edtn.Printf("%d", *pint);
608
609 break;
610 }
611
612 case HEADER_CELL_UPDATEDATE: {
613 unsigned char *buf =
614 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
615 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
616 .IsOk()) {
617 dun = 1;
618 break;
619 }
620 m_LastUpdateDate = wxString(buf, wxConvUTF8);
621 break;
622 }
623
624 case HEADER_CELL_UPDATE: {
625 unsigned char *buf =
626 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
627 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
628 .IsOk()) {
629 dun = 1;
630 break;
631 }
632 uint16_t *pint = (uint16_t *)buf;
633 m_read_last_applied_update = *pint;
634
635 break;
636 }
637
638 case HEADER_CELL_NATIVESCALE: {
639 unsigned char *buf =
640 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
641 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
642 .IsOk()) {
643 dun = 1;
644 break;
645 }
646 uint32_t *pint = (uint32_t *)buf;
647 m_Chart_Scale = *pint;
648 break;
649 }
650
651 case HEADER_CELL_SENCCREATEDATE: {
652 unsigned char *buf =
653 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
654 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
655 .IsOk()) {
656 dun = 1;
657 break;
658 }
659 break;
660 }
661
662 case CELL_EXTENT_RECORD: {
663 unsigned char *buf =
664 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
665 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
666 .IsOk()) {
667 dun = 1;
668 break;
669 }
672 m_extent.NLAT = pPayload->extent_nw_lat;
673 m_extent.SLAT = pPayload->extent_se_lat;
674 m_extent.WLON = pPayload->extent_nw_lon;
675 m_extent.ELON = pPayload->extent_se_lon;
676
677 // We declare the ref_lat/ref_lon to be the centroid of the extents
678 // This is how the SENC was created....
679 m_ref_lat = (m_extent.NLAT + m_extent.SLAT) / 2.;
680 m_ref_lon = (m_extent.ELON + m_extent.WLON) / 2.;
681
682 break;
683 }
684
685 case CELL_COVR_RECORD: {
686 unsigned char *buf =
687 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
688 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
689 .IsOk()) {
690 dun = 1;
691 break;
692 }
693
694 break;
695 }
696
697 case CELL_NOCOVR_RECORD: {
698 unsigned char *buf =
699 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
700 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
701 .IsOk()) {
702 dun = 1;
703 break;
704 }
705
706 break;
707 }
708
709 case FEATURE_ID_RECORD: {
710 unsigned char *buf =
711 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
712 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
713 .IsOk()) {
714 dun = 1;
715 break;
716 }
717
718 // Starting definition of a new feature
721
722 // Get the Feature type code and ID
723 int featureTypeCode = pPayload->feature_type_code;
724 featureID = pPayload->feature_ID;
725
726 // TODO
727 // if(207 == featureID)
728 // int yyp = 4;
729
730 // Look up the FeatureName from the Registrar
731
732 std::string acronym = GetFeatureAcronymFromTypecode(featureTypeCode);
733
734 // TODO debugging
735 // printf("%s\n", acronym.c_str());
736 // if(!strncmp(acronym.c_str(), "BOYLAT", 6))
737 // int yyp = 4;
738
739 if (acronym.length()) {
740 obj = new S57Obj(acronym.c_str());
741 obj->Index = featureID;
742
743 pObjectVector->push_back(obj);
744 }
745
746 break;
747 }
748
749 case FEATURE_ATTRIBUTE_RECORD: {
750 unsigned char *buf =
751 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
752 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
753 .IsOk()) {
754 dun = 1;
755 break;
756 }
757
758 // Get the payload
761
762 int attributeTypeCode = pPayload->attribute_type_code;
763
764 // Handle Special cases...
765
766 // The primitive type of the Feature is encoded in the SENC as an
767 // attribute of defined type.
768 // if( ATTRIBUTE_ID_PRIM == attributeTypeCode ){
769 // primitiveType = pPayload->attribute_value_int;
770 // }
771
772 // Get the standard attribute acronym
773 std::string acronym =
774 GetAttributeAcronymFromTypecode(attributeTypeCode);
775
776 int attributeValueType = pPayload->attribute_value_type;
777
778 if (acronym.length()) {
779 switch (attributeValueType) {
780 case 0: {
781 uint32_t val = pPayload->attribute_value_int;
782 if (obj) {
783 obj->AddIntegerAttribute(acronym.c_str(), val);
784 }
785 break;
786 }
787
788 case 1: // Integer list
789 {
790 // Calculate the number of elements from the record size
791 // int nCount = (record.record_length -
792 // sizeof(_OSENC_Attribute_Record)) ;
793
794 break;
795 }
796 case 2: // Single double precision real
797 {
798 double val = pPayload->attribute_value_double;
799 if (obj) obj->AddDoubleAttribute(acronym.c_str(), val);
800 break;
801 }
802
803 case 3: // List of double precision real
804 {
805 // TODO
806 break;
807 }
808
809 case 4: // Ascii String
810 {
811 char *val = (char *)&pPayload->attribute_value_char_ptr;
812 if (obj) obj->AddStringAttribute(acronym.c_str(), val);
813
814 break;
815 }
816
817 default:
818 break;
819 }
820 }
821
822 break;
823 }
824
825 case FEATURE_GEOMETRY_RECORD_POINT: {
826 unsigned char *buf =
827 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
828 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
829 .IsOk()) {
830 dun = 1;
831 break;
832 }
833
834 // Get the payload
837
838 if (obj) {
839 obj->SetPointGeometry(pPayload->lat, pPayload->lon, m_ref_lat,
840 m_ref_lon);
841 }
842
843 break;
844 }
845
846 case FEATURE_GEOMETRY_RECORD_AREA: {
847 unsigned char *buf =
848 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
849 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
850 .IsOk()) {
851 dun = 1;
852 break;
853 }
854
855 // Get the payload
858
859 if (obj) {
860 unsigned char *next_byte;
861 PolyTessGeo *pPTG = BuildPolyTessGeo(pPayload, &next_byte);
862
863 obj->SetAreaGeometry(pPTG, m_ref_lat, m_ref_lon);
864
865 // Set the Line geometry for the Feature
866 LineGeometryDescriptor Descriptor;
867
868 // Copy some simple stuff
869 Descriptor.extent_e_lon = pPayload->extent_e_lon;
870 Descriptor.extent_w_lon = pPayload->extent_w_lon;
871 Descriptor.extent_s_lat = pPayload->extent_s_lat;
872 Descriptor.extent_n_lat = pPayload->extent_n_lat;
873
874 Descriptor.indexCount = pPayload->edgeVector_count;
875
876 // Copy the line index table, which in this case is offset in the
877 // payload
878 Descriptor.indexTable =
879 (int *)malloc(pPayload->edgeVector_count * 3 * sizeof(int));
880 memcpy(Descriptor.indexTable, next_byte,
881 pPayload->edgeVector_count * 3 * sizeof(int));
882
883 obj->SetLineGeometry(&Descriptor, GEO_AREA, m_ref_lat, m_ref_lon);
884 }
885
886 break;
887 }
888
889 case FEATURE_GEOMETRY_RECORD_LINE: {
890 unsigned char *buf =
891 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
892 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
893 .IsOk()) {
894 dun = 1;
895 break;
896 }
897
898 // Get the payload & parse it
901 LineGeometryDescriptor lD;
902
903 // Copy some simple stuff
904 lD.extent_e_lon = pPayload->extent_e_lon;
905 lD.extent_w_lon = pPayload->extent_w_lon;
906 lD.extent_s_lat = pPayload->extent_s_lat;
907 lD.extent_n_lat = pPayload->extent_n_lat;
908
909 lD.indexCount = pPayload->edgeVector_count;
910
911 // Copy the payload tables
912 lD.indexTable =
913 (int *)malloc(pPayload->edgeVector_count * 3 * sizeof(int));
914 memcpy(lD.indexTable, &pPayload->payLoad,
915 pPayload->edgeVector_count * 3 * sizeof(int));
916
917 if (obj) obj->SetLineGeometry(&lD, GEO_LINE, m_ref_lat, m_ref_lon);
918
919 break;
920 }
921
922 case FEATURE_GEOMETRY_RECORD_MULTIPOINT: {
923 unsigned char *buf =
924 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
925 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
926 .IsOk()) {
927 dun = 1;
928 break;
929 }
930
931 // Get the payload & parse it
934
935 // Set the Multipoint geometry for the Feature
936 MultipointGeometryDescriptor Descriptor;
937
938 // Copy some simple stuff
939 Descriptor.extent_e_lon = pPayload->extent_e_lon;
940 Descriptor.extent_w_lon = pPayload->extent_w_lon;
941 Descriptor.extent_s_lat = pPayload->extent_s_lat;
942 Descriptor.extent_n_lat = pPayload->extent_n_lat;
943
944 Descriptor.pointCount = pPayload->point_count;
945 Descriptor.pointTable = &pPayload->payLoad;
946
947 if (obj) obj->SetMultipointGeometry(&Descriptor, m_ref_lat, m_ref_lon);
948
949 break;
950 }
951
952 case VECTOR_EDGE_NODE_TABLE_RECORD: {
953 unsigned char *buf =
954 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
955 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
956 .IsOk()) {
957 dun = 1;
958 break;
959 }
960
961 // Parse the buffer
962 uint8_t *pRun = (uint8_t *)buf;
963
964 // The Feature(Object) count
965 int nCount = *(int *)pRun;
966
967 pRun += sizeof(int);
968
969 for (int i = 0; i < nCount; i++) {
970 int featureIndex = *(int *)pRun;
971 pRun += sizeof(int);
972
973 int pointCount = *(int *)pRun;
974 pRun += sizeof(int);
975
976 float *pPoints = NULL;
977 if (pointCount) {
978 pPoints = (float *)malloc(pointCount * 2 * sizeof(float));
979 memcpy(pPoints, pRun, pointCount * 2 * sizeof(float));
980 }
981 pRun += pointCount * 2 * sizeof(float);
982
983 VE_Element *pvee = new VE_Element;
984 pvee->index = featureIndex;
985 pvee->nCount = pointCount;
986 pvee->pPoints = pPoints;
987 pvee->max_priority = 0; // Default
988
989 pVEArray->push_back(pvee);
990 }
991
992 break;
993 }
994
995 case VECTOR_CONNECTED_NODE_TABLE_RECORD: {
996 unsigned char *buf =
997 getBuffer(record.record_length - sizeof(OSENC_Record_Base));
998 if (!fpx.Read(buf, record.record_length - sizeof(OSENC_Record_Base))
999 .IsOk()) {
1000 dun = 1;
1001 break;
1002 }
1003
1004 // Parse the buffer
1005 uint8_t *pRun = (uint8_t *)buf;
1006
1007 // The Feature(Object) count
1008 int nCount = *(int *)pRun;
1009 pRun += sizeof(int);
1010
1011 for (int i = 0; i < nCount; i++) {
1012 int featureIndex = *(int *)pRun;
1013 pRun += sizeof(int);
1014
1015 float *pPoint = (float *)malloc(2 * sizeof(float));
1016 memcpy(pPoint, pRun, 2 * sizeof(float));
1017 pRun += 2 * sizeof(float);
1018
1019 VC_Element *pvce = new VC_Element;
1020 pvce->index = featureIndex;
1021 pvce->pPoint = pPoint;
1022
1023 pVCArray->push_back(pvce);
1024 }
1025
1026 break;
1027 }
1028
1029 default:
1030 break;
1031
1032 } // switch
1033 }
1034
1035 return ret_val;
1036}
1037
1038int Osenc::ingestCell(OGRS57DataSource *poS57DS, const wxString &FullPath000,
1039 const wxString &working_dir) {
1040 // Analyze Updates
1041 // The OGR library will apply updates automatically, if enabled.
1042 // Alternatively, we can explicitely find and apply updates from any
1043 // source directory. We need to keep track of the last sequential update
1044 // applied, to look out for new updates
1045
1046 wxString LastUpdateDate = m_date000.Format("%Y%m%d");
1047
1048 int available_updates =
1049 ValidateAndCountUpdates(FullPath000, working_dir, LastUpdateDate, true);
1050 m_LastUpdateDate =
1051 LastUpdateDate; // tentative, adjusted later on failure of update
1052
1053 if (m_bVerbose && (available_updates > m_UPDN)) {
1054 wxString msg1;
1055 msg1.Printf("Preparing to apply ENC updates, target final update is %3d.",
1056 available_updates);
1057 wxLogMessage(msg1);
1058 }
1059
1060 wxString sobj;
1061
1062 // Here comes the actual ISO8211 file reading
1063
1064 // Set up the options
1065 char **papszReaderOptions = NULL;
1066 // papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_LNAM_REFS,
1067 // "ON" );
1068 // papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_UPDATES,
1069 // "ON" );
1070 papszReaderOptions =
1071 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES, "ON");
1072 papszReaderOptions =
1073 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES, "ON");
1074 poS57DS->SetOptionList(papszReaderOptions);
1075
1076 // Open the OGRS57DataSource
1077 // This will ingest the .000 file from the working dir
1078
1079 bool b_current_debug = g_bGDAL_Debug;
1080 g_bGDAL_Debug = m_bVerbose;
1081
1082 // Form the .000 filename
1083 wxString s0_file = working_dir;
1084 if (s0_file.Last() != wxFileName::GetPathSeparator())
1085 s0_file.Append(wxFileName::GetPathSeparator());
1086 wxFileName f000(FullPath000);
1087
1088 s0_file.Append(f000.GetFullName());
1089
1090 if (poS57DS->Open(s0_file.mb_str(), TRUE, NULL)) return 1;
1091
1092 // Get a pointer to the reader
1093 S57Reader *poReader = poS57DS->GetModule(0);
1094
1095 m_last_applied_update = m_UPDN;
1096 wxString last_successful_update_file;
1097
1098 // Apply the updates...
1099 for (unsigned int i_up = 0; i_up < m_tmpup_array.GetCount(); i_up++) {
1100 wxFileName fn(m_tmpup_array[i_up]);
1101 wxString ext = fn.GetExt();
1102 long n_upd;
1103 ext.ToLong(&n_upd);
1104
1105 if (n_upd > 0) { // .000 is the base, not an update
1106 DDFModule oUpdateModule;
1107 if (!oUpdateModule.Open(m_tmpup_array[i_up].mb_str(), FALSE)) {
1108 break;
1109 }
1110 int upResult = poReader->ApplyUpdates(&oUpdateModule, n_upd);
1111 if (upResult) {
1112 break;
1113 }
1114 m_last_applied_update = n_upd;
1115 last_successful_update_file = m_tmpup_array[i_up];
1116 }
1117 }
1118
1119 // Check for bad/broken update chain....
1120 // It is a "warning" condition if an update fails.
1121 // We use the cell with all good updates applied so far, and so inform the
1122 // user. "Better a slightly out-of-date chart than no chart at all..."
1123
1124 // The logic will attempt to build a SENC on each instance of OCPN, so
1125 // eventually the
1126 // updates may be corrected, and the chart SENC is built correctly.
1127 // Or, the update files following the last good update may be manually
1128 // deleted.
1129
1130 if ((available_updates > 0) && (m_last_applied_update != available_updates)) {
1131 if (last_successful_update_file.Length()) {
1132 // Get the update date from the last good update module
1133 bool bSuccess;
1134 DDFModule oUpdateModule;
1135 wxString LastGoodUpdateDate;
1136 wxDateTime now = wxDateTime::Now();
1137 LastGoodUpdateDate = now.Format("%Y%m%d");
1138
1139 bSuccess = !(
1140 oUpdateModule.Open(last_successful_update_file.mb_str(), TRUE) == 0);
1141
1142 if (bSuccess) {
1143 // Get publish/update date
1144 oUpdateModule.Rewind();
1145 DDFRecord *pr = oUpdateModule.ReadRecord(); // Record 0
1146
1147 int nSuccess;
1148 char *u = NULL;
1149
1150 if (pr)
1151 u = (char *)(pr->GetStringSubfield("DSID", 0, "ISDT", 0, &nSuccess));
1152
1153 if (u) {
1154 if (strlen(u)) {
1155 LastGoodUpdateDate = wxString(u, wxConvUTF8);
1156 }
1157 }
1158 m_LastUpdateDate = LastGoodUpdateDate;
1159 }
1160
1161 // Inform the user
1162 wxString msg("WARNING---ENC Update failed. Last valid update file is:");
1163 msg += last_successful_update_file.mb_str();
1164 wxLogMessage(msg);
1165 wxLogMessage(
1166 " This ENC exchange set should be updated and SENCs rebuilt.");
1167
1168 if (!m_NoErrDialog) {
1169 OCPNMessageBox(
1170 NULL,
1171 _("S57 Cell Update failed.\nENC features may be incomplete or "
1172 "inaccurate.\n\nCheck the logfile for details."),
1173 _("OpenCPN Create SENC Warning"), wxOK | wxICON_EXCLAMATION, 5);
1174 }
1175 } else { // no updates applied.
1176 if (!m_NoErrDialog)
1177 OCPNMessageBox(NULL,
1178 _("S57 Cell Update failed.\nNo updates could be "
1179 "applied.\nENC features may be incomplete or "
1180 "inaccurate.\n\nCheck the logfile for details."),
1181 _("OpenCPN Create SENC Warning"),
1182 wxOK | wxICON_EXCLAMATION, 5);
1183 }
1184 }
1185
1186 // Unset verbose debug option
1187 g_bGDAL_Debug = b_current_debug;
1188
1189 // Update the options, removing the RETURN_PRIMITIVES flags
1190 // This flag needed to be set on ingest() to create the proper field
1191 // defns, but cleared to fetch normal features
1192
1193 papszReaderOptions =
1194 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES, "OFF");
1195 poReader->SetOptions(papszReaderOptions);
1196 CSLDestroy(papszReaderOptions);
1197
1198 wxRemoveFile(s0_file);
1199
1200 return 0;
1201}
1202
1203int Osenc::ValidateAndCountUpdates(const wxFileName file000,
1204 const wxString CopyDir,
1205 wxString &LastUpdateDate, bool b_copyfiles) {
1206 int retval = 0;
1207 wxFileName last_up_added;
1208
1209 // wxString DirName000 = file000.GetPath((int)(wxPATH_GET_SEPARATOR |
1210 // wxPATH_GET_VOLUME)); wxDir dir(DirName000);
1211 m_UpFiles = new wxArrayString;
1212 retval =
1213 s57chart::GetUpdateFileArray(file000, m_UpFiles, m_date000, m_edtn000);
1214 int upmax = retval;
1215
1216 if (m_UpFiles->GetCount()) {
1217 // The s57reader of ogr requires that update set be sequentially
1218 // complete to perform all the updates. However, some NOAA ENC
1219 // distributions are not complete, as apparently some interim updates
1220 // have been withdrawn. Example: as of 20 Dec, 2005, the update set
1221 // for US5MD11M.000 includes US5MD11M.017, ...018, and ...019. Updates
1222 // 001 through 016 are missing.
1223 //
1224 // Workaround.
1225 // Create temporary dummy update files to fill out the set before
1226 // invoking ogr file open/ingest. Delete after SENC file create
1227 // finishes. Set starts with .000, which has the effect of copying the
1228 // base file to the working dir
1229
1230 // bool chain_broken_mssage_shown = false;
1231
1232 if (b_copyfiles) {
1233 unsigned int jup = 0;
1234 for (int iff = 0; iff < retval + 1; iff++) {
1235 wxString upFile;
1236 wxString targetFile;
1237
1238 if (jup < m_UpFiles->GetCount()) upFile = m_UpFiles->Item(jup);
1239 wxFileName upCheck(upFile);
1240 long tl = -1;
1241 wxString text = upCheck.GetExt();
1242 text.ToLong(&tl);
1243 if (tl == iff) {
1244 targetFile = upFile;
1245 jup++; // used this one
1246 } else {
1247 targetFile = file000.GetFullName(); // ext will be updated
1248 }
1249
1250 wxFileName ufile(targetFile);
1251 wxString sext;
1252 sext.Printf("%03d", iff);
1253 ufile.SetExt(sext);
1254
1255 // Create the target update file name
1256 wxString cp_ufile = CopyDir;
1257 if (cp_ufile.Last() != ufile.GetPathSeparator())
1258 cp_ufile.Append(ufile.GetPathSeparator());
1259
1260 cp_ufile.Append(ufile.GetFullName());
1261
1262 wxString tfile = ufile.GetFullPath();
1263
1264 // Explicit check for a short update file, possibly left over from
1265 // a crash...
1266 int flen = 0;
1267 if (ufile.FileExists()) {
1268 wxFile uf(ufile.GetFullPath());
1269 if (uf.IsOpened()) {
1270 flen = uf.Length();
1271 uf.Close();
1272 }
1273 }
1274
1275 if (ufile.FileExists() &&
1276 (flen > 25)) // a valid update file or base file
1277 {
1278 // Copy the valid file to the SENC directory
1279 bool cpok = wxCopyFile(ufile.GetFullPath(), cp_ufile);
1280 if (!cpok) {
1281 wxString msg(" Cannot copy temporary working ENC file ");
1282 msg.Append(ufile.GetFullPath());
1283 msg.Append(" to ");
1284 msg.Append(cp_ufile);
1285 wxLogMessage(msg);
1286 }
1287 }
1288
1289 else {
1290 // Create a dummy ISO8211 file with no real content
1291 // Correct this. We should break the walk, and notify the user See
1292 // FS#1406
1293
1294 // if( !chain_broken_mssage_shown ){
1295 // OCPNMessageBox(NULL,
1296 // _("S57 Cell Update
1297 // chain
1298 // incomplete.\nENC
1299 // features may be
1300 // incomplete or
1301 // inaccurate.\nCheck
1302 // the logfile for
1303 // details."),
1304 // _("OpenCPN Create
1305 // SENC Warning"), wxOK
1306 // |
1307 // wxICON_EXCLAMATION,
1308 // 30 );
1309 // chain_broken_mssage_shown
1310 // = true;
1311 // }
1312
1313 wxString msg(
1314 "WARNING---ENC Update chain incomplete. Substituting NULL "
1315 "update file: ");
1316 msg += ufile.GetFullName();
1317 wxLogMessage(msg);
1318 wxLogMessage(" Subsequent ENC updates may produce errors.");
1319 wxLogMessage(
1320 " This ENC exchange set should be updated and SENCs "
1321 "rebuilt.");
1322
1323 bool bstat;
1324 DDFModule dupdate;
1325 dupdate.Initialize('3', 'L', 'E', '1', '0', "!!!", 3, 4, 4);
1326 bstat = !(dupdate.Create(cp_ufile.mb_str()) == 0);
1327 dupdate.Close();
1328
1329 if (!bstat) {
1330 wxString msg(" Error creating dummy update file: ");
1331 msg.Append(cp_ufile);
1332 wxLogMessage(msg);
1333 }
1334 }
1335
1336 m_tmpup_array.Add(cp_ufile);
1337 last_up_added = cp_ufile;
1338 }
1339 }
1340
1341 // Extract the date field from the last of the update files
1342 // which is by definition a valid, present update file....
1343
1344 wxFileName lastfile(last_up_added);
1345 wxString last_sext;
1346 last_sext.Printf("%03d", upmax);
1347 lastfile.SetExt(last_sext);
1348
1349 bool bSuccess;
1350 DDFModule oUpdateModule;
1351
1352 bSuccess =
1353 !(oUpdateModule.Open(lastfile.GetFullPath().mb_str(), TRUE) == 0);
1354
1355 if (bSuccess) {
1356 // Get publish/update date
1357 oUpdateModule.Rewind();
1358 DDFRecord *pr = oUpdateModule.ReadRecord(); // Record 0
1359
1360 int nSuccess;
1361 char *u = NULL;
1362
1363 if (pr)
1364 u = (char *)(pr->GetStringSubfield("DSID", 0, "ISDT", 0, &nSuccess));
1365
1366 if (u) {
1367 if (strlen(u)) {
1368 LastUpdateDate = wxString(u, wxConvUTF8);
1369 }
1370 } else {
1371 wxDateTime now = wxDateTime::Now();
1372 LastUpdateDate = now.Format("%Y%m%d");
1373 }
1374 }
1375 }
1376
1377 return retval;
1378}
1379
1380bool Osenc::GetBaseFileAttr(const wxString &FullPath000) {
1381 DDFModule oModule;
1382 if (!oModule.Open(FullPath000.mb_str())) {
1383 return false;
1384 }
1385
1386 oModule.Rewind();
1387
1388 // Read and parse DDFRecord 0 to get some interesting data
1389 // n.b. assumes that the required fields will be in Record 0.... Is this
1390 // always true?
1391
1392 DDFRecord *pr = oModule.ReadRecord(); // Record 0
1393 // pr->Dump(stdout);
1394
1395 // Fetch the Geo Feature Count, or something like it....
1396 m_nGeoRecords = pr->GetIntSubfield("DSSI", 0, "NOGR", 0);
1397 if (!m_nGeoRecords) {
1398 errorMessage = "GetBaseFileAttr: DDFRecord 0 does not contain DSSI:NOGR ";
1399
1400 m_nGeoRecords = 1; // backstop
1401 }
1402
1403 // Use ISDT(Issue Date) here, which is the same as UADT(Updates Applied) for
1404 // .000 files
1405 wxString date000;
1406 char *u = (char *)(pr->GetStringSubfield("DSID", 0, "ISDT", 0));
1407 if (u)
1408 date000 = wxString(u, wxConvUTF8);
1409 else {
1410 errorMessage = "GetBaseFileAttr: DDFRecord 0 does not contain DSID:ISDT ";
1411
1412 date000 =
1413 "20000101"; // backstop, very early, so any new files will update?
1414 }
1415 m_date000.ParseFormat(date000, "%Y%m%d");
1416 if (!m_date000.IsValid()) m_date000.ParseFormat("20000101", "%Y%m%d");
1417
1418 m_date000.ResetTime();
1419
1420 // Fetch the EDTN(Edition) field
1421 u = (char *)(pr->GetStringSubfield("DSID", 0, "EDTN", 0));
1422 if (u)
1423 m_edtn000 = wxString(u, wxConvUTF8);
1424 else {
1425 errorMessage = "GetBaseFileAttr: DDFRecord 0 does not contain DSID:EDTN ";
1426
1427 m_edtn000 = "1"; // backstop
1428 }
1429
1430 // m_SE = m_edtn000;
1431
1432 // Fetch the UPDN(Updates Applied) field
1433 u = (char *)(pr->GetStringSubfield("DSID", 0, "UPDN", 0));
1434 if (u) {
1435 long updn = 0;
1436 wxString tmp_updn = wxString(u, wxConvUTF8);
1437 if (tmp_updn.ToLong(&updn)) m_UPDN = updn;
1438
1439 } else {
1440 errorMessage = "GetBaseFileAttr: DDFRecord 0 does not contain DSID:UPDN ";
1441
1442 m_UPDN = 0; // backstop
1443 }
1444
1445 // Fetch the Native Scale by reading more records until DSPM is found
1446 m_native_scale = 0;
1447 for (; pr != NULL; pr = oModule.ReadRecord()) {
1448 if (pr->FindField("DSPM") != NULL) {
1449 m_native_scale = pr->GetIntSubfield("DSPM", 0, "CSCL", 0);
1450 break;
1451 }
1452 }
1453 if (!m_native_scale) {
1454 errorMessage = "GetBaseFileAttr: ENC not contain DSPM:CSCL ";
1455
1456 m_native_scale = 1000; // backstop
1457 }
1458
1459 return true;
1460}
1461
1462//---------------------------------------------------------------------------------------------------
1463/*
1464 * OpenCPN OSENC Version 2 Implementation
1465 */
1466//---------------------------------------------------------------------------------------------------
1467
1468int Osenc::createSenc200(const wxString &FullPath000,
1469 const wxString &SENCFileName, bool b_showProg) {
1470 lockCR.lock();
1471
1472 m_FullPath000 = FullPath000;
1473
1474 m_senc_file_create_version = 201;
1475
1476 if (!m_poRegistrar) {
1477 m_poRegistrar = new S57ClassRegistrar();
1478 m_poRegistrar->LoadInfo(g_csv_locn.mb_str(), FALSE);
1479 m_bPrivateRegistrar = true;
1480 // errorMessage = "S57 Registrar not set.";
1481 // return ERROR_REGISTRAR_NOT_SET;
1482 }
1483
1484 wxFileName SENCfile = wxFileName(SENCFileName);
1485 wxFileName file000 = wxFileName(FullPath000);
1486
1487 // Make the target directory if needed
1488 if (true != SENCfile.DirExists(SENCfile.GetPath())) {
1489 if (!SENCfile.Mkdir(SENCfile.GetPath())) {
1490 errorMessage =
1491 "Cannot create SENC file directory for " + SENCfile.GetFullPath();
1492 lockCR.unlock();
1493 return ERROR_CANNOT_CREATE_SENC_DIR;
1494 }
1495 }
1496
1497 // Make a temp file to create the SENC in
1498 wxFileName tfn;
1499 wxString tmp_file = tfn.CreateTempFileName("");
1500
1501 // FILE *fps57;
1502 // const char *pp = "wb";
1503 // fps57 = fopen( tmp_file.mb_str(), pp );
1504 //
1505 // if( fps57 == NULL ) {
1506 // errorMessage = "Unable to create temp SENC file: ";
1507 // errorMessage.Append( tfn.GetFullPath() );
1508 // return ERROR_CANNOT_CREATE_TEMP_SENC_FILE;
1509 // }
1510
1511 if (m_pauxOutstream) {
1512 m_pOutstream = m_pauxOutstream;
1513 } else {
1514 m_pOutstream = new Osenc_outstreamFile();
1515 }
1516
1517 Osenc_outstream *stream = m_pOutstream;
1518
1519 if (!stream->Open(tmp_file)) {
1520 errorMessage = "Unable to create temp SENC file: ";
1521 errorMessage += tmp_file;
1522 delete m_pOutstream;
1523 lockCR.unlock();
1524 return ERROR_CANNOT_CREATE_TEMP_SENC_FILE;
1525 }
1526
1527 // Take a quick scan of the 000 file to get some basic attributes of the
1528 // exchange set.
1529 if (!GetBaseFileAttr(FullPath000)) {
1530 delete m_pOutstream;
1531 lockCR.unlock();
1532 return ERROR_BASEFILE_ATTRIBUTES;
1533 }
1534
1535 OGRS57DataSource S57DS;
1536 OGRS57DataSource *poS57DS = &S57DS;
1537 poS57DS->SetS57Registrar(m_poRegistrar);
1538
1539 // Ingest the .000 cell, with updates applied
1540
1541 if (ingestCell(poS57DS, FullPath000, SENCfile.GetPath())) {
1542 errorMessage = "Error ingesting: " + FullPath000;
1543 delete m_pOutstream;
1544 lockCR.unlock();
1545 return ERROR_INGESTING000;
1546 }
1547
1548 S57Reader *poReader = poS57DS->GetModule(0);
1549
1550 // Create the Coverage table Records, which also calculates the chart extents
1551 if (!CreateCOVRTables(poReader, m_poRegistrar)) {
1552 delete m_pOutstream;
1553 lockCR.unlock();
1554 return ERROR_SENCFILE_ABORT;
1555 }
1556
1557 // Establish a common reference point for the chart, from the extent
1558 m_ref_lat = (m_extent.NLAT + m_extent.SLAT) / 2.;
1559 m_ref_lon = (m_extent.WLON + m_extent.ELON) / 2.;
1560
1561 bool bcont = true;
1562
1563 // Write the Header information
1564
1565 // char temp[201];
1566
1567 // fprintf( fps57, "SENC Version= %d\n", 200 );
1568
1569 // The chart cell "nice name"
1570 wxString nice_name;
1571 s57chart::GetChartNameFromTXT(FullPath000, nice_name);
1572
1573 string sname = "UTF8Error";
1574 wxCharBuffer buffer = nice_name.ToUTF8();
1575 if (buffer.data()) sname = buffer.data();
1576
1577 if (!WriteHeaderRecord200(stream, HEADER_SENC_VERSION,
1578 (uint16_t)m_senc_file_create_version)) {
1579 stream->Close();
1580 delete m_pOutstream;
1581 lockCR.unlock();
1582 return ERROR_SENCFILE_ABORT;
1583 }
1584
1585 if (!WriteHeaderRecord200(stream, HEADER_CELL_NAME, sname)) {
1586 stream->Close();
1587 delete m_pOutstream;
1588 lockCR.unlock();
1589 return ERROR_SENCFILE_ABORT;
1590 }
1591
1592 wxString date000 = m_date000.Format("%Y%m%d");
1593 string sdata = date000.ToStdString();
1594 if (!WriteHeaderRecord200(stream, HEADER_CELL_PUBLISHDATE, sdata)) {
1595 stream->Close();
1596 delete m_pOutstream;
1597 lockCR.unlock();
1598 return ERROR_SENCFILE_ABORT;
1599 }
1600
1601 long n000 = 0;
1602 m_edtn000.ToLong(&n000);
1603 if (!WriteHeaderRecord200(stream, HEADER_CELL_EDITION, (uint16_t)n000)) {
1604 stream->Close();
1605 delete m_pOutstream;
1606 lockCR.unlock();
1607 return ERROR_SENCFILE_ABORT;
1608 }
1609
1610 sdata = m_LastUpdateDate.ToStdString();
1611 if (!WriteHeaderRecord200(stream, HEADER_CELL_UPDATEDATE, sdata)) {
1612 stream->Close();
1613 delete m_pOutstream;
1614 lockCR.unlock();
1615 return ERROR_SENCFILE_ABORT;
1616 }
1617
1618 if (!WriteHeaderRecord200(stream, HEADER_CELL_UPDATE,
1619 (uint16_t)m_last_applied_update)) {
1620 stream->Close();
1621 delete m_pOutstream;
1622 lockCR.unlock();
1623 return ERROR_SENCFILE_ABORT;
1624 }
1625
1626 if (!WriteHeaderRecord200(stream, HEADER_CELL_NATIVESCALE,
1627 (uint32_t)m_native_scale)) {
1628 stream->Close();
1629 delete m_pOutstream;
1630 lockCR.unlock();
1631 return ERROR_SENCFILE_ABORT;
1632 }
1633
1634 wxDateTime now = wxDateTime::Now();
1635 wxString dateNow = now.Format("%Y%m%d");
1636 sdata = dateNow.ToStdString();
1637 if (!WriteHeaderRecord200(stream, HEADER_CELL_SENCCREATEDATE, sdata)) {
1638 stream->Close();
1639 delete m_pOutstream;
1640 lockCR.unlock();
1641 return ERROR_SENCFILE_ABORT;
1642 }
1643
1644 // Write the Coverage table Records
1645 if (!CreateCovrRecords(stream)) {
1646 stream->Close();
1647 delete m_pOutstream;
1648 lockCR.unlock();
1649 return ERROR_SENCFILE_ABORT;
1650 }
1651
1652 poReader->Rewind();
1653
1654 // Prepare Vector Edge Helper table
1655 // And fill in the table
1656 int feid = 0;
1657 OGRFeature *pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
1658 while (NULL != pEdgeVectorRecordFeature) {
1659 int record_id = pEdgeVectorRecordFeature->GetFieldAsInteger("RCID");
1660
1661 m_vector_helper_hash[record_id] = feid;
1662
1663 feid++;
1664 delete pEdgeVectorRecordFeature;
1665 pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
1666 }
1667
1668 wxString Message = SENCfile.GetFullPath();
1669 Message.Append("...Ingesting");
1670
1671 wxString Title(_("OpenCPN S57 SENC File Create..."));
1672 Title.append(SENCfile.GetFullPath());
1673
1674#if wxUSE_PROGRESSDLG
1675
1676 wxStopWatch progsw;
1677 int nProg = poReader->GetFeatureCount();
1678
1679 if (wxThread::IsMain() && b_showProg) {
1680 m_ProgDialog = new wxGenericProgressDialog();
1681
1682 wxFont *qFont = GetOCPNScaledFont(_("Dialog"));
1683 m_ProgDialog->SetFont(*qFont);
1684
1685 m_ProgDialog->Create(Title, Message, nProg, NULL,
1686 wxPD_AUTO_HIDE | wxPD_SMOOTH);
1687 }
1688#endif
1689
1690 // Loop in the S57 reader, extracting Features one-by-one
1691 OGRFeature *objectDef;
1692
1693 int iObj = 0;
1694
1695 while (bcont) {
1696 objectDef = poReader->ReadNextFeature();
1697
1698 if (objectDef != NULL) {
1699 iObj++;
1700
1701#if wxUSE_PROGRESSDLG
1702
1703 // Update the progress dialog
1704 // We update only every 200 milliseconds to improve performance as
1705 // updating the dialog is very expensive...
1706 // WXGTK is measurably slower even with 100ms here
1707 if (m_ProgDialog && progsw.Time() > 200) {
1708 progsw.Start();
1709
1710 wxString sobj =
1711 wxString(objectDef->GetDefnRef()->GetName(), wxConvUTF8);
1712 sobj.Append(wxString::Format(" %d/%d ", iObj, nProg));
1713
1714 bcont = m_ProgDialog->Update(iObj, sobj);
1715#if defined(__WXMSW__) || defined(__WXOSX__)
1716 wxSafeYield();
1717#endif
1718 }
1719#endif
1720
1721 OGRwkbGeometryType geoType = wkbUnknown;
1722 // This test should not be necessary for real (i.e not C_AGGR)
1723 // features However... some update files contain errors, and have
1724 // deleted some geometry without deleting the corresponding
1725 // feature(s). So, GeometryType becomes Unknown. e.g. US5MD11M.017 In
1726 // this case, all we can do is skip the feature....sigh.
1727
1728 if (objectDef->GetGeometryRef() != NULL)
1729 geoType = objectDef->GetGeometryRef()->getGeometryType();
1730
1731 // n.b This next line causes skip of C_AGGR features w/o geometry
1732 if (geoType != wkbUnknown) { // Write only if has wkbGeometry
1733 CreateSENCRecord200(objectDef, stream, 1, poReader);
1734 }
1735
1736 delete objectDef;
1737
1738 } else
1739 break;
1740 }
1741
1742 if (bcont) {
1743 // Create and write the Vector Edge Table
1744 CreateSENCVectorEdgeTableRecord200(stream, poReader);
1745
1746 // Create and write the Connected NodeTable
1747 CreateSENCVectorConnectedTableRecord200(stream, poReader);
1748 }
1749
1750 // All done, so clean up
1751 stream->Close();
1752 delete m_pOutstream;
1753
1754 // Delete any temporary (working) real and dummy update files,
1755 // as well as .000 file created by ValidateAndCountUpdates()
1756 for (unsigned int iff = 0; iff < m_tmpup_array.GetCount(); iff++)
1757 remove(m_tmpup_array[iff].mb_str());
1758
1759 int ret_code = 0;
1760
1761 if (!bcont) // aborted
1762 {
1763 wxRemoveFile(tmp_file); // kill the temp file
1764 ret_code = ERROR_SENCFILE_ABORT;
1765 }
1766
1767 if (bcont) {
1768 bool cpok = wxRenameFile(tmp_file, SENCfile.GetFullPath());
1769 if (!cpok) {
1770 errorMessage = " Cannot rename temporary SENC file ";
1771 errorMessage.Append(tmp_file);
1772 errorMessage.Append(" to ");
1773 errorMessage.Append(SENCfile.GetFullPath());
1774 ret_code = ERROR_SENCFILE_ABORT;
1775 } else
1776 ret_code = SENC_NO_ERROR;
1777 }
1778
1779#if wxUSE_PROGRESSDLG
1780 delete m_ProgDialog;
1781#endif
1782
1783 lockCR.unlock();
1784
1785 return ret_code;
1786}
1787
1788bool Osenc::CreateCovrRecords(Osenc_outstream *stream) {
1789 // First, create the Extent record
1790 _OSENC_EXTENT_Record record;
1791 record.record_type = CELL_EXTENT_RECORD;
1792 record.record_length = sizeof(_OSENC_EXTENT_Record);
1793 record.extent_sw_lat = m_extent.SLAT;
1794 record.extent_sw_lon = m_extent.WLON;
1795 record.extent_nw_lat = m_extent.NLAT;
1796 record.extent_nw_lon = m_extent.WLON;
1797 record.extent_ne_lat = m_extent.NLAT;
1798 record.extent_ne_lon = m_extent.ELON;
1799 record.extent_se_lat = m_extent.SLAT;
1800 record.extent_se_lon = m_extent.ELON;
1801
1802 size_t targetCount = sizeof(record);
1803 if (!stream->Write(&record, targetCount).IsOk()) return false;
1804
1805 for (int i = 0; i < m_nCOVREntries; i++) {
1806 int nPoints = m_pCOVRTablePoints[i];
1807
1808 float *fpbuf = m_pCOVRTable[i];
1809
1810 // Ready to write the record
1812 record.record_type = CELL_COVR_RECORD;
1813 record.record_length = sizeof(_OSENC_COVR_Record_Base) + sizeof(uint32_t) +
1814 (nPoints * 2 * sizeof(float));
1815
1816 // Write the base record
1817 size_t targetCount = sizeof(record);
1818 if (!stream->Write(&record, targetCount).IsOk()) return false;
1819
1820 // Write the point count
1821 targetCount = sizeof(uint32_t);
1822 if (!stream->Write(&nPoints, targetCount).IsOk()) return false;
1823
1824 // Write the point array
1825 targetCount = nPoints * 2 * sizeof(float);
1826 if (!stream->Write(fpbuf, targetCount).IsOk()) return false;
1827 }
1828
1829 for (int i = 0; i < m_nNoCOVREntries; i++) {
1830 int nPoints = m_pNoCOVRTablePoints[i];
1831
1832 float *fpbuf = m_pNoCOVRTable[i];
1833
1834 // Ready to write the record
1836 record.record_type = CELL_NOCOVR_RECORD;
1837 record.record_length = sizeof(_OSENC_NOCOVR_Record_Base) +
1838 sizeof(uint32_t) + (nPoints * 2 * sizeof(float));
1839
1840 // Write the base record
1841 size_t targetCount = sizeof(record);
1842 if (!stream->Write(&record, targetCount).IsOk()) return false;
1843
1844 // Write the point count
1845 targetCount = sizeof(uint32_t);
1846 if (!stream->Write(&nPoints, targetCount).IsOk()) return false;
1847
1848 // Write the point array
1849 targetCount = nPoints * 2 * sizeof(float);
1850 if (!stream->Write(fpbuf, targetCount).IsOk()) return false;
1851 }
1852
1853 return true;
1854}
1855
1856bool Osenc::WriteHeaderRecord200(Osenc_outstream *stream, int recordType,
1857 std::string payload) {
1858 int payloadLength = payload.length() + 1;
1859 int recordLength = payloadLength + sizeof(OSENC_Record_Base);
1860
1861 // Get a reference to the class persistent buffer
1862 unsigned char *pBuffer = getBuffer(recordLength);
1863
1864 OSENC_Record *pRecord = (OSENC_Record *)pBuffer;
1865 memset(pRecord, 0, recordLength);
1866 pRecord->record_type = recordType;
1867 pRecord->record_length = recordLength;
1868 memcpy(&pRecord->payload, payload.c_str(), payloadLength);
1869
1870 size_t targetCount = recordLength;
1871 if (!stream->Write(pBuffer, targetCount).IsOk())
1872 return false;
1873 else
1874 return true;
1875}
1876
1877bool Osenc::WriteHeaderRecord200(Osenc_outstream *stream, int recordType,
1878 uint16_t val) {
1879 int payloadLength = sizeof(uint16_t);
1880 int recordLength = payloadLength + sizeof(OSENC_Record_Base);
1881
1882 // Get a reference to the class persistent buffer
1883 unsigned char *pBuffer = getBuffer(recordLength);
1884
1885 OSENC_Record *pRecord = (OSENC_Record *)pBuffer;
1886 memset(pRecord, 0, recordLength);
1887 pRecord->record_type = recordType;
1888 pRecord->record_length = recordLength;
1889 memcpy(&pRecord->payload, &val, payloadLength);
1890
1891 size_t targetCount = recordLength;
1892 if (!stream->Write(pBuffer, targetCount).IsOk())
1893 return false;
1894 else
1895 return true;
1896}
1897
1898bool Osenc::WriteHeaderRecord200(Osenc_outstream *stream, int recordType,
1899 uint32_t val) {
1900 int payloadLength = sizeof(uint32_t);
1901 int recordLength = payloadLength + sizeof(OSENC_Record_Base);
1902
1903 // Get a reference to the class persistent buffer
1904 unsigned char *pBuffer = getBuffer(recordLength);
1905
1906 OSENC_Record *pRecord = (OSENC_Record *)pBuffer;
1907 memset(pRecord, 0, recordLength);
1908 pRecord->record_type = recordType;
1909 pRecord->record_length = recordLength;
1910 memcpy(&pRecord->payload, &val, payloadLength);
1911
1912 size_t targetCount = recordLength;
1913 if (!stream->Write(pBuffer, targetCount).IsOk())
1914 return false;
1915 else
1916 return true;
1917}
1918
1919bool Osenc::WriteFIDRecord200(Osenc_outstream *stream, int nOBJL, int featureID,
1920 int prim) {
1922 memset(&record, 0, sizeof(record));
1923
1924 record.record_type = FEATURE_ID_RECORD;
1925 record.record_length = sizeof(record);
1926
1927 record.feature_ID = featureID;
1928 record.feature_type_code = nOBJL;
1929 record.feature_primitive = prim;
1930
1931 size_t targetCount = sizeof(record);
1932 if (!stream->Write(&record, targetCount).IsOk())
1933 return false;
1934 else
1935 return true;
1936}
1937
1938bool Osenc::CreateMultiPointFeatureGeometryRecord200(OGRFeature *pFeature,
1939 Osenc_outstream *stream) {
1940 OGRGeometry *pGeo = pFeature->GetGeometryRef();
1941
1942 int wkb_len = pGeo->WkbSize();
1943 unsigned char *pwkb_buffer = (unsigned char *)malloc(wkb_len);
1944
1945 // Get the GDAL data representation
1946 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
1947
1948 // Capture a buffer of the raw geometry
1949
1950 unsigned char *ps = pwkb_buffer;
1951 ps += 5;
1952 int nPoints = *((int *)ps); // point count
1953
1954 int sb_len = (nPoints * 3 * sizeof(float)); // points as floats
1955
1956 unsigned char *psb_buffer = (unsigned char *)malloc(sb_len);
1957 unsigned char *pd = psb_buffer;
1958
1959 ps = pwkb_buffer;
1960 ps += 9; // skip byte order, type, and count
1961
1962 float *pdf = (float *)pd;
1963
1964 // Set absurd bbox starting limits
1965 float lonmax = -1000;
1966 float lonmin = 1000;
1967 float latmax = -1000;
1968 float latmin = 1000;
1969
1970 for (int ip = 0; ip < nPoints; ip++) {
1971 // Workaround a bug?? in OGRGeometryCollection
1972 // While exporting point geometries serially, OGRPoint->exportToWkb assumes
1973 // that if Z is identically 0, then the point must be a 2D point only. So,
1974 // the collection Wkb is corrupted with some 3D, and some 2D points.
1975 // Workaround: Get reference to the points serially, and explicitly read
1976 // X,Y,Z Ignore the previously read Wkb buffer
1977
1978 OGRGeometryCollection *temp_geometry_collection =
1979 (OGRGeometryCollection *)pGeo;
1980 OGRGeometry *temp_geometry = temp_geometry_collection->getGeometryRef(ip);
1981 OGRPoint *pt_geom = (OGRPoint *)temp_geometry;
1982
1983 double lon = pt_geom->getX();
1984 double lat = pt_geom->getY();
1985 double depth = pt_geom->getZ();
1986
1987 // Calculate SM from chart common reference point
1988 double easting, northing;
1989 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
1990
1991#ifdef __ARM_ARCH
1992 float __attribute__((aligned(16))) east = easting;
1993 float __attribute__((aligned(16))) north = northing;
1994 float __attribute__((aligned(16))) deep = depth;
1995 unsigned char *puceast = (unsigned char *)&east;
1996 unsigned char *pucnorth = (unsigned char *)&north;
1997 unsigned char *pucdeep = (unsigned char *)&deep;
1998
1999 memcpy(pdf++, puceast, sizeof(float));
2000 memcpy(pdf++, pucnorth, sizeof(float));
2001 memcpy(pdf++, pucdeep, sizeof(float));
2002
2003#else
2004 *pdf++ = easting;
2005 *pdf++ = northing;
2006 *pdf++ = (float)depth;
2007#endif
2008
2009 // Keep a running calculation of min/max
2010 lonmax = fmax(lon, lonmax);
2011 lonmin = fmin(lon, lonmin);
2012 latmax = fmax(lat, latmax);
2013 latmin = fmin(lat, latmin);
2014 }
2015
2016 // Ready to write the record
2018 record.record_type = FEATURE_GEOMETRY_RECORD_MULTIPOINT;
2019 record.record_length = sizeof(OSENC_MultipointGeometry_Record_Base) +
2020 (nPoints * 3 * sizeof(float));
2021 record.extent_e_lon = lonmax;
2022 record.extent_w_lon = lonmin;
2023 record.extent_n_lat = latmax;
2024 record.extent_s_lat = latmin;
2025 record.point_count = nPoints;
2026
2027 // Write the base record
2028 size_t targetCount = sizeof(record);
2029 if (!stream->Write(&record, targetCount).IsOk()) goto failure;
2030 // Write the 3D point array
2031 targetCount = nPoints * 3 * sizeof(float);
2032 if (!stream->Write(psb_buffer, targetCount).IsOk()) goto failure;
2033
2034 // Free the buffers
2035 free(psb_buffer);
2036 free(pwkb_buffer);
2037 return true;
2038failure:
2039 // Free the buffers
2040 free(psb_buffer);
2041 free(pwkb_buffer);
2042 return false;
2043}
2044
2045bool Osenc::CreateLineFeatureGeometryRecord200(S57Reader *poReader,
2046 OGRFeature *pFeature,
2047 Osenc_outstream *stream) {
2048 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2049
2050 int wkb_len = pGeo->WkbSize();
2051 unsigned char *pwkb_buffer = (unsigned char *)malloc(wkb_len);
2052
2053 // Get the GDAL data representation
2054 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
2055
2056 // Capture a buffer of the raw geometry
2057
2058 int sb_len =
2059 ((wkb_len - 9) / 2) + 9 + 16; // data will be 4 byte float, not double
2060
2061 unsigned char *psb_buffer = (unsigned char *)malloc(sb_len);
2062 unsigned char *pd = psb_buffer;
2063 unsigned char *ps = pwkb_buffer;
2064
2065 memcpy(pd, ps, 9); // byte order, type, and count
2066
2067 int ip = *((int *)(ps + 5)); // point count
2068
2069 pd += 9;
2070 ps += 9;
2071 double *psd = (double *)ps;
2072 float *pdf = (float *)pd;
2073
2074 // Set absurd bbox starting limits
2075 float lonmax = -1000;
2076 float lonmin = 1000;
2077 float latmax = -1000;
2078 float latmin = 1000;
2079
2080 for (int i = 0; i < ip; i++) { // convert doubles to floats
2081 // computing bbox as we go
2082 float lon, lat;
2083 double easting, northing;
2084#ifdef __ARM_ARCH
2085 double __attribute__((aligned(16))) east_d, north_d;
2086 unsigned char *pucd = (unsigned char *)psd;
2087
2088 memcpy(&east_d, pucd, sizeof(double));
2089 psd += 1;
2090 pucd += sizeof(double);
2091 memcpy(&north_d, pucd, sizeof(double));
2092 psd += 1;
2093 lon = east_d;
2094 lat = north_d;
2095
2096 // Calculate SM from chart common reference point
2097 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
2098 unsigned char *puceasting = (unsigned char *)&easting;
2099 unsigned char *pucnorthing = (unsigned char *)&northing;
2100
2101 memcpy(pdf++, puceasting, sizeof(float));
2102 memcpy(pdf++, pucnorthing, sizeof(float));
2103
2104#else
2105 lon = (float)*psd++;
2106 lat = (float)*psd++;
2107
2108 // Calculate SM from chart common reference point
2109 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
2110
2111 *pdf++ = easting;
2112 *pdf++ = northing;
2113#endif
2114
2115 lonmax = fmax(lon, lonmax);
2116 lonmin = fmin(lon, lonmin);
2117 latmax = fmax(lat, latmax);
2118 latmin = fmin(lat, latmin);
2119 }
2120
2121 int nEdgeVectorRecords = 0;
2122 unsigned char *pvec_buffer =
2123 getObjectVectorIndexTable(poReader, pFeature, nEdgeVectorRecords);
2124
2125#if 0
2126
2127 // Capture the Vector Table geometry indices into a memory buffer
2128 int *pNAME_RCID;
2129 int *pORNT;
2130 int nEdgeVectorRecords;
2131 OGRFeature *pEdgeVectorRecordFeature;
2132
2133 pNAME_RCID = (int *) pFeature->GetFieldAsIntegerList( "NAME_RCID", &nEdgeVectorRecords );
2134 pORNT = (int *) pFeature->GetFieldAsIntegerList( "ORNT", NULL );
2135
2136//fprintf( fpOut, "LSINDEXLIST %d\n", nEdgeVectorRecords );
2137// fwrite(pNAME_RCID, 1, nEdgeVectorRecords * sizeof(int), fpOut);
2138
2139
2140
2141 unsigned char *pvec_buffer = (unsigned char *)malloc(nEdgeVectorRecords * 3 * sizeof(int));
2142 unsigned char *pvRun = pvec_buffer;
2143
2144 // Set up the options, adding RETURN_PRIMITIVES
2145 char ** papszReaderOptions = NULL;
2146 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_UPDATES, "ON" );
2147 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_LINKAGES,"ON" );
2148 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,"ON" );
2149 poReader->SetOptions( papszReaderOptions );
2150
2151// Capture the beginning and end point connected nodes for each edge vector record
2152 for( int i = 0; i < nEdgeVectorRecords; i++ ) {
2153
2154 int *pI = (int *)pvRun;
2155
2156 int edge_rcid = pNAME_RCID[i];
2157
2158 int start_rcid, end_rcid;
2159 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2160 pEdgeVectorRecordFeature = poReader->ReadVector( target_record_feid, RCNM_VE );
2161
2162 if( NULL != pEdgeVectorRecordFeature ) {
2163 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger( "NAME_RCID_0" );
2164 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger( "NAME_RCID_1" );
2165
2166 // Make sure the start and end points exist....
2167 // Note this poReader method was converted to Public access to
2168 // facilitate this test. There might be another clean way....
2169 // Problem first found on Holand ENC 1R5YM009.000
2170 if( !poReader->FetchPoint( RCNM_VC, start_rcid, NULL, NULL, NULL, NULL ) )
2171 start_rcid = -1;
2172 if( !poReader->FetchPoint( RCNM_VC, end_rcid, NULL, NULL, NULL, NULL ) )
2173 end_rcid = -2;
2174
2175 OGRLineString *poLS = (OGRLineString *)pEdgeVectorRecordFeature->GetGeometryRef();
2176
2177 int edge_ornt = 1;
2178
2179 if( edge_ornt == 1 ){ // forward
2180 *pI++ = start_rcid;
2181 *pI++ = edge_rcid;
2182 *pI++ = end_rcid;
2183 } else { // reverse
2184 *pI++ = end_rcid;
2185 *pI++ = edge_rcid;
2186 *pI++ = start_rcid;
2187 }
2188
2189 delete pEdgeVectorRecordFeature;
2190 } else {
2191 start_rcid = -1; // error indication
2192 end_rcid = -2;
2193
2194 *pI++ = start_rcid;
2195 *pI++ = edge_rcid;
2196 *pI++ = end_rcid;
2197 }
2198
2199 pvRun += 3 * sizeof(int);
2200 }
2201
2202
2203 // Reset the options
2204 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,"OFF" );
2205 poReader->SetOptions( papszReaderOptions );
2206 CSLDestroy( papszReaderOptions );
2207#endif
2208
2209 // Ready to write the record
2211 record.record_type = FEATURE_GEOMETRY_RECORD_LINE;
2212 record.record_length = sizeof(OSENC_LineGeometry_Record_Base) +
2213 (nEdgeVectorRecords * 3 * sizeof(int));
2214 record.extent_e_lon = lonmax;
2215 record.extent_w_lon = lonmin;
2216 record.extent_n_lat = latmax;
2217 record.extent_s_lat = latmin;
2218 record.edgeVector_count = nEdgeVectorRecords;
2219
2220 // Write the base record
2221 size_t targetCount = sizeof(record);
2222 if (!stream->Write(&record, targetCount).IsOk()) return false;
2223
2224 // Write the table index array
2225 targetCount = nEdgeVectorRecords * 3 * sizeof(int);
2226 if (!stream->Write(pvec_buffer, targetCount).IsOk()) return false;
2227
2228 // Free the buffers
2229 free(pvec_buffer);
2230 free(psb_buffer);
2231 free(pwkb_buffer);
2232
2233 return true;
2234}
2235
2236bool Osenc::CreateAreaFeatureGeometryRecord200(S57Reader *poReader,
2237 OGRFeature *pFeature,
2238 Osenc_outstream *stream) {
2239 int error_code;
2240
2241 PolyTessGeo *ppg = NULL;
2242
2243 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2244 OGRPolygon *poly = (OGRPolygon *)(pGeo);
2245
2246 if (!poly->getExteriorRing()) return false;
2247
2248 lockCR.unlock();
2249 ppg = new PolyTessGeo(poly, true, m_ref_lat, m_ref_lon, m_LOD_meters);
2250 lockCR.lock();
2251
2252 error_code = ppg->ErrorCode;
2253
2254 if (error_code) {
2255 wxLogMessage(
2256 " Warning: S57 SENC Geometry Error %d, Some Features ignored.",
2257 ppg->ErrorCode);
2258 delete ppg;
2259
2260 return false;
2261 }
2262
2263 // Ready to create the record
2264
2265 // The base record, with writing deferred until length is known
2267 memset(&baseRecord, 0, sizeof(baseRecord));
2268
2269 baseRecord.record_type = FEATURE_GEOMETRY_RECORD_AREA;
2270
2271 // Length calculation is deferred...
2272
2273 baseRecord.extent_s_lat = ppg->Get_ymin();
2274 baseRecord.extent_n_lat = ppg->Get_ymax();
2275 baseRecord.extent_e_lon = ppg->Get_xmax();
2276 baseRecord.extent_w_lon = ppg->Get_xmin();
2277
2278 baseRecord.contour_count = ppg->GetnContours();
2279
2280 // Create the array of contour point counts
2281
2282 int contourPointCountArraySize = ppg->GetnContours() * sizeof(uint32_t);
2283 uint32_t *contourPointCountArray =
2284 (uint32_t *)malloc(contourPointCountArraySize);
2285
2286 uint32_t *pr = contourPointCountArray;
2287
2288 for (int i = 0; i < ppg->GetnContours(); i++) {
2289 *pr++ = ppg->Get_PolyTriGroup_head()->pn_vertex[i];
2290 }
2291
2292 // All that is left is the tesselation result Triangle lists...
2293 // This could be a large array, and we don't want to use a buffer.
2294 // Rather, we want to write directly to the output file.
2295
2296 // So, we
2297 // a walk the TriPrim chain once to get it's length,
2298 // b update the total record length,
2299 // c write everything before the TriPrim chain,
2300 // d and then directly write the TriPrim chain.
2301
2302 // Walk the TriPrim chain
2303 int geoLength = 0;
2304
2305 TriPrim *pTP = ppg->Get_PolyTriGroup_head()
2306 ->tri_prim_head; // head of linked list of TriPrims
2307
2308 int n_TriPrims = 0;
2309 while (pTP) {
2310 geoLength += sizeof(uint8_t) + sizeof(uint32_t); // type, nvert
2311 geoLength += pTP->nVert * 2 * sizeof(float); // vertices
2312 geoLength += 4 * sizeof(double); // Primitive bounding box
2313 pTP = pTP->p_next;
2314
2315 n_TriPrims++;
2316 }
2317
2318 baseRecord.triprim_count = n_TriPrims; // Set the number of TriPrims
2319
2320 int nEdgeVectorRecords = 0;
2321 unsigned char *pvec_buffer =
2322 getObjectVectorIndexTable(poReader, pFeature, nEdgeVectorRecords);
2323
2324#if 0
2325 // Create the Vector Edge Index table into a memory buffer
2326 // This buffer will follow the triangle buffer in the output stream
2327 int *pNAME_RCID;
2328 int *pORNT;
2329 int nEdgeVectorRecords;
2330 OGRFeature *pEdgeVectorRecordFeature;
2331
2332 pNAME_RCID = (int *) pFeature->GetFieldAsIntegerList( "NAME_RCID", &nEdgeVectorRecords );
2333 pORNT = (int *) pFeature->GetFieldAsIntegerList( "ORNT", NULL );
2334
2335 baseRecord.edgeVector_count = nEdgeVectorRecords;
2336
2337 unsigned char *pvec_buffer = (unsigned char *)malloc(nEdgeVectorRecords * 3 * sizeof(int));
2338 unsigned char *pvRun = pvec_buffer;
2339
2340 // Set up the options, adding RETURN_PRIMITIVES
2341 char ** papszReaderOptions = NULL;
2342 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_UPDATES, "ON" );
2343 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_LINKAGES,"ON" );
2344 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,"ON" );
2345 poReader->SetOptions( papszReaderOptions );
2346
2347 // Capture the beginning and end point connected nodes for each edge vector record
2348 for( int i = 0; i < nEdgeVectorRecords; i++ ) {
2349
2350 int *pI = (int *)pvRun;
2351
2352 int start_rcid, end_rcid;
2353 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2354 pEdgeVectorRecordFeature = poReader->ReadVector( target_record_feid, RCNM_VE );
2355
2356 int edge_rcid = pNAME_RCID[i];
2357
2358 if( NULL != pEdgeVectorRecordFeature ) {
2359 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger( "NAME_RCID_0" );
2360 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger( "NAME_RCID_1" );
2361 // Make sure the start and end points exist....
2362 // Note this poReader method was converted to Public access to
2363 // facilitate this test. There might be another clean way....
2364 // Problem first found on Holand ENC 1R5YM009.000
2365 if( !poReader->FetchPoint( RCNM_VC, start_rcid, NULL, NULL, NULL, NULL ) )
2366 start_rcid = -1;
2367 if( !poReader->FetchPoint( RCNM_VC, end_rcid, NULL, NULL, NULL, NULL ) )
2368 end_rcid = -2;
2369
2370 int edge_ornt = 1;
2371 // Allocate some storage for converted points
2372
2373 if( edge_ornt == 1 ){ // forward
2374 *pI++ = start_rcid;
2375 *pI++ = edge_rcid;
2376 *pI++ = end_rcid;
2377 } else { // reverse
2378 *pI++ = end_rcid;
2379 *pI++ = edge_rcid;
2380 *pI++ = start_rcid;
2381 }
2382
2383 delete pEdgeVectorRecordFeature;
2384 } else {
2385 start_rcid = -1; // error indication
2386 end_rcid = -2;
2387
2388 *pI++ = start_rcid;
2389 *pI++ = edge_rcid;
2390 *pI++ = end_rcid;
2391 }
2392
2393 pvRun += 3 * sizeof(int);
2394 }
2395
2396
2397 // Reset the options
2398 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,"OFF" );
2399 poReader->SetOptions( papszReaderOptions );
2400 CSLDestroy( papszReaderOptions );
2401
2402#endif
2403
2404 baseRecord.edgeVector_count = nEdgeVectorRecords;
2405
2406 // Calculate the total record length
2407 int recordLength = sizeof(OSENC_AreaGeometry_Record_Base);
2408 recordLength += contourPointCountArraySize;
2409 recordLength += geoLength;
2410 recordLength += nEdgeVectorRecords * 3 * sizeof(int);
2411 baseRecord.record_length = recordLength;
2412
2413 // Write the base record
2414 size_t targetCount = sizeof(baseRecord);
2415 if (!stream->Write(&baseRecord, targetCount).IsOk()) return false;
2416
2417 // Write the contour point count array
2418 targetCount = contourPointCountArraySize;
2419 if (!stream->Write(contourPointCountArray, targetCount).IsOk()) return false;
2420
2421 // Walk and transcribe the TriPrim chain
2422 pTP = ppg->Get_PolyTriGroup_head()
2423 ->tri_prim_head; // head of linked list of TriPrims
2424 while (pTP) {
2425 if (!stream->Write(&pTP->type, sizeof(uint8_t)).IsOk()) return false;
2426 if (!stream->Write(&pTP->nVert, sizeof(uint32_t)).IsOk()) return false;
2427
2428 // fwrite (&pTP->minxt , sizeof(double), 1, fpOut);
2429 // fwrite (&pTP->maxxt , sizeof(double), 1, fpOut);
2430 // fwrite (&pTP->minyt , sizeof(double), 1, fpOut);
2431 // fwrite (&pTP->maxyt , sizeof(double), 1, fpOut);
2432
2433 double minlat, minlon, maxlat, maxlon;
2434 minlat = pTP->tri_box.GetMinLat();
2435 minlon = pTP->tri_box.GetMinLon();
2436 maxlat = pTP->tri_box.GetMaxLat();
2437 maxlon = pTP->tri_box.GetMaxLon();
2438
2439 if (!stream->Write(&minlon, sizeof(double)).IsOk()) return false;
2440 if (!stream->Write(&maxlon, sizeof(double)).IsOk()) return false;
2441 if (!stream->Write(&minlat, sizeof(double)).IsOk()) return false;
2442 if (!stream->Write(&maxlat, sizeof(double)).IsOk()) return false;
2443
2444 // Testing TODO
2445 // float *pf = (float *)pTP->p_vertex;
2446 // float a = *pf++;
2447 // float b = *pf;
2448
2449 if (!stream->Write(pTP->p_vertex, pTP->nVert * 2 * sizeof(float)).IsOk())
2450 return false;
2451
2452 pTP = pTP->p_next;
2453 }
2454
2455 // Write the Edge Vector index table
2456 targetCount = nEdgeVectorRecords * 3 * sizeof(int);
2457 if (!stream->Write(pvec_buffer, targetCount).IsOk()) return false;
2458
2459 delete ppg;
2460 free(contourPointCountArray);
2461 free(pvec_buffer);
2462
2463 return true;
2464}
2465
2466unsigned char *Osenc::getObjectVectorIndexTable(S57Reader *poReader,
2467 OGRFeature *pFeature,
2468 int &nEntries) {
2469 // Create the Vector Edge Index table into a memory buffer
2470 // This buffer will follow the triangle buffer in the output stream
2471 int *pNAME_RCID;
2472 int *pORNT;
2473 int nEdgeVectorRecords;
2474 OGRFeature *pEdgeVectorRecordFeature;
2475
2476 pNAME_RCID =
2477 (int *)pFeature->GetFieldAsIntegerList("NAME_RCID", &nEdgeVectorRecords);
2478 pORNT = (int *)pFeature->GetFieldAsIntegerList("ORNT", NULL);
2479
2480 nEntries = nEdgeVectorRecords;
2481
2482 unsigned char *pvec_buffer =
2483 (unsigned char *)malloc(nEdgeVectorRecords * 3 * sizeof(int));
2484 unsigned char *pvRun = pvec_buffer;
2485
2486 // Set up the options, adding RETURN_PRIMITIVES
2487 char **papszReaderOptions = NULL;
2488 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES, "ON");
2489 papszReaderOptions =
2490 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES, "ON");
2491 papszReaderOptions =
2492 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES, "ON");
2493 poReader->SetOptions(papszReaderOptions);
2494
2495 // Capture the beginning and end point connected nodes for each edge vector
2496 // record
2497 for (int i = 0; i < nEdgeVectorRecords; i++) {
2498 int *pI = (int *)pvRun;
2499
2500 int edge_rcid = pNAME_RCID[i];
2501
2502 int start_rcid, end_rcid;
2503 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2504 pEdgeVectorRecordFeature =
2505 poReader->ReadVector(target_record_feid, RCNM_VE);
2506
2507 if (NULL != pEdgeVectorRecordFeature) {
2508 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger("NAME_RCID_0");
2509 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger("NAME_RCID_1");
2510
2511 // Make sure the start and end points exist....
2512 // Note this poReader method was converted to Public access to
2513 // facilitate this test. There might be another clean way....
2514 // Problem first found on Holand ENC 1R5YM009.000
2515 if (!poReader->FetchPoint(RCNM_VC, start_rcid, NULL, NULL, NULL, NULL))
2516 start_rcid = -1;
2517 if (!poReader->FetchPoint(RCNM_VC, end_rcid, NULL, NULL, NULL, NULL))
2518 end_rcid = -2;
2519
2520 OGRLineString *poLS =
2521 (OGRLineString *)pEdgeVectorRecordFeature->GetGeometryRef();
2522 if (!poLS)
2523 edge_rcid = 0;
2524 else if (poLS->getNumPoints() < 1)
2525 edge_rcid = 0;
2526
2527 int edge_ornt = pORNT[i];
2528
2529 if (edge_ornt == 1) { // forward
2530 *pI++ = start_rcid;
2531 *pI++ = edge_rcid;
2532 *pI++ = end_rcid;
2533 } else { // reverse
2534 *pI++ = end_rcid;
2535 *pI++ = -edge_rcid;
2536 *pI++ = start_rcid;
2537 }
2538
2539 delete pEdgeVectorRecordFeature;
2540 } else {
2541 start_rcid = -1; // error indication
2542 end_rcid = -2;
2543
2544 *pI++ = start_rcid;
2545 *pI++ = edge_rcid;
2546 *pI++ = end_rcid;
2547 }
2548
2549 pvRun += 3 * sizeof(int);
2550 }
2551
2552 // Reset the options
2553 papszReaderOptions =
2554 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES, "OFF");
2555 poReader->SetOptions(papszReaderOptions);
2556 CSLDestroy(papszReaderOptions);
2557
2558 return pvec_buffer;
2559}
2560
2561void Osenc::CreateSENCVectorEdgeTableRecord200(Osenc_outstream *stream,
2562 S57Reader *poReader) {
2563 // We create the payload first, so we can calculate the total record length
2564 uint8_t *pPayload = NULL;
2565 int payloadSize = 0;
2566 uint8_t *pRun = pPayload;
2567
2568 // Set up the S57Reader options, adding RETURN_PRIMITIVES
2569 char **papszReaderOptions = NULL;
2570 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES, "ON");
2571 papszReaderOptions =
2572 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES, "ON");
2573 papszReaderOptions =
2574 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES, "ON");
2575 poReader->SetOptions(papszReaderOptions);
2576
2577 int feid = 0;
2578 OGRLineString *pLS = NULL;
2579 OGRGeometry *pGeo;
2580 OGRFeature *pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
2581
2582 int nFeatures = 0;
2583
2584 // Read all the EdgeVector Features
2585 while (NULL != pEdgeVectorRecordFeature) {
2586 // Check for a zero point count. Dunno why this should happen, other than
2587 // bad ENC encoding
2588
2589 int nPoints = 0;
2590 if (pEdgeVectorRecordFeature->GetGeometryRef() != NULL) {
2591 pGeo = pEdgeVectorRecordFeature->GetGeometryRef();
2592 if (pGeo->getGeometryType() == wkbLineString) {
2593 pLS = (OGRLineString *)pGeo;
2594 nPoints = pLS->getNumPoints();
2595 } else
2596 nPoints = 0;
2597 }
2598
2599 if (nPoints) {
2600 int new_size = payloadSize + (2 * sizeof(int));
2601 pPayload = (uint8_t *)realloc(pPayload, new_size);
2602 pRun = pPayload + payloadSize; // recalculate the running pointer,
2603 // since realloc may have moved memory
2604 payloadSize = new_size;
2605
2606 // Fetch and store the Record ID
2607 int record_id = pEdgeVectorRecordFeature->GetFieldAsInteger("RCID");
2608 *(int *)pRun = record_id;
2609 pRun += sizeof(int);
2610
2611 // Transcribe points to a buffer
2612 // We reduce the maximum number of points in the table to
2613 // MAX_VECTOR_POINTS using a naive algorithm skipping the proportional
2614 // part of them to avoid too deep a recursion and crash in DouglasPeucker
2615 // later
2616 int reduction_ratio = nPoints / MAX_VECTOR_POINTS + 1;
2617 int reduced_points = 0;
2618
2619 double *ppd = (double *)malloc((nPoints / reduction_ratio + 1) * 2 *
2620 sizeof(double));
2621 double *ppr = ppd;
2622
2623 for (int i = 0; i < nPoints; i++) {
2624 if (i % reduction_ratio == 0) {
2625 OGRPoint p;
2626 pLS->getPoint(i, &p);
2627
2628 // Calculate SM from chart common reference point
2629 double easting, northing;
2630 toSM(p.getY(), p.getX(), m_ref_lat, m_ref_lon, &easting, &northing);
2631
2632 *ppr++ = easting;
2633 *ppr++ = northing;
2634 reduced_points++;
2635 }
2636 }
2637 nPoints = reduced_points;
2638
2639 // Reduce the LOD of this linestring
2640 std::vector<int> index_keep;
2641 if (nPoints > 5 && (m_LOD_meters > .01)) {
2642 index_keep.push_back(0);
2643 index_keep.push_back(nPoints - 1);
2644
2645 DouglasPeucker(ppd, 0, nPoints - 1, m_LOD_meters, &index_keep);
2646 // printf("DP Reduction: %d/%d\n", index_keep.GetCount(),
2647 // nPoints);
2648
2649 } else {
2650 index_keep.resize(nPoints);
2651 for (int i = 0; i < nPoints; i++) index_keep[i] = i;
2652 }
2653
2654 // Store the point count in the payload
2655 int nPointReduced = index_keep.size();
2656 *(int *)pRun = nPointReduced;
2657 pRun += sizeof(int);
2658
2659 // transcribe the (possibly) reduced linestring to the payload
2660
2661 // Grow the payload buffer
2662 int new_size_red = payloadSize + (nPointReduced * 2 * sizeof(float));
2663 pPayload = (uint8_t *)realloc(pPayload, new_size_red);
2664 pRun = pPayload + payloadSize; // recalculate the running pointer,
2665 // since realloc may have moved memory
2666 payloadSize = new_size_red;
2667
2668 float *npp = (float *)pRun;
2669 float *npp_run = npp;
2670 ppr = ppd;
2671 for (int ip = 0; ip < nPoints; ip++) {
2672 double x = *ppr++;
2673 double y = *ppr++;
2674
2675 for (unsigned int j = 0; j < index_keep.size(); j++) {
2676 if (index_keep[j] == ip) {
2677 *npp_run++ = x;
2678 *npp_run++ = y;
2679 pRun += 2 * sizeof(float);
2680 break;
2681 }
2682 }
2683 }
2684
2685 nFeatures++;
2686
2687 free(ppd);
2688 }
2689
2690 // Next vector record
2691 feid++;
2692 delete pEdgeVectorRecordFeature;
2693 pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
2694
2695 } // while
2696
2697 // Now we know the payload length and the Feature count
2698 if (nFeatures) {
2699 // Now write the record out
2700 OSENC_VET_Record record;
2701
2702 record.record_type = VECTOR_EDGE_NODE_TABLE_RECORD;
2703 record.record_length =
2704 sizeof(OSENC_VET_Record_Base) + payloadSize + sizeof(uint32_t);
2705
2706 // Write out the record
2707 stream->Write(&record, sizeof(OSENC_VET_Record_Base));
2708
2709 // Write out the Feature(Object) count
2710 stream->Write(&nFeatures, sizeof(uint32_t));
2711
2712 // Write out the payload
2713 stream->Write(pPayload, payloadSize);
2714 }
2715 // All done with buffer
2716 free(pPayload);
2717
2718 // Reset the S57Reader options
2719 papszReaderOptions =
2720 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES, "OFF");
2721 poReader->SetOptions(papszReaderOptions);
2722 CSLDestroy(papszReaderOptions);
2723}
2724
2725void Osenc::CreateSENCVectorConnectedTableRecord200(Osenc_outstream *stream,
2726 S57Reader *poReader) {
2727 // We create the payload first, so we can calculate the total record length
2728 uint8_t *pPayload = NULL;
2729 int payloadSize = 0;
2730 uint8_t *pRun = pPayload;
2731
2732 // Set up the S57Reader options, adding RETURN_PRIMITIVES
2733 char **papszReaderOptions = NULL;
2734 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES, "ON");
2735 papszReaderOptions =
2736 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES, "ON");
2737 papszReaderOptions =
2738 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES, "ON");
2739 poReader->SetOptions(papszReaderOptions);
2740
2741 int feid = 0;
2742 OGRPoint *pP;
2743 OGRGeometry *pGeo;
2744 OGRFeature *pConnNodeRecordFeature = poReader->ReadVector(feid, RCNM_VC);
2745 int featureCount = 0;
2746
2747 // Read all the ConnectedVector Features
2748 while (NULL != pConnNodeRecordFeature) {
2749 if (pConnNodeRecordFeature->GetGeometryRef() != NULL) {
2750 pGeo = pConnNodeRecordFeature->GetGeometryRef();
2751 if (pGeo->getGeometryType() == wkbPoint) {
2752 int new_size = payloadSize + sizeof(int) + (2 * sizeof(float));
2753 pPayload = (uint8_t *)realloc(pPayload, new_size);
2754 pRun = pPayload + payloadSize; // recalculate the running pointer,
2755 // since realloc may have moved memory
2756 payloadSize = new_size;
2757
2758 // Fetch and store the Record ID
2759 int record_id = pConnNodeRecordFeature->GetFieldAsInteger("RCID");
2760 *(int *)pRun = record_id;
2761 pRun += sizeof(int);
2762
2763 pP = (OGRPoint *)pGeo;
2764
2765 // Calculate SM from chart common reference point
2766 double easting, northing;
2767 toSM(pP->getY(), pP->getX(), m_ref_lat, m_ref_lon, &easting, &northing);
2768
2769 // MyPoint pd;
2770 // pd.x = easting;
2771 // pd.y = northing;
2772 // memcpy(pRun, &pd, sizeof(MyPoint));
2773 float *ps = (float *)pRun;
2774 *ps++ = easting;
2775 *ps = northing;
2776
2777 featureCount++;
2778 }
2779 }
2780
2781 // Next vector record
2782 feid++;
2783 delete pConnNodeRecordFeature;
2784 pConnNodeRecordFeature = poReader->ReadVector(feid, RCNM_VC);
2785 } // while
2786
2787 // Now we know the payload length and the Feature count
2788
2789 // Now write the record out
2790 if (featureCount) {
2791 OSENC_VCT_Record record;
2792
2793 record.record_type = VECTOR_CONNECTED_NODE_TABLE_RECORD;
2794 record.record_length =
2795 sizeof(OSENC_VCT_Record_Base) + payloadSize + sizeof(int);
2796
2797 // Write out the record
2798 stream->Write(&record, sizeof(OSENC_VCT_Record_Base));
2799
2800 // Write out the Feature(Object) count
2801 stream->Write(&featureCount, sizeof(uint32_t));
2802
2803 // Write out the payload
2804 stream->Write(pPayload, payloadSize);
2805 }
2806
2807 // All done with buffer
2808 free(pPayload);
2809
2810 // Reset the S57Reader options
2811 papszReaderOptions =
2812 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES, "OFF");
2813 poReader->SetOptions(papszReaderOptions);
2814 CSLDestroy(papszReaderOptions);
2815}
2816
2817bool Osenc::CreateSENCRecord200(OGRFeature *pFeature, Osenc_outstream *stream,
2818 int mode, S57Reader *poReader) {
2819 // TODO
2820 // if(pFeature->GetFID() == 207)
2821 // int yyp = 4;
2822
2823 // Create the Feature Identification Record
2824
2825 // Fetch the S57 Standard Object Class identifier
2826 OGRFeatureDefn *pFD = pFeature->GetDefnRef();
2827 int nOBJL = pFD->GetOBJL();
2828
2829 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2830 OGRwkbGeometryType gType = pGeo->getGeometryType();
2831
2832 int primitive = 0;
2833 switch (gType) {
2834 case wkbLineString:
2835 primitive = GEO_LINE;
2836 break;
2837 case wkbPoint:
2838 primitive = GEO_POINT;
2839 break;
2840 case wkbPolygon:
2841 primitive = GEO_AREA;
2842 break;
2843 default:
2844 primitive = 0;
2845 }
2846
2847 if (!WriteFIDRecord200(stream, nOBJL, pFeature->GetFID(), primitive))
2848 return false;
2849
2850#define MAX_HDR_LINE 400
2851
2852 // char line[MAX_HDR_LINE + 1];
2853 wxString sheader;
2854
2855 // fprintf( fpOut, "OGRFeature(%s):%ld\n",
2856 // pFeature->GetDefnRef()->GetName(), pFeature->GetFID() );
2857
2858 // In a loop, fetch attributes, and create OSENC Feature Attribute Records
2859 // In the interests of output file size, DO NOT report fields that are not
2860 // set.
2861
2862 // TODO Debugging
2863 // if(!strncmp(pFeature->GetDefnRef()->GetName(), "BOYLAT", 6))
2864 // int yyp = 4;
2865
2866 if (pFeature->GetFID() == 290) int yyp = 4;
2867
2868 int payloadLength = 0;
2869 void *payloadBuffer = NULL;
2870 unsigned int payloadBufferLength = 0;
2871
2872 for (int iField = 0; iField < pFeature->GetFieldCount(); iField++) {
2873 if (pFeature->IsFieldSet(iField)) {
2874 if ((iField == 1) || (iField > 7)) {
2875 OGRFieldDefn *poFDefn = pFeature->GetDefnRef()->GetFieldDefn(iField);
2876 // const char *pType = OGRFieldDefn::GetFieldTypeName(
2877 // poFDefn->GetType() );
2878 const char *pAttrName = poFDefn->GetNameRef();
2879 const char *pAttrVal = pFeature->GetFieldAsString(iField);
2880
2881 // Use the OCPN Registrar Manager to map attribute acronym to an
2882 // identifier. The mapping is defined by the file
2883 // {csv_dir}/s57attributes.csv
2884 int attributeID = m_pRegistrarMan->getAttributeID(pAttrName);
2885
2886 // Determine the {attribute_value_type} needed in the record
2887 int OGRvalueType = (int)poFDefn->GetType();
2888 int valueType = 0;
2889
2890 // Check for special cases
2891 if (-1 == attributeID) {
2892 if (!strncmp(pAttrName, "PRIM", 4)) {
2893 attributeID = ATTRIBUTE_ID_PRIM;
2894 }
2895 }
2896
2897#if 0
OFTInteger = 0, OFTIntegerList = 1, OFTReal = 2, OFTRealList = 3, OFTString = 4, OFTStringList = 5, OFTWideString = 6, OFTWideStringList = 7, OFTBinary = 8
2907#endif
2908 switch (OGRvalueType) {
2909 case 0: // Single integer
2910 {
2911 valueType = OGRvalueType;
2912
2913 if (payloadBufferLength < 4) {
2914 payloadBuffer = realloc(payloadBuffer, 4);
2915 payloadBufferLength = 4;
2916 }
2917
2918 int aValue = pFeature->GetFieldAsInteger(iField);
2919 memcpy(payloadBuffer, &aValue, sizeof(int));
2920 payloadLength = sizeof(int);
2921
2922 break;
2923 }
2924 case 1: // Integer list
2925 {
2926 valueType = OGRvalueType;
2927
2928 int nCount = 0;
2929 const int *aValueList =
2930 pFeature->GetFieldAsIntegerList(iField, &nCount);
2931
2932 if (payloadBufferLength < nCount * sizeof(int)) {
2933 payloadBuffer = realloc(payloadBuffer, nCount * sizeof(int));
2934 payloadBufferLength = nCount * sizeof(int);
2935 }
2936
2937 int *pBuffRun = (int *)payloadBuffer;
2938 for (int i = 0; i < nCount; i++) {
2939 *pBuffRun++ = aValueList[i];
2940 }
2941 payloadLength = nCount * sizeof(int);
2942
2943 break;
2944 }
2945 case 2: // Single double precision real
2946 {
2947 valueType = OGRvalueType;
2948
2949 if (payloadBufferLength < sizeof(double)) {
2950 payloadBuffer = realloc(payloadBuffer, sizeof(double));
2951 payloadBufferLength = sizeof(double);
2952 }
2953
2954 double aValue = pFeature->GetFieldAsDouble(iField);
2955 memcpy(payloadBuffer, &aValue, sizeof(double));
2956 payloadLength = sizeof(double);
2957
2958 break;
2959 }
2960
2961 case 3: // List of double precision real
2962 {
2963 valueType = OGRvalueType;
2964
2965 int nCount = 0;
2966 const double *aValueList =
2967 pFeature->GetFieldAsDoubleList(iField, &nCount);
2968
2969 if (payloadBufferLength < nCount * sizeof(double)) {
2970 payloadBuffer = realloc(payloadBuffer, nCount * sizeof(double));
2971 payloadBufferLength = nCount * sizeof(double);
2972 }
2973
2974 double *pBuffRun = (double *)payloadBuffer;
2975 for (int i = 0; i < nCount; i++) {
2976 *pBuffRun++ = aValueList[i];
2977 }
2978 payloadLength = nCount * sizeof(double);
2979
2980 break;
2981 }
2982
2983 case 4: // Ascii String
2984 {
2985 valueType = OGRvalueType;
2986 const char *pAttrVal = pFeature->GetFieldAsString(iField);
2987
2988 wxString wxAttrValue;
2989
2990 if ((0 == strncmp("NOBJNM", pAttrName, 6)) ||
2991 (0 == strncmp("NINFOM", pAttrName, 6)) ||
2992 (0 == strncmp("NPLDST", pAttrName, 6)) ||
2993 (0 == strncmp("NTXTDS", pAttrName, 6))) {
2994 if (poReader->GetNall() ==
2995 2) { // ENC is using UCS-2 / UTF-16 encoding
2996 wxMBConvUTF16 conv;
2997 wxString att_conv(pAttrVal, conv);
2998 att_conv.RemoveLast(); // Remove the \037 that terminates
2999 // UTF-16 strings in S57
3000 att_conv.Replace("\n",
3001 "|"); // Replace <new line> with special
3002 // break character
3003 wxAttrValue = att_conv;
3004 } else if (poReader->GetNall() ==
3005 1) { // ENC is using Lex level 1 (ISO 8859_1) encoding
3006 wxCSConv conv("iso8859-1");
3007 wxString att_conv(pAttrVal, conv);
3008 wxAttrValue = att_conv;
3009 }
3010 } else {
3011 if (poReader->GetAall() ==
3012 1) { // ENC is using Lex level 1 (ISO 8859_1) encoding for
3013 // "General Text"
3014 wxCSConv conv("iso8859-1");
3015 wxString att_conv(pAttrVal, conv);
3016 wxAttrValue = att_conv;
3017 } else
3018 wxAttrValue =
3019 wxString(pAttrVal); // ENC must be using Lex level 0
3020 // (ASCII) encoding for "General Text"
3021 }
3022
3023 unsigned int stringPayloadLength = 0;
3024
3025 wxCharBuffer buffer;
3026 if (wxAttrValue.Length()) { // need to explicitely encode as UTF8
3027 buffer = wxAttrValue.ToUTF8();
3028 pAttrVal = buffer.data();
3029 stringPayloadLength = strlen(buffer.data());
3030 }
3031
3032 if (stringPayloadLength) {
3033 if (payloadBufferLength < stringPayloadLength + 1) {
3034 payloadBuffer = realloc(payloadBuffer, stringPayloadLength + 1);
3035 payloadBufferLength = stringPayloadLength + 1;
3036 }
3037
3038 strcpy((char *)payloadBuffer, pAttrVal);
3039 payloadLength = stringPayloadLength + 1;
3040 } else
3041 attributeID = -1; // cancel this attribute record
3042
3043 break;
3044 }
3045
3046 default:
3047 valueType = -1;
3048 break;
3049 }
3050
3051 if (-1 != attributeID) {
3052 // Build the record
3053 int recordLength =
3054 sizeof(OSENC_Attribute_Record_Base) + payloadLength;
3055
3056 // Get a reference to the class persistent buffer
3057 unsigned char *pBuffer = getBuffer(recordLength);
3058
3059 OSENC_Attribute_Record *pRecord = (OSENC_Attribute_Record *)pBuffer;
3060 memset(pRecord, 0, sizeof(OSENC_Attribute_Record));
3061 pRecord->record_type = FEATURE_ATTRIBUTE_RECORD;
3062 pRecord->record_length = recordLength;
3063 pRecord->attribute_type = attributeID;
3064 pRecord->attribute_value_type = valueType;
3065 memcpy(&pRecord->payload, payloadBuffer, payloadLength);
3066
3067 // Write the record out....
3068 size_t targetCount = recordLength;
3069 if (!stream->Write(pBuffer, targetCount).IsOk()) {
3070 free(payloadBuffer);
3071 return false;
3072 }
3073 }
3074 }
3075 }
3076 }
3077 if (wkbPoint == pGeo->getGeometryType()) {
3078 OGRPoint *pp = (OGRPoint *)pGeo;
3079 int nqual = pp->getnQual();
3080 if (10 != nqual) // only add attribute if nQual is not "precisely known"
3081 {
3082 int attributeID = m_pRegistrarMan->getAttributeID("QUAPOS");
3083 int valueType = 0;
3084 if (-1 != attributeID) {
3085 if (payloadBufferLength < 4) {
3086 payloadBuffer = realloc(payloadBuffer, 4);
3087 payloadBufferLength = 4;
3088 }
3089
3090 memcpy(payloadBuffer, &nqual, sizeof(int));
3091 payloadLength = sizeof(int);
3092 // Build the record
3093 int recordLength = sizeof(OSENC_Attribute_Record_Base) + payloadLength;
3094
3095 // Get a reference to the class persistent buffer
3096 unsigned char *pBuffer = getBuffer(recordLength);
3097
3098 OSENC_Attribute_Record *pRecord = (OSENC_Attribute_Record *)pBuffer;
3099 memset(pRecord, 0, sizeof(OSENC_Attribute_Record));
3100 pRecord->record_type = FEATURE_ATTRIBUTE_RECORD;
3101 pRecord->record_length = recordLength;
3102 pRecord->attribute_type = attributeID;
3103 pRecord->attribute_value_type = valueType;
3104 memcpy(&pRecord->payload, payloadBuffer, payloadLength);
3105
3106 // Write the record out....
3107 size_t targetCount = recordLength;
3108 if (!stream->Write(pBuffer, targetCount).IsOk()) {
3109 free(payloadBuffer);
3110 return false;
3111 }
3112 }
3113 }
3114 }
3115
3116 free(payloadBuffer);
3117
3118#if 0
3119 // Special geometry cases
3121 if( wkbPoint == pGeo->getGeometryType() ) {
3122 OGRPoint *pp = (OGRPoint *) pGeo;
3123 int nqual = pp->getnQual();
3124 if( 10 != nqual ) // only add attribute if nQual is not "precisely known"
3125 {
3126 snprintf( line, MAX_HDR_LINE - 2, " %s (%c) = %d", "QUALTY", 'I', nqual );
3127 sheader += wxString( line, wxConvUTF8 );
3128 sheader += '\n';
3129 }
3130
3131 }
3132
3133 if( mode == 1 ) {
3134 sprintf( line, " %s %f %f\n", pGeo->getGeometryName(), m_ref_lat, m_ref_lon );
3135 sheader += wxString( line, wxConvUTF8 );
3136 }
3137
3138 wxCharBuffer buffer=sheader.ToUTF8();
3139 fprintf( fpOut, "HDRLEN=%lu\n", (unsigned long) strlen(buffer) );
3140 fwrite( buffer.data(), 1, strlen(buffer), fpOut );
3141
3142#endif
3143
3144 if ((pGeo != NULL)) {
3145 wxString msg;
3146
3147 OGRwkbGeometryType gType = pGeo->getGeometryType();
3148 switch (gType) {
3149 case wkbLineString: {
3150 if (!CreateLineFeatureGeometryRecord200(poReader, pFeature, stream))
3151 return false;
3152
3153 break;
3154 }
3155
3156 case wkbPoint: {
3158 record.record_type = FEATURE_GEOMETRY_RECORD_POINT;
3159 record.record_length = sizeof(record);
3160
3161 int wkb_len = pGeo->WkbSize();
3162 unsigned char *pwkb_buffer = (unsigned char *)malloc(wkb_len);
3163
3164 // Get the GDAL data representation
3165 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
3166
3167 int nq_len = 4; // nQual length
3168 unsigned char *ps = pwkb_buffer;
3169
3170 ps += 5 + nq_len;
3171 double *psd = (double *)ps;
3172
3173 double lat, lon;
3174#ifdef __ARM_ARCH
3175 __attribute__((aligned(16))) double lata, lona;
3176 unsigned char *pucsd = (unsigned char *)psd;
3177
3178 memcpy(&lona, pucsd, sizeof(double));
3179 pucsd += sizeof(double);
3180 memcpy(&lata, pucsd, sizeof(double));
3181 lon = lona;
3182 lat = lata;
3183#else
3184 lon = *psd++; // fetch the point
3185 lat = *psd;
3186#endif
3187
3188 free(pwkb_buffer);
3189
3190 record.lat = lat;
3191 record.lon = lon;
3192
3193 // Write the record out....
3194 size_t targetCount = record.record_length;
3195 if (!stream->Write(&record, targetCount).IsOk()) return false;
3196
3197 break;
3198 }
3199
3200 case wkbMultiPoint:
3201 case wkbMultiPoint25D: {
3202 if (!CreateMultiPointFeatureGeometryRecord200(pFeature, stream))
3203 return false;
3204 break;
3205 }
3206
3207#if 1
3208 // Special case, polygons are handled separately
3209 case wkbPolygon: {
3210 if (!CreateAreaFeatureGeometryRecord200(poReader, pFeature, stream))
3211 return false;
3212
3213 break;
3214 }
3215#endif
3216 // All others
3217 default:
3218 msg = " Warning: Unimplemented ogr geotype record ";
3219 wxLogMessage(msg);
3220
3221 break;
3222 } // switch
3223 }
3224 return true;
3225}
3226
3227// Build PolyGeo Object from OSENC200 file record
3228// Return an integer count of bytes consumed from the record in creating
3229// the PolyTessGeo
3230PolyTessGeo *Osenc::BuildPolyTessGeo(_OSENC_AreaGeometry_Record_Payload *record,
3231 unsigned char **next_byte) {
3232 PolyTessGeo *pPTG = new PolyTessGeo();
3233
3234 pPTG->SetExtents(record->extent_w_lon, record->extent_s_lat,
3235 record->extent_e_lon, record->extent_n_lat);
3236
3237 unsigned int n_TriPrim = record->triprim_count;
3238 int nContours = record->contour_count;
3239
3240 // Get a pointer to the payload
3241 void *payLoad = &record->payLoad;
3242
3243 // skip over the contour vertex count array, for now TODO
3244 // uint8_t *pTriPrims = (uint8_t *)payLoad + (nContours * sizeof(uint32_t));
3245
3246 // Create the head of the linked list of TriPrims
3247 PolyTriGroup *ppg = new PolyTriGroup;
3248 ppg->m_bSMSENC = true;
3249 ppg->data_type = DATA_TYPE_DOUBLE;
3250
3251 ppg->nContours = nContours;
3252
3253 ppg->pn_vertex = (int *)malloc(nContours * sizeof(int));
3254 int *pctr = ppg->pn_vertex;
3255
3256 // The point count array is the first element in the payload, length is known
3257 int *contour_pointcount_array_run = (int *)payLoad;
3258 for (int i = 0; i < nContours; i++) {
3259 *pctr++ = *contour_pointcount_array_run++;
3260 }
3261
3262 // Read Raw Geometry
3263 ppg->pgroup_geom = NULL;
3264
3265 // Now the triangle primitives
3266
3267 TriPrim **p_prev_triprim = &(ppg->tri_prim_head);
3268
3269 // Read the PTG_Triangle Geometry in a loop
3270 unsigned int tri_type;
3271 int nvert;
3272 int nvert_max = 0;
3273 int total_byte_size = 2 * sizeof(float);
3274
3275 uint8_t *pPayloadRun =
3276 (uint8_t *)contour_pointcount_array_run; // Points to the start of the
3277 // triangle primitives
3278
3279 for (unsigned int i = 0; i < n_TriPrim; i++) {
3280 tri_type = *pPayloadRun++;
3281 nvert = *(uint32_t *)pPayloadRun;
3282 pPayloadRun += sizeof(uint32_t);
3283
3284 TriPrim *tp = new TriPrim;
3285 *p_prev_triprim = tp; // make the link
3286 p_prev_triprim = &(tp->p_next);
3287 tp->p_next = NULL;
3288
3289 tp->type = tri_type;
3290 tp->nVert = nvert;
3291
3292 nvert_max =
3293 wxMax(nvert_max, nvert); // Keep a running tab of largest vertex count
3294
3295 // Read the triangle primitive bounding box as lat/lon
3296 double *pbb = (double *)pPayloadRun;
3297
3298 double minxt, minyt, maxxt, maxyt;
3299
3300#ifdef __ARM_ARCH
3301 double __attribute__((aligned(16))) abox[4];
3302 unsigned char *pucbb = (unsigned char *)pPayloadRun;
3303 memcpy(&abox[0], pucbb, 4 * sizeof(double));
3304
3305 minxt = abox[0];
3306 maxxt = abox[1];
3307 minyt = abox[2];
3308 maxyt = abox[3];
3309#else
3310 minxt = *pbb++;
3311 maxxt = *pbb++;
3312 minyt = *pbb++;
3313 maxyt = *pbb;
3314#endif
3315
3316 tp->tri_box.Set(minyt, minxt, maxyt, maxxt);
3317
3318 pPayloadRun += 4 * sizeof(double);
3319
3320 int byte_size = nvert * 2 * sizeof(float); // the vertices
3321 total_byte_size += byte_size;
3322
3323 tp->p_vertex = (double *)pPayloadRun;
3324
3325 pPayloadRun += byte_size;
3326 }
3327
3328 if (next_byte) *next_byte = pPayloadRun;
3329
3330 // Convert the vertex arrays into a single float memory allocation to enable
3331 // efficient access later
3332 unsigned char *vbuf = (unsigned char *)malloc(total_byte_size);
3333
3334 TriPrim *p_tp = ppg->tri_prim_head;
3335 unsigned char *p_run = vbuf;
3336 while (p_tp) {
3337 memcpy(p_run, p_tp->p_vertex, p_tp->nVert * 2 * sizeof(float));
3338 p_tp->p_vertex = (double *)p_run;
3339 p_run += p_tp->nVert * 2 * sizeof(float);
3340 p_tp = p_tp->p_next; // pick up the next in chain
3341 }
3342 ppg->bsingle_alloc = true;
3343 ppg->single_buffer = vbuf;
3344 ppg->single_buffer_size = total_byte_size;
3345 ppg->data_type = DATA_TYPE_FLOAT;
3346
3347 pPTG->SetPPGHead(ppg);
3348 pPTG->SetnVertexMax(nvert_max);
3349
3350 pPTG->Set_OK(true);
3351
3352 return pPTG;
3353}
3354
3355bool Osenc::CreateCOVRTables(S57Reader *poReader,
3356 S57ClassRegistrar *poRegistrar) {
3357 poReader->Rewind();
3358
3359 OGRFeature *pFeat;
3360 int catcov;
3361 float LatMax, LatMin, LonMax, LonMin;
3362 LatMax = -90.;
3363 LatMin = 90.;
3364 LonMax = -179.;
3365 LonMin = 179.;
3366
3367 m_pCOVRTablePoints = NULL;
3368 m_pCOVRTable = NULL;
3369
3370 // Create arrays to hold geometry objects temporarily
3371 MyFloatPtrArray *pAuxPtrArray = new MyFloatPtrArray;
3372 std::vector<int> auxCntArray, noCovrCntArray;
3373
3374 MyFloatPtrArray *pNoCovrPtrArray = new MyFloatPtrArray;
3375
3376 // Get the first M_COVR object
3377 pFeat = GetChartFirstM_COVR(catcov, poReader, poRegistrar);
3378
3379 while (pFeat) {
3380 // Get the next M_COVR feature, and create possible additional entries
3381 // for COVR
3382 OGRPolygon *poly = (OGRPolygon *)(pFeat->GetGeometryRef());
3383 OGRLinearRing *xring = poly->getExteriorRing();
3384
3385 int npt = xring->getNumPoints();
3386
3387 float *pf = NULL;
3388
3389 if (npt >= 3) {
3390 pf = (float *)malloc(2 * npt * sizeof(float));
3391 float *pfr = pf;
3392
3393 for (int i = 0; i < npt; i++) {
3394 OGRPoint p;
3395 xring->getPoint(i, &p);
3396
3397 if (catcov == 1) {
3398 LatMax = fmax(LatMax, p.getY());
3399 LatMin = fmin(LatMin, p.getY());
3400 LonMax = fmax(LonMax, p.getX());
3401 LonMin = fmin(LonMin, p.getX());
3402 }
3403
3404 pfr[0] = p.getY(); // lat
3405 pfr[1] = p.getX(); // lon
3406
3407 pfr += 2;
3408 }
3409
3410 if (catcov == 1) {
3411 pAuxPtrArray->Add(pf);
3412 auxCntArray.push_back(npt);
3413 } else if (catcov == 2) {
3414 pNoCovrPtrArray->Add(pf);
3415 noCovrCntArray.push_back(npt);
3416 } else
3417 free(pf);
3418 }
3419
3420 delete pFeat;
3421 pFeat = GetChartNextM_COVR(catcov, poReader);
3422 } // while
3423
3424 // Allocate the storage
3425
3426 m_nCOVREntries = auxCntArray.size();
3427
3428 // If only one M_COVR,CATCOV=1 object was found,
3429 // assign the geometry to the one and only COVR
3430
3431 if (m_nCOVREntries == 1) {
3432 m_pCOVRTablePoints = (int *)malloc(sizeof(int));
3433 *m_pCOVRTablePoints = auxCntArray[0];
3434 m_pCOVRTable = (float **)malloc(sizeof(float *));
3435 *m_pCOVRTable = (float *)malloc(auxCntArray[0] * 2 * sizeof(float));
3436 memcpy(*m_pCOVRTable, pAuxPtrArray->Item(0),
3437 auxCntArray[0] * 2 * sizeof(float));
3438 }
3439
3440 else if (m_nCOVREntries > 1) {
3441 // Create new COVR entries
3442 m_pCOVRTablePoints = (int *)malloc(m_nCOVREntries * sizeof(int));
3443 m_pCOVRTable = (float **)malloc(m_nCOVREntries * sizeof(float *));
3444
3445 for (unsigned int j = 0; j < (unsigned int)m_nCOVREntries; j++) {
3446 m_pCOVRTablePoints[j] = auxCntArray[j];
3447 m_pCOVRTable[j] = (float *)malloc(auxCntArray[j] * 2 * sizeof(float));
3448 memcpy(m_pCOVRTable[j], pAuxPtrArray->Item(j),
3449 auxCntArray[j] * 2 * sizeof(float));
3450 }
3451 }
3452
3453 else // strange case, found no CATCOV=1 M_COVR objects
3454 {
3455 wxString msg(" ENC contains no useable M_COVR, CATCOV=1 features: ");
3456 msg.Append(m_FullPath000);
3457 wxLogMessage(msg);
3458 }
3459
3460 // And for the NoCovr regions
3461 m_nNoCOVREntries = noCovrCntArray.size();
3462
3463 if (m_nNoCOVREntries) {
3464 // Create new NoCOVR entries
3465 m_pNoCOVRTablePoints = (int *)malloc(m_nNoCOVREntries * sizeof(int));
3466 m_pNoCOVRTable = (float **)malloc(m_nNoCOVREntries * sizeof(float *));
3467
3468 for (unsigned int j = 0; j < (unsigned int)m_nNoCOVREntries; j++) {
3469 int npoints = noCovrCntArray[j];
3470 m_pNoCOVRTablePoints[j] = npoints;
3471 m_pNoCOVRTable[j] = (float *)malloc(npoints * 2 * sizeof(float));
3472 memcpy(m_pNoCOVRTable[j], pNoCovrPtrArray->Item(j),
3473 npoints * 2 * sizeof(float));
3474 }
3475 } else {
3476 m_pNoCOVRTablePoints = NULL;
3477 m_pNoCOVRTable = NULL;
3478 }
3479
3480 for (unsigned int j = 0; j < (unsigned int)m_nNoCOVREntries; j++)
3481 free(pNoCovrPtrArray->Item(j));
3482 for (unsigned int j = 0; j < (unsigned int)m_nCOVREntries; j++)
3483 free(pAuxPtrArray->Item(j));
3484
3485 delete pAuxPtrArray;
3486 delete pNoCovrPtrArray;
3487
3488 if (0 == m_nCOVREntries) { // fallback
3489 wxString msg(" ENC contains no M_COVR features: ");
3490 msg.Append(m_FullPath000);
3491 wxLogMessage(msg);
3492
3493 msg = " Calculating Chart Extents as fallback.";
3494 wxLogMessage(msg);
3495
3496 OGREnvelope Env;
3497
3498 if (poReader->GetExtent(&Env, true) == OGRERR_NONE) {
3499 LatMax = Env.MaxY;
3500 LonMax = Env.MaxX;
3501 LatMin = Env.MinY;
3502 LonMin = Env.MinX;
3503
3504 m_nCOVREntries = 1;
3505 m_pCOVRTablePoints = (int *)malloc(sizeof(int));
3506 *m_pCOVRTablePoints = 4;
3507 m_pCOVRTable = (float **)malloc(sizeof(float *));
3508 float *pf = (float *)malloc(2 * 4 * sizeof(float));
3509 *m_pCOVRTable = pf;
3510 float *pfe = pf;
3511
3512 *pfe++ = LatMax;
3513 *pfe++ = LonMin;
3514
3515 *pfe++ = LatMax;
3516 *pfe++ = LonMax;
3517
3518 *pfe++ = LatMin;
3519 *pfe++ = LonMax;
3520
3521 *pfe++ = LatMin;
3522 *pfe++ = LonMin;
3523
3524 } else {
3525 wxString msg(" Cannot calculate Extents for ENC: ");
3526 msg.Append(m_FullPath000);
3527 wxLogMessage(msg);
3528
3529 return false; // chart is completely unusable
3530 }
3531 }
3532
3533 // Populate the oSENC clone of the chart's extent structure
3534 m_extent.NLAT = LatMax;
3535 m_extent.SLAT = LatMin;
3536 m_extent.ELON = LonMax;
3537 m_extent.WLON = LonMin;
3538
3539 return true;
3540}
3541
3542OGRFeature *Osenc::GetChartFirstM_COVR(int &catcov, S57Reader *pENCReader,
3543 S57ClassRegistrar *poRegistrar) {
3544 OGRFeature *rv = NULL;
3545
3546 if ((NULL != pENCReader) && (NULL != poRegistrar)) {
3547 // Select the proper class
3548 poRegistrar->SelectClass("M_COVR");
3549
3550 // OGRFeatureDefn *M_COVRdef = S57GenerateObjectClassDefn(
3551 // poRegistrar, 302, 0);
3552
3553 // find this feature
3554 bool bFound = false;
3555 OGRFeature *pobjectDef = pENCReader->ReadNextFeature(/*M_COVRdef*/);
3556 while (!bFound) {
3557 if (pobjectDef) {
3558 OGRFeatureDefn *poDefn = pobjectDef->GetDefnRef();
3559 if (poDefn && (poDefn->GetOBJL() == 302 /*poRegistrar->GetOBJL()*/)) {
3560 // Fetch the CATCOV attribute
3561 catcov = pobjectDef->GetFieldAsInteger("CATCOV");
3562 rv = pobjectDef;
3563 break;
3564 } else
3565 delete pobjectDef;
3566 } else {
3567 break;
3568 }
3569 pobjectDef = pENCReader->ReadNextFeature();
3570 }
3571 }
3572
3573 return rv;
3574}
3575
3576OGRFeature *Osenc::GetChartNextM_COVR(int &catcov, S57Reader *pENCReader) {
3577 catcov = -1;
3578
3579 if (pENCReader) {
3580 bool bFound = false;
3581 OGRFeature *pobjectDef = pENCReader->ReadNextFeature();
3582
3583 while (!bFound) {
3584 if (pobjectDef) {
3585 OGRFeatureDefn *poDefn = pobjectDef->GetDefnRef();
3586 if (poDefn && (poDefn->GetOBJL() == 302)) {
3587 // Fetch the CATCOV attribute
3588 catcov = pobjectDef->GetFieldAsInteger("CATCOV");
3589 return pobjectDef;
3590 } else
3591 delete pobjectDef;
3592
3593 } else
3594 return NULL;
3595
3596 pobjectDef = pENCReader->ReadNextFeature();
3597 }
3598 }
3599
3600 return NULL;
3601}
3602
3603int Osenc::GetBaseFileInfo(const wxString &FullPath000,
3604 const wxString &SENCFileName) {
3605 wxFileName SENCfile = wxFileName(SENCFileName);
3606
3607 // Take a quick scan of the 000 file to get some basic attributes of the
3608 // exchange set.
3609 if (!GetBaseFileAttr(FullPath000)) {
3610 return ERROR_BASEFILE_ATTRIBUTES;
3611 }
3612
3613 OGRS57DataSource oS57DS;
3614 oS57DS.SetS57Registrar(m_poRegistrar);
3615
3616 bool b_current_debug = g_bGDAL_Debug;
3617 g_bGDAL_Debug = false;
3618
3619 // Ingest the .000 cell, with updates applied
3620
3621 if (ingestCell(&oS57DS, FullPath000, SENCfile.GetPath())) {
3622 errorMessage = "Error ingesting: " + FullPath000;
3623 return ERROR_INGESTING000;
3624 }
3625
3626 S57Reader *poReader = oS57DS.GetModule(0);
3627
3628 CalculateExtent(poReader, m_poRegistrar);
3629
3630 g_bGDAL_Debug = b_current_debug;
3631
3632 // delete poReader;
3633
3634 return SENC_NO_ERROR;
3635}
3636
3637bool Osenc::CalculateExtent(S57Reader *poReader,
3638 S57ClassRegistrar *poRegistrar) {
3639 poReader->Rewind();
3640
3641 OGRFeature *pFeat;
3642 int catcov;
3643 float LatMax, LatMin, LonMax, LonMin;
3644 LatMax = -90.;
3645 LatMin = 90.;
3646 LonMax = -179.;
3647 LonMin = 179.;
3648
3649 m_pCOVRTablePoints = NULL;
3650 m_pCOVRTable = NULL;
3651
3652 // // Create arrays to hold geometry objects temporarily
3653 // MyFloatPtrArray *pAuxPtrArray = new MyFloatPtrArray;
3654 // wxArrayInt *pAuxCntArray = new wxArrayInt;
3655 //
3656 // MyFloatPtrArray *pNoCovrPtrArray = new MyFloatPtrArray;
3657 // wxArrayInt *pNoCovrCntArray = new wxArrayInt;
3658
3659 // Get the first M_COVR object
3660 pFeat = GetChartFirstM_COVR(catcov, poReader, poRegistrar);
3661
3662 while (pFeat) {
3663 // Get the next M_COVR feature, and create possible additional entries
3664 // for COVR
3665 OGRPolygon *poly = (OGRPolygon *)(pFeat->GetGeometryRef());
3666 OGRLinearRing *xring = poly->getExteriorRing();
3667
3668 int npt = xring->getNumPoints();
3669
3670 if (npt >= 3) {
3671 for (int i = 0; i < npt; i++) {
3672 OGRPoint p;
3673 xring->getPoint(i, &p);
3674
3675 if (catcov == 1) {
3676 LatMax = fmax(LatMax, p.getY());
3677 LatMin = fmin(LatMin, p.getY());
3678 LonMax = fmax(LonMax, p.getX());
3679 LonMin = fmin(LonMin, p.getX());
3680 }
3681 }
3682 }
3683
3684 delete pFeat;
3685 pFeat = GetChartNextM_COVR(catcov, poReader);
3686 } // while
3687
3688 // Populate the oSENC clone of the chart's extent structure
3689 m_extent.NLAT = LatMax;
3690 m_extent.SLAT = LatMin;
3691 m_extent.ELON = LonMax;
3692 m_extent.WLON = LonMin;
3693
3694 return true;
3695}
3696
3697void Osenc::InitializePersistentBuffer(void) {
3698 pBuffer = (unsigned char *)malloc(1024);
3699 bufferSize = 1024;
3700}
3701
3702unsigned char *Osenc::getBuffer(size_t length) {
3703 if (length > bufferSize) {
3704 pBuffer = (unsigned char *)realloc(pBuffer, length * 2);
3705 bufferSize = length * 2;
3706 }
3707
3708 return pBuffer;
3709}
wxFont * GetOCPNScaledFont(wxString item, int default_size)
Retrieves a font from FontMgr, optionally scaled for physical readability.
Definition gui_lib.cpp:56
General purpose GUI support.