diff --git a/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.h b/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.h index 03fa62c64affa2493eee88df6621288eae96d9ae..a6fd4d8b90b1c17b591cce8d3bfacb08529b28c8 100644 --- a/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.h +++ b/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.h @@ -32,8 +32,8 @@ #include "itkObjectFactory.h" // that should have been included by itkMacro.h #include "otbOGRLayerWrapper.h" +#include "otbOGRVersionProxy.h" -class GDALDataset; class OGRLayer; class OGRSpatialReference; class OGRGeometry; @@ -147,7 +147,7 @@ public: * \note No condition is assumed on the non-nullity of \c source. * \see \c DataSource(GDALDataset *) */ - static Pointer New(GDALDataset * sourcemode, Modes::type mode = Modes::Read); + static Pointer New(OGRVersionProxy::GDALDatasetType * sourcemode, Modes::type mode = Modes::Read); //@} /**\name Projection Reference property */ @@ -284,7 +284,7 @@ public: * \throw None * \post Assumes ownership of the \c source. */ - void Reset(GDALDataset * source); + void Reset(OGRVersionProxy::GDALDatasetType * source); /**\name Layers modification */ //@{ @@ -480,7 +480,7 @@ public: * \warning You must under no circonstance try to delete the \c GDALDataset * obtained this way. */ - GDALDataset & ogr(); + OGRVersionProxy::GDALDatasetType & ogr(); protected: /** Default constructor. @@ -496,7 +496,7 @@ protected: /** Init constructor. * \post The newly constructed object owns the \c source parameter. */ - DataSource(GDALDataset * source, Modes::type mode); + DataSource(OGRVersionProxy::GDALDatasetType * source, Modes::type mode); /** Destructor. * \post The \c GDALDataset owned is released (if not null). */ @@ -529,12 +529,8 @@ private: size_t GetLayerID(std::string const& name) const; int GetLayerIDUnchecked(std::string const& name) const; - /** Return a string containing space separated items retrieved by - the GetFileList() method in GDALDataset (for error reporting) */ - std::string GetFileListAsString() const; - private: - GDALDataset *m_DataSource; + OGRVersionProxy::GDALDatasetType *m_DataSource; Modes::type m_OpenMode; int m_FirstModifiableLayerID; }; // end class DataSource diff --git a/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.txx b/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.txx index b14e3ac4f01e8e212713c94552cc271987308575..7c20ada18a61f062d00e804134689361da33d0fa 100644 --- a/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.txx +++ b/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.txx @@ -38,7 +38,7 @@ // to the compilation mode of the client code. inline -GDALDataset & otb::ogr::DataSource::ogr() +otb::OGRVersionProxy::GDALDatasetType & otb::ogr::DataSource::ogr() { assert(m_DataSource && "GDALDataset not initialized"); return *m_DataSource; diff --git a/Modules/Adapters/GdalAdapters/include/otbOGRLayerWrapper.h b/Modules/Adapters/GdalAdapters/include/otbOGRLayerWrapper.h index da7ed61bd4ef1f89a96a8a45583182aedc71db9a..2281c75c501d1534c97bc1aac11dcb8effbb9acd 100644 --- a/Modules/Adapters/GdalAdapters/include/otbOGRLayerWrapper.h +++ b/Modules/Adapters/GdalAdapters/include/otbOGRLayerWrapper.h @@ -24,11 +24,11 @@ PURPOSE. See the above copyright notices for more information. #include <boost/utility/enable_if.hpp> // #include "itkIndent.h", included from field #include "otbOGRFeatureWrapper.h" +#include "otbOGRVersionProxy.h" // #include "ogr_core.h" // OGRwkbGeometryType, included from feature -> field // Forward declarations class OGRLayer; -class GDALDataset; class OGRGeometry; class OGRFeatureDefn; @@ -105,7 +105,7 @@ public: * OGRDataSource::ExecuteSQL(). It's actually the constructor called by \c * DataSource::ExecuteSQL(). */ - Layer(OGRLayer* layer, GDALDataset& sourceInChargeOfLifeTime, bool modifiable); + Layer(OGRLayer* layer, otb::OGRVersionProxy::GDALDatasetType& sourceInChargeOfLifeTime, bool modifiable); //@} /**\name Features collection */ diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx index 565abffff3128cba87cd440168af5a1cc696873b..86f6eb5d9bb22f76ac8d575849d9120760eccc87 100644 --- a/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx +++ b/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx @@ -34,7 +34,6 @@ #include "otbOGRDriversInit.h" #include "otbSystem.h" // OGR includes -#include "ogrsf_frmts.h" /*===========================================================================*/ /*=======================[ construction/destruction ]========================*/ @@ -45,15 +44,13 @@ bool otb::ogr::DataSource::Clear() return true; } -void otb::ogr::DataSource::Reset(GDALDataset * source) +void otb::ogr::DataSource::Reset(otb::OGRVersionProxy::GDALDatasetType * source) { if (m_DataSource) { // OGR makes a pointless check for non-nullity in // GDALDataset::DestroyDataSource (pointless because "delete 0" is // perfectly valid -> it's a no-op) - // JML: GDALClose is said to be better for windows than calling - // delete operator - GDALClose(m_DataSource); // void, noexcept + OGRVersionProxy::Close(m_DataSource); // void, noexcept } m_DataSource = source; } @@ -121,15 +118,15 @@ otb::ogr::DataSource::DataSource() { Drivers::Init(); - GDALDriver * d = GetGDALDriverManager()->GetDriverByName("Memory"); + OGRVersionProxy::GDALDriverType * d = OGRVersionProxy::GetDriverByName("Memory"); assert(d && "OGR Memory driver not found"); - m_DataSource = d->Create("in-memory",0,0,0,GDT_Unknown,NULL); + m_DataSource = OGRVersionProxy::Create(d,"in-memory"); if (!m_DataSource) { itkExceptionMacro(<< "Failed to create OGRMemDataSource: " << CPLGetLastErrorMsg()); } } -otb::ogr::DataSource::DataSource(GDALDataset * source, Modes::type mode) +otb::ogr::DataSource::DataSource(otb::OGRVersionProxy::GDALDatasetType * source, Modes::type mode) : m_DataSource(source), m_OpenMode(mode), m_FirstModifiableLayerID(0) @@ -141,7 +138,7 @@ otb::ogr::DataSource::Pointer otb::ogr::DataSource::OpenDataSource(std::string c { bool update = (mode != Modes::Read); - GDALDataset * source = (GDALDataset *)GDALOpenEx(datasourceName.c_str(), (update? GDAL_OF_UPDATE : GDAL_OF_READONLY) | GDAL_OF_VECTOR,NULL,NULL,NULL); + OGRVersionProxy::GDALDatasetType * source = OGRVersionProxy::Open(datasourceName.c_str(),!update); if (!source) { // In read mode, this is a failure @@ -160,14 +157,14 @@ otb::ogr::DataSource::Pointer otb::ogr::DataSource::OpenDataSource(std::string c <<datasourceName<<">."); } - GDALDriver * d = GetGDALDriverManager()->GetDriverByName(driverName); + OGRVersionProxy::GDALDriverType * d = OGRVersionProxy::GetDriverByName(driverName); if(!d) { itkGenericExceptionMacro(<<"Could not create OGR driver "<<driverName<<", check your OGR configuration for available drivers."); } - source = d->Create(datasourceName.c_str(),0,0,0,GDT_Unknown,NULL); + source = OGRVersionProxy::Create(d,datasourceName.c_str()); if (!source) { itkGenericExceptionMacro(<< "Failed to create GDALDataset <"<<datasourceName <<"> (driver name: <" << driverName<<">: " << CPLGetLastErrorMsg()); @@ -179,30 +176,30 @@ otb::ogr::DataSource::Pointer otb::ogr::DataSource::OpenDataSource(std::string c void DeleteDataSource(std::string const& datasourceName) { // Attempt to delete the datasource if it already exists - GDALDataset * poDS = (GDALDataset *)GDALOpenEx(datasourceName.c_str(), GDAL_OF_UPDATE | GDAL_OF_VECTOR,NULL,NULL,NULL); + otb::OGRVersionProxy::GDALDatasetType * poDS = otb::OGRVersionProxy::Open(datasourceName.c_str(),false); if (poDS != NULL) { - GDALDriver * ogrDriver = poDS->GetDriver(); + otb::OGRVersionProxy::GDALDriverType * ogrDriver = poDS->GetDriver(); //Erase the data if possible - if (poDS->TestCapability(ODrCDeleteDataSource)) + if (otb::OGRVersionProxy::TestCapability(ogrDriver,poDS,ODrCDeleteDataSource)) { //Delete datasource - OGRErr ret = ogrDriver->Delete(datasourceName.c_str()); - if (ret != OGRERR_NONE) + bool ret = otb::OGRVersionProxy::Delete(ogrDriver,datasourceName.c_str()); + if (!ret) { - GDALClose(poDS); + otb::OGRVersionProxy::Close(poDS); itkGenericExceptionMacro(<< "Deletion of data source " << datasourceName << " failed: " << CPLGetLastErrorMsg()); } } else { - GDALClose(poDS); + otb::OGRVersionProxy::Close(poDS); itkGenericExceptionMacro(<< "Cannot delete data source " << datasourceName); } - GDALClose(poDS); + otb::OGRVersionProxy::Close(poDS); } // if (poDS != NULL) } @@ -226,7 +223,7 @@ otb::ogr::DataSource::New(std::string const& datasourceName, Modes::type mode) /*static*/ otb::ogr::DataSource::Pointer -otb::ogr::DataSource::New(GDALDataset * source, Modes::type mode) +otb::ogr::DataSource::New(otb::OGRVersionProxy::GDALDatasetType * source, Modes::type mode) { Pointer res = new DataSource(source, mode); res->UnRegister(); @@ -297,7 +294,7 @@ otb::ogr::Layer otb::ogr::DataSource::CreateLayer( if (!ol) { itkGenericExceptionMacro(<< "Failed to create the layer <"<<name - << "> in the GDALDataset file <" << GetFileListAsString() + << "> in the GDALDataset file <" << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) <<">: " << CPLGetLastErrorMsg()); } @@ -320,7 +317,7 @@ otb::ogr::Layer otb::ogr::DataSource::CreateLayer( if (!ol) { itkGenericExceptionMacro(<< "Failed to create the layer <"<<name - << "> in the GDALDataset file <" << GetFileListAsString() + << "> in the GDALDataset file <" << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) <<">: " << CPLGetLastErrorMsg()); } @@ -345,7 +342,7 @@ otb::ogr::Layer otb::ogr::DataSource::CreateLayer( { itkGenericExceptionMacro(<< "Failed to create the layer <"<<name - << "> in the GDALDataset file <" << GetFileListAsString() + << "> in the GDALDataset file <" << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) <<">: " << CPLGetLastErrorMsg()); } @@ -389,7 +386,7 @@ otb::ogr::Layer otb::ogr::DataSource::CopyLayer( { itkGenericExceptionMacro(<< "Failed to copy the layer <" << srcLayer.GetName() << "> into the new layer <" <<newName - << "> in the GDALDataset file <" << GetFileListAsString() + << "> in the GDALDataset file <" << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) <<">: " << CPLGetLastErrorMsg()); } const bool modifiable = true; @@ -422,14 +419,14 @@ void otb::ogr::DataSource::DeleteLayer(size_t i) { itkExceptionMacro(<< "Cannot delete " << i << "th layer in the GDALDataset <" - << GetFileListAsString() << "> as it contains only " << nb_layers << "layers."); + << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) << "> as it contains only " << nb_layers << "layers."); } const OGRErr err = m_DataSource->DeleteLayer(int(i)); if (err != OGRERR_NONE) { itkExceptionMacro(<< "Cannot delete " << i << "th layer in the GDALDataset <" - << GetFileListAsString() << ">: " << CPLGetLastErrorMsg()); + << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) << ">: " << CPLGetLastErrorMsg()); } } @@ -486,7 +483,7 @@ size_t otb::ogr::DataSource::GetLayerID(std::string const& name) const if (id < 0) { itkExceptionMacro( << "Cannot fetch any layer named <" << name - << "> in the GDALDataset <" << GetFileListAsString() << ">: " + << "> in the GDALDataset <" << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) << ">: " << CPLGetLastErrorMsg()); } return 0; // keep compiler happy @@ -499,13 +496,13 @@ otb::ogr::Layer otb::ogr::DataSource::GetLayerChecked(size_t i) if (int(i) >= nb_layers) { itkExceptionMacro(<< "Cannot fetch " << i << "th layer in the GDALDataset <" - << GetFileListAsString() << "> as it contains only " << nb_layers << "layers."); + << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) << "> as it contains only " << nb_layers << "layers."); } OGRLayer * layer_ptr = m_DataSource->GetLayer(int(i)); if (!layer_ptr) { itkExceptionMacro( << "Unexpected error: cannot fetch " << i << "th layer in the GDALDataset <" - << GetFileListAsString() << ">: " << CPLGetLastErrorMsg()); + << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) << ">: " << CPLGetLastErrorMsg()); } return otb::ogr::Layer(layer_ptr, IsLayerModifiable(i)); } @@ -531,7 +528,7 @@ otb::ogr::Layer otb::ogr::DataSource::GetLayerChecked(std::string const& name) if (!layer_ptr) { itkExceptionMacro( << "Cannot fetch any layer named <" << name - << "> in the GDALDataset <" << GetFileListAsString() << ">: " + << "> in the GDALDataset <" << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) << ">: " << CPLGetLastErrorMsg()); } return otb::ogr::Layer(layer_ptr, IsLayerModifiable(name)); @@ -556,7 +553,7 @@ otb::ogr::Layer otb::ogr::DataSource::ExecuteSQL( { #if defined(PREFER_EXCEPTION) itkExceptionMacro( << "Unexpected error: cannot execute the SQL request <" << statement - << "> in the GDALDataset <" << GetFileListAsString() << ">: " << CPLGetLastErrorMsg()); + << "> in the GDALDataset <" << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) << ">: " << CPLGetLastErrorMsg()); #else // Cannot use the deleter made for result sets obtained from // GDALDataset::ExecuteSQL because it checks for non-nullity.... @@ -699,30 +696,17 @@ void otb::ogr::DataSource::PrintSelf( bool otb::ogr::DataSource::HasCapability(std::string const& capabilityName) const { assert(m_DataSource && "Datasource not initialized"); - return m_DataSource->TestCapability(capabilityName.c_str()); + return otb::OGRVersionProxy::TestCapability(m_DataSource->GetDriver(),m_DataSource,capabilityName.c_str()); } void otb::ogr::DataSource::SyncToDisk() { assert(m_DataSource && "Datasource not initialized"); - m_DataSource->FlushCache(); -} - -std::string otb::ogr::DataSource::GetFileListAsString() const -{ - char ** files = m_DataSource->GetFileList(); + bool ret = otb::OGRVersionProxy::SyncToDisk(m_DataSource); - std::string files_str=""; - - if(files) + if(!ret) { - unsigned int i = 0; - while(files[i]!=NULL) - { - files_str+=std::string(files[i])+" "; - ++i; - } - CSLDestroy(files); + itkExceptionMacro( << "Cannot flush the pending of the OGRDataSource <" + << otb::OGRVersionProxy::GetDatasetDescription(m_DataSource) << ">: " << CPLGetLastErrorMsg()); } - return files_str; } diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRLayerWrapper.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRLayerWrapper.cxx index 777bfa518e6f6b00a7433ac2df31cfd2603bc231..e917acd9f961da50f5c0bd2a1b11f7e0ec081750 100644 --- a/Modules/Adapters/GdalAdapters/src/otbOGRLayerWrapper.cxx +++ b/Modules/Adapters/GdalAdapters/src/otbOGRLayerWrapper.cxx @@ -60,8 +60,8 @@ otb::ogr::Layer::Layer(OGRLayer* layer, bool modifiable) { } -otb::ogr::Layer::Layer(OGRLayer* layer, GDALDataset& sourceInChargeOfLifeTime, bool modifiable) -: m_Layer(layer, boost::bind(&OGRDataSource::ReleaseResultSet, boost::ref(sourceInChargeOfLifeTime), _1)) +otb::ogr::Layer::Layer(OGRLayer* layer, otb::OGRVersionProxy::GDALDatasetType& sourceInChargeOfLifeTime, bool modifiable) +: m_Layer(layer, boost::bind(&otb::OGRVersionProxy::GDALDatasetType::ReleaseResultSet, boost::ref(sourceInChargeOfLifeTime), _1)) , m_Modifiable(modifiable) { assert(layer && "A null OGRlayer cannot belong to an OGRDataSource" );