36#include <wx/arrimpl.cpp>
37#include <wx/filename.h>
38#include <wx/progdlg.h>
39#include <wx/wfstream.h>
43#include "gdal/cpl_csv.h"
44#include "gdal/cpl_string.h"
50#include "LOD_reduce.h"
59#define MAX_VECTOR_POINTS 1000
60#define ERR_BUF_LEN 2000
66WX_DEFINE_ARRAY(
float *, MyFloatPtrArray);
69sigjmp_buf env_osenc_ogrf;
74static bool g_OsencVerbose;
81void OpenCPN_OGR_OSENC_ErrorHandler(CPLErr eErrClass,
int nError,
82 const char *pszErrorMsg) {
83 char buf[ERR_BUF_LEN + 1];
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);
90 sprintf(buf,
" ERROR %d: %s\n", nError, pszErrorMsg);
92 if (g_bGDAL_Debug || (CE_Debug != eErrClass)) {
93 wxString msg(buf, wxConvUTF8);
100 if (eErrClass == CE_Fatal) {
101 longjmp(env_osenc_ogrf, 1);
110Osenc_instreamFile::Osenc_instreamFile() { Init(); }
112Osenc_instreamFile::~Osenc_instreamFile() {
delete m_instream; }
114bool Osenc_instreamFile::Open(
const wxString &senc_file_name) {
115 m_instream =
new wxFFileInputStream(senc_file_name);
116 return m_instream->IsOk();
119void Osenc_instreamFile::Close() {}
121Osenc_instream &Osenc_instreamFile::Read(
void *buffer,
size_t size) {
122 if (m_instream) m_ok = m_instream->Read(buffer, size).IsOk();
127bool Osenc_instreamFile::IsOk() {
128 if (m_instream) m_ok = m_instream->IsOk();
133bool Osenc_instreamFile::isAvailable() {
return true; }
135void Osenc_instreamFile::Shutdown() {}
137void Osenc_instreamFile::Init() {
146Osenc_outstreamFile::Osenc_outstreamFile() { Init(); }
148Osenc_outstreamFile::~Osenc_outstreamFile() {
delete m_outstream; }
150bool Osenc_outstreamFile::Open(
const wxString &file) {
152 m_outstream =
new wxFFileOutputStream(file);
153 if (m_outstream) m_ok = m_outstream->IsOk();
158void Osenc_outstreamFile::Close() {
159 if (m_outstream) m_ok = m_outstream->Close();
162Osenc_outstream &Osenc_outstreamFile::Write(
const void *buffer,
size_t size) {
163 if (m_outstream) m_ok = m_outstream->Write(buffer, size).IsOk();
168bool Osenc_outstreamFile::IsOk() {
169 if (m_outstream) m_ok = m_outstream->IsOk();
174void Osenc_outstreamFile::Init() {
183Osenc::Osenc() { init(); }
186 if (m_bPrivateRegistrar)
delete m_poRegistrar;
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]);
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]);
205 for (
unsigned int j = 0; j < (
unsigned int)m_nNoCOVREntries; j++)
206 free(m_pNoCOVRTable[j]);
208 for (
unsigned int j = 0; j < (
unsigned int)m_nCOVREntries; j++)
209 free(m_pCOVRTable[j]);
211 free(m_pCOVRTablePoints);
213 free(m_pNoCOVRTablePoints);
214 free(m_pNoCOVRTable);
216 CPLPopErrorHandler();
221 m_poRegistrar = NULL;
222 m_bPrivateRegistrar =
false;
223 m_senc_file_read_version = 0;
225 InitializePersistentBuffer();
230 m_read_base_edtn =
"-1";
232 m_nNoCOVREntries = 0;
234 m_pCOVRTablePoints = NULL;
236 m_pNoCOVRTablePoints = NULL;
237 m_pNoCOVRTable = NULL;
239 m_pauxOutstream = NULL;
240 m_pauxInstream = NULL;
246 g_OsencVerbose =
true;
247 m_NoErrDialog =
false;
252 CPLPushErrorHandler(OpenCPN_OGR_OSENC_ErrorHandler);
254 lockCR = std::unique_lock<std::mutex>(m, std::defer_lock);
257void Osenc::setVerbose(
bool verbose) {
258 m_bVerbose = verbose;
259 g_OsencVerbose = verbose;
262int Osenc::ingestHeader(
const wxString &senc_file_name) {
266 int ret_val = SENC_NO_ERROR;
268 wxFileName fn(senc_file_name);
284 fpx.Open(senc_file_name);
285 if (!fpx.IsOk())
return ERROR_SENCFILE_NOT_FOUND;
293 return ERROR_SENCFILE_NOT_FOUND;
297 if (HEADER_SENC_VERSION != record.record_type) {
298 return ERROR_SENCFILE_NOT_FOUND;
305 return ERROR_SENCFILE_NOT_FOUND;
307 uint16_t *pint = (uint16_t *)buf;
308 m_senc_file_read_version = *pint;
326 switch (record.record_type) {
327 case HEADER_SENC_VERSION: {
335 uint16_t *pint = (uint16_t *)buf;
336 m_senc_file_read_version = *pint;
339 case HEADER_CELL_NAME: {
347 m_Name = wxString(buf, wxConvUTF8);
350 case HEADER_CELL_PUBLISHDATE: {
361 case HEADER_CELL_EDITION: {
369 uint16_t *pint = (uint16_t *)buf;
370 m_read_base_edtn.Printf(
"%d", *pint);
374 case HEADER_CELL_UPDATEDATE: {
385 case HEADER_CELL_UPDATE: {
394 uint16_t *pint = (uint16_t *)buf;
395 m_read_last_applied_update = *pint;
399 case HEADER_CELL_NATIVESCALE: {
407 uint32_t *pint = (uint32_t *)buf;
408 m_Chart_Scale = *pint;
413 case HEADER_CELL_SENCCREATEDATE: {
421 m_readFileCreateDate = wxString(buf, wxConvUTF8);
426 case CELL_EXTENT_RECORD: {
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;
444 case CELL_COVR_RECORD: {
456 int point_count = pPayload->point_count;
457 m_AuxCntArray.push_back(point_count);
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);
466 case CELL_NOCOVR_RECORD: {
478 int point_count = pPayload->point_count;
479 m_NoCovrCntArray.push_back(point_count);
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);
488 case FEATURE_ID_RECORD: {
505std::string Osenc::GetFeatureAcronymFromTypecode(
int typeCode) {
508 return acronym.c_str();
513std::string Osenc::GetAttributeAcronymFromTypecode(
int typeCode) {
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;
541 fpx.Open(senc_file_name);
542 if (!fpx.IsOk())
return ERROR_SENCFILE_NOT_FOUND;
562 switch (record.record_type) {
563 case HEADER_SENC_VERSION: {
571 uint16_t *pint = (uint16_t *)buf;
572 m_senc_file_read_version = *pint;
575 case HEADER_CELL_NAME: {
583 m_Name = wxString(buf, wxConvUTF8);
586 case HEADER_CELL_PUBLISHDATE: {
594 m_sdate000 = wxString(buf, wxConvUTF8);
598 case HEADER_CELL_EDITION: {
606 uint16_t *pint = (uint16_t *)buf;
607 m_read_base_edtn.Printf(
"%d", *pint);
612 case HEADER_CELL_UPDATEDATE: {
620 m_LastUpdateDate = wxString(buf, wxConvUTF8);
624 case HEADER_CELL_UPDATE: {
632 uint16_t *pint = (uint16_t *)buf;
633 m_read_last_applied_update = *pint;
638 case HEADER_CELL_NATIVESCALE: {
646 uint32_t *pint = (uint32_t *)buf;
647 m_Chart_Scale = *pint;
651 case HEADER_CELL_SENCCREATEDATE: {
662 case CELL_EXTENT_RECORD: {
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;
679 m_ref_lat = (m_extent.NLAT + m_extent.SLAT) / 2.;
680 m_ref_lon = (m_extent.ELON + m_extent.WLON) / 2.;
685 case CELL_COVR_RECORD: {
697 case CELL_NOCOVR_RECORD: {
709 case FEATURE_ID_RECORD: {
723 int featureTypeCode = pPayload->feature_type_code;
724 featureID = pPayload->feature_ID;
732 std::string acronym = GetFeatureAcronymFromTypecode(featureTypeCode);
739 if (acronym.length()) {
740 obj =
new S57Obj(acronym.c_str());
741 obj->Index = featureID;
743 pObjectVector->push_back(obj);
749 case FEATURE_ATTRIBUTE_RECORD: {
762 int attributeTypeCode = pPayload->attribute_type_code;
773 std::string acronym =
774 GetAttributeAcronymFromTypecode(attributeTypeCode);
776 int attributeValueType = pPayload->attribute_value_type;
778 if (acronym.length()) {
779 switch (attributeValueType) {
781 uint32_t val = pPayload->attribute_value_int;
783 obj->AddIntegerAttribute(acronym.c_str(), val);
798 double val = pPayload->attribute_value_double;
799 if (obj) obj->AddDoubleAttribute(acronym.c_str(), val);
811 char *val = (
char *)&pPayload->attribute_value_char_ptr;
812 if (obj) obj->AddStringAttribute(acronym.c_str(), val);
825 case FEATURE_GEOMETRY_RECORD_POINT: {
839 obj->SetPointGeometry(pPayload->lat, pPayload->lon, m_ref_lat,
846 case FEATURE_GEOMETRY_RECORD_AREA: {
860 unsigned char *next_byte;
861 PolyTessGeo *pPTG = BuildPolyTessGeo(pPayload, &next_byte);
863 obj->SetAreaGeometry(pPTG, m_ref_lat, m_ref_lon);
866 LineGeometryDescriptor Descriptor;
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;
874 Descriptor.indexCount = pPayload->edgeVector_count;
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));
883 obj->SetLineGeometry(&Descriptor, GEO_AREA, m_ref_lat, m_ref_lon);
889 case FEATURE_GEOMETRY_RECORD_LINE: {
901 LineGeometryDescriptor lD;
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;
909 lD.indexCount = pPayload->edgeVector_count;
913 (
int *)malloc(pPayload->edgeVector_count * 3 *
sizeof(
int));
914 memcpy(lD.indexTable, &pPayload->payLoad,
915 pPayload->edgeVector_count * 3 *
sizeof(
int));
917 if (obj) obj->SetLineGeometry(&lD, GEO_LINE, m_ref_lat, m_ref_lon);
922 case FEATURE_GEOMETRY_RECORD_MULTIPOINT: {
936 MultipointGeometryDescriptor Descriptor;
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;
944 Descriptor.pointCount = pPayload->point_count;
945 Descriptor.pointTable = &pPayload->payLoad;
947 if (obj) obj->SetMultipointGeometry(&Descriptor, m_ref_lat, m_ref_lon);
952 case VECTOR_EDGE_NODE_TABLE_RECORD: {
962 uint8_t *pRun = (uint8_t *)buf;
965 int nCount = *(
int *)pRun;
969 for (
int i = 0; i < nCount; i++) {
970 int featureIndex = *(
int *)pRun;
973 int pointCount = *(
int *)pRun;
976 float *pPoints = NULL;
978 pPoints = (
float *)malloc(pointCount * 2 *
sizeof(
float));
979 memcpy(pPoints, pRun, pointCount * 2 *
sizeof(
float));
981 pRun += pointCount * 2 *
sizeof(float);
983 VE_Element *pvee =
new VE_Element;
984 pvee->index = featureIndex;
985 pvee->nCount = pointCount;
986 pvee->pPoints = pPoints;
987 pvee->max_priority = 0;
989 pVEArray->push_back(pvee);
995 case VECTOR_CONNECTED_NODE_TABLE_RECORD: {
1005 uint8_t *pRun = (uint8_t *)buf;
1008 int nCount = *(
int *)pRun;
1009 pRun +=
sizeof(int);
1011 for (
int i = 0; i < nCount; i++) {
1012 int featureIndex = *(
int *)pRun;
1013 pRun +=
sizeof(int);
1015 float *pPoint = (
float *)malloc(2 *
sizeof(
float));
1016 memcpy(pPoint, pRun, 2 *
sizeof(
float));
1017 pRun += 2 *
sizeof(float);
1019 VC_Element *pvce =
new VC_Element;
1020 pvce->index = featureIndex;
1021 pvce->pPoint = pPoint;
1023 pVCArray->push_back(pvce);
1038int Osenc::ingestCell(
OGRS57DataSource *poS57DS,
const wxString &FullPath000,
1039 const wxString &working_dir) {
1046 wxString LastUpdateDate = m_date000.Format(
"%Y%m%d");
1048 int available_updates =
1049 ValidateAndCountUpdates(FullPath000, working_dir, LastUpdateDate,
true);
1053 if (m_bVerbose && (available_updates > m_UPDN)) {
1055 msg1.Printf(
"Preparing to apply ENC updates, target final update is %3d.",
1065 char **papszReaderOptions = NULL;
1070 papszReaderOptions =
1071 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
1072 papszReaderOptions =
1073 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
1074 poS57DS->SetOptionList(papszReaderOptions);
1079 bool b_current_debug = g_bGDAL_Debug;
1080 g_bGDAL_Debug = m_bVerbose;
1083 wxString s0_file = working_dir;
1084 if (s0_file.Last() != wxFileName::GetPathSeparator())
1085 s0_file.Append(wxFileName::GetPathSeparator());
1086 wxFileName f000(FullPath000);
1088 s0_file.Append(f000.GetFullName());
1090 if (poS57DS->Open(s0_file.mb_str(), TRUE, NULL))
return 1;
1093 S57Reader *poReader = poS57DS->GetModule(0);
1095 m_last_applied_update = m_UPDN;
1096 wxString last_successful_update_file;
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();
1106 DDFModule oUpdateModule;
1107 if (!oUpdateModule.Open(m_tmpup_array[i_up].mb_str(), FALSE)) {
1110 int upResult = poReader->ApplyUpdates(&oUpdateModule, n_upd);
1114 m_last_applied_update = n_upd;
1115 last_successful_update_file = m_tmpup_array[i_up];
1130 if ((available_updates > 0) && (m_last_applied_update != available_updates)) {
1131 if (last_successful_update_file.Length()) {
1134 DDFModule oUpdateModule;
1135 wxString LastGoodUpdateDate;
1136 wxDateTime now = wxDateTime::Now();
1137 LastGoodUpdateDate = now.Format(
"%Y%m%d");
1140 oUpdateModule.Open(last_successful_update_file.mb_str(), TRUE) == 0);
1144 oUpdateModule.Rewind();
1145 DDFRecord *pr = oUpdateModule.ReadRecord();
1151 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"ISDT", 0, &nSuccess));
1155 LastGoodUpdateDate = wxString(u, wxConvUTF8);
1158 m_LastUpdateDate = LastGoodUpdateDate;
1162 wxString msg(
"WARNING---ENC Update failed. Last valid update file is:");
1163 msg += last_successful_update_file.mb_str();
1166 " This ENC exchange set should be updated and SENCs rebuilt.");
1168 if (!m_NoErrDialog) {
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);
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);
1187 g_bGDAL_Debug = b_current_debug;
1193 papszReaderOptions =
1194 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
1195 poReader->SetOptions(papszReaderOptions);
1196 CSLDestroy(papszReaderOptions);
1201int Osenc::ValidateAndCountUpdates(
const wxFileName file000,
1202 const wxString CopyDir,
1203 wxString &LastUpdateDate,
bool b_copyfiles) {
1205 wxFileName last_up_added;
1209 m_UpFiles =
new wxArrayString;
1211 s57chart::GetUpdateFileArray(file000, m_UpFiles, m_date000, m_edtn000);
1214 if (m_UpFiles->GetCount()) {
1231 unsigned int jup = 0;
1232 for (
int iff = 0; iff < retval + 1; iff++) {
1234 wxString targetFile;
1236 if (jup < m_UpFiles->GetCount()) upFile = m_UpFiles->Item(jup);
1237 wxFileName upCheck(upFile);
1239 wxString text = upCheck.GetExt();
1242 targetFile = upFile;
1245 targetFile = file000.GetFullName();
1248 wxFileName ufile(targetFile);
1250 sext.Printf(
"%03d", iff);
1254 wxString cp_ufile = CopyDir;
1255 if (cp_ufile.Last() != ufile.GetPathSeparator())
1256 cp_ufile.Append(ufile.GetPathSeparator());
1258 cp_ufile.Append(ufile.GetFullName());
1260 wxString tfile = ufile.GetFullPath();
1265 if (ufile.FileExists()) {
1266 wxFile uf(ufile.GetFullPath());
1267 if (uf.IsOpened()) {
1273 if (ufile.FileExists() &&
1277 bool cpok = wxCopyFile(ufile.GetFullPath(), cp_ufile);
1279 wxString msg(
" Cannot copy temporary working ENC file ");
1280 msg.Append(ufile.GetFullPath());
1282 msg.Append(cp_ufile);
1312 "WARNING---ENC Update chain incomplete. Substituting NULL "
1314 msg += ufile.GetFullName();
1316 wxLogMessage(
" Subsequent ENC updates may produce errors.");
1318 " This ENC exchange set should be updated and SENCs "
1323 dupdate.Initialize(
'3',
'L',
'E',
'1',
'0',
"!!!", 3, 4, 4);
1324 bstat = !(dupdate.Create(cp_ufile.mb_str()) == 0);
1328 wxString msg(
" Error creating dummy update file: ");
1329 msg.Append(cp_ufile);
1334 m_tmpup_array.Add(cp_ufile);
1335 last_up_added = cp_ufile;
1342 wxFileName lastfile(last_up_added);
1344 last_sext.Printf(
"%03d", upmax);
1345 lastfile.SetExt(last_sext);
1348 DDFModule oUpdateModule;
1351 !(oUpdateModule.Open(lastfile.GetFullPath().mb_str(), TRUE) == 0);
1355 oUpdateModule.Rewind();
1356 DDFRecord *pr = oUpdateModule.ReadRecord();
1362 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"ISDT", 0, &nSuccess));
1366 LastUpdateDate = wxString(u, wxConvUTF8);
1369 wxDateTime now = wxDateTime::Now();
1370 LastUpdateDate = now.Format(
"%Y%m%d");
1378bool Osenc::GetBaseFileAttr(
const wxString &FullPath000) {
1380 if (!oModule.Open(FullPath000.mb_str())) {
1390 DDFRecord *pr = oModule.ReadRecord();
1394 m_nGeoRecords = pr->GetIntSubfield(
"DSSI", 0,
"NOGR", 0);
1395 if (!m_nGeoRecords) {
1396 errorMessage =
"GetBaseFileAttr: DDFRecord 0 does not contain DSSI:NOGR ";
1404 char *u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"ISDT", 0));
1406 date000 = wxString(u, wxConvUTF8);
1408 errorMessage =
"GetBaseFileAttr: DDFRecord 0 does not contain DSID:ISDT ";
1413 m_date000.ParseFormat(date000,
"%Y%m%d");
1414 if (!m_date000.IsValid()) m_date000.ParseFormat(
"20000101",
"%Y%m%d");
1416 m_date000.ResetTime();
1419 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"EDTN", 0));
1421 m_edtn000 = wxString(u, wxConvUTF8);
1423 errorMessage =
"GetBaseFileAttr: DDFRecord 0 does not contain DSID:EDTN ";
1431 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"UPDN", 0));
1434 wxString tmp_updn = wxString(u, wxConvUTF8);
1435 if (tmp_updn.ToLong(&updn)) m_UPDN = updn;
1438 errorMessage =
"GetBaseFileAttr: DDFRecord 0 does not contain DSID:UPDN ";
1445 for (; pr != NULL; pr = oModule.ReadRecord()) {
1446 if (pr->FindField(
"DSPM") != NULL) {
1447 m_native_scale = pr->GetIntSubfield(
"DSPM", 0,
"CSCL", 0);
1451 if (!m_native_scale) {
1452 errorMessage =
"GetBaseFileAttr: ENC not contain DSPM:CSCL ";
1454 m_native_scale = 1000;
1466int Osenc::createSenc200(
const wxString &FullPath000,
1467 const wxString &SENCFileName,
bool b_showProg) {
1470 m_FullPath000 = FullPath000;
1472 m_senc_file_create_version = 201;
1474 if (!m_poRegistrar) {
1476 m_poRegistrar->LoadInfo(g_csv_locn.mb_str(), FALSE);
1477 m_bPrivateRegistrar =
true;
1482 wxFileName SENCfile = wxFileName(SENCFileName);
1483 wxFileName file000 = wxFileName(FullPath000);
1486 if (
true != SENCfile.DirExists(SENCfile.GetPath())) {
1487 if (!SENCfile.Mkdir(SENCfile.GetPath())) {
1489 "Cannot create SENC file directory for " + SENCfile.GetFullPath();
1491 return ERROR_CANNOT_CREATE_SENC_DIR;
1497 wxString tmp_file = tfn.CreateTempFileName(
"");
1509 if (m_pauxOutstream) {
1510 m_pOutstream = m_pauxOutstream;
1517 if (!stream->Open(tmp_file)) {
1518 errorMessage =
"Unable to create temp SENC file: ";
1519 errorMessage += tmp_file;
1520 delete m_pOutstream;
1522 return ERROR_CANNOT_CREATE_TEMP_SENC_FILE;
1527 if (!GetBaseFileAttr(FullPath000)) {
1528 delete m_pOutstream;
1530 return ERROR_BASEFILE_ATTRIBUTES;
1535 poS57DS->SetS57Registrar(m_poRegistrar);
1539 if (ingestCell(poS57DS, FullPath000, SENCfile.GetPath())) {
1540 errorMessage =
"Error ingesting: " + FullPath000;
1541 delete m_pOutstream;
1543 return ERROR_INGESTING000;
1546 S57Reader *poReader = poS57DS->GetModule(0);
1549 if (!CreateCOVRTables(poReader, m_poRegistrar)) {
1550 delete m_pOutstream;
1552 return ERROR_SENCFILE_ABORT;
1556 m_ref_lat = (m_extent.NLAT + m_extent.SLAT) / 2.;
1557 m_ref_lon = (m_extent.WLON + m_extent.ELON) / 2.;
1569 s57chart::GetChartNameFromTXT(FullPath000, nice_name);
1571 string sname =
"UTF8Error";
1572 wxCharBuffer buffer = nice_name.ToUTF8();
1573 if (buffer.data()) sname = buffer.data();
1575 if (!WriteHeaderRecord200(stream, HEADER_SENC_VERSION,
1576 (uint16_t)m_senc_file_create_version)) {
1578 delete m_pOutstream;
1580 return ERROR_SENCFILE_ABORT;
1583 if (!WriteHeaderRecord200(stream, HEADER_CELL_NAME, sname)) {
1585 delete m_pOutstream;
1587 return ERROR_SENCFILE_ABORT;
1590 wxString date000 = m_date000.Format(
"%Y%m%d");
1591 string sdata = date000.ToStdString();
1592 if (!WriteHeaderRecord200(stream, HEADER_CELL_PUBLISHDATE, sdata)) {
1594 delete m_pOutstream;
1596 return ERROR_SENCFILE_ABORT;
1600 m_edtn000.ToLong(&n000);
1601 if (!WriteHeaderRecord200(stream, HEADER_CELL_EDITION, (uint16_t)n000)) {
1603 delete m_pOutstream;
1605 return ERROR_SENCFILE_ABORT;
1608 sdata = m_LastUpdateDate.ToStdString();
1609 if (!WriteHeaderRecord200(stream, HEADER_CELL_UPDATEDATE, sdata)) {
1611 delete m_pOutstream;
1613 return ERROR_SENCFILE_ABORT;
1616 if (!WriteHeaderRecord200(stream, HEADER_CELL_UPDATE,
1617 (uint16_t)m_last_applied_update)) {
1619 delete m_pOutstream;
1621 return ERROR_SENCFILE_ABORT;
1624 if (!WriteHeaderRecord200(stream, HEADER_CELL_NATIVESCALE,
1625 (uint32_t)m_native_scale)) {
1627 delete m_pOutstream;
1629 return ERROR_SENCFILE_ABORT;
1632 wxDateTime now = wxDateTime::Now();
1633 wxString dateNow = now.Format(
"%Y%m%d");
1634 sdata = dateNow.ToStdString();
1635 if (!WriteHeaderRecord200(stream, HEADER_CELL_SENCCREATEDATE, sdata)) {
1637 delete m_pOutstream;
1639 return ERROR_SENCFILE_ABORT;
1643 if (!CreateCovrRecords(stream)) {
1645 delete m_pOutstream;
1647 return ERROR_SENCFILE_ABORT;
1655 OGRFeature *pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
1656 while (NULL != pEdgeVectorRecordFeature) {
1657 int record_id = pEdgeVectorRecordFeature->GetFieldAsInteger(
"RCID");
1659 m_vector_helper_hash[record_id] = feid;
1662 delete pEdgeVectorRecordFeature;
1663 pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
1666 wxString Message = SENCfile.GetFullPath();
1667 Message.Append(
"...Ingesting");
1669 wxString Title(_(
"OpenCPN S57 SENC File Create..."));
1670 Title.append(SENCfile.GetFullPath());
1672#if wxUSE_PROGRESSDLG
1675 int nProg = poReader->GetFeatureCount();
1677 if (wxThread::IsMain() && b_showProg) {
1678 m_ProgDialog =
new wxGenericProgressDialog();
1681 m_ProgDialog->SetFont(*qFont);
1683 m_ProgDialog->Create(Title, Message, nProg, NULL,
1684 wxPD_AUTO_HIDE | wxPD_SMOOTH);
1689 OGRFeature *objectDef;
1694 objectDef = poReader->ReadNextFeature();
1696 if (objectDef != NULL) {
1699#if wxUSE_PROGRESSDLG
1705 if (m_ProgDialog && progsw.Time() > 200) {
1709 wxString(objectDef->GetDefnRef()->GetName(), wxConvUTF8);
1710 sobj.Append(wxString::Format(
" %d/%d ", iObj, nProg));
1712 bcont = m_ProgDialog->Update(iObj, sobj);
1713#if defined(__WXMSW__) || defined(__WXOSX__)
1719 OGRwkbGeometryType geoType = wkbUnknown;
1726 if (objectDef->GetGeometryRef() != NULL)
1727 geoType = objectDef->GetGeometryRef()->getGeometryType();
1730 if (geoType != wkbUnknown) {
1731 CreateSENCRecord200(objectDef, stream, 1, poReader);
1742 CreateSENCVectorEdgeTableRecord200(stream, poReader);
1745 CreateSENCVectorConnectedTableRecord200(stream, poReader);
1750 delete m_pOutstream;
1754 for (
unsigned int iff = 0; iff < m_tmpup_array.GetCount(); iff++)
1755 remove(m_tmpup_array[iff].mb_str());
1761 wxRemoveFile(tmp_file);
1762 ret_code = ERROR_SENCFILE_ABORT;
1766 bool cpok = wxRenameFile(tmp_file, SENCfile.GetFullPath());
1768 errorMessage =
" Cannot rename temporary SENC file ";
1769 errorMessage.Append(tmp_file);
1770 errorMessage.Append(
" to ");
1771 errorMessage.Append(SENCfile.GetFullPath());
1772 ret_code = ERROR_SENCFILE_ABORT;
1774 ret_code = SENC_NO_ERROR;
1777#if wxUSE_PROGRESSDLG
1778 delete m_ProgDialog;
1789 record.record_type = CELL_EXTENT_RECORD;
1791 record.extent_sw_lat = m_extent.SLAT;
1792 record.extent_sw_lon = m_extent.WLON;
1793 record.extent_nw_lat = m_extent.NLAT;
1794 record.extent_nw_lon = m_extent.WLON;
1795 record.extent_ne_lat = m_extent.NLAT;
1796 record.extent_ne_lon = m_extent.ELON;
1797 record.extent_se_lat = m_extent.SLAT;
1798 record.extent_se_lon = m_extent.ELON;
1800 size_t targetCount =
sizeof(record);
1801 if (!stream->Write(&record, targetCount).IsOk())
return false;
1803 for (
int i = 0; i < m_nCOVREntries; i++) {
1804 int nPoints = m_pCOVRTablePoints[i];
1806 float *fpbuf = m_pCOVRTable[i];
1810 record.record_type = CELL_COVR_RECORD;
1812 (nPoints * 2 *
sizeof(float));
1815 size_t targetCount =
sizeof(record);
1816 if (!stream->Write(&record, targetCount).IsOk())
return false;
1819 targetCount =
sizeof(uint32_t);
1820 if (!stream->Write(&nPoints, targetCount).IsOk())
return false;
1823 targetCount = nPoints * 2 *
sizeof(float);
1824 if (!stream->Write(fpbuf, targetCount).IsOk())
return false;
1827 for (
int i = 0; i < m_nNoCOVREntries; i++) {
1828 int nPoints = m_pNoCOVRTablePoints[i];
1830 float *fpbuf = m_pNoCOVRTable[i];
1834 record.record_type = CELL_NOCOVR_RECORD;
1836 sizeof(uint32_t) + (nPoints * 2 *
sizeof(float));
1839 size_t targetCount =
sizeof(record);
1840 if (!stream->Write(&record, targetCount).IsOk())
return false;
1843 targetCount =
sizeof(uint32_t);
1844 if (!stream->Write(&nPoints, targetCount).IsOk())
return false;
1847 targetCount = nPoints * 2 *
sizeof(float);
1848 if (!stream->Write(fpbuf, targetCount).IsOk())
return false;
1854bool Osenc::WriteHeaderRecord200(
Osenc_outstream *stream,
int recordType,
1855 std::string payload) {
1856 int payloadLength = payload.length() + 1;
1860 unsigned char *pBuffer = getBuffer(recordLength);
1863 memset(pRecord, 0, recordLength);
1864 pRecord->record_type = recordType;
1865 pRecord->record_length = recordLength;
1866 memcpy(&pRecord->payload, payload.c_str(), payloadLength);
1868 size_t targetCount = recordLength;
1869 if (!stream->Write(pBuffer, targetCount).IsOk())
1875bool Osenc::WriteHeaderRecord200(
Osenc_outstream *stream,
int recordType,
1877 int payloadLength =
sizeof(uint16_t);
1881 unsigned char *pBuffer = getBuffer(recordLength);
1884 memset(pRecord, 0, recordLength);
1885 pRecord->record_type = recordType;
1886 pRecord->record_length = recordLength;
1887 memcpy(&pRecord->payload, &val, payloadLength);
1889 size_t targetCount = recordLength;
1890 if (!stream->Write(pBuffer, targetCount).IsOk())
1896bool Osenc::WriteHeaderRecord200(
Osenc_outstream *stream,
int recordType,
1898 int payloadLength =
sizeof(uint32_t);
1902 unsigned char *pBuffer = getBuffer(recordLength);
1905 memset(pRecord, 0, recordLength);
1906 pRecord->record_type = recordType;
1907 pRecord->record_length = recordLength;
1908 memcpy(&pRecord->payload, &val, payloadLength);
1910 size_t targetCount = recordLength;
1911 if (!stream->Write(pBuffer, targetCount).IsOk())
1917bool Osenc::WriteFIDRecord200(
Osenc_outstream *stream,
int nOBJL,
int featureID,
1920 memset(&record, 0,
sizeof(record));
1922 record.record_type = FEATURE_ID_RECORD;
1923 record.record_length =
sizeof(record);
1925 record.feature_ID = featureID;
1926 record.feature_type_code = nOBJL;
1927 record.feature_primitive = prim;
1929 size_t targetCount =
sizeof(record);
1930 if (!stream->Write(&record, targetCount).IsOk())
1936bool Osenc::CreateMultiPointFeatureGeometryRecord200(OGRFeature *pFeature,
1938 OGRGeometry *pGeo = pFeature->GetGeometryRef();
1940 int wkb_len = pGeo->WkbSize();
1941 unsigned char *pwkb_buffer = (
unsigned char *)malloc(wkb_len);
1944 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
1948 unsigned char *ps = pwkb_buffer;
1950 int nPoints = *((
int *)ps);
1952 int sb_len = (nPoints * 3 *
sizeof(float));
1954 unsigned char *psb_buffer = (
unsigned char *)malloc(sb_len);
1955 unsigned char *pd = psb_buffer;
1960 float *pdf = (
float *)pd;
1963 float lonmax = -1000;
1964 float lonmin = 1000;
1965 float latmax = -1000;
1966 float latmin = 1000;
1968 for (
int ip = 0; ip < nPoints; ip++) {
1976 OGRGeometryCollection *temp_geometry_collection =
1977 (OGRGeometryCollection *)pGeo;
1978 OGRGeometry *temp_geometry = temp_geometry_collection->getGeometryRef(ip);
1979 OGRPoint *pt_geom = (OGRPoint *)temp_geometry;
1981 double lon = pt_geom->getX();
1982 double lat = pt_geom->getY();
1983 double depth = pt_geom->getZ();
1986 double easting, northing;
1987 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
1990 float __attribute__((aligned(16))) east = easting;
1991 float __attribute__((aligned(16))) north = northing;
1992 float __attribute__((aligned(16))) deep = depth;
1993 unsigned char *puceast = (
unsigned char *)&east;
1994 unsigned char *pucnorth = (
unsigned char *)&north;
1995 unsigned char *pucdeep = (
unsigned char *)&deep;
1997 memcpy(pdf++, puceast, sizeof(
float));
1998 memcpy(pdf++, pucnorth, sizeof(
float));
1999 memcpy(pdf++, pucdeep, sizeof(
float));
2004 *pdf++ = (float)depth;
2008 lonmax = fmax(lon, lonmax);
2009 lonmin = fmin(lon, lonmin);
2010 latmax = fmax(lat, latmax);
2011 latmin = fmin(lat, latmin);
2016 record.record_type = FEATURE_GEOMETRY_RECORD_MULTIPOINT;
2018 (nPoints * 3 *
sizeof(
float));
2019 record.extent_e_lon = lonmax;
2020 record.extent_w_lon = lonmin;
2021 record.extent_n_lat = latmax;
2022 record.extent_s_lat = latmin;
2023 record.point_count = nPoints;
2026 size_t targetCount =
sizeof(record);
2027 if (!stream->Write(&record, targetCount).IsOk())
goto failure;
2029 targetCount = nPoints * 3 *
sizeof(float);
2030 if (!stream->Write(psb_buffer, targetCount).IsOk())
goto failure;
2043bool Osenc::CreateLineFeatureGeometryRecord200(
S57Reader *poReader,
2044 OGRFeature *pFeature,
2046 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2048 int wkb_len = pGeo->WkbSize();
2049 unsigned char *pwkb_buffer = (
unsigned char *)malloc(wkb_len);
2052 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
2057 ((wkb_len - 9) / 2) + 9 + 16;
2059 unsigned char *psb_buffer = (
unsigned char *)malloc(sb_len);
2060 unsigned char *pd = psb_buffer;
2061 unsigned char *ps = pwkb_buffer;
2065 int ip = *((
int *)(ps + 5));
2069 double *psd = (
double *)ps;
2070 float *pdf = (
float *)pd;
2073 float lonmax = -1000;
2074 float lonmin = 1000;
2075 float latmax = -1000;
2076 float latmin = 1000;
2078 for (
int i = 0; i < ip; i++) {
2081 double easting, northing;
2083 double __attribute__((aligned(16))) east_d, north_d;
2084 unsigned char *pucd = (
unsigned char *)psd;
2086 memcpy(&east_d, pucd, sizeof(
double));
2088 pucd += sizeof(
double);
2089 memcpy(&north_d, pucd, sizeof(
double));
2095 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
2096 unsigned char *puceasting = (
unsigned char *)&easting;
2097 unsigned char *pucnorthing = (
unsigned char *)&northing;
2099 memcpy(pdf++, puceasting, sizeof(
float));
2100 memcpy(pdf++, pucnorthing, sizeof(
float));
2103 lon = (float)*psd++;
2104 lat = (float)*psd++;
2107 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
2113 lonmax = fmax(lon, lonmax);
2114 lonmin = fmin(lon, lonmin);
2115 latmax = fmax(lat, latmax);
2116 latmin = fmin(lat, latmin);
2119 int nEdgeVectorRecords = 0;
2120 unsigned char *pvec_buffer =
2121 getObjectVectorIndexTable(poReader, pFeature, nEdgeVectorRecords);
2128 int nEdgeVectorRecords;
2129 OGRFeature *pEdgeVectorRecordFeature;
2131 pNAME_RCID = (
int *) pFeature->GetFieldAsIntegerList(
"NAME_RCID", &nEdgeVectorRecords );
2132 pORNT = (
int *) pFeature->GetFieldAsIntegerList(
"ORNT", NULL );
2139 unsigned char *pvec_buffer = (
unsigned char *)malloc(nEdgeVectorRecords * 3 *
sizeof(
int));
2140 unsigned char *pvRun = pvec_buffer;
2143 char ** papszReaderOptions = NULL;
2144 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_UPDATES,
"ON" );
2145 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_LINKAGES,
"ON" );
2146 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON" );
2147 poReader->SetOptions( papszReaderOptions );
2150 for(
int i = 0; i < nEdgeVectorRecords; i++ ) {
2152 int *pI = (
int *)pvRun;
2154 int edge_rcid = pNAME_RCID[i];
2156 int start_rcid, end_rcid;
2157 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2158 pEdgeVectorRecordFeature = poReader->ReadVector( target_record_feid, RCNM_VE );
2160 if( NULL != pEdgeVectorRecordFeature ) {
2161 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_0" );
2162 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_1" );
2168 if( !poReader->FetchPoint( RCNM_VC, start_rcid, NULL, NULL, NULL, NULL ) )
2170 if( !poReader->FetchPoint( RCNM_VC, end_rcid, NULL, NULL, NULL, NULL ) )
2173 OGRLineString *poLS = (OGRLineString *)pEdgeVectorRecordFeature->GetGeometryRef();
2177 if( edge_ornt == 1 ){
2187 delete pEdgeVectorRecordFeature;
2197 pvRun += 3 *
sizeof(int);
2202 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF" );
2203 poReader->SetOptions( papszReaderOptions );
2204 CSLDestroy( papszReaderOptions );
2209 record.record_type = FEATURE_GEOMETRY_RECORD_LINE;
2211 (nEdgeVectorRecords * 3 *
sizeof(
int));
2212 record.extent_e_lon = lonmax;
2213 record.extent_w_lon = lonmin;
2214 record.extent_n_lat = latmax;
2215 record.extent_s_lat = latmin;
2216 record.edgeVector_count = nEdgeVectorRecords;
2219 size_t targetCount =
sizeof(record);
2220 if (!stream->Write(&record, targetCount).IsOk())
return false;
2223 targetCount = nEdgeVectorRecords * 3 *
sizeof(int);
2224 if (!stream->Write(pvec_buffer, targetCount).IsOk())
return false;
2234bool Osenc::CreateAreaFeatureGeometryRecord200(
S57Reader *poReader,
2235 OGRFeature *pFeature,
2239 PolyTessGeo *ppg = NULL;
2241 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2242 OGRPolygon *poly = (OGRPolygon *)(pGeo);
2244 if (!poly->getExteriorRing())
return false;
2247 ppg =
new PolyTessGeo(poly,
true, m_ref_lat, m_ref_lon, m_LOD_meters);
2250 error_code = ppg->ErrorCode;
2254 " Warning: S57 SENC Geometry Error %d, Some Features ignored.",
2265 memset(&baseRecord, 0,
sizeof(baseRecord));
2267 baseRecord.record_type = FEATURE_GEOMETRY_RECORD_AREA;
2271 baseRecord.extent_s_lat = ppg->Get_ymin();
2272 baseRecord.extent_n_lat = ppg->Get_ymax();
2273 baseRecord.extent_e_lon = ppg->Get_xmax();
2274 baseRecord.extent_w_lon = ppg->Get_xmin();
2276 baseRecord.contour_count = ppg->GetnContours();
2280 int contourPointCountArraySize = ppg->GetnContours() *
sizeof(uint32_t);
2281 uint32_t *contourPointCountArray =
2282 (uint32_t *)malloc(contourPointCountArraySize);
2284 uint32_t *pr = contourPointCountArray;
2286 for (
int i = 0; i < ppg->GetnContours(); i++) {
2287 *pr++ = ppg->Get_PolyTriGroup_head()->pn_vertex[i];
2303 TriPrim *pTP = ppg->Get_PolyTriGroup_head()
2308 geoLength +=
sizeof(uint8_t) +
sizeof(uint32_t);
2309 geoLength += pTP->nVert * 2 *
sizeof(float);
2310 geoLength += 4 *
sizeof(double);
2316 baseRecord.triprim_count = n_TriPrims;
2318 int nEdgeVectorRecords = 0;
2319 unsigned char *pvec_buffer =
2320 getObjectVectorIndexTable(poReader, pFeature, nEdgeVectorRecords);
2327 int nEdgeVectorRecords;
2328 OGRFeature *pEdgeVectorRecordFeature;
2330 pNAME_RCID = (
int *) pFeature->GetFieldAsIntegerList(
"NAME_RCID", &nEdgeVectorRecords );
2331 pORNT = (
int *) pFeature->GetFieldAsIntegerList(
"ORNT", NULL );
2333 baseRecord.edgeVector_count = nEdgeVectorRecords;
2335 unsigned char *pvec_buffer = (
unsigned char *)malloc(nEdgeVectorRecords * 3 *
sizeof(
int));
2336 unsigned char *pvRun = pvec_buffer;
2339 char ** papszReaderOptions = NULL;
2340 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_UPDATES,
"ON" );
2341 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_LINKAGES,
"ON" );
2342 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON" );
2343 poReader->SetOptions( papszReaderOptions );
2346 for(
int i = 0; i < nEdgeVectorRecords; i++ ) {
2348 int *pI = (
int *)pvRun;
2350 int start_rcid, end_rcid;
2351 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2352 pEdgeVectorRecordFeature = poReader->ReadVector( target_record_feid, RCNM_VE );
2354 int edge_rcid = pNAME_RCID[i];
2356 if( NULL != pEdgeVectorRecordFeature ) {
2357 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_0" );
2358 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_1" );
2363 if( !poReader->FetchPoint( RCNM_VC, start_rcid, NULL, NULL, NULL, NULL ) )
2365 if( !poReader->FetchPoint( RCNM_VC, end_rcid, NULL, NULL, NULL, NULL ) )
2371 if( edge_ornt == 1 ){
2381 delete pEdgeVectorRecordFeature;
2391 pvRun += 3 *
sizeof(int);
2396 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF" );
2397 poReader->SetOptions( papszReaderOptions );
2398 CSLDestroy( papszReaderOptions );
2402 baseRecord.edgeVector_count = nEdgeVectorRecords;
2406 recordLength += contourPointCountArraySize;
2407 recordLength += geoLength;
2408 recordLength += nEdgeVectorRecords * 3 *
sizeof(int);
2409 baseRecord.record_length = recordLength;
2412 size_t targetCount =
sizeof(baseRecord);
2413 if (!stream->Write(&baseRecord, targetCount).IsOk())
return false;
2416 targetCount = contourPointCountArraySize;
2417 if (!stream->Write(contourPointCountArray, targetCount).IsOk())
return false;
2420 pTP = ppg->Get_PolyTriGroup_head()
2423 if (!stream->Write(&pTP->type,
sizeof(uint8_t)).IsOk())
return false;
2424 if (!stream->Write(&pTP->nVert,
sizeof(uint32_t)).IsOk())
return false;
2431 double minlat, minlon, maxlat, maxlon;
2432 minlat = pTP->tri_box.GetMinLat();
2433 minlon = pTP->tri_box.GetMinLon();
2434 maxlat = pTP->tri_box.GetMaxLat();
2435 maxlon = pTP->tri_box.GetMaxLon();
2437 if (!stream->Write(&minlon,
sizeof(
double)).IsOk())
return false;
2438 if (!stream->Write(&maxlon,
sizeof(
double)).IsOk())
return false;
2439 if (!stream->Write(&minlat,
sizeof(
double)).IsOk())
return false;
2440 if (!stream->Write(&maxlat,
sizeof(
double)).IsOk())
return false;
2447 if (!stream->Write(pTP->p_vertex, pTP->nVert * 2 *
sizeof(
float)).IsOk())
2454 targetCount = nEdgeVectorRecords * 3 *
sizeof(int);
2455 if (!stream->Write(pvec_buffer, targetCount).IsOk())
return false;
2458 free(contourPointCountArray);
2464unsigned char *Osenc::getObjectVectorIndexTable(
S57Reader *poReader,
2465 OGRFeature *pFeature,
2471 int nEdgeVectorRecords;
2472 OGRFeature *pEdgeVectorRecordFeature;
2475 (
int *)pFeature->GetFieldAsIntegerList(
"NAME_RCID", &nEdgeVectorRecords);
2476 pORNT = (
int *)pFeature->GetFieldAsIntegerList(
"ORNT", NULL);
2478 nEntries = nEdgeVectorRecords;
2480 unsigned char *pvec_buffer =
2481 (
unsigned char *)malloc(nEdgeVectorRecords * 3 *
sizeof(
int));
2482 unsigned char *pvRun = pvec_buffer;
2485 char **papszReaderOptions = NULL;
2486 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
"ON");
2487 papszReaderOptions =
2488 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
2489 papszReaderOptions =
2490 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
2491 poReader->SetOptions(papszReaderOptions);
2495 for (
int i = 0; i < nEdgeVectorRecords; i++) {
2496 int *pI = (
int *)pvRun;
2498 int edge_rcid = pNAME_RCID[i];
2500 int start_rcid, end_rcid;
2501 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2502 pEdgeVectorRecordFeature =
2503 poReader->ReadVector(target_record_feid, RCNM_VE);
2505 if (NULL != pEdgeVectorRecordFeature) {
2506 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_0");
2507 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_1");
2513 if (!poReader->FetchPoint(RCNM_VC, start_rcid, NULL, NULL, NULL, NULL))
2515 if (!poReader->FetchPoint(RCNM_VC, end_rcid, NULL, NULL, NULL, NULL))
2518 OGRLineString *poLS =
2519 (OGRLineString *)pEdgeVectorRecordFeature->GetGeometryRef();
2522 else if (poLS->getNumPoints() < 1)
2525 int edge_ornt = pORNT[i];
2527 if (edge_ornt == 1) {
2537 delete pEdgeVectorRecordFeature;
2547 pvRun += 3 *
sizeof(int);
2551 papszReaderOptions =
2552 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
2553 poReader->SetOptions(papszReaderOptions);
2554 CSLDestroy(papszReaderOptions);
2559void Osenc::CreateSENCVectorEdgeTableRecord200(
Osenc_outstream *stream,
2562 uint8_t *pPayload = NULL;
2563 int payloadSize = 0;
2564 uint8_t *pRun = pPayload;
2567 char **papszReaderOptions = NULL;
2568 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
"ON");
2569 papszReaderOptions =
2570 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
2571 papszReaderOptions =
2572 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
2573 poReader->SetOptions(papszReaderOptions);
2576 OGRLineString *pLS = NULL;
2578 OGRFeature *pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
2583 while (NULL != pEdgeVectorRecordFeature) {
2588 if (pEdgeVectorRecordFeature->GetGeometryRef() != NULL) {
2589 pGeo = pEdgeVectorRecordFeature->GetGeometryRef();
2590 if (pGeo->getGeometryType() == wkbLineString) {
2591 pLS = (OGRLineString *)pGeo;
2592 nPoints = pLS->getNumPoints();
2598 int new_size = payloadSize + (2 *
sizeof(int));
2599 pPayload = (uint8_t *)realloc(pPayload, new_size);
2600 pRun = pPayload + payloadSize;
2602 payloadSize = new_size;
2605 int record_id = pEdgeVectorRecordFeature->GetFieldAsInteger(
"RCID");
2606 *(
int *)pRun = record_id;
2607 pRun +=
sizeof(int);
2614 int reduction_ratio = nPoints / MAX_VECTOR_POINTS + 1;
2615 int reduced_points = 0;
2617 double *ppd = (
double *)malloc((nPoints / reduction_ratio + 1) * 2 *
2621 for (
int i = 0; i < nPoints; i++) {
2622 if (i % reduction_ratio == 0) {
2624 pLS->getPoint(i, &p);
2627 double easting, northing;
2628 toSM(p.getY(), p.getX(), m_ref_lat, m_ref_lon, &easting, &northing);
2635 nPoints = reduced_points;
2638 std::vector<int> index_keep;
2639 if (nPoints > 5 && (m_LOD_meters > .01)) {
2640 index_keep.push_back(0);
2641 index_keep.push_back(nPoints - 1);
2643 DouglasPeucker(ppd, 0, nPoints - 1, m_LOD_meters, &index_keep);
2648 index_keep.resize(nPoints);
2649 for (
int i = 0; i < nPoints; i++) index_keep[i] = i;
2653 int nPointReduced = index_keep.size();
2654 *(
int *)pRun = nPointReduced;
2655 pRun +=
sizeof(int);
2660 int new_size_red = payloadSize + (nPointReduced * 2 *
sizeof(float));
2661 pPayload = (uint8_t *)realloc(pPayload, new_size_red);
2662 pRun = pPayload + payloadSize;
2664 payloadSize = new_size_red;
2666 float *npp = (
float *)pRun;
2667 float *npp_run = npp;
2669 for (
int ip = 0; ip < nPoints; ip++) {
2673 for (
unsigned int j = 0; j < index_keep.size(); j++) {
2674 if (index_keep[j] == ip) {
2677 pRun += 2 *
sizeof(float);
2690 delete pEdgeVectorRecordFeature;
2691 pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
2700 record.record_type = VECTOR_EDGE_NODE_TABLE_RECORD;
2701 record.record_length =
2708 stream->Write(&nFeatures,
sizeof(uint32_t));
2711 stream->Write(pPayload, payloadSize);
2717 papszReaderOptions =
2718 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
2719 poReader->SetOptions(papszReaderOptions);
2720 CSLDestroy(papszReaderOptions);
2723void Osenc::CreateSENCVectorConnectedTableRecord200(
Osenc_outstream *stream,
2726 uint8_t *pPayload = NULL;
2727 int payloadSize = 0;
2728 uint8_t *pRun = pPayload;
2731 char **papszReaderOptions = NULL;
2732 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
"ON");
2733 papszReaderOptions =
2734 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
2735 papszReaderOptions =
2736 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
2737 poReader->SetOptions(papszReaderOptions);
2742 OGRFeature *pConnNodeRecordFeature = poReader->ReadVector(feid, RCNM_VC);
2743 int featureCount = 0;
2746 while (NULL != pConnNodeRecordFeature) {
2747 if (pConnNodeRecordFeature->GetGeometryRef() != NULL) {
2748 pGeo = pConnNodeRecordFeature->GetGeometryRef();
2749 if (pGeo->getGeometryType() == wkbPoint) {
2750 int new_size = payloadSize +
sizeof(int) + (2 *
sizeof(
float));
2751 pPayload = (uint8_t *)realloc(pPayload, new_size);
2752 pRun = pPayload + payloadSize;
2754 payloadSize = new_size;
2757 int record_id = pConnNodeRecordFeature->GetFieldAsInteger(
"RCID");
2758 *(
int *)pRun = record_id;
2759 pRun +=
sizeof(int);
2761 pP = (OGRPoint *)pGeo;
2764 double easting, northing;
2765 toSM(pP->getY(), pP->getX(), m_ref_lat, m_ref_lon, &easting, &northing);
2771 float *ps = (
float *)pRun;
2781 delete pConnNodeRecordFeature;
2782 pConnNodeRecordFeature = poReader->ReadVector(feid, RCNM_VC);
2791 record.record_type = VECTOR_CONNECTED_NODE_TABLE_RECORD;
2792 record.record_length =
2799 stream->Write(&featureCount,
sizeof(uint32_t));
2802 stream->Write(pPayload, payloadSize);
2809 papszReaderOptions =
2810 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
2811 poReader->SetOptions(papszReaderOptions);
2812 CSLDestroy(papszReaderOptions);
2815bool Osenc::CreateSENCRecord200(OGRFeature *pFeature,
Osenc_outstream *stream,
2824 OGRFeatureDefn *pFD = pFeature->GetDefnRef();
2825 int nOBJL = pFD->GetOBJL();
2827 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2828 OGRwkbGeometryType gType = pGeo->getGeometryType();
2833 primitive = GEO_LINE;
2836 primitive = GEO_POINT;
2839 primitive = GEO_AREA;
2845 if (!WriteFIDRecord200(stream, nOBJL, pFeature->GetFID(), primitive))
2848#define MAX_HDR_LINE 400
2864 if (pFeature->GetFID() == 290)
int yyp = 4;
2866 int payloadLength = 0;
2867 void *payloadBuffer = NULL;
2868 unsigned int payloadBufferLength = 0;
2870 for (
int iField = 0; iField < pFeature->GetFieldCount(); iField++) {
2871 if (pFeature->IsFieldSet(iField)) {
2872 if ((iField == 1) || (iField > 7)) {
2873 OGRFieldDefn *poFDefn = pFeature->GetDefnRef()->GetFieldDefn(iField);
2876 const char *pAttrName = poFDefn->GetNameRef();
2877 const char *pAttrVal = pFeature->GetFieldAsString(iField);
2885 int OGRvalueType = (int)poFDefn->GetType();
2889 if (-1 == attributeID) {
2890 if (!strncmp(pAttrName,
"PRIM", 4)) {
2891 attributeID = ATTRIBUTE_ID_PRIM;
OFTInteger = 0, OFTIntegerList = 1, OFTReal = 2, OFTRealList = 3, OFTString = 4, OFTStringList = 5, OFTWideString = 6, OFTWideStringList = 7, OFTBinary = 8
2906 switch (OGRvalueType) {
2909 valueType = OGRvalueType;
2911 if (payloadBufferLength < 4) {
2912 payloadBuffer = realloc(payloadBuffer, 4);
2913 payloadBufferLength = 4;
2916 int aValue = pFeature->GetFieldAsInteger(iField);
2917 memcpy(payloadBuffer, &aValue,
sizeof(
int));
2918 payloadLength =
sizeof(int);
2924 valueType = OGRvalueType;
2927 const int *aValueList =
2928 pFeature->GetFieldAsIntegerList(iField, &nCount);
2930 if (payloadBufferLength < nCount *
sizeof(
int)) {
2931 payloadBuffer = realloc(payloadBuffer, nCount *
sizeof(
int));
2932 payloadBufferLength = nCount *
sizeof(int);
2935 int *pBuffRun = (
int *)payloadBuffer;
2936 for (
int i = 0; i < nCount; i++) {
2937 *pBuffRun++ = aValueList[i];
2939 payloadLength = nCount *
sizeof(int);
2945 valueType = OGRvalueType;
2947 if (payloadBufferLength <
sizeof(
double)) {
2948 payloadBuffer = realloc(payloadBuffer,
sizeof(
double));
2949 payloadBufferLength =
sizeof(double);
2952 double aValue = pFeature->GetFieldAsDouble(iField);
2953 memcpy(payloadBuffer, &aValue,
sizeof(
double));
2954 payloadLength =
sizeof(double);
2961 valueType = OGRvalueType;
2964 const double *aValueList =
2965 pFeature->GetFieldAsDoubleList(iField, &nCount);
2967 if (payloadBufferLength < nCount *
sizeof(
double)) {
2968 payloadBuffer = realloc(payloadBuffer, nCount *
sizeof(
double));
2969 payloadBufferLength = nCount *
sizeof(double);
2972 double *pBuffRun = (
double *)payloadBuffer;
2973 for (
int i = 0; i < nCount; i++) {
2974 *pBuffRun++ = aValueList[i];
2976 payloadLength = nCount *
sizeof(double);
2983 valueType = OGRvalueType;
2984 const char *pAttrVal = pFeature->GetFieldAsString(iField);
2986 wxString wxAttrValue;
2988 if ((0 == strncmp(
"NOBJNM", pAttrName, 6)) ||
2989 (0 == strncmp(
"NINFOM", pAttrName, 6)) ||
2990 (0 == strncmp(
"NPLDST", pAttrName, 6)) ||
2991 (0 == strncmp(
"NTXTDS", pAttrName, 6))) {
2992 if (poReader->GetNall() ==
2995 wxString att_conv(pAttrVal, conv);
2996 att_conv.RemoveLast();
2998 att_conv.Replace(
"\n",
3001 wxAttrValue = att_conv;
3002 }
else if (poReader->GetNall() ==
3004 wxCSConv conv(
"iso8859-1");
3005 wxString att_conv(pAttrVal, conv);
3006 wxAttrValue = att_conv;
3009 if (poReader->GetAall() ==
3012 wxCSConv conv(
"iso8859-1");
3013 wxString att_conv(pAttrVal, conv);
3014 wxAttrValue = att_conv;
3021 unsigned int stringPayloadLength = 0;
3023 wxCharBuffer buffer;
3024 if (wxAttrValue.Length()) {
3025 buffer = wxAttrValue.ToUTF8();
3026 pAttrVal = buffer.data();
3027 stringPayloadLength = strlen(buffer.data());
3030 if (stringPayloadLength) {
3031 if (payloadBufferLength < stringPayloadLength + 1) {
3032 payloadBuffer = realloc(payloadBuffer, stringPayloadLength + 1);
3033 payloadBufferLength = stringPayloadLength + 1;
3036 strcpy((
char *)payloadBuffer, pAttrVal);
3037 payloadLength = stringPayloadLength + 1;
3049 if (-1 != attributeID) {
3055 unsigned char *pBuffer = getBuffer(recordLength);
3059 pRecord->record_type = FEATURE_ATTRIBUTE_RECORD;
3060 pRecord->record_length = recordLength;
3061 pRecord->attribute_type = attributeID;
3062 pRecord->attribute_value_type = valueType;
3063 memcpy(&pRecord->payload, payloadBuffer, payloadLength);
3066 size_t targetCount = recordLength;
3067 if (!stream->Write(pBuffer, targetCount).IsOk()) {
3068 free(payloadBuffer);
3075 if (wkbPoint == pGeo->getGeometryType()) {
3076 OGRPoint *pp = (OGRPoint *)pGeo;
3077 int nqual = pp->getnQual();
3082 if (-1 != attributeID) {
3083 if (payloadBufferLength < 4) {
3084 payloadBuffer = realloc(payloadBuffer, 4);
3085 payloadBufferLength = 4;
3088 memcpy(payloadBuffer, &nqual,
sizeof(
int));
3089 payloadLength =
sizeof(int);
3094 unsigned char *pBuffer = getBuffer(recordLength);
3098 pRecord->record_type = FEATURE_ATTRIBUTE_RECORD;
3099 pRecord->record_length = recordLength;
3100 pRecord->attribute_type = attributeID;
3101 pRecord->attribute_value_type = valueType;
3102 memcpy(&pRecord->payload, payloadBuffer, payloadLength);
3105 size_t targetCount = recordLength;
3106 if (!stream->Write(pBuffer, targetCount).IsOk()) {
3107 free(payloadBuffer);
3114 free(payloadBuffer);
3119 if( wkbPoint == pGeo->getGeometryType() ) {
3120 OGRPoint *pp = (OGRPoint *) pGeo;
3121 int nqual = pp->getnQual();
3124 snprintf( line, MAX_HDR_LINE - 2,
" %s (%c) = %d",
"QUALTY",
'I', nqual );
3125 sheader += wxString( line, wxConvUTF8 );
3132 sprintf( line,
" %s %f %f\n", pGeo->getGeometryName(), m_ref_lat, m_ref_lon );
3133 sheader += wxString( line, wxConvUTF8 );
3136 wxCharBuffer buffer=sheader.ToUTF8();
3137 fprintf( fpOut,
"HDRLEN=%lu\n", (
unsigned long) strlen(buffer) );
3138 fwrite( buffer.data(), 1, strlen(buffer), fpOut );
3142 if ((pGeo != NULL)) {
3145 OGRwkbGeometryType gType = pGeo->getGeometryType();
3147 case wkbLineString: {
3148 if (!CreateLineFeatureGeometryRecord200(poReader, pFeature, stream))
3156 record.record_type = FEATURE_GEOMETRY_RECORD_POINT;
3157 record.record_length =
sizeof(record);
3159 int wkb_len = pGeo->WkbSize();
3160 unsigned char *pwkb_buffer = (
unsigned char *)malloc(wkb_len);
3163 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
3166 unsigned char *ps = pwkb_buffer;
3169 double *psd = (
double *)ps;
3173 __attribute__((aligned(16))) double lata, lona;
3174 unsigned char *pucsd = (
unsigned char *)psd;
3176 memcpy(&lona, pucsd, sizeof(
double));
3177 pucsd += sizeof(
double);
3178 memcpy(&lata, pucsd, sizeof(
double));
3192 size_t targetCount = record.record_length;
3193 if (!stream->Write(&record, targetCount).IsOk())
return false;
3199 case wkbMultiPoint25D: {
3200 if (!CreateMultiPointFeatureGeometryRecord200(pFeature, stream))
3207 if (!CreateAreaFeatureGeometryRecord200(poReader, pFeature, stream)) {
3209 msga.Printf(
"Error in S57 cell file: %s\n",
3210 m_FullPath000.ToStdString().c_str());
3219 msg =
" Warning: Unimplemented ogr geotype record ";
3232 unsigned char **next_byte) {
3233 PolyTessGeo *pPTG =
new PolyTessGeo();
3235 pPTG->SetExtents(record->extent_w_lon, record->extent_s_lat,
3236 record->extent_e_lon, record->extent_n_lat);
3238 unsigned int n_TriPrim = record->triprim_count;
3239 int nContours = record->contour_count;
3242 void *payLoad = &record->payLoad;
3248 PolyTriGroup *ppg =
new PolyTriGroup;
3249 ppg->m_bSMSENC =
true;
3250 ppg->data_type = DATA_TYPE_DOUBLE;
3252 ppg->nContours = nContours;
3254 ppg->pn_vertex = (
int *)malloc(nContours *
sizeof(
int));
3255 int *pctr = ppg->pn_vertex;
3258 int *contour_pointcount_array_run = (
int *)payLoad;
3259 for (
int i = 0; i < nContours; i++) {
3260 *pctr++ = *contour_pointcount_array_run++;
3264 ppg->pgroup_geom = NULL;
3268 TriPrim **p_prev_triprim = &(ppg->tri_prim_head);
3271 unsigned int tri_type;
3274 int total_byte_size = 2 *
sizeof(float);
3276 uint8_t *pPayloadRun =
3277 (uint8_t *)contour_pointcount_array_run;
3280 for (
unsigned int i = 0; i < n_TriPrim; i++) {
3281 tri_type = *pPayloadRun++;
3282 nvert = *(uint32_t *)pPayloadRun;
3283 pPayloadRun +=
sizeof(uint32_t);
3285 TriPrim *tp =
new TriPrim;
3286 *p_prev_triprim = tp;
3287 p_prev_triprim = &(tp->p_next);
3290 tp->type = tri_type;
3294 wxMax(nvert_max, nvert);
3297 double *pbb = (
double *)pPayloadRun;
3299 double minxt, minyt, maxxt, maxyt;
3302 double __attribute__((aligned(16))) abox[4];
3303 unsigned char *pucbb = (
unsigned char *)pPayloadRun;
3304 memcpy(&abox[0], pucbb, 4 * sizeof(
double));
3317 tp->tri_box.Set(minyt, minxt, maxyt, maxxt);
3319 pPayloadRun += 4 *
sizeof(double);
3321 int byte_size = nvert * 2 *
sizeof(float);
3322 total_byte_size += byte_size;
3324 tp->p_vertex = (
double *)pPayloadRun;
3326 pPayloadRun += byte_size;
3329 if (next_byte) *next_byte = pPayloadRun;
3333 unsigned char *vbuf = (
unsigned char *)malloc(total_byte_size);
3335 TriPrim *p_tp = ppg->tri_prim_head;
3336 unsigned char *p_run = vbuf;
3338 memcpy(p_run, p_tp->p_vertex, p_tp->nVert * 2 *
sizeof(
float));
3339 p_tp->p_vertex = (
double *)p_run;
3340 p_run += p_tp->nVert * 2 *
sizeof(float);
3341 p_tp = p_tp->p_next;
3343 ppg->bsingle_alloc =
true;
3344 ppg->single_buffer = vbuf;
3345 ppg->single_buffer_size = total_byte_size;
3346 ppg->data_type = DATA_TYPE_FLOAT;
3348 pPTG->SetPPGHead(ppg);
3349 pPTG->SetnVertexMax(nvert_max);
3356bool Osenc::CreateCOVRTables(
S57Reader *poReader,
3362 float LatMax, LatMin, LonMax, LonMin;
3368 m_pCOVRTablePoints = NULL;
3369 m_pCOVRTable = NULL;
3372 MyFloatPtrArray *pAuxPtrArray =
new MyFloatPtrArray;
3373 std::vector<int> auxCntArray, noCovrCntArray;
3375 MyFloatPtrArray *pNoCovrPtrArray =
new MyFloatPtrArray;
3378 pFeat = GetChartFirstM_COVR(catcov, poReader, poRegistrar);
3383 OGRPolygon *poly = (OGRPolygon *)(pFeat->GetGeometryRef());
3384 OGRLinearRing *xring = poly->getExteriorRing();
3386 int npt = xring->getNumPoints();
3391 pf = (
float *)malloc(2 * npt *
sizeof(
float));
3394 for (
int i = 0; i < npt; i++) {
3396 xring->getPoint(i, &p);
3399 LatMax = fmax(LatMax, p.getY());
3400 LatMin = fmin(LatMin, p.getY());
3401 LonMax = fmax(LonMax, p.getX());
3402 LonMin = fmin(LonMin, p.getX());
3412 pAuxPtrArray->Add(pf);
3413 auxCntArray.push_back(npt);
3414 }
else if (catcov == 2) {
3415 pNoCovrPtrArray->Add(pf);
3416 noCovrCntArray.push_back(npt);
3422 pFeat = GetChartNextM_COVR(catcov, poReader);
3427 m_nCOVREntries = auxCntArray.size();
3432 if (m_nCOVREntries == 1) {
3433 m_pCOVRTablePoints = (
int *)malloc(
sizeof(
int));
3434 *m_pCOVRTablePoints = auxCntArray[0];
3435 m_pCOVRTable = (
float **)malloc(
sizeof(
float *));
3436 *m_pCOVRTable = (
float *)malloc(auxCntArray[0] * 2 *
sizeof(
float));
3437 memcpy(*m_pCOVRTable, pAuxPtrArray->Item(0),
3438 auxCntArray[0] * 2 *
sizeof(
float));
3441 else if (m_nCOVREntries > 1) {
3443 m_pCOVRTablePoints = (
int *)malloc(m_nCOVREntries *
sizeof(
int));
3444 m_pCOVRTable = (
float **)malloc(m_nCOVREntries *
sizeof(
float *));
3446 for (
unsigned int j = 0; j < (
unsigned int)m_nCOVREntries; j++) {
3447 m_pCOVRTablePoints[j] = auxCntArray[j];
3448 m_pCOVRTable[j] = (
float *)malloc(auxCntArray[j] * 2 *
sizeof(
float));
3449 memcpy(m_pCOVRTable[j], pAuxPtrArray->Item(j),
3450 auxCntArray[j] * 2 *
sizeof(
float));
3456 wxString msg(
" ENC contains no useable M_COVR, CATCOV=1 features: ");
3457 msg.Append(m_FullPath000);
3462 m_nNoCOVREntries = noCovrCntArray.size();
3464 if (m_nNoCOVREntries) {
3466 m_pNoCOVRTablePoints = (
int *)malloc(m_nNoCOVREntries *
sizeof(
int));
3467 m_pNoCOVRTable = (
float **)malloc(m_nNoCOVREntries *
sizeof(
float *));
3469 for (
unsigned int j = 0; j < (
unsigned int)m_nNoCOVREntries; j++) {
3470 int npoints = noCovrCntArray[j];
3471 m_pNoCOVRTablePoints[j] = npoints;
3472 m_pNoCOVRTable[j] = (
float *)malloc(npoints * 2 *
sizeof(
float));
3473 memcpy(m_pNoCOVRTable[j], pNoCovrPtrArray->Item(j),
3474 npoints * 2 *
sizeof(
float));
3477 m_pNoCOVRTablePoints = NULL;
3478 m_pNoCOVRTable = NULL;
3481 for (
unsigned int j = 0; j < (
unsigned int)m_nNoCOVREntries; j++)
3482 free(pNoCovrPtrArray->Item(j));
3483 for (
unsigned int j = 0; j < (
unsigned int)m_nCOVREntries; j++)
3484 free(pAuxPtrArray->Item(j));
3486 delete pAuxPtrArray;
3487 delete pNoCovrPtrArray;
3489 if (0 == m_nCOVREntries) {
3490 wxString msg(
" ENC contains no M_COVR features: ");
3491 msg.Append(m_FullPath000);
3494 msg =
" Calculating Chart Extents as fallback.";
3499 if (poReader->GetExtent(&Env,
true) == OGRERR_NONE) {
3506 m_pCOVRTablePoints = (
int *)malloc(
sizeof(
int));
3507 *m_pCOVRTablePoints = 4;
3508 m_pCOVRTable = (
float **)malloc(
sizeof(
float *));
3509 float *pf = (
float *)malloc(2 * 4 *
sizeof(
float));
3526 wxString msg(
" Cannot calculate Extents for ENC: ");
3527 msg.Append(m_FullPath000);
3535 m_extent.NLAT = LatMax;
3536 m_extent.SLAT = LatMin;
3537 m_extent.ELON = LonMax;
3538 m_extent.WLON = LonMin;
3543OGRFeature *Osenc::GetChartFirstM_COVR(
int &catcov,
S57Reader *pENCReader,
3545 OGRFeature *rv = NULL;
3547 if ((NULL != pENCReader) && (NULL != poRegistrar)) {
3549 poRegistrar->SelectClass(
"M_COVR");
3555 bool bFound =
false;
3556 OGRFeature *pobjectDef = pENCReader->ReadNextFeature();
3559 OGRFeatureDefn *poDefn = pobjectDef->GetDefnRef();
3560 if (poDefn && (poDefn->GetOBJL() == 302 )) {
3562 catcov = pobjectDef->GetFieldAsInteger(
"CATCOV");
3570 pobjectDef = pENCReader->ReadNextFeature();
3577OGRFeature *Osenc::GetChartNextM_COVR(
int &catcov,
S57Reader *pENCReader) {
3581 bool bFound =
false;
3582 OGRFeature *pobjectDef = pENCReader->ReadNextFeature();
3586 OGRFeatureDefn *poDefn = pobjectDef->GetDefnRef();
3587 if (poDefn && (poDefn->GetOBJL() == 302)) {
3589 catcov = pobjectDef->GetFieldAsInteger(
"CATCOV");
3597 pobjectDef = pENCReader->ReadNextFeature();
3604int Osenc::GetBaseFileInfo(
const wxString &FullPath000,
3605 const wxString &SENCFileName) {
3606 wxFileName SENCfile = wxFileName(SENCFileName);
3610 if (!GetBaseFileAttr(FullPath000)) {
3611 return ERROR_BASEFILE_ATTRIBUTES;
3615 oS57DS.SetS57Registrar(m_poRegistrar);
3617 bool b_current_debug = g_bGDAL_Debug;
3618 g_bGDAL_Debug =
false;
3622 if (ingestCell(&oS57DS, FullPath000, SENCfile.GetPath())) {
3623 errorMessage =
"Error ingesting: " + FullPath000;
3624 return ERROR_INGESTING000;
3627 S57Reader *poReader = oS57DS.GetModule(0);
3629 CalculateExtent(poReader, m_poRegistrar);
3631 g_bGDAL_Debug = b_current_debug;
3635 return SENC_NO_ERROR;
3638bool Osenc::CalculateExtent(
S57Reader *poReader,
3644 float LatMax, LatMin, LonMax, LonMin;
3650 m_pCOVRTablePoints = NULL;
3651 m_pCOVRTable = NULL;
3661 pFeat = GetChartFirstM_COVR(catcov, poReader, poRegistrar);
3666 OGRPolygon *poly = (OGRPolygon *)(pFeat->GetGeometryRef());
3667 OGRLinearRing *xring = poly->getExteriorRing();
3669 int npt = xring->getNumPoints();
3672 for (
int i = 0; i < npt; i++) {
3674 xring->getPoint(i, &p);
3677 LatMax = fmax(LatMax, p.getY());
3678 LatMin = fmin(LatMin, p.getY());
3679 LonMax = fmax(LonMax, p.getX());
3680 LonMin = fmin(LonMin, p.getX());
3686 pFeat = GetChartNextM_COVR(catcov, poReader);
3690 m_extent.NLAT = LatMax;
3691 m_extent.SLAT = LatMin;
3692 m_extent.ELON = LonMax;
3693 m_extent.WLON = LonMin;
3698void Osenc::InitializePersistentBuffer() {
3699 pBuffer = (
unsigned char *)malloc(1024);
3703unsigned char *Osenc::getBuffer(
size_t length) {
3704 if (length > bufferSize) {
3705 pBuffer = (
unsigned char *)realloc(pBuffer, length * 2);
3706 bufferSize = length * 2;
s57RegistrarMgr Definition This is a class holding the ctor and dtor for the global registrar
Global variables stored in configuration file.
Extern C linked utilities.
wxFont * GetOCPNScaledFont(wxString item, int default_size)
Retrieves a font from FontMgr, optionally scaled for physical readability.
General purpose GUI support.
Miscellaneous globals primarely used by gui layer, not persisted in configuration file.
s57RegistrarMgr * m_pRegistrarMan
Global instance.
s57RegistrarMgr * m_pRegistrarMan
Global instance.
Declarations for classes binding S57 support onto OGRLayer, OGRDataSource and OGRDriver.