diff --git a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx index 2aa039e96d30162f23bbee479b1e8898b8438297..2562d104176782cf37538efde4cededc2de8295c 100644 --- a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx +++ b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx @@ -440,11 +440,21 @@ bool otb::ogr::DataSource::IsLayerModifiable(size_t i) const bool otb::ogr::DataSource::IsLayerModifiable(std::string const& layername) const { assert(m_DataSource && "Datasource not initialized"); - const size_t id = GetLayerID(layername); - return IsLayerModifiable(id); + switch(m_OpenMode) + { + case Modes::Read: + return false; + case Modes::Update_LayerCreateOnly: + { + const int id = this->GetLayerIDUnchecked(layername); + return id >= m_FirstModifiableLayerID; + } + default: + return true; + } } -size_t otb::ogr::DataSource::GetLayerID(std::string const& name) const +int otb::ogr::DataSource::GetLayerIDUnchecked(std::string const& name) const { assert(m_DataSource && "Datasource not initialized"); for (int i = 0, N = GetLayersCount(); i < N; i++) @@ -457,10 +467,18 @@ size_t otb::ogr::DataSource::GetLayerID(std::string const& name) const return i; } } + return -1; +} - itkExceptionMacro( << "Cannot fetch any layer named <" << name - << "> in the OGRDataSource <" << m_DataSource->GetName() << ">: " - << CPLGetLastErrorMsg()); +size_t otb::ogr::DataSource::GetLayerID(std::string const& name) const +{ + int const id = GetLayerIDUnchecked(name); + if (id < 0) + { + itkExceptionMacro( << "Cannot fetch any layer named <" << name + << "> in the OGRDataSource <" << m_DataSource->GetName() << ">: " + << CPLGetLastErrorMsg()); + } return 0; // keep compiler happy } diff --git a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h index 832348cf47c2307a54abe3e73f3bbe1b34edc071..6b5ce9b82b8d273f580fd455f36927724468efcc 100644 --- a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h +++ b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h @@ -520,10 +520,10 @@ private: OGRLayer* GetLayerUnchecked(size_t i) const; bool IsLayerModifiable(size_t i) const; - bool IsLayerModifiable(std::string const& name) const; size_t GetLayerID(std::string const& name) const; + int GetLayerIDUnchecked(std::string const& name) const; private: OGRDataSource *m_DataSource;