33#include "gdal/cpl_conv.h"
34#include "gdal/cpl_string.h"
42OGRS57DataSource::OGRS57DataSource()
78OGRS57DataSource::~OGRS57DataSource()
83 for (i = 0; i < nLayers; i++)
delete papoLayers[i];
87 for (i = 0; i < nModules; i++) {
88 if (papoModules[i]) papoModules[i]->Close();
89 delete papoModules[i];
95 CSLDestroy(papszOptions);
112void OGRS57DataSource::SetOptionList(
char **papszNewOptions)
115 CSLDestroy(papszOptions);
116 papszOptions = CSLDuplicate(papszNewOptions);
123const char *OGRS57DataSource::GetOption(
const char *pszOption)
126 return CSLFetchNameValue(papszOptions, pszOption);
133int OGRS57DataSource::TestCapability(
const char *)
142int OGRS57DataSource::Open(
const char *pszFilename,
int bTestOpen,
143 CallBackFunction pcallback)
147 int error_return = 0;
149 pszName = CPLStrdup(pszFilename);
159 fp = VSIFOpen(pszFilename,
"rb");
160 if (fp == NULL)
return BAD_FILE;
162 if (VSIFRead(pachLeader, 1, 10, fp) != 10 ||
163 (pachLeader[5] !=
'1' && pachLeader[5] !=
'2' &&
164 pachLeader[5] !=
'3') ||
165 pachLeader[6] !=
'L' ||
166 (pachLeader[8] !=
'1' && pachLeader[8] !=
' ')) {
177 char **papszReaderOptions = NULL;
183 CSLSetNameValue(papszReaderOptions, S57O_LNAM_REFS,
"ON");
184 if (GetOption(S57O_UPDATES) != NULL)
185 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
186 GetOption(S57O_UPDATES));
188 if (GetOption(S57O_SPLIT_MULTIPOINT) != NULL)
190 CSLSetNameValue(papszReaderOptions, S57O_SPLIT_MULTIPOINT,
191 GetOption(S57O_SPLIT_MULTIPOINT));
193 if (GetOption(S57O_ADD_SOUNDG_DEPTH) != NULL)
195 CSLSetNameValue(papszReaderOptions, S57O_ADD_SOUNDG_DEPTH,
196 GetOption(S57O_ADD_SOUNDG_DEPTH));
198 if (GetOption(S57O_PRESERVE_EMPTY_NUMBERS) != NULL)
200 CSLSetNameValue(papszReaderOptions, S57O_PRESERVE_EMPTY_NUMBERS,
201 GetOption(S57O_PRESERVE_EMPTY_NUMBERS));
203 if (GetOption(S57O_RETURN_PRIMITIVES) != NULL)
205 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
206 GetOption(S57O_RETURN_PRIMITIVES));
208 if (GetOption(S57O_RETURN_LINKAGES) != NULL)
210 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
211 GetOption(S57O_RETURN_LINKAGES));
213 poModule->SetOptions(papszReaderOptions);
214 CSLDestroy(papszReaderOptions);
222 if (!poModule->Open(bTestOpen)) {
229 papoModules = (
S57Reader **)CPLMalloc(
sizeof(
void *));
230 papoModules[0] = poModule;
235 if (poRegistrar == NULL) {
238 if (!poRegistrar->LoadInfo(NULL, FALSE)) {
248 if (GetOption(S57O_RETURN_PRIMITIVES) != NULL) {
249 OGRFeatureDefn *poDefn;
251 poDefn = S57GenerateVectorPrimitiveFeatureDefn(RCNM_VI,
252 poModule->GetOptionFlags());
255 poDefn = S57GenerateVectorPrimitiveFeatureDefn(RCNM_VC,
256 poModule->GetOptionFlags());
259 poDefn = S57GenerateVectorPrimitiveFeatureDefn(RCNM_VE,
260 poModule->GetOptionFlags());
263 poDefn = S57GenerateVectorPrimitiveFeatureDefn(RCNM_VF,
264 poModule->GetOptionFlags());
300 OGRFeatureDefn *poDefn;
302 int iClass, bGeneric = FALSE;
304 for (iModule = 0; iModule < nModules; iModule++) {
305 papoModules[iModule]->SetClassBased(poRegistrar);
308 for (iModule = 0; iModule < nModules; iModule++) {
309 int ingest_error = papoModules[iModule]->Ingest(pcallback);
310 if (ingest_error) error_return = ingest_error;
313 panClassCount = (
int *)CPLCalloc(
sizeof(
int), MAX_CLASSES);
315 for (iModule = 0; iModule < nModules; iModule++)
316 papoModules[iModule]->CollectClassList(panClassCount, MAX_CLASSES);
318 for (iClass = 0; iClass < MAX_CLASSES; iClass++) {
320 if (!(*pcallback)()) {
326 if (panClassCount[iClass] > 0) {
327 poDefn = S57GenerateObjectClassDefn(poRegistrar, iClass,
328 poModule->GetOptionFlags());
331 AddLayer(
new OGRS57Layer(
this, poDefn, panClassCount[iClass]));
334 CPLDebug(
"S57",
"Unable to find definition for OBJL=%d\n", iClass);
346 CPLFree(panClassCount);
352 for (iModule = 0; iModule < nModules; iModule++) {
353 for (
int iLayer = 0; iLayer < nLayers; iLayer++) {
354 papoModules[iModule]->AddFeatureDefn(papoLayers[iLayer]->GetLayerDefn());
365int OGRS57DataSource::OpenMin(
const char *pszFilename,
int bTestOpen)
368 pszName = CPLStrdup(pszFilename);
378 fp = VSIFOpen(pszFilename,
"rb");
379 if (fp == NULL)
return FALSE;
381 if (VSIFRead(pachLeader, 1, 10, fp) != 10 ||
382 (pachLeader[5] !=
'1' && pachLeader[5] !=
'2' &&
383 pachLeader[5] !=
'3') ||
384 pachLeader[6] !=
'L' ||
385 (pachLeader[8] !=
'1' && pachLeader[8] !=
' ')) {
396 char **papszReaderOptions = NULL;
402 CSLSetNameValue(papszReaderOptions, S57O_LNAM_REFS,
"ON");
403 if (GetOption(S57O_UPDATES) != NULL)
404 papszReaderOptions = CSLSetNameValue(papszReaderOptions, S57O_UPDATES,
405 GetOption(S57O_UPDATES));
407 if (GetOption(S57O_SPLIT_MULTIPOINT) != NULL)
409 CSLSetNameValue(papszReaderOptions, S57O_SPLIT_MULTIPOINT,
410 GetOption(S57O_SPLIT_MULTIPOINT));
412 if (GetOption(S57O_ADD_SOUNDG_DEPTH) != NULL)
414 CSLSetNameValue(papszReaderOptions, S57O_ADD_SOUNDG_DEPTH,
415 GetOption(S57O_ADD_SOUNDG_DEPTH));
417 if (GetOption(S57O_PRESERVE_EMPTY_NUMBERS) != NULL)
419 CSLSetNameValue(papszReaderOptions, S57O_PRESERVE_EMPTY_NUMBERS,
420 GetOption(S57O_PRESERVE_EMPTY_NUMBERS));
422 if (GetOption(S57O_RETURN_PRIMITIVES) != NULL)
424 CSLSetNameValue(papszReaderOptions, S57O_RETURN_PRIMITIVES,
425 GetOption(S57O_RETURN_PRIMITIVES));
427 if (GetOption(S57O_RETURN_LINKAGES) != NULL)
429 CSLSetNameValue(papszReaderOptions, S57O_RETURN_LINKAGES,
430 GetOption(S57O_RETURN_LINKAGES));
432 poModule->SetOptions(papszReaderOptions);
433 CSLDestroy(papszReaderOptions);
441 if (!poModule->Open(bTestOpen)) {
448 papoModules = (
S57Reader **)CPLMalloc(
sizeof(
void *));
449 papoModules[0] = poModule;
587OGRLayer *OGRS57DataSource::GetLayer(
int iLayer)
590 if (iLayer < 0 || iLayer >= nLayers)
593 return papoLayers[iLayer];
600void OGRS57DataSource::AddLayer(
OGRS57Layer *poNewLayer)
604 (
OGRS57Layer **)CPLRealloc(papoLayers,
sizeof(
void *) * ++nLayers);
606 papoLayers[nLayers - 1] = poNewLayer;
613S57Reader *OGRS57DataSource::GetModule(
int i)
616 if (i < 0 || i >= nModules)
619 return papoModules[i];
626OGRErr OGRS57DataSource::GetDSExtent(OGREnvelope *psExtent,
int bForce)
633 *psExtent = oExtents;
637 if (nModules == 0)
return OGRERR_FAILURE;
642 for (
int iModule = 0; iModule < nModules; iModule++) {
643 OGREnvelope oModuleEnvelope;
646 eErr = papoModules[iModule]->GetExtent(&oModuleEnvelope, bForce);
647 if (eErr != OGRERR_NONE)
return eErr;
650 oExtents = oModuleEnvelope;
652 oExtents.MinX = MIN(oExtents.MinX, oModuleEnvelope.MinX);
653 oExtents.MaxX = MAX(oExtents.MaxX, oModuleEnvelope.MaxX);
654 oExtents.MinY = MIN(oExtents.MinY, oModuleEnvelope.MinY);
655 oExtents.MaxX = MAX(oExtents.MaxY, oModuleEnvelope.MaxY);
659 *psExtent = oExtents;