35#include <wx/wfstream.h>
36#include <wx/filename.h>
37#include <wx/progdlg.h>
43#include "s57RegistrarMgr.h"
44#include "gdal/cpl_csv.h"
46#include "gdal/cpl_string.h"
47#include "LOD_reduce.h"
55extern wxString g_csv_locn;
56extern bool g_bGDAL_Debug;
58bool chain_broken_mssage_shown =
false;
62#include <wx/arrimpl.cpp>
63WX_DEFINE_ARRAY(
float *, MyFloatPtrArray);
65#define MAX_VECTOR_POINTS 1000
68sigjmp_buf env_osenc_ogrf;
79void OpenCPN_OGR_OSENC_ErrorHandler(CPLErr eErrClass,
int nError,
80 const char *pszErrorMsg) {
81#define ERR_BUF_LEN 2000
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();
219void Osenc::init(
void) {
221 m_poRegistrar = NULL;
222 m_bPrivateRegistrar =
false;
223 m_senc_file_read_version = 0;
225 InitializePersistentBuffer();
230 m_read_base_edtn = _T(
"-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(_T(
"%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) {
506 if (m_pRegistrarMan) {
507 std::string acronym = m_pRegistrarMan->getFeatureAcronym(typeCode);
508 return acronym.c_str();
513std::string Osenc::GetAttributeAcronymFromTypecode(
int typeCode) {
515 return m_pRegistrarMan->getAttributeAcronym(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(_T(
"%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(_T(
"%Y%m%d"));
1048 int available_updates =
1049 ValidateAndCountUpdates(FullPath000, working_dir, LastUpdateDate,
true);
1053 if (m_bVerbose && (available_updates > m_UPDN)) {
1056 _T(
"Preparing to apply ENC updates, target final update is %3d."),
1066 char **papszReaderOptions = NULL;
1071 papszReaderOptions =
1072 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
1073 papszReaderOptions =
1074 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
1075 poS57DS->SetOptionList(papszReaderOptions);
1080 bool b_current_debug = g_bGDAL_Debug;
1081 g_bGDAL_Debug = m_bVerbose;
1084 wxString s0_file = working_dir;
1085 if (s0_file.Last() != wxFileName::GetPathSeparator())
1086 s0_file.Append(wxFileName::GetPathSeparator());
1087 wxFileName f000(FullPath000);
1089 s0_file.Append(f000.GetFullName());
1091 if (poS57DS->Open(s0_file.mb_str(), TRUE, NULL))
return 1;
1094 S57Reader *poReader = poS57DS->GetModule(0);
1096 m_last_applied_update = m_UPDN;
1097 wxString last_successful_update_file;
1100 for (
unsigned int i_up = 0; i_up < m_tmpup_array.GetCount(); i_up++) {
1101 wxFileName fn(m_tmpup_array[i_up]);
1102 wxString ext = fn.GetExt();
1107 DDFModule oUpdateModule;
1108 if (!oUpdateModule.Open(m_tmpup_array[i_up].mb_str(), FALSE)) {
1111 int upResult = poReader->ApplyUpdates(&oUpdateModule, n_upd);
1115 m_last_applied_update = n_upd;
1116 last_successful_update_file = m_tmpup_array[i_up];
1131 if ((available_updates > 0) && (m_last_applied_update != available_updates)) {
1132 if (last_successful_update_file.Length()) {
1135 DDFModule oUpdateModule;
1136 wxString LastGoodUpdateDate;
1137 wxDateTime now = wxDateTime::Now();
1138 LastGoodUpdateDate = now.Format(_T(
"%Y%m%d"));
1141 oUpdateModule.Open(last_successful_update_file.mb_str(), TRUE) == 0);
1145 oUpdateModule.Rewind();
1146 DDFRecord *pr = oUpdateModule.ReadRecord();
1152 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"ISDT", 0, &nSuccess));
1156 LastGoodUpdateDate = wxString(u, wxConvUTF8);
1159 m_LastUpdateDate = LastGoodUpdateDate;
1164 _T(
"WARNING---ENC Update failed. Last valid update file is:"));
1165 msg += last_successful_update_file.mb_str();
1168 _T(
" This ENC exchange set should be updated and SENCs rebuilt."));
1170 if (!m_NoErrDialog) {
1173 _(
"S57 Cell Update failed.\nENC features may be incomplete or "
1174 "inaccurate.\n\nCheck the logfile for details."),
1175 _(
"OpenCPN Create SENC Warning"), wxOK | wxICON_EXCLAMATION, 5);
1179 OCPNMessageBox(NULL,
1180 _(
"S57 Cell Update failed.\nNo updates could be "
1181 "applied.\nENC features may be incomplete or "
1182 "inaccurate.\n\nCheck the logfile for details."),
1183 _(
"OpenCPN Create SENC Warning"),
1184 wxOK | wxICON_EXCLAMATION, 5);
1189 g_bGDAL_Debug = b_current_debug;
1195 papszReaderOptions =
1196 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
1197 poReader->SetOptions(papszReaderOptions);
1198 CSLDestroy(papszReaderOptions);
1200 wxRemoveFile(s0_file);
1205int Osenc::ValidateAndCountUpdates(
const wxFileName file000,
1206 const wxString CopyDir,
1207 wxString &LastUpdateDate,
bool b_copyfiles) {
1209 wxFileName last_up_added;
1213 m_UpFiles =
new wxArrayString;
1215 s57chart::GetUpdateFileArray(file000, m_UpFiles, m_date000, m_edtn000);
1218 if (m_UpFiles->GetCount()) {
1235 unsigned int jup = 0;
1236 for (
int iff = 0; iff < retval + 1; iff++) {
1238 wxString targetFile;
1240 if (jup < m_UpFiles->GetCount()) upFile = m_UpFiles->Item(jup);
1241 wxFileName upCheck(upFile);
1243 wxString text = upCheck.GetExt();
1246 targetFile = upFile;
1249 targetFile = file000.GetFullName();
1252 wxFileName ufile(targetFile);
1254 sext.Printf(_T(
"%03d"), iff);
1258 wxString cp_ufile = CopyDir;
1259 if (cp_ufile.Last() != ufile.GetPathSeparator())
1260 cp_ufile.Append(ufile.GetPathSeparator());
1262 cp_ufile.Append(ufile.GetFullName());
1264 wxString tfile = ufile.GetFullPath();
1269 if (ufile.FileExists()) {
1270 wxFile uf(ufile.GetFullPath());
1271 if (uf.IsOpened()) {
1277 if (ufile.FileExists() &&
1281 bool cpok = wxCopyFile(ufile.GetFullPath(), cp_ufile);
1283 wxString msg(_T(
" Cannot copy temporary working ENC file "));
1284 msg.Append(ufile.GetFullPath());
1285 msg.Append(_T(
" to "));
1286 msg.Append(cp_ufile);
1316 _T(
"WARNING---ENC Update chain incomplete. Substituting NULL ")
1317 _T(
"update file: "));
1318 msg += ufile.GetFullName();
1320 wxLogMessage(_T(
" Subsequent ENC updates may produce errors."));
1322 _T(
" This ENC exchange set should be updated and SENCs ")
1327 dupdate.Initialize(
'3',
'L',
'E',
'1',
'0',
"!!!", 3, 4, 4);
1328 bstat = !(dupdate.Create(cp_ufile.mb_str()) == 0);
1332 wxString msg(_T(
" Error creating dummy update file: "));
1333 msg.Append(cp_ufile);
1338 m_tmpup_array.Add(cp_ufile);
1339 last_up_added = cp_ufile;
1346 wxFileName lastfile(last_up_added);
1348 last_sext.Printf(_T(
"%03d"), upmax);
1349 lastfile.SetExt(last_sext);
1352 DDFModule oUpdateModule;
1355 !(oUpdateModule.Open(lastfile.GetFullPath().mb_str(), TRUE) == 0);
1359 oUpdateModule.Rewind();
1360 DDFRecord *pr = oUpdateModule.ReadRecord();
1366 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"ISDT", 0, &nSuccess));
1370 LastUpdateDate = wxString(u, wxConvUTF8);
1373 wxDateTime now = wxDateTime::Now();
1374 LastUpdateDate = now.Format(_T(
"%Y%m%d"));
1382bool Osenc::GetBaseFileAttr(
const wxString &FullPath000) {
1384 if (!oModule.Open(FullPath000.mb_str())) {
1394 DDFRecord *pr = oModule.ReadRecord();
1398 m_nGeoRecords = pr->GetIntSubfield(
"DSSI", 0,
"NOGR", 0);
1399 if (!m_nGeoRecords) {
1401 _T(
"GetBaseFileAttr: DDFRecord 0 does not contain DSSI:NOGR ");
1409 char *u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"ISDT", 0));
1411 date000 = wxString(u, wxConvUTF8);
1414 _T(
"GetBaseFileAttr: DDFRecord 0 does not contain DSID:ISDT ");
1419 m_date000.ParseFormat(date000, _T(
"%Y%m%d"));
1420 if (!m_date000.IsValid()) m_date000.ParseFormat(_T(
"20000101"), _T(
"%Y%m%d"));
1422 m_date000.ResetTime();
1425 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"EDTN", 0));
1427 m_edtn000 = wxString(u, wxConvUTF8);
1430 _T(
"GetBaseFileAttr: DDFRecord 0 does not contain DSID:EDTN ");
1432 m_edtn000 = _T(
"1");
1438 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"UPDN", 0));
1441 wxString tmp_updn = wxString(u, wxConvUTF8);
1442 if (tmp_updn.ToLong(&updn)) m_UPDN = updn;
1446 _T(
"GetBaseFileAttr: DDFRecord 0 does not contain DSID:UPDN ");
1453 for (; pr != NULL; pr = oModule.ReadRecord()) {
1454 if (pr->FindField(
"DSPM") != NULL) {
1455 m_native_scale = pr->GetIntSubfield(
"DSPM", 0,
"CSCL", 0);
1459 if (!m_native_scale) {
1460 errorMessage = _T(
"GetBaseFileAttr: ENC not contain DSPM:CSCL ");
1462 m_native_scale = 1000;
1474int Osenc::createSenc200(
const wxString &FullPath000,
1475 const wxString &SENCFileName,
bool b_showProg) {
1478 m_FullPath000 = FullPath000;
1480 m_senc_file_create_version = 201;
1482 if (!m_poRegistrar) {
1484 m_poRegistrar->LoadInfo(g_csv_locn.mb_str(), FALSE);
1485 m_bPrivateRegistrar =
true;
1490 wxFileName SENCfile = wxFileName(SENCFileName);
1491 wxFileName file000 = wxFileName(FullPath000);
1494 if (
true != SENCfile.DirExists(SENCfile.GetPath())) {
1495 if (!SENCfile.Mkdir(SENCfile.GetPath())) {
1497 _T(
"Cannot create SENC file directory for ") + SENCfile.GetFullPath();
1499 return ERROR_CANNOT_CREATE_SENC_DIR;
1505 wxString tmp_file = tfn.CreateTempFileName(_T(
""));
1517 if (m_pauxOutstream) {
1518 m_pOutstream = m_pauxOutstream;
1525 if (!stream->Open(tmp_file)) {
1526 errorMessage = _T(
"Unable to create temp SENC file: ");
1527 errorMessage += tmp_file;
1529 return ERROR_CANNOT_CREATE_TEMP_SENC_FILE;
1534 if (!GetBaseFileAttr(FullPath000)) {
1536 return ERROR_BASEFILE_ATTRIBUTES;
1541 poS57DS->SetS57Registrar(m_poRegistrar);
1545 if (ingestCell(poS57DS, FullPath000, SENCfile.GetPath())) {
1546 errorMessage = _T(
"Error ingesting: ") + FullPath000;
1548 return ERROR_INGESTING000;
1551 S57Reader *poReader = poS57DS->GetModule(0);
1554 if (!CreateCOVRTables(poReader, m_poRegistrar)) {
1556 return ERROR_SENCFILE_ABORT;
1560 m_ref_lat = (m_extent.NLAT + m_extent.SLAT) / 2.;
1561 m_ref_lon = (m_extent.WLON + m_extent.ELON) / 2.;
1573 s57chart::GetChartNameFromTXT(FullPath000, nice_name);
1575 string sname =
"UTF8Error";
1576 wxCharBuffer buffer = nice_name.ToUTF8();
1577 if (buffer.data()) sname = buffer.data();
1579 if (!WriteHeaderRecord200(stream, HEADER_SENC_VERSION,
1580 (uint16_t)m_senc_file_create_version)) {
1583 return ERROR_SENCFILE_ABORT;
1586 if (!WriteHeaderRecord200(stream, HEADER_CELL_NAME, sname)) {
1589 return ERROR_SENCFILE_ABORT;
1592 wxString date000 = m_date000.Format(_T(
"%Y%m%d"));
1593 string sdata = date000.ToStdString();
1594 if (!WriteHeaderRecord200(stream, HEADER_CELL_PUBLISHDATE, sdata)) {
1597 return ERROR_SENCFILE_ABORT;
1601 m_edtn000.ToLong(&n000);
1602 if (!WriteHeaderRecord200(stream, HEADER_CELL_EDITION, (uint16_t)n000)) {
1605 return ERROR_SENCFILE_ABORT;
1608 sdata = m_LastUpdateDate.ToStdString();
1609 if (!WriteHeaderRecord200(stream, HEADER_CELL_UPDATEDATE, sdata)) {
1612 return ERROR_SENCFILE_ABORT;
1615 if (!WriteHeaderRecord200(stream, HEADER_CELL_UPDATE,
1616 (uint16_t)m_last_applied_update)) {
1619 return ERROR_SENCFILE_ABORT;
1622 if (!WriteHeaderRecord200(stream, HEADER_CELL_NATIVESCALE,
1623 (uint32_t)m_native_scale)) {
1626 return ERROR_SENCFILE_ABORT;
1629 wxDateTime now = wxDateTime::Now();
1630 wxString dateNow = now.Format(_T(
"%Y%m%d"));
1631 sdata = dateNow.ToStdString();
1632 if (!WriteHeaderRecord200(stream, HEADER_CELL_SENCCREATEDATE, sdata)) {
1635 return ERROR_SENCFILE_ABORT;
1639 if (!CreateCovrRecords(stream)) {
1642 return ERROR_SENCFILE_ABORT;
1650 OGRFeature *pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
1651 while (NULL != pEdgeVectorRecordFeature) {
1652 int record_id = pEdgeVectorRecordFeature->GetFieldAsInteger(
"RCID");
1654 m_vector_helper_hash[record_id] = feid;
1657 delete pEdgeVectorRecordFeature;
1658 pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
1661 wxString Message = SENCfile.GetFullPath();
1662 Message.Append(_T(
"...Ingesting"));
1664 wxString Title(_(
"OpenCPN S57 SENC File Create..."));
1665 Title.append(SENCfile.GetFullPath());
1667#if wxUSE_PROGRESSDLG
1670 int nProg = poReader->GetFeatureCount();
1672 if (wxThread::IsMain() && b_showProg) {
1673 m_ProgDialog =
new wxGenericProgressDialog();
1675 wxFont *qFont = GetOCPNScaledFont(_(
"Dialog"));
1676 m_ProgDialog->SetFont(*qFont);
1678 m_ProgDialog->Create(Title, Message, nProg, NULL,
1679 wxPD_AUTO_HIDE | wxPD_SMOOTH);
1684 OGRFeature *objectDef;
1689 objectDef = poReader->ReadNextFeature();
1691 if (objectDef != NULL) {
1694#if wxUSE_PROGRESSDLG
1700 if (m_ProgDialog && progsw.Time() > 200) {
1704 wxString(objectDef->GetDefnRef()->GetName(), wxConvUTF8);
1705 sobj.Append(wxString::Format(_T(
" %d/%d "), iObj, nProg));
1707 bcont = m_ProgDialog->Update(iObj, sobj);
1708#if defined(__WXMSW__) || defined(__WXOSX__)
1714 OGRwkbGeometryType geoType = wkbUnknown;
1721 if (objectDef->GetGeometryRef() != NULL)
1722 geoType = objectDef->GetGeometryRef()->getGeometryType();
1725 if (geoType != wkbUnknown) {
1726 CreateSENCRecord200(objectDef, stream, 1, poReader);
1737 CreateSENCVectorEdgeTableRecord200(stream, poReader);
1740 CreateSENCVectorConnectedTableRecord200(stream, poReader);
1748 for (
unsigned int iff = 0; iff < m_tmpup_array.GetCount(); iff++)
1749 remove(m_tmpup_array[iff].mb_str());
1755 wxRemoveFile(tmp_file);
1756 ret_code = ERROR_SENCFILE_ABORT;
1760 bool cpok = wxRenameFile(tmp_file, SENCfile.GetFullPath());
1762 errorMessage = _T(
" Cannot rename temporary SENC file ");
1763 errorMessage.Append(tmp_file);
1764 errorMessage.Append(_T(
" to "));
1765 errorMessage.Append(SENCfile.GetFullPath());
1766 ret_code = ERROR_SENCFILE_ABORT;
1768 ret_code = SENC_NO_ERROR;
1771#if wxUSE_PROGRESSDLG
1772 delete m_ProgDialog;
1783 record.record_type = CELL_EXTENT_RECORD;
1785 record.extent_sw_lat = m_extent.SLAT;
1786 record.extent_sw_lon = m_extent.WLON;
1787 record.extent_nw_lat = m_extent.NLAT;
1788 record.extent_nw_lon = m_extent.WLON;
1789 record.extent_ne_lat = m_extent.NLAT;
1790 record.extent_ne_lon = m_extent.ELON;
1791 record.extent_se_lat = m_extent.SLAT;
1792 record.extent_se_lon = m_extent.ELON;
1794 size_t targetCount =
sizeof(record);
1795 if (!stream->Write(&record, targetCount).IsOk())
return false;
1797 for (
int i = 0; i < m_nCOVREntries; i++) {
1798 int nPoints = m_pCOVRTablePoints[i];
1800 float *fpbuf = m_pCOVRTable[i];
1804 record.record_type = CELL_COVR_RECORD;
1806 (nPoints * 2 *
sizeof(float));
1809 size_t targetCount =
sizeof(record);
1810 if (!stream->Write(&record, targetCount).IsOk())
return false;
1813 targetCount =
sizeof(uint32_t);
1814 if (!stream->Write(&nPoints, targetCount).IsOk())
return false;
1817 targetCount = nPoints * 2 *
sizeof(float);
1818 if (!stream->Write(fpbuf, targetCount).IsOk())
return false;
1821 for (
int i = 0; i < m_nNoCOVREntries; i++) {
1822 int nPoints = m_pNoCOVRTablePoints[i];
1824 float *fpbuf = m_pNoCOVRTable[i];
1828 record.record_type = CELL_NOCOVR_RECORD;
1830 sizeof(uint32_t) + (nPoints * 2 *
sizeof(float));
1833 size_t targetCount =
sizeof(record);
1834 if (!stream->Write(&record, targetCount).IsOk())
return false;
1837 targetCount =
sizeof(uint32_t);
1838 if (!stream->Write(&nPoints, targetCount).IsOk())
return false;
1841 targetCount = nPoints * 2 *
sizeof(float);
1842 if (!stream->Write(fpbuf, targetCount).IsOk())
return false;
1848bool Osenc::WriteHeaderRecord200(
Osenc_outstream *stream,
int recordType,
1849 std::string payload) {
1850 int payloadLength = payload.length() + 1;
1854 unsigned char *pBuffer = getBuffer(recordLength);
1857 memset(pRecord, 0, recordLength);
1858 pRecord->record_type = recordType;
1859 pRecord->record_length = recordLength;
1860 memcpy(&pRecord->payload, payload.c_str(), payloadLength);
1862 size_t targetCount = recordLength;
1863 if (!stream->Write(pBuffer, targetCount).IsOk())
1869bool Osenc::WriteHeaderRecord200(
Osenc_outstream *stream,
int recordType,
1871 int payloadLength =
sizeof(uint16_t);
1875 unsigned char *pBuffer = getBuffer(recordLength);
1878 memset(pRecord, 0, recordLength);
1879 pRecord->record_type = recordType;
1880 pRecord->record_length = recordLength;
1881 memcpy(&pRecord->payload, &val, payloadLength);
1883 size_t targetCount = recordLength;
1884 if (!stream->Write(pBuffer, targetCount).IsOk())
1890bool Osenc::WriteHeaderRecord200(
Osenc_outstream *stream,
int recordType,
1892 int payloadLength =
sizeof(uint32_t);
1896 unsigned char *pBuffer = getBuffer(recordLength);
1899 memset(pRecord, 0, recordLength);
1900 pRecord->record_type = recordType;
1901 pRecord->record_length = recordLength;
1902 memcpy(&pRecord->payload, &val, payloadLength);
1904 size_t targetCount = recordLength;
1905 if (!stream->Write(pBuffer, targetCount).IsOk())
1911bool Osenc::WriteFIDRecord200(
Osenc_outstream *stream,
int nOBJL,
int featureID,
1914 memset(&record, 0,
sizeof(record));
1916 record.record_type = FEATURE_ID_RECORD;
1917 record.record_length =
sizeof(record);
1919 record.feature_ID = featureID;
1920 record.feature_type_code = nOBJL;
1921 record.feature_primitive = prim;
1923 size_t targetCount =
sizeof(record);
1924 if (!stream->Write(&record, targetCount).IsOk())
1930bool Osenc::CreateMultiPointFeatureGeometryRecord200(OGRFeature *pFeature,
1932 OGRGeometry *pGeo = pFeature->GetGeometryRef();
1934 int wkb_len = pGeo->WkbSize();
1935 unsigned char *pwkb_buffer = (
unsigned char *)malloc(wkb_len);
1938 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
1942 unsigned char *ps = pwkb_buffer;
1944 int nPoints = *((
int *)ps);
1946 int sb_len = (nPoints * 3 *
sizeof(float));
1948 unsigned char *psb_buffer = (
unsigned char *)malloc(sb_len);
1949 unsigned char *pd = psb_buffer;
1954 float *pdf = (
float *)pd;
1957 float lonmax = -1000;
1958 float lonmin = 1000;
1959 float latmax = -1000;
1960 float latmin = 1000;
1962 for (
int ip = 0; ip < nPoints; ip++) {
1970 OGRGeometryCollection *temp_geometry_collection =
1971 (OGRGeometryCollection *)pGeo;
1972 OGRGeometry *temp_geometry = temp_geometry_collection->getGeometryRef(ip);
1973 OGRPoint *pt_geom = (OGRPoint *)temp_geometry;
1975 double lon = pt_geom->getX();
1976 double lat = pt_geom->getY();
1977 double depth = pt_geom->getZ();
1980 double easting, northing;
1981 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
1984 float __attribute__((aligned(16))) east = easting;
1985 float __attribute__((aligned(16))) north = northing;
1986 float __attribute__((aligned(16))) deep = depth;
1987 unsigned char *puceast = (
unsigned char *)&east;
1988 unsigned char *pucnorth = (
unsigned char *)&north;
1989 unsigned char *pucdeep = (
unsigned char *)&deep;
1991 memcpy(pdf++, puceast, sizeof(
float));
1992 memcpy(pdf++, pucnorth, sizeof(
float));
1993 memcpy(pdf++, pucdeep, sizeof(
float));
1998 *pdf++ = (float)depth;
2002 lonmax = fmax(lon, lonmax);
2003 lonmin = fmin(lon, lonmin);
2004 latmax = fmax(lat, latmax);
2005 latmin = fmin(lat, latmin);
2010 record.record_type = FEATURE_GEOMETRY_RECORD_MULTIPOINT;
2012 (nPoints * 3 *
sizeof(
float));
2013 record.extent_e_lon = lonmax;
2014 record.extent_w_lon = lonmin;
2015 record.extent_n_lat = latmax;
2016 record.extent_s_lat = latmin;
2017 record.point_count = nPoints;
2020 size_t targetCount =
sizeof(record);
2021 if (!stream->Write(&record, targetCount).IsOk())
goto failure;
2023 targetCount = nPoints * 3 *
sizeof(float);
2024 if (!stream->Write(psb_buffer, targetCount).IsOk())
goto failure;
2037bool Osenc::CreateLineFeatureGeometryRecord200(
S57Reader *poReader,
2038 OGRFeature *pFeature,
2040 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2042 int wkb_len = pGeo->WkbSize();
2043 unsigned char *pwkb_buffer = (
unsigned char *)malloc(wkb_len);
2046 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
2051 ((wkb_len - 9) / 2) + 9 + 16;
2053 unsigned char *psb_buffer = (
unsigned char *)malloc(sb_len);
2054 unsigned char *pd = psb_buffer;
2055 unsigned char *ps = pwkb_buffer;
2059 int ip = *((
int *)(ps + 5));
2063 double *psd = (
double *)ps;
2064 float *pdf = (
float *)pd;
2067 float lonmax = -1000;
2068 float lonmin = 1000;
2069 float latmax = -1000;
2070 float latmin = 1000;
2072 for (
int i = 0; i < ip; i++) {
2075 double easting, northing;
2077 double __attribute__((aligned(16))) east_d, north_d;
2078 unsigned char *pucd = (
unsigned char *)psd;
2080 memcpy(&east_d, pucd, sizeof(
double));
2082 pucd += sizeof(
double);
2083 memcpy(&north_d, pucd, sizeof(
double));
2089 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
2090 unsigned char *puceasting = (
unsigned char *)&easting;
2091 unsigned char *pucnorthing = (
unsigned char *)&northing;
2093 memcpy(pdf++, puceasting, sizeof(
float));
2094 memcpy(pdf++, pucnorthing, sizeof(
float));
2097 lon = (float)*psd++;
2098 lat = (float)*psd++;
2101 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
2107 lonmax = fmax(lon, lonmax);
2108 lonmin = fmin(lon, lonmin);
2109 latmax = fmax(lat, latmax);
2110 latmin = fmin(lat, latmin);
2113 int nEdgeVectorRecords = 0;
2114 unsigned char *pvec_buffer =
2115 getObjectVectorIndexTable(poReader, pFeature, nEdgeVectorRecords);
2122 int nEdgeVectorRecords;
2123 OGRFeature *pEdgeVectorRecordFeature;
2125 pNAME_RCID = (
int *) pFeature->GetFieldAsIntegerList(
"NAME_RCID", &nEdgeVectorRecords );
2126 pORNT = (
int *) pFeature->GetFieldAsIntegerList(
"ORNT", NULL );
2133 unsigned char *pvec_buffer = (
unsigned char *)malloc(nEdgeVectorRecords * 3 *
sizeof(
int));
2134 unsigned char *pvRun = pvec_buffer;
2137 char ** papszReaderOptions = NULL;
2138 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_UPDATES,
"ON" );
2139 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_LINKAGES,
"ON" );
2140 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON" );
2141 poReader->SetOptions( papszReaderOptions );
2144 for(
int i = 0; i < nEdgeVectorRecords; i++ ) {
2146 int *pI = (
int *)pvRun;
2148 int edge_rcid = pNAME_RCID[i];
2150 int start_rcid, end_rcid;
2151 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2152 pEdgeVectorRecordFeature = poReader->ReadVector( target_record_feid, RCNM_VE );
2154 if( NULL != pEdgeVectorRecordFeature ) {
2155 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_0" );
2156 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_1" );
2162 if( !poReader->FetchPoint( RCNM_VC, start_rcid, NULL, NULL, NULL, NULL ) )
2164 if( !poReader->FetchPoint( RCNM_VC, end_rcid, NULL, NULL, NULL, NULL ) )
2167 OGRLineString *poLS = (OGRLineString *)pEdgeVectorRecordFeature->GetGeometryRef();
2171 if( edge_ornt == 1 ){
2181 delete pEdgeVectorRecordFeature;
2191 pvRun += 3 *
sizeof(int);
2196 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF" );
2197 poReader->SetOptions( papszReaderOptions );
2198 CSLDestroy( papszReaderOptions );
2203 record.record_type = FEATURE_GEOMETRY_RECORD_LINE;
2205 (nEdgeVectorRecords * 3 *
sizeof(
int));
2206 record.extent_e_lon = lonmax;
2207 record.extent_w_lon = lonmin;
2208 record.extent_n_lat = latmax;
2209 record.extent_s_lat = latmin;
2210 record.edgeVector_count = nEdgeVectorRecords;
2213 size_t targetCount =
sizeof(record);
2214 if (!stream->Write(&record, targetCount).IsOk())
return false;
2217 targetCount = nEdgeVectorRecords * 3 *
sizeof(int);
2218 if (!stream->Write(pvec_buffer, targetCount).IsOk())
return false;
2228bool Osenc::CreateAreaFeatureGeometryRecord200(
S57Reader *poReader,
2229 OGRFeature *pFeature,
2233 PolyTessGeo *ppg = NULL;
2235 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2236 OGRPolygon *poly = (OGRPolygon *)(pGeo);
2238 if (!poly->getExteriorRing())
return false;
2241 ppg =
new PolyTessGeo(poly,
true, m_ref_lat, m_ref_lon, m_LOD_meters);
2244 error_code = ppg->ErrorCode;
2248 _T(
" Warning: S57 SENC Geometry Error %d, Some Features ignored."),
2259 memset(&baseRecord, 0,
sizeof(baseRecord));
2261 baseRecord.record_type = FEATURE_GEOMETRY_RECORD_AREA;
2265 baseRecord.extent_s_lat = ppg->Get_ymin();
2266 baseRecord.extent_n_lat = ppg->Get_ymax();
2267 baseRecord.extent_e_lon = ppg->Get_xmax();
2268 baseRecord.extent_w_lon = ppg->Get_xmin();
2270 baseRecord.contour_count = ppg->GetnContours();
2274 int contourPointCountArraySize = ppg->GetnContours() *
sizeof(uint32_t);
2275 uint32_t *contourPointCountArray =
2276 (uint32_t *)malloc(contourPointCountArraySize);
2278 uint32_t *pr = contourPointCountArray;
2280 for (
int i = 0; i < ppg->GetnContours(); i++) {
2281 *pr++ = ppg->Get_PolyTriGroup_head()->pn_vertex[i];
2297 TriPrim *pTP = ppg->Get_PolyTriGroup_head()
2302 geoLength +=
sizeof(uint8_t) +
sizeof(uint32_t);
2303 geoLength += pTP->nVert * 2 *
sizeof(float);
2304 geoLength += 4 *
sizeof(double);
2310 baseRecord.triprim_count = n_TriPrims;
2312 int nEdgeVectorRecords = 0;
2313 unsigned char *pvec_buffer =
2314 getObjectVectorIndexTable(poReader, pFeature, nEdgeVectorRecords);
2321 int nEdgeVectorRecords;
2322 OGRFeature *pEdgeVectorRecordFeature;
2324 pNAME_RCID = (
int *) pFeature->GetFieldAsIntegerList(
"NAME_RCID", &nEdgeVectorRecords );
2325 pORNT = (
int *) pFeature->GetFieldAsIntegerList(
"ORNT", NULL );
2327 baseRecord.edgeVector_count = nEdgeVectorRecords;
2329 unsigned char *pvec_buffer = (
unsigned char *)malloc(nEdgeVectorRecords * 3 *
sizeof(
int));
2330 unsigned char *pvRun = pvec_buffer;
2333 char ** papszReaderOptions = NULL;
2334 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_UPDATES,
"ON" );
2335 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_LINKAGES,
"ON" );
2336 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON" );
2337 poReader->SetOptions( papszReaderOptions );
2340 for(
int i = 0; i < nEdgeVectorRecords; i++ ) {
2342 int *pI = (
int *)pvRun;
2344 int start_rcid, end_rcid;
2345 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2346 pEdgeVectorRecordFeature = poReader->ReadVector( target_record_feid, RCNM_VE );
2348 int edge_rcid = pNAME_RCID[i];
2350 if( NULL != pEdgeVectorRecordFeature ) {
2351 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_0" );
2352 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_1" );
2357 if( !poReader->FetchPoint( RCNM_VC, start_rcid, NULL, NULL, NULL, NULL ) )
2359 if( !poReader->FetchPoint( RCNM_VC, end_rcid, NULL, NULL, NULL, NULL ) )
2365 if( edge_ornt == 1 ){
2375 delete pEdgeVectorRecordFeature;
2385 pvRun += 3 *
sizeof(int);
2390 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF" );
2391 poReader->SetOptions( papszReaderOptions );
2392 CSLDestroy( papszReaderOptions );
2396 baseRecord.edgeVector_count = nEdgeVectorRecords;
2400 recordLength += contourPointCountArraySize;
2401 recordLength += geoLength;
2402 recordLength += nEdgeVectorRecords * 3 *
sizeof(int);
2403 baseRecord.record_length = recordLength;
2406 size_t targetCount =
sizeof(baseRecord);
2407 if (!stream->Write(&baseRecord, targetCount).IsOk())
return false;
2410 targetCount = contourPointCountArraySize;
2411 if (!stream->Write(contourPointCountArray, targetCount).IsOk())
return false;
2414 pTP = ppg->Get_PolyTriGroup_head()
2417 if (!stream->Write(&pTP->type,
sizeof(uint8_t)).IsOk())
return false;
2418 if (!stream->Write(&pTP->nVert,
sizeof(uint32_t)).IsOk())
return false;
2425 double minlat, minlon, maxlat, maxlon;
2426 minlat = pTP->tri_box.GetMinLat();
2427 minlon = pTP->tri_box.GetMinLon();
2428 maxlat = pTP->tri_box.GetMaxLat();
2429 maxlon = pTP->tri_box.GetMaxLon();
2431 if (!stream->Write(&minlon,
sizeof(
double)).IsOk())
return false;
2432 if (!stream->Write(&maxlon,
sizeof(
double)).IsOk())
return false;
2433 if (!stream->Write(&minlat,
sizeof(
double)).IsOk())
return false;
2434 if (!stream->Write(&maxlat,
sizeof(
double)).IsOk())
return false;
2441 if (!stream->Write(pTP->p_vertex, pTP->nVert * 2 *
sizeof(
float)).IsOk())
2448 targetCount = nEdgeVectorRecords * 3 *
sizeof(int);
2449 if (!stream->Write(pvec_buffer, targetCount).IsOk())
return false;
2452 free(contourPointCountArray);
2458unsigned char *Osenc::getObjectVectorIndexTable(
S57Reader *poReader,
2459 OGRFeature *pFeature,
2465 int nEdgeVectorRecords;
2466 OGRFeature *pEdgeVectorRecordFeature;
2469 (
int *)pFeature->GetFieldAsIntegerList(
"NAME_RCID", &nEdgeVectorRecords);
2470 pORNT = (
int *)pFeature->GetFieldAsIntegerList(
"ORNT", NULL);
2472 nEntries = nEdgeVectorRecords;
2474 unsigned char *pvec_buffer =
2475 (
unsigned char *)malloc(nEdgeVectorRecords * 3 *
sizeof(
int));
2476 unsigned char *pvRun = pvec_buffer;
2479 char **papszReaderOptions = NULL;
2480 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
"ON");
2481 papszReaderOptions =
2482 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
2483 papszReaderOptions =
2484 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
2485 poReader->SetOptions(papszReaderOptions);
2489 for (
int i = 0; i < nEdgeVectorRecords; i++) {
2490 int *pI = (
int *)pvRun;
2492 int edge_rcid = pNAME_RCID[i];
2494 int start_rcid, end_rcid;
2495 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2496 pEdgeVectorRecordFeature =
2497 poReader->ReadVector(target_record_feid, RCNM_VE);
2499 if (NULL != pEdgeVectorRecordFeature) {
2500 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_0");
2501 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_1");
2507 if (!poReader->FetchPoint(RCNM_VC, start_rcid, NULL, NULL, NULL, NULL))
2509 if (!poReader->FetchPoint(RCNM_VC, end_rcid, NULL, NULL, NULL, NULL))
2512 OGRLineString *poLS =
2513 (OGRLineString *)pEdgeVectorRecordFeature->GetGeometryRef();
2516 else if (poLS->getNumPoints() < 1)
2519 int edge_ornt = pORNT[i];
2521 if (edge_ornt == 1) {
2531 delete pEdgeVectorRecordFeature;
2541 pvRun += 3 *
sizeof(int);
2545 papszReaderOptions =
2546 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
2547 poReader->SetOptions(papszReaderOptions);
2548 CSLDestroy(papszReaderOptions);
2553void Osenc::CreateSENCVectorEdgeTableRecord200(
Osenc_outstream *stream,
2556 uint8_t *pPayload = NULL;
2557 int payloadSize = 0;
2558 uint8_t *pRun = pPayload;
2561 char **papszReaderOptions = NULL;
2562 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
"ON");
2563 papszReaderOptions =
2564 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
2565 papszReaderOptions =
2566 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
2567 poReader->SetOptions(papszReaderOptions);
2570 OGRLineString *pLS = NULL;
2572 OGRFeature *pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
2577 while (NULL != pEdgeVectorRecordFeature) {
2582 if (pEdgeVectorRecordFeature->GetGeometryRef() != NULL) {
2583 pGeo = pEdgeVectorRecordFeature->GetGeometryRef();
2584 if (pGeo->getGeometryType() == wkbLineString) {
2585 pLS = (OGRLineString *)pGeo;
2586 nPoints = pLS->getNumPoints();
2592 int new_size = payloadSize + (2 *
sizeof(int));
2593 pPayload = (uint8_t *)realloc(pPayload, new_size);
2594 pRun = pPayload + payloadSize;
2596 payloadSize = new_size;
2599 int record_id = pEdgeVectorRecordFeature->GetFieldAsInteger(
"RCID");
2600 *(
int *)pRun = record_id;
2601 pRun +=
sizeof(int);
2608 int reduction_ratio = nPoints / MAX_VECTOR_POINTS + 1;
2609 int reduced_points = 0;
2611 double *ppd = (
double *)malloc((nPoints / reduction_ratio + 1) * 2 *
2615 for (
int i = 0; i < nPoints; i++) {
2616 if (i % reduction_ratio == 0) {
2618 pLS->getPoint(i, &p);
2621 double easting, northing;
2622 toSM(p.getY(), p.getX(), m_ref_lat, m_ref_lon, &easting, &northing);
2629 nPoints = reduced_points;
2632 std::vector<int> index_keep;
2633 if (nPoints > 5 && (m_LOD_meters > .01)) {
2634 index_keep.push_back(0);
2635 index_keep.push_back(nPoints - 1);
2637 DouglasPeucker(ppd, 0, nPoints - 1, m_LOD_meters, &index_keep);
2642 index_keep.resize(nPoints);
2643 for (
int i = 0; i < nPoints; i++) index_keep[i] = i;
2647 int nPointReduced = index_keep.size();
2648 *(
int *)pRun = nPointReduced;
2649 pRun +=
sizeof(int);
2654 int new_size_red = payloadSize + (nPointReduced * 2 *
sizeof(float));
2655 pPayload = (uint8_t *)realloc(pPayload, new_size_red);
2656 pRun = pPayload + payloadSize;
2658 payloadSize = new_size_red;
2660 float *npp = (
float *)pRun;
2661 float *npp_run = npp;
2663 for (
int ip = 0; ip < nPoints; ip++) {
2667 for (
unsigned int j = 0; j < index_keep.size(); j++) {
2668 if (index_keep[j] == ip) {
2671 pRun += 2 *
sizeof(float);
2684 delete pEdgeVectorRecordFeature;
2685 pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
2694 record.record_type = VECTOR_EDGE_NODE_TABLE_RECORD;
2695 record.record_length =
2702 stream->Write(&nFeatures,
sizeof(uint32_t));
2705 stream->Write(pPayload, payloadSize);
2711 papszReaderOptions =
2712 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
2713 poReader->SetOptions(papszReaderOptions);
2714 CSLDestroy(papszReaderOptions);
2717void Osenc::CreateSENCVectorConnectedTableRecord200(
Osenc_outstream *stream,
2720 uint8_t *pPayload = NULL;
2721 int payloadSize = 0;
2722 uint8_t *pRun = pPayload;
2725 char **papszReaderOptions = NULL;
2726 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
"ON");
2727 papszReaderOptions =
2728 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
2729 papszReaderOptions =
2730 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
2731 poReader->SetOptions(papszReaderOptions);
2736 OGRFeature *pConnNodeRecordFeature = poReader->ReadVector(feid, RCNM_VC);
2737 int featureCount = 0;
2740 while (NULL != pConnNodeRecordFeature) {
2741 if (pConnNodeRecordFeature->GetGeometryRef() != NULL) {
2742 pGeo = pConnNodeRecordFeature->GetGeometryRef();
2743 if (pGeo->getGeometryType() == wkbPoint) {
2744 int new_size = payloadSize +
sizeof(int) + (2 *
sizeof(
float));
2745 pPayload = (uint8_t *)realloc(pPayload, new_size);
2746 pRun = pPayload + payloadSize;
2748 payloadSize = new_size;
2751 int record_id = pConnNodeRecordFeature->GetFieldAsInteger(
"RCID");
2752 *(
int *)pRun = record_id;
2753 pRun +=
sizeof(int);
2755 pP = (OGRPoint *)pGeo;
2758 double easting, northing;
2759 toSM(pP->getY(), pP->getX(), m_ref_lat, m_ref_lon, &easting, &northing);
2765 float *ps = (
float *)pRun;
2775 delete pConnNodeRecordFeature;
2776 pConnNodeRecordFeature = poReader->ReadVector(feid, RCNM_VC);
2785 record.record_type = VECTOR_CONNECTED_NODE_TABLE_RECORD;
2786 record.record_length =
2793 stream->Write(&featureCount,
sizeof(uint32_t));
2796 stream->Write(pPayload, payloadSize);
2803 papszReaderOptions =
2804 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
2805 poReader->SetOptions(papszReaderOptions);
2806 CSLDestroy(papszReaderOptions);
2809bool Osenc::CreateSENCRecord200(OGRFeature *pFeature,
Osenc_outstream *stream,
2818 OGRFeatureDefn *pFD = pFeature->GetDefnRef();
2819 int nOBJL = pFD->GetOBJL();
2821 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2822 OGRwkbGeometryType gType = pGeo->getGeometryType();
2827 primitive = GEO_LINE;
2830 primitive = GEO_POINT;
2833 primitive = GEO_AREA;
2839 if (!WriteFIDRecord200(stream, nOBJL, pFeature->GetFID(), primitive))
2842#define MAX_HDR_LINE 400
2858 if (pFeature->GetFID() == 290)
int yyp = 4;
2860 int payloadLength = 0;
2861 void *payloadBuffer = NULL;
2862 unsigned int payloadBufferLength = 0;
2864 for (
int iField = 0; iField < pFeature->GetFieldCount(); iField++) {
2865 if (pFeature->IsFieldSet(iField)) {
2866 if ((iField == 1) || (iField > 7)) {
2867 OGRFieldDefn *poFDefn = pFeature->GetDefnRef()->GetFieldDefn(iField);
2870 const char *pAttrName = poFDefn->GetNameRef();
2871 const char *pAttrVal = pFeature->GetFieldAsString(iField);
2876 int attributeID = m_pRegistrarMan->getAttributeID(pAttrName);
2879 int OGRvalueType = (int)poFDefn->GetType();
2883 if (-1 == attributeID) {
2884 if (!strncmp(pAttrName,
"PRIM", 4)) {
2885 attributeID = ATTRIBUTE_ID_PRIM;
2889#if 0 OFTInteger = 0, OFTIntegerList = 1, OFTReal = 2, OFTRealList = 3, OFTString = 4, OFTStringList = 5, OFTWideString = 6, OFTWideStringList = 7, OFTBinary = 8
2900 switch (OGRvalueType) {
2903 valueType = OGRvalueType;
2905 if (payloadBufferLength < 4) {
2906 payloadBuffer = realloc(payloadBuffer, 4);
2907 payloadBufferLength = 4;
2910 int aValue = pFeature->GetFieldAsInteger(iField);
2911 memcpy(payloadBuffer, &aValue,
sizeof(
int));
2912 payloadLength =
sizeof(int);
2918 valueType = OGRvalueType;
2921 const int *aValueList =
2922 pFeature->GetFieldAsIntegerList(iField, &nCount);
2924 if (payloadBufferLength < nCount *
sizeof(
int)) {
2925 payloadBuffer = realloc(payloadBuffer, nCount *
sizeof(
int));
2926 payloadBufferLength = nCount *
sizeof(int);
2929 int *pBuffRun = (
int *)payloadBuffer;
2930 for (
int i = 0; i < nCount; i++) {
2931 *pBuffRun++ = aValueList[i];
2933 payloadLength = nCount *
sizeof(int);
2939 valueType = OGRvalueType;
2941 if (payloadBufferLength <
sizeof(
double)) {
2942 payloadBuffer = realloc(payloadBuffer,
sizeof(
double));
2943 payloadBufferLength =
sizeof(double);
2946 double aValue = pFeature->GetFieldAsDouble(iField);
2947 memcpy(payloadBuffer, &aValue,
sizeof(
double));
2948 payloadLength =
sizeof(double);
2955 valueType = OGRvalueType;
2958 const double *aValueList =
2959 pFeature->GetFieldAsDoubleList(iField, &nCount);
2961 if (payloadBufferLength < nCount *
sizeof(
double)) {
2962 payloadBuffer = realloc(payloadBuffer, nCount *
sizeof(
double));
2963 payloadBufferLength = nCount *
sizeof(double);
2966 double *pBuffRun = (
double *)payloadBuffer;
2967 for (
int i = 0; i < nCount; i++) {
2968 *pBuffRun++ = aValueList[i];
2970 payloadLength = nCount *
sizeof(double);
2977 valueType = OGRvalueType;
2978 const char *pAttrVal = pFeature->GetFieldAsString(iField);
2980 wxString wxAttrValue;
2982 if ((0 == strncmp(
"NOBJNM", pAttrName, 6)) ||
2983 (0 == strncmp(
"NINFOM", pAttrName, 6)) ||
2984 (0 == strncmp(
"NPLDST", pAttrName, 6)) ||
2985 (0 == strncmp(
"NTXTDS", pAttrName, 6))) {
2986 if (poReader->GetNall() ==
2989 wxString att_conv(pAttrVal, conv);
2990 att_conv.RemoveLast();
2992 att_conv.Replace(_T(
"\n"),
2995 wxAttrValue = att_conv;
2996 }
else if (poReader->GetNall() ==
2998 wxCSConv conv(_T(
"iso8859-1"));
2999 wxString att_conv(pAttrVal, conv);
3000 wxAttrValue = att_conv;
3003 if (poReader->GetAall() ==
3006 wxCSConv conv(_T(
"iso8859-1"));
3007 wxString att_conv(pAttrVal, conv);
3008 wxAttrValue = att_conv;
3015 unsigned int stringPayloadLength = 0;
3017 wxCharBuffer buffer;
3018 if (wxAttrValue.Length()) {
3019 buffer = wxAttrValue.ToUTF8();
3020 pAttrVal = buffer.data();
3021 stringPayloadLength = strlen(buffer.data());
3024 if (stringPayloadLength) {
3025 if (payloadBufferLength < stringPayloadLength + 1) {
3026 payloadBuffer = realloc(payloadBuffer, stringPayloadLength + 1);
3027 payloadBufferLength = stringPayloadLength + 1;
3030 strcpy((
char *)payloadBuffer, pAttrVal);
3031 payloadLength = stringPayloadLength + 1;
3043 if (-1 != attributeID) {
3049 unsigned char *pBuffer = getBuffer(recordLength);
3053 pRecord->record_type = FEATURE_ATTRIBUTE_RECORD;
3054 pRecord->record_length = recordLength;
3055 pRecord->attribute_type = attributeID;
3056 pRecord->attribute_value_type = valueType;
3057 memcpy(&pRecord->payload, payloadBuffer, payloadLength);
3060 size_t targetCount = recordLength;
3061 if (!stream->Write(pBuffer, targetCount).IsOk()) {
3062 free(payloadBuffer);
3069 if (wkbPoint == pGeo->getGeometryType()) {
3070 OGRPoint *pp = (OGRPoint *)pGeo;
3071 int nqual = pp->getnQual();
3074 int attributeID = m_pRegistrarMan->getAttributeID(
"QUAPOS");
3076 if (-1 != attributeID) {
3077 if (payloadBufferLength < 4) {
3078 payloadBuffer = realloc(payloadBuffer, 4);
3079 payloadBufferLength = 4;
3082 memcpy(payloadBuffer, &nqual,
sizeof(
int));
3083 payloadLength =
sizeof(int);
3088 unsigned char *pBuffer = getBuffer(recordLength);
3092 pRecord->record_type = FEATURE_ATTRIBUTE_RECORD;
3093 pRecord->record_length = recordLength;
3094 pRecord->attribute_type = attributeID;
3095 pRecord->attribute_value_type = valueType;
3096 memcpy(&pRecord->payload, payloadBuffer, payloadLength);
3099 size_t targetCount = recordLength;
3100 if (!stream->Write(pBuffer, targetCount).IsOk()) {
3101 free(payloadBuffer);
3108 free(payloadBuffer);
3113 if( wkbPoint == pGeo->getGeometryType() ) {
3114 OGRPoint *pp = (OGRPoint *) pGeo;
3115 int nqual = pp->getnQual();
3118 snprintf( line, MAX_HDR_LINE - 2,
" %s (%c) = %d",
"QUALTY",
'I', nqual );
3119 sheader += wxString( line, wxConvUTF8 );
3126 sprintf( line,
" %s %f %f\n", pGeo->getGeometryName(), m_ref_lat, m_ref_lon );
3127 sheader += wxString( line, wxConvUTF8 );
3130 wxCharBuffer buffer=sheader.ToUTF8();
3131 fprintf( fpOut,
"HDRLEN=%lu\n", (
unsigned long) strlen(buffer) );
3132 fwrite( buffer.data(), 1, strlen(buffer), fpOut );
3136 if ((pGeo != NULL)) {
3139 OGRwkbGeometryType gType = pGeo->getGeometryType();
3141 case wkbLineString: {
3142 if (!CreateLineFeatureGeometryRecord200(poReader, pFeature, stream))
3150 record.record_type = FEATURE_GEOMETRY_RECORD_POINT;
3151 record.record_length =
sizeof(record);
3153 int wkb_len = pGeo->WkbSize();
3154 unsigned char *pwkb_buffer = (
unsigned char *)malloc(wkb_len);
3157 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
3160 unsigned char *ps = pwkb_buffer;
3163 double *psd = (
double *)ps;
3167 __attribute__((aligned(16))) double lata, lona;
3168 unsigned char *pucsd = (
unsigned char *)psd;
3170 memcpy(&lona, pucsd, sizeof(
double));
3171 pucsd += sizeof(
double);
3172 memcpy(&lata, pucsd, sizeof(
double));
3186 size_t targetCount = record.record_length;
3187 if (!stream->Write(&record, targetCount).IsOk())
return false;
3193 case wkbMultiPoint25D: {
3194 if (!CreateMultiPointFeatureGeometryRecord200(pFeature, stream))
3202 if (!CreateAreaFeatureGeometryRecord200(poReader, pFeature, stream))
3210 msg = _T(
" Warning: Unimplemented ogr geotype record ");
3223 unsigned char **next_byte) {
3224 PolyTessGeo *pPTG =
new PolyTessGeo();
3226 pPTG->SetExtents(record->extent_w_lon, record->extent_s_lat,
3227 record->extent_e_lon, record->extent_n_lat);
3229 unsigned int n_TriPrim = record->triprim_count;
3230 int nContours = record->contour_count;
3233 void *payLoad = &record->payLoad;
3239 PolyTriGroup *ppg =
new PolyTriGroup;
3240 ppg->m_bSMSENC =
true;
3241 ppg->data_type = DATA_TYPE_DOUBLE;
3243 ppg->nContours = nContours;
3245 ppg->pn_vertex = (
int *)malloc(nContours *
sizeof(
int));
3246 int *pctr = ppg->pn_vertex;
3249 int *contour_pointcount_array_run = (
int *)payLoad;
3250 for (
int i = 0; i < nContours; i++) {
3251 *pctr++ = *contour_pointcount_array_run++;
3255 ppg->pgroup_geom = NULL;
3259 TriPrim **p_prev_triprim = &(ppg->tri_prim_head);
3262 unsigned int tri_type;
3265 int total_byte_size = 2 *
sizeof(float);
3267 uint8_t *pPayloadRun =
3268 (uint8_t *)contour_pointcount_array_run;
3271 for (
unsigned int i = 0; i < n_TriPrim; i++) {
3272 tri_type = *pPayloadRun++;
3273 nvert = *(uint32_t *)pPayloadRun;
3274 pPayloadRun +=
sizeof(uint32_t);
3276 TriPrim *tp =
new TriPrim;
3277 *p_prev_triprim = tp;
3278 p_prev_triprim = &(tp->p_next);
3281 tp->type = tri_type;
3285 wxMax(nvert_max, nvert);
3288 double *pbb = (
double *)pPayloadRun;
3290 double minxt, minyt, maxxt, maxyt;
3293 double __attribute__((aligned(16))) abox[4];
3294 unsigned char *pucbb = (
unsigned char *)pPayloadRun;
3295 memcpy(&abox[0], pucbb, 4 * sizeof(
double));
3308 tp->tri_box.Set(minyt, minxt, maxyt, maxxt);
3310 pPayloadRun += 4 *
sizeof(double);
3312 int byte_size = nvert * 2 *
sizeof(float);
3313 total_byte_size += byte_size;
3315 tp->p_vertex = (
double *)pPayloadRun;
3317 pPayloadRun += byte_size;
3320 if (next_byte) *next_byte = pPayloadRun;
3324 unsigned char *vbuf = (
unsigned char *)malloc(total_byte_size);
3326 TriPrim *p_tp = ppg->tri_prim_head;
3327 unsigned char *p_run = vbuf;
3329 memcpy(p_run, p_tp->p_vertex, p_tp->nVert * 2 *
sizeof(
float));
3330 p_tp->p_vertex = (
double *)p_run;
3331 p_run += p_tp->nVert * 2 *
sizeof(float);
3332 p_tp = p_tp->p_next;
3334 ppg->bsingle_alloc =
true;
3335 ppg->single_buffer = vbuf;
3336 ppg->single_buffer_size = total_byte_size;
3337 ppg->data_type = DATA_TYPE_FLOAT;
3339 pPTG->SetPPGHead(ppg);
3340 pPTG->SetnVertexMax(nvert_max);
3347bool Osenc::CreateCOVRTables(
S57Reader *poReader,
3353 float LatMax, LatMin, LonMax, LonMin;
3359 m_pCOVRTablePoints = NULL;
3360 m_pCOVRTable = NULL;
3363 MyFloatPtrArray *pAuxPtrArray =
new MyFloatPtrArray;
3364 std::vector<int> auxCntArray, noCovrCntArray;
3366 MyFloatPtrArray *pNoCovrPtrArray =
new MyFloatPtrArray;
3369 pFeat = GetChartFirstM_COVR(catcov, poReader, poRegistrar);
3374 OGRPolygon *poly = (OGRPolygon *)(pFeat->GetGeometryRef());
3375 OGRLinearRing *xring = poly->getExteriorRing();
3377 int npt = xring->getNumPoints();
3382 pf = (
float *)malloc(2 * npt *
sizeof(
float));
3385 for (
int i = 0; i < npt; i++) {
3387 xring->getPoint(i, &p);
3390 LatMax = fmax(LatMax, p.getY());
3391 LatMin = fmin(LatMin, p.getY());
3392 LonMax = fmax(LonMax, p.getX());
3393 LonMin = fmin(LonMin, p.getX());
3403 pAuxPtrArray->Add(pf);
3404 auxCntArray.push_back(npt);
3405 }
else if (catcov == 2) {
3406 pNoCovrPtrArray->Add(pf);
3407 noCovrCntArray.push_back(npt);
3413 pFeat = GetChartNextM_COVR(catcov, poReader);
3418 m_nCOVREntries = auxCntArray.size();
3423 if (m_nCOVREntries == 1) {
3424 m_pCOVRTablePoints = (
int *)malloc(
sizeof(
int));
3425 *m_pCOVRTablePoints = auxCntArray[0];
3426 m_pCOVRTable = (
float **)malloc(
sizeof(
float *));
3427 *m_pCOVRTable = (
float *)malloc(auxCntArray[0] * 2 *
sizeof(
float));
3428 memcpy(*m_pCOVRTable, pAuxPtrArray->Item(0),
3429 auxCntArray[0] * 2 *
sizeof(
float));
3432 else if (m_nCOVREntries > 1) {
3434 m_pCOVRTablePoints = (
int *)malloc(m_nCOVREntries *
sizeof(
int));
3435 m_pCOVRTable = (
float **)malloc(m_nCOVREntries *
sizeof(
float *));
3437 for (
unsigned int j = 0; j < (
unsigned int)m_nCOVREntries; j++) {
3438 m_pCOVRTablePoints[j] = auxCntArray[j];
3439 m_pCOVRTable[j] = (
float *)malloc(auxCntArray[j] * 2 *
sizeof(
float));
3440 memcpy(m_pCOVRTable[j], pAuxPtrArray->Item(j),
3441 auxCntArray[j] * 2 *
sizeof(
float));
3447 wxString msg(_T(
" ENC contains no useable M_COVR, CATCOV=1 features: "));
3448 msg.Append(m_FullPath000);
3453 m_nNoCOVREntries = noCovrCntArray.size();
3455 if (m_nNoCOVREntries) {
3457 m_pNoCOVRTablePoints = (
int *)malloc(m_nNoCOVREntries *
sizeof(
int));
3458 m_pNoCOVRTable = (
float **)malloc(m_nNoCOVREntries *
sizeof(
float *));
3460 for (
unsigned int j = 0; j < (
unsigned int)m_nNoCOVREntries; j++) {
3461 int npoints = noCovrCntArray[j];
3462 m_pNoCOVRTablePoints[j] = npoints;
3463 m_pNoCOVRTable[j] = (
float *)malloc(npoints * 2 *
sizeof(
float));
3464 memcpy(m_pNoCOVRTable[j], pNoCovrPtrArray->Item(j),
3465 npoints * 2 *
sizeof(
float));
3468 m_pNoCOVRTablePoints = NULL;
3469 m_pNoCOVRTable = NULL;
3472 for (
unsigned int j = 0; j < (
unsigned int)m_nNoCOVREntries; j++)
3473 free(pNoCovrPtrArray->Item(j));
3474 for (
unsigned int j = 0; j < (
unsigned int)m_nCOVREntries; j++)
3475 free(pAuxPtrArray->Item(j));
3477 delete pAuxPtrArray;
3478 delete pNoCovrPtrArray;
3480 if (0 == m_nCOVREntries) {
3481 wxString msg(_T(
" ENC contains no M_COVR features: "));
3482 msg.Append(m_FullPath000);
3485 msg = _T(
" Calculating Chart Extents as fallback.");
3490 if (poReader->GetExtent(&Env,
true) == OGRERR_NONE) {
3497 m_pCOVRTablePoints = (
int *)malloc(
sizeof(
int));
3498 *m_pCOVRTablePoints = 4;
3499 m_pCOVRTable = (
float **)malloc(
sizeof(
float *));
3500 float *pf = (
float *)malloc(2 * 4 *
sizeof(
float));
3517 wxString msg(_T(
" Cannot calculate Extents for ENC: "));
3518 msg.Append(m_FullPath000);
3526 m_extent.NLAT = LatMax;
3527 m_extent.SLAT = LatMin;
3528 m_extent.ELON = LonMax;
3529 m_extent.WLON = LonMin;
3534OGRFeature *Osenc::GetChartFirstM_COVR(
int &catcov,
S57Reader *pENCReader,
3536 OGRFeature *rv = NULL;
3538 if ((NULL != pENCReader) && (NULL != poRegistrar)) {
3540 poRegistrar->SelectClass(
"M_COVR");
3546 bool bFound =
false;
3547 OGRFeature *pobjectDef = pENCReader->ReadNextFeature();
3550 OGRFeatureDefn *poDefn = pobjectDef->GetDefnRef();
3551 if (poDefn && (poDefn->GetOBJL() == 302 )) {
3553 catcov = pobjectDef->GetFieldAsInteger(
"CATCOV");
3561 pobjectDef = pENCReader->ReadNextFeature();
3568OGRFeature *Osenc::GetChartNextM_COVR(
int &catcov,
S57Reader *pENCReader) {
3572 bool bFound =
false;
3573 OGRFeature *pobjectDef = pENCReader->ReadNextFeature();
3577 OGRFeatureDefn *poDefn = pobjectDef->GetDefnRef();
3578 if (poDefn && (poDefn->GetOBJL() == 302)) {
3580 catcov = pobjectDef->GetFieldAsInteger(
"CATCOV");
3588 pobjectDef = pENCReader->ReadNextFeature();
3595int Osenc::GetBaseFileInfo(
const wxString &FullPath000,
3596 const wxString &SENCFileName) {
3597 wxFileName SENCfile = wxFileName(SENCFileName);
3601 if (!GetBaseFileAttr(FullPath000)) {
3602 return ERROR_BASEFILE_ATTRIBUTES;
3606 oS57DS.SetS57Registrar(m_poRegistrar);
3608 bool b_current_debug = g_bGDAL_Debug;
3609 g_bGDAL_Debug =
false;
3613 if (ingestCell(&oS57DS, FullPath000, SENCfile.GetPath())) {
3614 errorMessage = _T(
"Error ingesting: ") + FullPath000;
3615 return ERROR_INGESTING000;
3618 S57Reader *poReader = oS57DS.GetModule(0);
3620 CalculateExtent(poReader, m_poRegistrar);
3622 g_bGDAL_Debug = b_current_debug;
3626 return SENC_NO_ERROR;
3629bool Osenc::CalculateExtent(
S57Reader *poReader,
3635 float LatMax, LatMin, LonMax, LonMin;
3641 m_pCOVRTablePoints = NULL;
3642 m_pCOVRTable = NULL;
3652 pFeat = GetChartFirstM_COVR(catcov, poReader, poRegistrar);
3657 OGRPolygon *poly = (OGRPolygon *)(pFeat->GetGeometryRef());
3658 OGRLinearRing *xring = poly->getExteriorRing();
3660 int npt = xring->getNumPoints();
3663 for (
int i = 0; i < npt; i++) {
3665 xring->getPoint(i, &p);
3668 LatMax = fmax(LatMax, p.getY());
3669 LatMin = fmin(LatMin, p.getY());
3670 LonMax = fmax(LonMax, p.getX());
3671 LonMin = fmin(LonMin, p.getX());
3677 pFeat = GetChartNextM_COVR(catcov, poReader);
3681 m_extent.NLAT = LatMax;
3682 m_extent.SLAT = LatMin;
3683 m_extent.ELON = LonMax;
3684 m_extent.WLON = LonMin;
3689void Osenc::InitializePersistentBuffer(
void) {
3690 pBuffer = (
unsigned char *)malloc(1024);
3694unsigned char *Osenc::getBuffer(
size_t length) {
3695 if (length > bufferSize) {
3696 pBuffer = (
unsigned char *)realloc(pBuffer, length * 2);
3697 bufferSize = length * 2;
s57RegistrarMgr Definition This is a class holding the ctor and dtor for the global registrar