35#include <wx/wfstream.h>
36#include <wx/filename.h>
37#include <wx/progdlg.h>
42#include "model/cutil.h"
43#include "s57registrar_mgr.h"
44#include "gdal/cpl_csv.h"
46#include "gdal/cpl_string.h"
47#include "LOD_reduce.h"
50#include "model/georef.h"
54extern s57RegistrarMgr *m_pRegistrarMan;
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 =
"-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) {
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(
"%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);
1198 wxRemoveFile(s0_file);
1203int Osenc::ValidateAndCountUpdates(
const wxFileName file000,
1204 const wxString CopyDir,
1205 wxString &LastUpdateDate,
bool b_copyfiles) {
1207 wxFileName last_up_added;
1211 m_UpFiles =
new wxArrayString;
1213 s57chart::GetUpdateFileArray(file000, m_UpFiles, m_date000, m_edtn000);
1216 if (m_UpFiles->GetCount()) {
1233 unsigned int jup = 0;
1234 for (
int iff = 0; iff < retval + 1; iff++) {
1236 wxString targetFile;
1238 if (jup < m_UpFiles->GetCount()) upFile = m_UpFiles->Item(jup);
1239 wxFileName upCheck(upFile);
1241 wxString text = upCheck.GetExt();
1244 targetFile = upFile;
1247 targetFile = file000.GetFullName();
1250 wxFileName ufile(targetFile);
1252 sext.Printf(
"%03d", iff);
1256 wxString cp_ufile = CopyDir;
1257 if (cp_ufile.Last() != ufile.GetPathSeparator())
1258 cp_ufile.Append(ufile.GetPathSeparator());
1260 cp_ufile.Append(ufile.GetFullName());
1262 wxString tfile = ufile.GetFullPath();
1267 if (ufile.FileExists()) {
1268 wxFile uf(ufile.GetFullPath());
1269 if (uf.IsOpened()) {
1275 if (ufile.FileExists() &&
1279 bool cpok = wxCopyFile(ufile.GetFullPath(), cp_ufile);
1281 wxString msg(
" Cannot copy temporary working ENC file ");
1282 msg.Append(ufile.GetFullPath());
1284 msg.Append(cp_ufile);
1314 "WARNING---ENC Update chain incomplete. Substituting NULL "
1316 msg += ufile.GetFullName();
1318 wxLogMessage(
" Subsequent ENC updates may produce errors.");
1320 " This ENC exchange set should be updated and SENCs "
1325 dupdate.Initialize(
'3',
'L',
'E',
'1',
'0',
"!!!", 3, 4, 4);
1326 bstat = !(dupdate.Create(cp_ufile.mb_str()) == 0);
1330 wxString msg(
" Error creating dummy update file: ");
1331 msg.Append(cp_ufile);
1336 m_tmpup_array.Add(cp_ufile);
1337 last_up_added = cp_ufile;
1344 wxFileName lastfile(last_up_added);
1346 last_sext.Printf(
"%03d", upmax);
1347 lastfile.SetExt(last_sext);
1350 DDFModule oUpdateModule;
1353 !(oUpdateModule.Open(lastfile.GetFullPath().mb_str(), TRUE) == 0);
1357 oUpdateModule.Rewind();
1358 DDFRecord *pr = oUpdateModule.ReadRecord();
1364 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"ISDT", 0, &nSuccess));
1368 LastUpdateDate = wxString(u, wxConvUTF8);
1371 wxDateTime now = wxDateTime::Now();
1372 LastUpdateDate = now.Format(
"%Y%m%d");
1380bool Osenc::GetBaseFileAttr(
const wxString &FullPath000) {
1382 if (!oModule.Open(FullPath000.mb_str())) {
1392 DDFRecord *pr = oModule.ReadRecord();
1396 m_nGeoRecords = pr->GetIntSubfield(
"DSSI", 0,
"NOGR", 0);
1397 if (!m_nGeoRecords) {
1398 errorMessage =
"GetBaseFileAttr: DDFRecord 0 does not contain DSSI:NOGR ";
1406 char *u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"ISDT", 0));
1408 date000 = wxString(u, wxConvUTF8);
1410 errorMessage =
"GetBaseFileAttr: DDFRecord 0 does not contain DSID:ISDT ";
1415 m_date000.ParseFormat(date000,
"%Y%m%d");
1416 if (!m_date000.IsValid()) m_date000.ParseFormat(
"20000101",
"%Y%m%d");
1418 m_date000.ResetTime();
1421 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"EDTN", 0));
1423 m_edtn000 = wxString(u, wxConvUTF8);
1425 errorMessage =
"GetBaseFileAttr: DDFRecord 0 does not contain DSID:EDTN ";
1433 u = (
char *)(pr->GetStringSubfield(
"DSID", 0,
"UPDN", 0));
1436 wxString tmp_updn = wxString(u, wxConvUTF8);
1437 if (tmp_updn.ToLong(&updn)) m_UPDN = updn;
1440 errorMessage =
"GetBaseFileAttr: DDFRecord 0 does not contain DSID:UPDN ";
1447 for (; pr != NULL; pr = oModule.ReadRecord()) {
1448 if (pr->FindField(
"DSPM") != NULL) {
1449 m_native_scale = pr->GetIntSubfield(
"DSPM", 0,
"CSCL", 0);
1453 if (!m_native_scale) {
1454 errorMessage =
"GetBaseFileAttr: ENC not contain DSPM:CSCL ";
1456 m_native_scale = 1000;
1468int Osenc::createSenc200(
const wxString &FullPath000,
1469 const wxString &SENCFileName,
bool b_showProg) {
1472 m_FullPath000 = FullPath000;
1474 m_senc_file_create_version = 201;
1476 if (!m_poRegistrar) {
1477 m_poRegistrar =
new S57ClassRegistrar();
1478 m_poRegistrar->LoadInfo(g_csv_locn.mb_str(), FALSE);
1479 m_bPrivateRegistrar =
true;
1484 wxFileName SENCfile = wxFileName(SENCFileName);
1485 wxFileName file000 = wxFileName(FullPath000);
1488 if (
true != SENCfile.DirExists(SENCfile.GetPath())) {
1489 if (!SENCfile.Mkdir(SENCfile.GetPath())) {
1491 "Cannot create SENC file directory for " + SENCfile.GetFullPath();
1493 return ERROR_CANNOT_CREATE_SENC_DIR;
1499 wxString tmp_file = tfn.CreateTempFileName(
"");
1511 if (m_pauxOutstream) {
1512 m_pOutstream = m_pauxOutstream;
1519 if (!stream->Open(tmp_file)) {
1520 errorMessage =
"Unable to create temp SENC file: ";
1521 errorMessage += tmp_file;
1522 delete m_pOutstream;
1524 return ERROR_CANNOT_CREATE_TEMP_SENC_FILE;
1529 if (!GetBaseFileAttr(FullPath000)) {
1530 delete m_pOutstream;
1532 return ERROR_BASEFILE_ATTRIBUTES;
1535 OGRS57DataSource S57DS;
1536 OGRS57DataSource *poS57DS = &S57DS;
1537 poS57DS->SetS57Registrar(m_poRegistrar);
1541 if (ingestCell(poS57DS, FullPath000, SENCfile.GetPath())) {
1542 errorMessage =
"Error ingesting: " + FullPath000;
1543 delete m_pOutstream;
1545 return ERROR_INGESTING000;
1548 S57Reader *poReader = poS57DS->GetModule(0);
1551 if (!CreateCOVRTables(poReader, m_poRegistrar)) {
1552 delete m_pOutstream;
1554 return ERROR_SENCFILE_ABORT;
1558 m_ref_lat = (m_extent.NLAT + m_extent.SLAT) / 2.;
1559 m_ref_lon = (m_extent.WLON + m_extent.ELON) / 2.;
1571 s57chart::GetChartNameFromTXT(FullPath000, nice_name);
1573 string sname =
"UTF8Error";
1574 wxCharBuffer buffer = nice_name.ToUTF8();
1575 if (buffer.data()) sname = buffer.data();
1577 if (!WriteHeaderRecord200(stream, HEADER_SENC_VERSION,
1578 (uint16_t)m_senc_file_create_version)) {
1580 delete m_pOutstream;
1582 return ERROR_SENCFILE_ABORT;
1585 if (!WriteHeaderRecord200(stream, HEADER_CELL_NAME, sname)) {
1587 delete m_pOutstream;
1589 return ERROR_SENCFILE_ABORT;
1592 wxString date000 = m_date000.Format(
"%Y%m%d");
1593 string sdata = date000.ToStdString();
1594 if (!WriteHeaderRecord200(stream, HEADER_CELL_PUBLISHDATE, sdata)) {
1596 delete m_pOutstream;
1598 return ERROR_SENCFILE_ABORT;
1602 m_edtn000.ToLong(&n000);
1603 if (!WriteHeaderRecord200(stream, HEADER_CELL_EDITION, (uint16_t)n000)) {
1605 delete m_pOutstream;
1607 return ERROR_SENCFILE_ABORT;
1610 sdata = m_LastUpdateDate.ToStdString();
1611 if (!WriteHeaderRecord200(stream, HEADER_CELL_UPDATEDATE, sdata)) {
1613 delete m_pOutstream;
1615 return ERROR_SENCFILE_ABORT;
1618 if (!WriteHeaderRecord200(stream, HEADER_CELL_UPDATE,
1619 (uint16_t)m_last_applied_update)) {
1621 delete m_pOutstream;
1623 return ERROR_SENCFILE_ABORT;
1626 if (!WriteHeaderRecord200(stream, HEADER_CELL_NATIVESCALE,
1627 (uint32_t)m_native_scale)) {
1629 delete m_pOutstream;
1631 return ERROR_SENCFILE_ABORT;
1634 wxDateTime now = wxDateTime::Now();
1635 wxString dateNow = now.Format(
"%Y%m%d");
1636 sdata = dateNow.ToStdString();
1637 if (!WriteHeaderRecord200(stream, HEADER_CELL_SENCCREATEDATE, sdata)) {
1639 delete m_pOutstream;
1641 return ERROR_SENCFILE_ABORT;
1645 if (!CreateCovrRecords(stream)) {
1647 delete m_pOutstream;
1649 return ERROR_SENCFILE_ABORT;
1657 OGRFeature *pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
1658 while (NULL != pEdgeVectorRecordFeature) {
1659 int record_id = pEdgeVectorRecordFeature->GetFieldAsInteger(
"RCID");
1661 m_vector_helper_hash[record_id] = feid;
1664 delete pEdgeVectorRecordFeature;
1665 pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
1668 wxString Message = SENCfile.GetFullPath();
1669 Message.Append(
"...Ingesting");
1671 wxString Title(_(
"OpenCPN S57 SENC File Create..."));
1672 Title.append(SENCfile.GetFullPath());
1674#if wxUSE_PROGRESSDLG
1677 int nProg = poReader->GetFeatureCount();
1679 if (wxThread::IsMain() && b_showProg) {
1680 m_ProgDialog =
new wxGenericProgressDialog();
1683 m_ProgDialog->SetFont(*qFont);
1685 m_ProgDialog->Create(Title, Message, nProg, NULL,
1686 wxPD_AUTO_HIDE | wxPD_SMOOTH);
1691 OGRFeature *objectDef;
1696 objectDef = poReader->ReadNextFeature();
1698 if (objectDef != NULL) {
1701#if wxUSE_PROGRESSDLG
1707 if (m_ProgDialog && progsw.Time() > 200) {
1711 wxString(objectDef->GetDefnRef()->GetName(), wxConvUTF8);
1712 sobj.Append(wxString::Format(
" %d/%d ", iObj, nProg));
1714 bcont = m_ProgDialog->Update(iObj, sobj);
1715#if defined(__WXMSW__) || defined(__WXOSX__)
1721 OGRwkbGeometryType geoType = wkbUnknown;
1728 if (objectDef->GetGeometryRef() != NULL)
1729 geoType = objectDef->GetGeometryRef()->getGeometryType();
1732 if (geoType != wkbUnknown) {
1733 CreateSENCRecord200(objectDef, stream, 1, poReader);
1744 CreateSENCVectorEdgeTableRecord200(stream, poReader);
1747 CreateSENCVectorConnectedTableRecord200(stream, poReader);
1752 delete m_pOutstream;
1756 for (
unsigned int iff = 0; iff < m_tmpup_array.GetCount(); iff++)
1757 remove(m_tmpup_array[iff].mb_str());
1763 wxRemoveFile(tmp_file);
1764 ret_code = ERROR_SENCFILE_ABORT;
1768 bool cpok = wxRenameFile(tmp_file, SENCfile.GetFullPath());
1770 errorMessage =
" Cannot rename temporary SENC file ";
1771 errorMessage.Append(tmp_file);
1772 errorMessage.Append(
" to ");
1773 errorMessage.Append(SENCfile.GetFullPath());
1774 ret_code = ERROR_SENCFILE_ABORT;
1776 ret_code = SENC_NO_ERROR;
1779#if wxUSE_PROGRESSDLG
1780 delete m_ProgDialog;
1791 record.record_type = CELL_EXTENT_RECORD;
1793 record.extent_sw_lat = m_extent.SLAT;
1794 record.extent_sw_lon = m_extent.WLON;
1795 record.extent_nw_lat = m_extent.NLAT;
1796 record.extent_nw_lon = m_extent.WLON;
1797 record.extent_ne_lat = m_extent.NLAT;
1798 record.extent_ne_lon = m_extent.ELON;
1799 record.extent_se_lat = m_extent.SLAT;
1800 record.extent_se_lon = m_extent.ELON;
1802 size_t targetCount =
sizeof(record);
1803 if (!stream->Write(&record, targetCount).IsOk())
return false;
1805 for (
int i = 0; i < m_nCOVREntries; i++) {
1806 int nPoints = m_pCOVRTablePoints[i];
1808 float *fpbuf = m_pCOVRTable[i];
1812 record.record_type = CELL_COVR_RECORD;
1814 (nPoints * 2 *
sizeof(float));
1817 size_t targetCount =
sizeof(record);
1818 if (!stream->Write(&record, targetCount).IsOk())
return false;
1821 targetCount =
sizeof(uint32_t);
1822 if (!stream->Write(&nPoints, targetCount).IsOk())
return false;
1825 targetCount = nPoints * 2 *
sizeof(float);
1826 if (!stream->Write(fpbuf, targetCount).IsOk())
return false;
1829 for (
int i = 0; i < m_nNoCOVREntries; i++) {
1830 int nPoints = m_pNoCOVRTablePoints[i];
1832 float *fpbuf = m_pNoCOVRTable[i];
1836 record.record_type = CELL_NOCOVR_RECORD;
1838 sizeof(uint32_t) + (nPoints * 2 *
sizeof(float));
1841 size_t targetCount =
sizeof(record);
1842 if (!stream->Write(&record, targetCount).IsOk())
return false;
1845 targetCount =
sizeof(uint32_t);
1846 if (!stream->Write(&nPoints, targetCount).IsOk())
return false;
1849 targetCount = nPoints * 2 *
sizeof(float);
1850 if (!stream->Write(fpbuf, targetCount).IsOk())
return false;
1856bool Osenc::WriteHeaderRecord200(
Osenc_outstream *stream,
int recordType,
1857 std::string payload) {
1858 int payloadLength = payload.length() + 1;
1862 unsigned char *pBuffer = getBuffer(recordLength);
1865 memset(pRecord, 0, recordLength);
1866 pRecord->record_type = recordType;
1867 pRecord->record_length = recordLength;
1868 memcpy(&pRecord->payload, payload.c_str(), payloadLength);
1870 size_t targetCount = recordLength;
1871 if (!stream->Write(pBuffer, targetCount).IsOk())
1877bool Osenc::WriteHeaderRecord200(
Osenc_outstream *stream,
int recordType,
1879 int payloadLength =
sizeof(uint16_t);
1883 unsigned char *pBuffer = getBuffer(recordLength);
1886 memset(pRecord, 0, recordLength);
1887 pRecord->record_type = recordType;
1888 pRecord->record_length = recordLength;
1889 memcpy(&pRecord->payload, &val, payloadLength);
1891 size_t targetCount = recordLength;
1892 if (!stream->Write(pBuffer, targetCount).IsOk())
1898bool Osenc::WriteHeaderRecord200(
Osenc_outstream *stream,
int recordType,
1900 int payloadLength =
sizeof(uint32_t);
1904 unsigned char *pBuffer = getBuffer(recordLength);
1907 memset(pRecord, 0, recordLength);
1908 pRecord->record_type = recordType;
1909 pRecord->record_length = recordLength;
1910 memcpy(&pRecord->payload, &val, payloadLength);
1912 size_t targetCount = recordLength;
1913 if (!stream->Write(pBuffer, targetCount).IsOk())
1919bool Osenc::WriteFIDRecord200(
Osenc_outstream *stream,
int nOBJL,
int featureID,
1922 memset(&record, 0,
sizeof(record));
1924 record.record_type = FEATURE_ID_RECORD;
1925 record.record_length =
sizeof(record);
1927 record.feature_ID = featureID;
1928 record.feature_type_code = nOBJL;
1929 record.feature_primitive = prim;
1931 size_t targetCount =
sizeof(record);
1932 if (!stream->Write(&record, targetCount).IsOk())
1938bool Osenc::CreateMultiPointFeatureGeometryRecord200(OGRFeature *pFeature,
1940 OGRGeometry *pGeo = pFeature->GetGeometryRef();
1942 int wkb_len = pGeo->WkbSize();
1943 unsigned char *pwkb_buffer = (
unsigned char *)malloc(wkb_len);
1946 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
1950 unsigned char *ps = pwkb_buffer;
1952 int nPoints = *((
int *)ps);
1954 int sb_len = (nPoints * 3 *
sizeof(float));
1956 unsigned char *psb_buffer = (
unsigned char *)malloc(sb_len);
1957 unsigned char *pd = psb_buffer;
1962 float *pdf = (
float *)pd;
1965 float lonmax = -1000;
1966 float lonmin = 1000;
1967 float latmax = -1000;
1968 float latmin = 1000;
1970 for (
int ip = 0; ip < nPoints; ip++) {
1978 OGRGeometryCollection *temp_geometry_collection =
1979 (OGRGeometryCollection *)pGeo;
1980 OGRGeometry *temp_geometry = temp_geometry_collection->getGeometryRef(ip);
1981 OGRPoint *pt_geom = (OGRPoint *)temp_geometry;
1983 double lon = pt_geom->getX();
1984 double lat = pt_geom->getY();
1985 double depth = pt_geom->getZ();
1988 double easting, northing;
1989 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
1992 float __attribute__((aligned(16))) east = easting;
1993 float __attribute__((aligned(16))) north = northing;
1994 float __attribute__((aligned(16))) deep = depth;
1995 unsigned char *puceast = (
unsigned char *)&east;
1996 unsigned char *pucnorth = (
unsigned char *)&north;
1997 unsigned char *pucdeep = (
unsigned char *)&deep;
1999 memcpy(pdf++, puceast, sizeof(
float));
2000 memcpy(pdf++, pucnorth, sizeof(
float));
2001 memcpy(pdf++, pucdeep, sizeof(
float));
2006 *pdf++ = (float)depth;
2010 lonmax = fmax(lon, lonmax);
2011 lonmin = fmin(lon, lonmin);
2012 latmax = fmax(lat, latmax);
2013 latmin = fmin(lat, latmin);
2018 record.record_type = FEATURE_GEOMETRY_RECORD_MULTIPOINT;
2020 (nPoints * 3 *
sizeof(
float));
2021 record.extent_e_lon = lonmax;
2022 record.extent_w_lon = lonmin;
2023 record.extent_n_lat = latmax;
2024 record.extent_s_lat = latmin;
2025 record.point_count = nPoints;
2028 size_t targetCount =
sizeof(record);
2029 if (!stream->Write(&record, targetCount).IsOk())
goto failure;
2031 targetCount = nPoints * 3 *
sizeof(float);
2032 if (!stream->Write(psb_buffer, targetCount).IsOk())
goto failure;
2045bool Osenc::CreateLineFeatureGeometryRecord200(S57Reader *poReader,
2046 OGRFeature *pFeature,
2048 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2050 int wkb_len = pGeo->WkbSize();
2051 unsigned char *pwkb_buffer = (
unsigned char *)malloc(wkb_len);
2054 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
2059 ((wkb_len - 9) / 2) + 9 + 16;
2061 unsigned char *psb_buffer = (
unsigned char *)malloc(sb_len);
2062 unsigned char *pd = psb_buffer;
2063 unsigned char *ps = pwkb_buffer;
2067 int ip = *((
int *)(ps + 5));
2071 double *psd = (
double *)ps;
2072 float *pdf = (
float *)pd;
2075 float lonmax = -1000;
2076 float lonmin = 1000;
2077 float latmax = -1000;
2078 float latmin = 1000;
2080 for (
int i = 0; i < ip; i++) {
2083 double easting, northing;
2085 double __attribute__((aligned(16))) east_d, north_d;
2086 unsigned char *pucd = (
unsigned char *)psd;
2088 memcpy(&east_d, pucd, sizeof(
double));
2090 pucd += sizeof(
double);
2091 memcpy(&north_d, pucd, sizeof(
double));
2097 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
2098 unsigned char *puceasting = (
unsigned char *)&easting;
2099 unsigned char *pucnorthing = (
unsigned char *)&northing;
2101 memcpy(pdf++, puceasting, sizeof(
float));
2102 memcpy(pdf++, pucnorthing, sizeof(
float));
2105 lon = (float)*psd++;
2106 lat = (float)*psd++;
2109 toSM(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
2115 lonmax = fmax(lon, lonmax);
2116 lonmin = fmin(lon, lonmin);
2117 latmax = fmax(lat, latmax);
2118 latmin = fmin(lat, latmin);
2121 int nEdgeVectorRecords = 0;
2122 unsigned char *pvec_buffer =
2123 getObjectVectorIndexTable(poReader, pFeature, nEdgeVectorRecords);
2130 int nEdgeVectorRecords;
2131 OGRFeature *pEdgeVectorRecordFeature;
2133 pNAME_RCID = (
int *) pFeature->GetFieldAsIntegerList(
"NAME_RCID", &nEdgeVectorRecords );
2134 pORNT = (
int *) pFeature->GetFieldAsIntegerList(
"ORNT", NULL );
2141 unsigned char *pvec_buffer = (
unsigned char *)malloc(nEdgeVectorRecords * 3 *
sizeof(
int));
2142 unsigned char *pvRun = pvec_buffer;
2145 char ** papszReaderOptions = NULL;
2146 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_UPDATES,
"ON" );
2147 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_LINKAGES,
"ON" );
2148 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON" );
2149 poReader->SetOptions( papszReaderOptions );
2152 for(
int i = 0; i < nEdgeVectorRecords; i++ ) {
2154 int *pI = (
int *)pvRun;
2156 int edge_rcid = pNAME_RCID[i];
2158 int start_rcid, end_rcid;
2159 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2160 pEdgeVectorRecordFeature = poReader->ReadVector( target_record_feid, RCNM_VE );
2162 if( NULL != pEdgeVectorRecordFeature ) {
2163 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_0" );
2164 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_1" );
2170 if( !poReader->FetchPoint( RCNM_VC, start_rcid, NULL, NULL, NULL, NULL ) )
2172 if( !poReader->FetchPoint( RCNM_VC, end_rcid, NULL, NULL, NULL, NULL ) )
2175 OGRLineString *poLS = (OGRLineString *)pEdgeVectorRecordFeature->GetGeometryRef();
2179 if( edge_ornt == 1 ){
2189 delete pEdgeVectorRecordFeature;
2199 pvRun += 3 *
sizeof(int);
2204 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF" );
2205 poReader->SetOptions( papszReaderOptions );
2206 CSLDestroy( papszReaderOptions );
2211 record.record_type = FEATURE_GEOMETRY_RECORD_LINE;
2213 (nEdgeVectorRecords * 3 *
sizeof(
int));
2214 record.extent_e_lon = lonmax;
2215 record.extent_w_lon = lonmin;
2216 record.extent_n_lat = latmax;
2217 record.extent_s_lat = latmin;
2218 record.edgeVector_count = nEdgeVectorRecords;
2221 size_t targetCount =
sizeof(record);
2222 if (!stream->Write(&record, targetCount).IsOk())
return false;
2225 targetCount = nEdgeVectorRecords * 3 *
sizeof(int);
2226 if (!stream->Write(pvec_buffer, targetCount).IsOk())
return false;
2236bool Osenc::CreateAreaFeatureGeometryRecord200(S57Reader *poReader,
2237 OGRFeature *pFeature,
2241 PolyTessGeo *ppg = NULL;
2243 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2244 OGRPolygon *poly = (OGRPolygon *)(pGeo);
2246 if (!poly->getExteriorRing())
return false;
2249 ppg =
new PolyTessGeo(poly,
true, m_ref_lat, m_ref_lon, m_LOD_meters);
2252 error_code = ppg->ErrorCode;
2256 " Warning: S57 SENC Geometry Error %d, Some Features ignored.",
2267 memset(&baseRecord, 0,
sizeof(baseRecord));
2269 baseRecord.record_type = FEATURE_GEOMETRY_RECORD_AREA;
2273 baseRecord.extent_s_lat = ppg->Get_ymin();
2274 baseRecord.extent_n_lat = ppg->Get_ymax();
2275 baseRecord.extent_e_lon = ppg->Get_xmax();
2276 baseRecord.extent_w_lon = ppg->Get_xmin();
2278 baseRecord.contour_count = ppg->GetnContours();
2282 int contourPointCountArraySize = ppg->GetnContours() *
sizeof(uint32_t);
2283 uint32_t *contourPointCountArray =
2284 (uint32_t *)malloc(contourPointCountArraySize);
2286 uint32_t *pr = contourPointCountArray;
2288 for (
int i = 0; i < ppg->GetnContours(); i++) {
2289 *pr++ = ppg->Get_PolyTriGroup_head()->pn_vertex[i];
2305 TriPrim *pTP = ppg->Get_PolyTriGroup_head()
2310 geoLength +=
sizeof(uint8_t) +
sizeof(uint32_t);
2311 geoLength += pTP->nVert * 2 *
sizeof(float);
2312 geoLength += 4 *
sizeof(double);
2318 baseRecord.triprim_count = n_TriPrims;
2320 int nEdgeVectorRecords = 0;
2321 unsigned char *pvec_buffer =
2322 getObjectVectorIndexTable(poReader, pFeature, nEdgeVectorRecords);
2329 int nEdgeVectorRecords;
2330 OGRFeature *pEdgeVectorRecordFeature;
2332 pNAME_RCID = (
int *) pFeature->GetFieldAsIntegerList(
"NAME_RCID", &nEdgeVectorRecords );
2333 pORNT = (
int *) pFeature->GetFieldAsIntegerList(
"ORNT", NULL );
2335 baseRecord.edgeVector_count = nEdgeVectorRecords;
2337 unsigned char *pvec_buffer = (
unsigned char *)malloc(nEdgeVectorRecords * 3 *
sizeof(
int));
2338 unsigned char *pvRun = pvec_buffer;
2341 char ** papszReaderOptions = NULL;
2342 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_UPDATES,
"ON" );
2343 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_LINKAGES,
"ON" );
2344 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON" );
2345 poReader->SetOptions( papszReaderOptions );
2348 for(
int i = 0; i < nEdgeVectorRecords; i++ ) {
2350 int *pI = (
int *)pvRun;
2352 int start_rcid, end_rcid;
2353 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2354 pEdgeVectorRecordFeature = poReader->ReadVector( target_record_feid, RCNM_VE );
2356 int edge_rcid = pNAME_RCID[i];
2358 if( NULL != pEdgeVectorRecordFeature ) {
2359 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_0" );
2360 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_1" );
2365 if( !poReader->FetchPoint( RCNM_VC, start_rcid, NULL, NULL, NULL, NULL ) )
2367 if( !poReader->FetchPoint( RCNM_VC, end_rcid, NULL, NULL, NULL, NULL ) )
2373 if( edge_ornt == 1 ){
2383 delete pEdgeVectorRecordFeature;
2393 pvRun += 3 *
sizeof(int);
2398 papszReaderOptions = CSLSetNameValue( papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF" );
2399 poReader->SetOptions( papszReaderOptions );
2400 CSLDestroy( papszReaderOptions );
2404 baseRecord.edgeVector_count = nEdgeVectorRecords;
2408 recordLength += contourPointCountArraySize;
2409 recordLength += geoLength;
2410 recordLength += nEdgeVectorRecords * 3 *
sizeof(int);
2411 baseRecord.record_length = recordLength;
2414 size_t targetCount =
sizeof(baseRecord);
2415 if (!stream->Write(&baseRecord, targetCount).IsOk())
return false;
2418 targetCount = contourPointCountArraySize;
2419 if (!stream->Write(contourPointCountArray, targetCount).IsOk())
return false;
2422 pTP = ppg->Get_PolyTriGroup_head()
2425 if (!stream->Write(&pTP->type,
sizeof(uint8_t)).IsOk())
return false;
2426 if (!stream->Write(&pTP->nVert,
sizeof(uint32_t)).IsOk())
return false;
2433 double minlat, minlon, maxlat, maxlon;
2434 minlat = pTP->tri_box.GetMinLat();
2435 minlon = pTP->tri_box.GetMinLon();
2436 maxlat = pTP->tri_box.GetMaxLat();
2437 maxlon = pTP->tri_box.GetMaxLon();
2439 if (!stream->Write(&minlon,
sizeof(
double)).IsOk())
return false;
2440 if (!stream->Write(&maxlon,
sizeof(
double)).IsOk())
return false;
2441 if (!stream->Write(&minlat,
sizeof(
double)).IsOk())
return false;
2442 if (!stream->Write(&maxlat,
sizeof(
double)).IsOk())
return false;
2449 if (!stream->Write(pTP->p_vertex, pTP->nVert * 2 *
sizeof(
float)).IsOk())
2456 targetCount = nEdgeVectorRecords * 3 *
sizeof(int);
2457 if (!stream->Write(pvec_buffer, targetCount).IsOk())
return false;
2460 free(contourPointCountArray);
2466unsigned char *Osenc::getObjectVectorIndexTable(S57Reader *poReader,
2467 OGRFeature *pFeature,
2473 int nEdgeVectorRecords;
2474 OGRFeature *pEdgeVectorRecordFeature;
2477 (
int *)pFeature->GetFieldAsIntegerList(
"NAME_RCID", &nEdgeVectorRecords);
2478 pORNT = (
int *)pFeature->GetFieldAsIntegerList(
"ORNT", NULL);
2480 nEntries = nEdgeVectorRecords;
2482 unsigned char *pvec_buffer =
2483 (
unsigned char *)malloc(nEdgeVectorRecords * 3 *
sizeof(
int));
2484 unsigned char *pvRun = pvec_buffer;
2487 char **papszReaderOptions = NULL;
2488 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
"ON");
2489 papszReaderOptions =
2490 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
2491 papszReaderOptions =
2492 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
2493 poReader->SetOptions(papszReaderOptions);
2497 for (
int i = 0; i < nEdgeVectorRecords; i++) {
2498 int *pI = (
int *)pvRun;
2500 int edge_rcid = pNAME_RCID[i];
2502 int start_rcid, end_rcid;
2503 int target_record_feid = m_vector_helper_hash[pNAME_RCID[i]];
2504 pEdgeVectorRecordFeature =
2505 poReader->ReadVector(target_record_feid, RCNM_VE);
2507 if (NULL != pEdgeVectorRecordFeature) {
2508 start_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_0");
2509 end_rcid = pEdgeVectorRecordFeature->GetFieldAsInteger(
"NAME_RCID_1");
2515 if (!poReader->FetchPoint(RCNM_VC, start_rcid, NULL, NULL, NULL, NULL))
2517 if (!poReader->FetchPoint(RCNM_VC, end_rcid, NULL, NULL, NULL, NULL))
2520 OGRLineString *poLS =
2521 (OGRLineString *)pEdgeVectorRecordFeature->GetGeometryRef();
2524 else if (poLS->getNumPoints() < 1)
2527 int edge_ornt = pORNT[i];
2529 if (edge_ornt == 1) {
2539 delete pEdgeVectorRecordFeature;
2549 pvRun += 3 *
sizeof(int);
2553 papszReaderOptions =
2554 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
2555 poReader->SetOptions(papszReaderOptions);
2556 CSLDestroy(papszReaderOptions);
2561void Osenc::CreateSENCVectorEdgeTableRecord200(
Osenc_outstream *stream,
2562 S57Reader *poReader) {
2564 uint8_t *pPayload = NULL;
2565 int payloadSize = 0;
2566 uint8_t *pRun = pPayload;
2569 char **papszReaderOptions = NULL;
2570 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
"ON");
2571 papszReaderOptions =
2572 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
2573 papszReaderOptions =
2574 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
2575 poReader->SetOptions(papszReaderOptions);
2578 OGRLineString *pLS = NULL;
2580 OGRFeature *pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
2585 while (NULL != pEdgeVectorRecordFeature) {
2590 if (pEdgeVectorRecordFeature->GetGeometryRef() != NULL) {
2591 pGeo = pEdgeVectorRecordFeature->GetGeometryRef();
2592 if (pGeo->getGeometryType() == wkbLineString) {
2593 pLS = (OGRLineString *)pGeo;
2594 nPoints = pLS->getNumPoints();
2600 int new_size = payloadSize + (2 *
sizeof(int));
2601 pPayload = (uint8_t *)realloc(pPayload, new_size);
2602 pRun = pPayload + payloadSize;
2604 payloadSize = new_size;
2607 int record_id = pEdgeVectorRecordFeature->GetFieldAsInteger(
"RCID");
2608 *(
int *)pRun = record_id;
2609 pRun +=
sizeof(int);
2616 int reduction_ratio = nPoints / MAX_VECTOR_POINTS + 1;
2617 int reduced_points = 0;
2619 double *ppd = (
double *)malloc((nPoints / reduction_ratio + 1) * 2 *
2623 for (
int i = 0; i < nPoints; i++) {
2624 if (i % reduction_ratio == 0) {
2626 pLS->getPoint(i, &p);
2629 double easting, northing;
2630 toSM(p.getY(), p.getX(), m_ref_lat, m_ref_lon, &easting, &northing);
2637 nPoints = reduced_points;
2640 std::vector<int> index_keep;
2641 if (nPoints > 5 && (m_LOD_meters > .01)) {
2642 index_keep.push_back(0);
2643 index_keep.push_back(nPoints - 1);
2645 DouglasPeucker(ppd, 0, nPoints - 1, m_LOD_meters, &index_keep);
2650 index_keep.resize(nPoints);
2651 for (
int i = 0; i < nPoints; i++) index_keep[i] = i;
2655 int nPointReduced = index_keep.size();
2656 *(
int *)pRun = nPointReduced;
2657 pRun +=
sizeof(int);
2662 int new_size_red = payloadSize + (nPointReduced * 2 *
sizeof(float));
2663 pPayload = (uint8_t *)realloc(pPayload, new_size_red);
2664 pRun = pPayload + payloadSize;
2666 payloadSize = new_size_red;
2668 float *npp = (
float *)pRun;
2669 float *npp_run = npp;
2671 for (
int ip = 0; ip < nPoints; ip++) {
2675 for (
unsigned int j = 0; j < index_keep.size(); j++) {
2676 if (index_keep[j] == ip) {
2679 pRun += 2 *
sizeof(float);
2692 delete pEdgeVectorRecordFeature;
2693 pEdgeVectorRecordFeature = poReader->ReadVector(feid, RCNM_VE);
2702 record.record_type = VECTOR_EDGE_NODE_TABLE_RECORD;
2703 record.record_length =
2710 stream->Write(&nFeatures,
sizeof(uint32_t));
2713 stream->Write(pPayload, payloadSize);
2719 papszReaderOptions =
2720 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
2721 poReader->SetOptions(papszReaderOptions);
2722 CSLDestroy(papszReaderOptions);
2725void Osenc::CreateSENCVectorConnectedTableRecord200(
Osenc_outstream *stream,
2726 S57Reader *poReader) {
2728 uint8_t *pPayload = NULL;
2729 int payloadSize = 0;
2730 uint8_t *pRun = pPayload;
2733 char **papszReaderOptions = NULL;
2734 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
"ON");
2735 papszReaderOptions =
2736 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
"ON");
2737 papszReaderOptions =
2738 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"ON");
2739 poReader->SetOptions(papszReaderOptions);
2744 OGRFeature *pConnNodeRecordFeature = poReader->ReadVector(feid, RCNM_VC);
2745 int featureCount = 0;
2748 while (NULL != pConnNodeRecordFeature) {
2749 if (pConnNodeRecordFeature->GetGeometryRef() != NULL) {
2750 pGeo = pConnNodeRecordFeature->GetGeometryRef();
2751 if (pGeo->getGeometryType() == wkbPoint) {
2752 int new_size = payloadSize +
sizeof(int) + (2 *
sizeof(
float));
2753 pPayload = (uint8_t *)realloc(pPayload, new_size);
2754 pRun = pPayload + payloadSize;
2756 payloadSize = new_size;
2759 int record_id = pConnNodeRecordFeature->GetFieldAsInteger(
"RCID");
2760 *(
int *)pRun = record_id;
2761 pRun +=
sizeof(int);
2763 pP = (OGRPoint *)pGeo;
2766 double easting, northing;
2767 toSM(pP->getY(), pP->getX(), m_ref_lat, m_ref_lon, &easting, &northing);
2773 float *ps = (
float *)pRun;
2783 delete pConnNodeRecordFeature;
2784 pConnNodeRecordFeature = poReader->ReadVector(feid, RCNM_VC);
2793 record.record_type = VECTOR_CONNECTED_NODE_TABLE_RECORD;
2794 record.record_length =
2801 stream->Write(&featureCount,
sizeof(uint32_t));
2804 stream->Write(pPayload, payloadSize);
2811 papszReaderOptions =
2812 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
"OFF");
2813 poReader->SetOptions(papszReaderOptions);
2814 CSLDestroy(papszReaderOptions);
2817bool Osenc::CreateSENCRecord200(OGRFeature *pFeature,
Osenc_outstream *stream,
2818 int mode, S57Reader *poReader) {
2826 OGRFeatureDefn *pFD = pFeature->GetDefnRef();
2827 int nOBJL = pFD->GetOBJL();
2829 OGRGeometry *pGeo = pFeature->GetGeometryRef();
2830 OGRwkbGeometryType gType = pGeo->getGeometryType();
2835 primitive = GEO_LINE;
2838 primitive = GEO_POINT;
2841 primitive = GEO_AREA;
2847 if (!WriteFIDRecord200(stream, nOBJL, pFeature->GetFID(), primitive))
2850#define MAX_HDR_LINE 400
2866 if (pFeature->GetFID() == 290)
int yyp = 4;
2868 int payloadLength = 0;
2869 void *payloadBuffer = NULL;
2870 unsigned int payloadBufferLength = 0;
2872 for (
int iField = 0; iField < pFeature->GetFieldCount(); iField++) {
2873 if (pFeature->IsFieldSet(iField)) {
2874 if ((iField == 1) || (iField > 7)) {
2875 OGRFieldDefn *poFDefn = pFeature->GetDefnRef()->GetFieldDefn(iField);
2878 const char *pAttrName = poFDefn->GetNameRef();
2879 const char *pAttrVal = pFeature->GetFieldAsString(iField);
2884 int attributeID = m_pRegistrarMan->getAttributeID(pAttrName);
2887 int OGRvalueType = (int)poFDefn->GetType();
2891 if (-1 == attributeID) {
2892 if (!strncmp(pAttrName,
"PRIM", 4)) {
2893 attributeID = ATTRIBUTE_ID_PRIM;
OFTInteger = 0, OFTIntegerList = 1, OFTReal = 2, OFTRealList = 3, OFTString = 4, OFTStringList = 5, OFTWideString = 6, OFTWideStringList = 7, OFTBinary = 8
2908 switch (OGRvalueType) {
2911 valueType = OGRvalueType;
2913 if (payloadBufferLength < 4) {
2914 payloadBuffer = realloc(payloadBuffer, 4);
2915 payloadBufferLength = 4;
2918 int aValue = pFeature->GetFieldAsInteger(iField);
2919 memcpy(payloadBuffer, &aValue,
sizeof(
int));
2920 payloadLength =
sizeof(int);
2926 valueType = OGRvalueType;
2929 const int *aValueList =
2930 pFeature->GetFieldAsIntegerList(iField, &nCount);
2932 if (payloadBufferLength < nCount *
sizeof(
int)) {
2933 payloadBuffer = realloc(payloadBuffer, nCount *
sizeof(
int));
2934 payloadBufferLength = nCount *
sizeof(int);
2937 int *pBuffRun = (
int *)payloadBuffer;
2938 for (
int i = 0; i < nCount; i++) {
2939 *pBuffRun++ = aValueList[i];
2941 payloadLength = nCount *
sizeof(int);
2947 valueType = OGRvalueType;
2949 if (payloadBufferLength <
sizeof(
double)) {
2950 payloadBuffer = realloc(payloadBuffer,
sizeof(
double));
2951 payloadBufferLength =
sizeof(double);
2954 double aValue = pFeature->GetFieldAsDouble(iField);
2955 memcpy(payloadBuffer, &aValue,
sizeof(
double));
2956 payloadLength =
sizeof(double);
2963 valueType = OGRvalueType;
2966 const double *aValueList =
2967 pFeature->GetFieldAsDoubleList(iField, &nCount);
2969 if (payloadBufferLength < nCount *
sizeof(
double)) {
2970 payloadBuffer = realloc(payloadBuffer, nCount *
sizeof(
double));
2971 payloadBufferLength = nCount *
sizeof(double);
2974 double *pBuffRun = (
double *)payloadBuffer;
2975 for (
int i = 0; i < nCount; i++) {
2976 *pBuffRun++ = aValueList[i];
2978 payloadLength = nCount *
sizeof(double);
2985 valueType = OGRvalueType;
2986 const char *pAttrVal = pFeature->GetFieldAsString(iField);
2988 wxString wxAttrValue;
2990 if ((0 == strncmp(
"NOBJNM", pAttrName, 6)) ||
2991 (0 == strncmp(
"NINFOM", pAttrName, 6)) ||
2992 (0 == strncmp(
"NPLDST", pAttrName, 6)) ||
2993 (0 == strncmp(
"NTXTDS", pAttrName, 6))) {
2994 if (poReader->GetNall() ==
2997 wxString att_conv(pAttrVal, conv);
2998 att_conv.RemoveLast();
3000 att_conv.Replace(
"\n",
3003 wxAttrValue = att_conv;
3004 }
else if (poReader->GetNall() ==
3006 wxCSConv conv(
"iso8859-1");
3007 wxString att_conv(pAttrVal, conv);
3008 wxAttrValue = att_conv;
3011 if (poReader->GetAall() ==
3014 wxCSConv conv(
"iso8859-1");
3015 wxString att_conv(pAttrVal, conv);
3016 wxAttrValue = att_conv;
3023 unsigned int stringPayloadLength = 0;
3025 wxCharBuffer buffer;
3026 if (wxAttrValue.Length()) {
3027 buffer = wxAttrValue.ToUTF8();
3028 pAttrVal = buffer.data();
3029 stringPayloadLength = strlen(buffer.data());
3032 if (stringPayloadLength) {
3033 if (payloadBufferLength < stringPayloadLength + 1) {
3034 payloadBuffer = realloc(payloadBuffer, stringPayloadLength + 1);
3035 payloadBufferLength = stringPayloadLength + 1;
3038 strcpy((
char *)payloadBuffer, pAttrVal);
3039 payloadLength = stringPayloadLength + 1;
3051 if (-1 != attributeID) {
3057 unsigned char *pBuffer = getBuffer(recordLength);
3061 pRecord->record_type = FEATURE_ATTRIBUTE_RECORD;
3062 pRecord->record_length = recordLength;
3063 pRecord->attribute_type = attributeID;
3064 pRecord->attribute_value_type = valueType;
3065 memcpy(&pRecord->payload, payloadBuffer, payloadLength);
3068 size_t targetCount = recordLength;
3069 if (!stream->Write(pBuffer, targetCount).IsOk()) {
3070 free(payloadBuffer);
3077 if (wkbPoint == pGeo->getGeometryType()) {
3078 OGRPoint *pp = (OGRPoint *)pGeo;
3079 int nqual = pp->getnQual();
3082 int attributeID = m_pRegistrarMan->getAttributeID(
"QUAPOS");
3084 if (-1 != attributeID) {
3085 if (payloadBufferLength < 4) {
3086 payloadBuffer = realloc(payloadBuffer, 4);
3087 payloadBufferLength = 4;
3090 memcpy(payloadBuffer, &nqual,
sizeof(
int));
3091 payloadLength =
sizeof(int);
3096 unsigned char *pBuffer = getBuffer(recordLength);
3100 pRecord->record_type = FEATURE_ATTRIBUTE_RECORD;
3101 pRecord->record_length = recordLength;
3102 pRecord->attribute_type = attributeID;
3103 pRecord->attribute_value_type = valueType;
3104 memcpy(&pRecord->payload, payloadBuffer, payloadLength);
3107 size_t targetCount = recordLength;
3108 if (!stream->Write(pBuffer, targetCount).IsOk()) {
3109 free(payloadBuffer);
3116 free(payloadBuffer);
3121 if( wkbPoint == pGeo->getGeometryType() ) {
3122 OGRPoint *pp = (OGRPoint *) pGeo;
3123 int nqual = pp->getnQual();
3126 snprintf( line, MAX_HDR_LINE - 2,
" %s (%c) = %d",
"QUALTY",
'I', nqual );
3127 sheader += wxString( line, wxConvUTF8 );
3134 sprintf( line,
" %s %f %f\n", pGeo->getGeometryName(), m_ref_lat, m_ref_lon );
3135 sheader += wxString( line, wxConvUTF8 );
3138 wxCharBuffer buffer=sheader.ToUTF8();
3139 fprintf( fpOut,
"HDRLEN=%lu\n", (
unsigned long) strlen(buffer) );
3140 fwrite( buffer.data(), 1, strlen(buffer), fpOut );
3144 if ((pGeo != NULL)) {
3147 OGRwkbGeometryType gType = pGeo->getGeometryType();
3149 case wkbLineString: {
3150 if (!CreateLineFeatureGeometryRecord200(poReader, pFeature, stream))
3158 record.record_type = FEATURE_GEOMETRY_RECORD_POINT;
3159 record.record_length =
sizeof(record);
3161 int wkb_len = pGeo->WkbSize();
3162 unsigned char *pwkb_buffer = (
unsigned char *)malloc(wkb_len);
3165 pGeo->exportToWkb(wkbNDR, pwkb_buffer);
3168 unsigned char *ps = pwkb_buffer;
3171 double *psd = (
double *)ps;
3175 __attribute__((aligned(16))) double lata, lona;
3176 unsigned char *pucsd = (
unsigned char *)psd;
3178 memcpy(&lona, pucsd, sizeof(
double));
3179 pucsd += sizeof(
double);
3180 memcpy(&lata, pucsd, sizeof(
double));
3194 size_t targetCount = record.record_length;
3195 if (!stream->Write(&record, targetCount).IsOk())
return false;
3201 case wkbMultiPoint25D: {
3202 if (!CreateMultiPointFeatureGeometryRecord200(pFeature, stream))
3210 if (!CreateAreaFeatureGeometryRecord200(poReader, pFeature, stream))
3218 msg =
" Warning: Unimplemented ogr geotype record ";
3231 unsigned char **next_byte) {
3232 PolyTessGeo *pPTG =
new PolyTessGeo();
3234 pPTG->SetExtents(record->extent_w_lon, record->extent_s_lat,
3235 record->extent_e_lon, record->extent_n_lat);
3237 unsigned int n_TriPrim = record->triprim_count;
3238 int nContours = record->contour_count;
3241 void *payLoad = &record->payLoad;
3247 PolyTriGroup *ppg =
new PolyTriGroup;
3248 ppg->m_bSMSENC =
true;
3249 ppg->data_type = DATA_TYPE_DOUBLE;
3251 ppg->nContours = nContours;
3253 ppg->pn_vertex = (
int *)malloc(nContours *
sizeof(
int));
3254 int *pctr = ppg->pn_vertex;
3257 int *contour_pointcount_array_run = (
int *)payLoad;
3258 for (
int i = 0; i < nContours; i++) {
3259 *pctr++ = *contour_pointcount_array_run++;
3263 ppg->pgroup_geom = NULL;
3267 TriPrim **p_prev_triprim = &(ppg->tri_prim_head);
3270 unsigned int tri_type;
3273 int total_byte_size = 2 *
sizeof(float);
3275 uint8_t *pPayloadRun =
3276 (uint8_t *)contour_pointcount_array_run;
3279 for (
unsigned int i = 0; i < n_TriPrim; i++) {
3280 tri_type = *pPayloadRun++;
3281 nvert = *(uint32_t *)pPayloadRun;
3282 pPayloadRun +=
sizeof(uint32_t);
3284 TriPrim *tp =
new TriPrim;
3285 *p_prev_triprim = tp;
3286 p_prev_triprim = &(tp->p_next);
3289 tp->type = tri_type;
3293 wxMax(nvert_max, nvert);
3296 double *pbb = (
double *)pPayloadRun;
3298 double minxt, minyt, maxxt, maxyt;
3301 double __attribute__((aligned(16))) abox[4];
3302 unsigned char *pucbb = (
unsigned char *)pPayloadRun;
3303 memcpy(&abox[0], pucbb, 4 * sizeof(
double));
3316 tp->tri_box.Set(minyt, minxt, maxyt, maxxt);
3318 pPayloadRun += 4 *
sizeof(double);
3320 int byte_size = nvert * 2 *
sizeof(float);
3321 total_byte_size += byte_size;
3323 tp->p_vertex = (
double *)pPayloadRun;
3325 pPayloadRun += byte_size;
3328 if (next_byte) *next_byte = pPayloadRun;
3332 unsigned char *vbuf = (
unsigned char *)malloc(total_byte_size);
3334 TriPrim *p_tp = ppg->tri_prim_head;
3335 unsigned char *p_run = vbuf;
3337 memcpy(p_run, p_tp->p_vertex, p_tp->nVert * 2 *
sizeof(
float));
3338 p_tp->p_vertex = (
double *)p_run;
3339 p_run += p_tp->nVert * 2 *
sizeof(float);
3340 p_tp = p_tp->p_next;
3342 ppg->bsingle_alloc =
true;
3343 ppg->single_buffer = vbuf;
3344 ppg->single_buffer_size = total_byte_size;
3345 ppg->data_type = DATA_TYPE_FLOAT;
3347 pPTG->SetPPGHead(ppg);
3348 pPTG->SetnVertexMax(nvert_max);
3355bool Osenc::CreateCOVRTables(S57Reader *poReader,
3356 S57ClassRegistrar *poRegistrar) {
3361 float LatMax, LatMin, LonMax, LonMin;
3367 m_pCOVRTablePoints = NULL;
3368 m_pCOVRTable = NULL;
3371 MyFloatPtrArray *pAuxPtrArray =
new MyFloatPtrArray;
3372 std::vector<int> auxCntArray, noCovrCntArray;
3374 MyFloatPtrArray *pNoCovrPtrArray =
new MyFloatPtrArray;
3377 pFeat = GetChartFirstM_COVR(catcov, poReader, poRegistrar);
3382 OGRPolygon *poly = (OGRPolygon *)(pFeat->GetGeometryRef());
3383 OGRLinearRing *xring = poly->getExteriorRing();
3385 int npt = xring->getNumPoints();
3390 pf = (
float *)malloc(2 * npt *
sizeof(
float));
3393 for (
int i = 0; i < npt; i++) {
3395 xring->getPoint(i, &p);
3398 LatMax = fmax(LatMax, p.getY());
3399 LatMin = fmin(LatMin, p.getY());
3400 LonMax = fmax(LonMax, p.getX());
3401 LonMin = fmin(LonMin, p.getX());
3411 pAuxPtrArray->Add(pf);
3412 auxCntArray.push_back(npt);
3413 }
else if (catcov == 2) {
3414 pNoCovrPtrArray->Add(pf);
3415 noCovrCntArray.push_back(npt);
3421 pFeat = GetChartNextM_COVR(catcov, poReader);
3426 m_nCOVREntries = auxCntArray.size();
3431 if (m_nCOVREntries == 1) {
3432 m_pCOVRTablePoints = (
int *)malloc(
sizeof(
int));
3433 *m_pCOVRTablePoints = auxCntArray[0];
3434 m_pCOVRTable = (
float **)malloc(
sizeof(
float *));
3435 *m_pCOVRTable = (
float *)malloc(auxCntArray[0] * 2 *
sizeof(
float));
3436 memcpy(*m_pCOVRTable, pAuxPtrArray->Item(0),
3437 auxCntArray[0] * 2 *
sizeof(
float));
3440 else if (m_nCOVREntries > 1) {
3442 m_pCOVRTablePoints = (
int *)malloc(m_nCOVREntries *
sizeof(
int));
3443 m_pCOVRTable = (
float **)malloc(m_nCOVREntries *
sizeof(
float *));
3445 for (
unsigned int j = 0; j < (
unsigned int)m_nCOVREntries; j++) {
3446 m_pCOVRTablePoints[j] = auxCntArray[j];
3447 m_pCOVRTable[j] = (
float *)malloc(auxCntArray[j] * 2 *
sizeof(
float));
3448 memcpy(m_pCOVRTable[j], pAuxPtrArray->Item(j),
3449 auxCntArray[j] * 2 *
sizeof(
float));
3455 wxString msg(
" ENC contains no useable M_COVR, CATCOV=1 features: ");
3456 msg.Append(m_FullPath000);
3461 m_nNoCOVREntries = noCovrCntArray.size();
3463 if (m_nNoCOVREntries) {
3465 m_pNoCOVRTablePoints = (
int *)malloc(m_nNoCOVREntries *
sizeof(
int));
3466 m_pNoCOVRTable = (
float **)malloc(m_nNoCOVREntries *
sizeof(
float *));
3468 for (
unsigned int j = 0; j < (
unsigned int)m_nNoCOVREntries; j++) {
3469 int npoints = noCovrCntArray[j];
3470 m_pNoCOVRTablePoints[j] = npoints;
3471 m_pNoCOVRTable[j] = (
float *)malloc(npoints * 2 *
sizeof(
float));
3472 memcpy(m_pNoCOVRTable[j], pNoCovrPtrArray->Item(j),
3473 npoints * 2 *
sizeof(
float));
3476 m_pNoCOVRTablePoints = NULL;
3477 m_pNoCOVRTable = NULL;
3480 for (
unsigned int j = 0; j < (
unsigned int)m_nNoCOVREntries; j++)
3481 free(pNoCovrPtrArray->Item(j));
3482 for (
unsigned int j = 0; j < (
unsigned int)m_nCOVREntries; j++)
3483 free(pAuxPtrArray->Item(j));
3485 delete pAuxPtrArray;
3486 delete pNoCovrPtrArray;
3488 if (0 == m_nCOVREntries) {
3489 wxString msg(
" ENC contains no M_COVR features: ");
3490 msg.Append(m_FullPath000);
3493 msg =
" Calculating Chart Extents as fallback.";
3498 if (poReader->GetExtent(&Env,
true) == OGRERR_NONE) {
3505 m_pCOVRTablePoints = (
int *)malloc(
sizeof(
int));
3506 *m_pCOVRTablePoints = 4;
3507 m_pCOVRTable = (
float **)malloc(
sizeof(
float *));
3508 float *pf = (
float *)malloc(2 * 4 *
sizeof(
float));
3525 wxString msg(
" Cannot calculate Extents for ENC: ");
3526 msg.Append(m_FullPath000);
3534 m_extent.NLAT = LatMax;
3535 m_extent.SLAT = LatMin;
3536 m_extent.ELON = LonMax;
3537 m_extent.WLON = LonMin;
3542OGRFeature *Osenc::GetChartFirstM_COVR(
int &catcov, S57Reader *pENCReader,
3543 S57ClassRegistrar *poRegistrar) {
3544 OGRFeature *rv = NULL;
3546 if ((NULL != pENCReader) && (NULL != poRegistrar)) {
3548 poRegistrar->SelectClass(
"M_COVR");
3554 bool bFound =
false;
3555 OGRFeature *pobjectDef = pENCReader->ReadNextFeature();
3558 OGRFeatureDefn *poDefn = pobjectDef->GetDefnRef();
3559 if (poDefn && (poDefn->GetOBJL() == 302 )) {
3561 catcov = pobjectDef->GetFieldAsInteger(
"CATCOV");
3569 pobjectDef = pENCReader->ReadNextFeature();
3576OGRFeature *Osenc::GetChartNextM_COVR(
int &catcov, S57Reader *pENCReader) {
3580 bool bFound =
false;
3581 OGRFeature *pobjectDef = pENCReader->ReadNextFeature();
3585 OGRFeatureDefn *poDefn = pobjectDef->GetDefnRef();
3586 if (poDefn && (poDefn->GetOBJL() == 302)) {
3588 catcov = pobjectDef->GetFieldAsInteger(
"CATCOV");
3596 pobjectDef = pENCReader->ReadNextFeature();
3603int Osenc::GetBaseFileInfo(
const wxString &FullPath000,
3604 const wxString &SENCFileName) {
3605 wxFileName SENCfile = wxFileName(SENCFileName);
3609 if (!GetBaseFileAttr(FullPath000)) {
3610 return ERROR_BASEFILE_ATTRIBUTES;
3613 OGRS57DataSource oS57DS;
3614 oS57DS.SetS57Registrar(m_poRegistrar);
3616 bool b_current_debug = g_bGDAL_Debug;
3617 g_bGDAL_Debug =
false;
3621 if (ingestCell(&oS57DS, FullPath000, SENCfile.GetPath())) {
3622 errorMessage =
"Error ingesting: " + FullPath000;
3623 return ERROR_INGESTING000;
3626 S57Reader *poReader = oS57DS.GetModule(0);
3628 CalculateExtent(poReader, m_poRegistrar);
3630 g_bGDAL_Debug = b_current_debug;
3634 return SENC_NO_ERROR;
3637bool Osenc::CalculateExtent(S57Reader *poReader,
3638 S57ClassRegistrar *poRegistrar) {
3643 float LatMax, LatMin, LonMax, LonMin;
3649 m_pCOVRTablePoints = NULL;
3650 m_pCOVRTable = NULL;
3660 pFeat = GetChartFirstM_COVR(catcov, poReader, poRegistrar);
3665 OGRPolygon *poly = (OGRPolygon *)(pFeat->GetGeometryRef());
3666 OGRLinearRing *xring = poly->getExteriorRing();
3668 int npt = xring->getNumPoints();
3671 for (
int i = 0; i < npt; i++) {
3673 xring->getPoint(i, &p);
3676 LatMax = fmax(LatMax, p.getY());
3677 LatMin = fmin(LatMin, p.getY());
3678 LonMax = fmax(LonMax, p.getX());
3679 LonMin = fmin(LonMin, p.getX());
3685 pFeat = GetChartNextM_COVR(catcov, poReader);
3689 m_extent.NLAT = LatMax;
3690 m_extent.SLAT = LatMin;
3691 m_extent.ELON = LonMax;
3692 m_extent.WLON = LonMin;
3697void Osenc::InitializePersistentBuffer(
void) {
3698 pBuffer = (
unsigned char *)malloc(1024);
3702unsigned char *Osenc::getBuffer(
size_t length) {
3703 if (length > bufferSize) {
3704 pBuffer = (
unsigned char *)realloc(pBuffer, length * 2);
3705 bufferSize = length * 2;
wxFont * GetOCPNScaledFont(wxString item, int default_size)
Retrieves a font from FontMgr, optionally scaled for physical readability.
General purpose GUI support.