diff --git a/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.h b/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.h index 0a37948fab58167d9426007187279e5468497bd8..ed11c526e49b076ed576cb0e095a5b226c561a8a 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 OGRDataSource; class OGRLayer; class OGRSpatialReference; class OGRGeometry; @@ -46,7 +46,7 @@ namespace otb { namespace ogr { * * This class is meant to supercede \c otb::VectorData class. It provides * an encapsulation of OGR classes. In that particular case, it's an - * encapsulation of \c OGRDataSource. + * encapsulation of \c GDALDataset. * * \note Not meant to be inherited. * \note This class has an entity semantics: \em non-copyable, nor \em @@ -77,10 +77,10 @@ public: //@{ /** Default builder. * This builder function creates a new \c DataSource with its default - * constructor. The actual \c OGRDataSource is using the <em>in-memory</em> + * constructor. The actual \c GDALDataset is using the <em>in-memory</em> * \c OGRSFDriver: \c OGRMemDriver. * - * \throw itk::ExceptionObject if the inner \c OGRDataSource cannot be + * \throw itk::ExceptionObject if the inner \c GDALDataset cannot be * opened. * * \note \c OGRRegisterAll() is implicitly called on construction. @@ -130,24 +130,24 @@ public: * \param[in] datasourcename OGR identifier of the data source * \param[in] mode opening mode (read or read-write) * \return a newly created \c DataSource. - * \throw itk::ExceptionObject if the inner \c OGRDataSource cannot be + * \throw itk::ExceptionObject if the inner \c GDALDataset cannot be * opened. * \note \c OGRRegisterAll() is implicitly called on construction - * \see \c DataSource(OGRDataSource *) + * \see \c DataSource(GDALDataset *) */ static Pointer New(std::string const& datasourcename, Modes::type mode = Modes::Read); /** - * Builder from a built \c OGRDataSource. - * \param[in,out] source \c OGRDataSource already constructed. + * Builder from a built \c GDALDataset. + * \param[in,out] source \c GDALDataset already constructed. * \return a newly created \c DataSource that assumes ownership of \c * source. * \throw Nothing * \note \c OGRRegisterAll() is supposed to have been called before building * \c source. * \note No condition is assumed on the non-nullity of \c source. - * \see \c DataSource(OGRDataSource *) + * \see \c DataSource(GDALDataset *) */ - static Pointer New(OGRDataSource * sourcemode, Modes::type mode = Modes::Read); + static Pointer New(OGRVersionProxy::GDALDatasetType * sourcemode, Modes::type mode = Modes::Read); //@} /**\name Projection Reference property */ @@ -159,7 +159,7 @@ public: //@} /** Clears the data source. - * \post The \c OGRDataSource owned is destroyed with the dedicated function + * \post The \c GDALDataset owned is destroyed with the dedicated function * from OGR %API. * \post <tt>m_DataSource = 0</tt> */ @@ -274,17 +274,17 @@ public: * * This is a convenience function to setup a second data source with all the * meta information of another data source and use the same underlying \c - * OGRDataSource. + * GDALDataset. */ virtual void Graft(const itk::DataObject *data); /** * Resets current data source with the one in parameter. - * \param[in,out] source source \c OGRDataSource that this instance will own. + * \param[in,out] source source \c GDALDataset that this instance will own. * \throw None * \post Assumes ownership of the \c source. */ - void Reset(OGRDataSource * source); + void Reset(OGRVersionProxy::GDALDatasetType * source); /**\name Layers modification */ //@{ @@ -309,7 +309,7 @@ public: * with \c CreateLayer(), you must use \c DeleteLayer(). * \note The \c papszOptions parameter may later become a \c * std::vector<std::string>. - * \sa \c OGRDataSource::CreateLayer() + * \sa \c GDALDataset::CreateLayer() */ Layer CreateLayer( std::string const& name, @@ -327,7 +327,7 @@ public: * * \pre The data source must support the delete operation. * \pre The index \c i must be in range [0, GetLayersCount()). - * \sa \c OGRDataSource::DeleteLayer() + * \sa \c GDALDataset::DeleteLayer() */ void DeleteLayer(size_t i); @@ -348,7 +348,7 @@ public: * with \c CreateLayer(), you must use \c DeleteLayer(). * \note The \c papszOptions parameter may later become a \c * std::vector<std::string>. - * \sa \c OGRDataSource::CopyLayer() + * \sa \c GDALDataset::CopyLayer() */ Layer CopyLayer( Layer & srcLayer, @@ -361,7 +361,7 @@ public: */ //@{ /** Returns the number of layers. - * \sa \c OGRDataSource::GetLayersCount() + * \sa \c GDALDataset::GetLayersCount() */ int GetLayersCount() const; @@ -437,8 +437,8 @@ public: * neither this wrapping. * \note The returned \c Layer will be automatically collected on its * destruction; i.e. unlike OGR API, no need to explicitly call \c - * OGRDataSource::ReleaseResultSet(). - * \sa \c OGRDataSource::ExecuteSQL() + * GDALDataset::ReleaseResultSet(). + * \sa \c GDALDataset::ExecuteSQL() */ Layer ExecuteSQL( std::string const& statement, @@ -461,32 +461,32 @@ public: /** Flushes all changes to disk. * \throw itd::ExceptionObject in case the flush operation failed. - * \sa \c OGRDataSource::SyncToDisk() + * \sa \c GDALDataset::SyncToDisk() */ void SyncToDisk(); /** Returns whether a capability is avalaible. * \param[in] capabilityName name of the capability to check. * \throw None - * \sa \c OGRDataSource::TestCapability() + * \sa \c GDALDataset::TestCapability() */ bool HasCapability(std::string const& capabilityName) const; - /** Access to raw \c OGRDataSource. + /** Access to raw \c GDALDataset. * This function provides an abstraction leak in case deeper control on the - * underlying \c OGRDataSource is required. - * \pre The underlying \c OGRDataSource must be valid, i.e. + * underlying \c GDALDataset is required. + * \pre The underlying \c GDALDataset must be valid, i.e. * <tt>m_DataSource != 0</tt>, an assertion is fired otherwise. - * \warning You must under no circonstance try to delete the \c OGRDataSource + * \warning You must under no circonstance try to delete the \c GDALDataset * obtained this way. */ - OGRDataSource & ogr(); + OGRVersionProxy::GDALDatasetType & ogr(); protected: /** Default constructor. - * The actual \c OGRDataSource is using the <em>in-memory</em> \c + * The actual \c GDALDataset is using the <em>in-memory</em> \c * OGRSFDriver: \c OGRMemDriver. - * \throw itk::ExceptionObject if the inner \c OGRDataSource cannot be + * \throw itk::ExceptionObject if the inner \c GDALDataset cannot be * opened. * * \note \c OGRRegisterAll() is implicitly called on construction @@ -496,9 +496,9 @@ protected: /** Init constructor. * \post The newly constructed object owns the \c source parameter. */ - DataSource(OGRDataSource * source, Modes::type mode); + DataSource(OGRVersionProxy::GDALDatasetType * source, Modes::type mode); /** Destructor. - * \post The \c OGRDataSource owned is released (if not null). + * \post The \c GDALDataset owned is released (if not null). */ virtual ~DataSource(); @@ -516,7 +516,7 @@ private: * \pre The layer must available, 0 is returned otherwise. * \throw None * \internal this function is a simple encapsulation of \c - * OGRDataSource::GetLayer(). + * GDALDataset::GetLayer(). */ OGRLayer* GetLayerUnchecked(size_t i); /** @copydoc OGRLayer* otb::ogr::DataSource::GetLayerUnchecked(size_t i) @@ -529,8 +529,11 @@ private: size_t GetLayerID(std::string const& name) const; int GetLayerIDUnchecked(std::string const& name) const; + /** Get a string describing the dataset */ + std::string GetDatasetDescription() const; + private: - OGRDataSource *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 d9f240e07872467c2eb34c996e016326cbe6a740..7c20ada18a61f062d00e804134689361da33d0fa 100644 --- a/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.txx +++ b/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.txx @@ -38,9 +38,9 @@ // to the compilation mode of the client code. inline -OGRDataSource & otb::ogr::DataSource::ogr() +otb::OGRVersionProxy::GDALDatasetType & otb::ogr::DataSource::ogr() { - assert(m_DataSource && "OGRDataSource not initialized"); + 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 007229fc9ac2224b0ee822afe9250c0c86ad1bc7..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 OGRDataSource; class OGRGeometry; class OGRFeatureDefn; @@ -94,7 +94,7 @@ public: /** * Init constructor for layers that need to be released. * \param layer \c OGRLayer owned by the client code. - * \param sourceInChargeOfLifeTime reference to the actual \c OGRDataSource + * \param sourceInChargeOfLifeTime reference to the actual \c GDALDataset * that knows how to release the layer. * \post In this case, \c m_datasource is left null: we suppose (for now, that * the layer won't need access to the datasource meta-information). @@ -105,7 +105,7 @@ public: * OGRDataSource::ExecuteSQL(). It's actually the constructor called by \c * DataSource::ExecuteSQL(). */ - Layer(OGRLayer* layer, OGRDataSource& sourceInChargeOfLifeTime, bool modifiable); + Layer(OGRLayer* layer, otb::OGRVersionProxy::GDALDatasetType& sourceInChargeOfLifeTime, bool modifiable); //@} /**\name Features collection */ diff --git a/Modules/Adapters/GdalAdapters/include/otbOGRVersionProxy.h b/Modules/Adapters/GdalAdapters/include/otbOGRVersionProxy.h new file mode 100644 index 0000000000000000000000000000000000000000..dbd27a850983deb779425664b6b22c0ab2dd5778 --- /dev/null +++ b/Modules/Adapters/GdalAdapters/include/otbOGRVersionProxy.h @@ -0,0 +1,206 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbOGRVersionProxy_h +#define __otbOGRVersionProxy_h + +#include <string> +#include <vector> +#include "otbConfigure.h" + +#ifdef OTB_USE_GDAL_20 +class GDALDataset; +class GDALDriver; +#else +class OGRDataSource; +class OGRSFDriver; +#endif + +namespace otb +{ +/** + * \brief This class serves as a proxy hiding interface changes in gdal 2.0 + * + * This static class serves as a proxy hiding interface changes in OGR + * dataset between gdal 1.x (x>10) and gdal 2.x. It defines a common + * interface that should be used in place of calling directly the + * wrapped gdal methods. + * + * Whenever GDALDataset and GDALDriver have to be used to open a + * vector dataset (or OGRDataSource an OGRSFDriver for gdal 1.x), one + * should use OGRVersionProxy type members GDALDatasetType and + * GDALDriverType. + * + * See static method documentation for details. + */ + +class OGRVersionProxy +{ +public: + #ifdef OTB_USE_GDAL_20 + typedef GDALDataset GDALDatasetType; + typedef GDALDriver GDALDriverType; + #else + typedef OGRDataSource GDALDatasetType; + typedef OGRSFDriver GDALDriverType; +#endif + + /** + * This method opens a file, possibly in read-only mode, and returns + * a dataset. + * + * Calls OGRSFDriverRegistrar::Open for gdal 1.x implementation and GDALopenEx for + * gdal 2.x implementation. + + * \param filename Filename of the file to open + * \param readOnly: If true, dataset is open in read-only mode. + * \return NULL if file could not be open. + */ + static GDALDatasetType * Open(const char * filename, bool readOnly = true); + + /** + * This method closes a dataset. + * + * Calls OGRDataSource::DestroyDataSource for gdal 1.x + * implementation and GDALClose for gdal 2.x implementation. + * + * \param dataset Pointer to the dataset to close. Will not be + * checked for null pointer. + */ + static void Close(GDALDatasetType * dataset); + + /** + * This method creates a new dataset. + * + * Calls OGRSFDriver::CreateDataSource for gdal 1.x implementation + * and GDALDriver::Create with (0,0) raster size for gdal 2.x + * implementation + * + * \param driver Pointer to the driver used for creation. Will not + * be checked for null pointer. + * + * \param name Name of the dataset to create. + * + * \return NULL if dataset could not be created. + */ + static GDALDatasetType * Create(GDALDriverType * driver, const char * name); + + + /** + * This method physically deletes an existing dataset. + * + * Calls OGRDataSource::DeleteDataSource for gdal 1.x implementation + * and GDALDriver::Delete for gdal 2.x implementation. + * + * \param driver Pointer to the driver used for creation. Will not + * be checked for null pointer. + * + * \param name Name of the dataset to destroy. + */ + static bool Delete(GDALDriverType * driver, const char * name); + + /** + * This method returns a pointer to the driver from its name. + * + * Calls OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName() for + * gdal 1.x implementation and + * GetGDALDriverManager()->GetDriverByName() for gdal 2.x + * implementation. + * + * \param name Name of the driver to retrieve + * + * \return NULL if no driver could be retrieved. + */ + static GDALDriverType * GetDriverByName(const char * name); + + /** + * Test driver and dataset for a given capability. + * + * Calls OGRSFDriver::TestCapability for gdal 1.x implementation and + * GDALDataset::TestCapability for gdal 2.x implementation. + * + * \param driver Pointer to the driver to test. Will not + * be checked for null pointer. + * + * \param dataset Pointer to the dataset to test. Will not be + * checked for null pointer. + * + * \param capability Name of the capability to test. + * + * \return True if the capability is supported. + */ + static bool TestCapability(const GDALDriverType * driver, const GDALDatasetType * dataset, const char * capability); + + /** + * Sync dataset to disk. + * + * Calls OGRDataSource::SyncToDisk() for gdal 1.x implementation and + * GDALDataset::FlushCache() for gdal 2.x implementation. + * + * \param dataset Pointer to the dataset to sync. Will not be + * checked for null pointer. + * + * \return True if sync went on without any error. + */ + static bool SyncToDisk(GDALDatasetType * dataset); + + /** + * \return The name of the dataset class behind the implementation + * (OGRDataSource for gdal 1.x and GdalDataset for gdal 2.x) + */ + static std::string GetDatasetClassName(); + + /** + * \return The name of the driver class behind the implementation + * (OGRSFDriver for gdal 1.x and GDALDriver for gdal 2.x) + */ + static std::string GetDriverClassName(); + + /** + * Return the list of files composing the dataset. + * + * Calls OGRDataSource::GetName() and wrap in string vector for gdal + * 1.x implementation, and GDALDataset::GetFileList and wrap in + * string vector for gdal 2.x implementation. + * + * \param dataset Pointer to the dataset to get the file list from. Will not be + * checked for null pointer. + * + * \return A vector of string containing the list of files. + */ + static std::vector<std::string> GetFileListAsStringVector(GDALDatasetType * dataset); + + /** + * Return the list of available drivers. + * + * Calls OGRSFDriverRegistrar::GetRegistrar() for gdal 1.x + * implementation and GetGDALDriverManager() for gdal 2.x + * implementation. + * + * \return A vector of string containing the list of available drivers. + */ + static std::vector<std::string> GetAvailableDriversAsStringVector(); + +private: + OGRVersionProxy(); // purposely not implemented + OGRVersionProxy(const OGRVersionProxy&); // purposely not implemented + void operator=(const OGRVersionProxy&); // purposely not implemented +}; + +} // end namespace otb + +#endif diff --git a/Modules/Adapters/GdalAdapters/src/CMakeLists.txt b/Modules/Adapters/GdalAdapters/src/CMakeLists.txt index 87e83c558e27b3f3dddf9c167eb7d81a46b66a84..a052f01656ce4b0090ff69f13b6f4a06b544c482 100644 --- a/Modules/Adapters/GdalAdapters/src/CMakeLists.txt +++ b/Modules/Adapters/GdalAdapters/src/CMakeLists.txt @@ -10,6 +10,12 @@ set(OTBGdalAdapters_SRC otbOGRDataSourceWrapper.cxx ) +if(OTB_USE_GDAL_20) + set(OTBGdalAdapters_SRC ${OTBGdalAdapters_SRC} otbOGRVersionProxy2x.cxx) +else(Otb_use_gdal_20) + set(OTBGdalAdapters_SRC ${OTBGdalAdapters_SRC} otbOGRVersionProxy1x.cxx) +endif(OTB_USE_GDAL_20) + add_library(OTBGdalAdapters ${OTBGdalAdapters_SRC}) target_link_libraries(OTBGdalAdapters ${OTBBoost_LIBRARIES} diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx index d2be6f3652f70946a75a90cfb77183908f23c247..3856c0610fc5c069bdacfcb3b8427c6f67965a0b 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,13 +44,13 @@ bool otb::ogr::DataSource::Clear() return true; } -void otb::ogr::DataSource::Reset(OGRDataSource * source) +void otb::ogr::DataSource::Reset(otb::OGRVersionProxy::GDALDatasetType * source) { if (m_DataSource) { // OGR makes a pointless check for non-nullity in - // OGRDataSource::DestroyDataSource (pointless because "delete 0" is + // GDALDataset::DestroyDataSource (pointless because "delete 0" is // perfectly valid -> it's a no-op) - OGRDataSource::DestroyDataSource(m_DataSource); // void, noexcept + OGRVersionProxy::Close(m_DataSource); // void, noexcept } m_DataSource = source; } @@ -119,16 +118,15 @@ otb::ogr::DataSource::DataSource() { Drivers::Init(); - OGRSFDriver * d = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("Memory"); + OGRVersionProxy::GDALDriverType * d = OGRVersionProxy::GetDriverByName("Memory"); assert(d && "OGR Memory driver not found"); - m_DataSource = d->CreateDataSource("in-memory"); + m_DataSource = OGRVersionProxy::Create(d,"in-memory"); if (!m_DataSource) { itkExceptionMacro(<< "Failed to create OGRMemDataSource: " << CPLGetLastErrorMsg()); } - m_DataSource->SetDriver(d); } -otb::ogr::DataSource::DataSource(OGRDataSource * source, Modes::type mode) +otb::ogr::DataSource::DataSource(otb::OGRVersionProxy::GDALDatasetType * source, Modes::type mode) : m_DataSource(source), m_OpenMode(mode), m_FirstModifiableLayerID(0) @@ -140,14 +138,14 @@ otb::ogr::DataSource::Pointer otb::ogr::DataSource::OpenDataSource(std::string c { bool update = (mode != Modes::Read); - OGRDataSource * source = OGRSFDriverRegistrar::Open(datasourceName.c_str(), update); + OGRVersionProxy::GDALDatasetType * source = OGRVersionProxy::Open(datasourceName.c_str(),!update); if (!source) { // In read mode, this is a failure // In write mode (Overwrite and Update), create the data source transparently if (mode == Modes::Read) { - itkGenericExceptionMacro(<< "Failed to open OGRDataSource file " + itkGenericExceptionMacro(<< "Failed to open GDALDataset file " << datasourceName<<" : " << CPLGetLastErrorMsg()); } @@ -159,19 +157,18 @@ otb::ogr::DataSource::Pointer otb::ogr::DataSource::OpenDataSource(std::string c <<datasourceName<<">."); } - OGRSFDriver * d = OGRSFDriverRegistrar::GetRegistrar()->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->CreateDataSource(datasourceName.c_str()); + source = OGRVersionProxy::Create(d,datasourceName.c_str()); if (!source) { - itkGenericExceptionMacro(<< "Failed to create OGRDataSource <"<<datasourceName + itkGenericExceptionMacro(<< "Failed to create GDALDataset <"<<datasourceName <<"> (driver name: <" << driverName<<">: " << CPLGetLastErrorMsg()); } - source->SetDriver(d); } return otb::ogr::DataSource::New(source, mode); } @@ -179,27 +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 - OGRDataSource * poDS = OGRSFDriverRegistrar::Open(datasourceName.c_str(), TRUE); + otb::OGRVersionProxy::GDALDatasetType * poDS = otb::OGRVersionProxy::Open(datasourceName.c_str(),false); if (poDS != NULL) { - OGRSFDriver * ogrDriver = poDS->GetDriver(); - OGRDataSource::DestroyDataSource(poDS); + otb::OGRVersionProxy::GDALDriverType * ogrDriver = poDS->GetDriver(); + //Erase the data if possible - if (ogrDriver->TestCapability(ODrCDeleteDataSource)) + if (otb::OGRVersionProxy::TestCapability(ogrDriver,poDS,ODrCDeleteDataSource)) { //Delete datasource - OGRErr ret = ogrDriver->DeleteDataSource(datasourceName.c_str()); - if (ret != OGRERR_NONE) + bool ret = otb::OGRVersionProxy::Delete(ogrDriver,datasourceName.c_str()); + if (!ret) { + otb::OGRVersionProxy::Close(poDS); itkGenericExceptionMacro(<< "Deletion of data source " << datasourceName << " failed: " << CPLGetLastErrorMsg()); } } else { + otb::OGRVersionProxy::Close(poDS); itkGenericExceptionMacro(<< "Cannot delete data source " << datasourceName); } + otb::OGRVersionProxy::Close(poDS); } // if (poDS != NULL) } @@ -223,7 +223,7 @@ otb::ogr::DataSource::New(std::string const& datasourceName, Modes::type mode) /*static*/ otb::ogr::DataSource::Pointer -otb::ogr::DataSource::New(OGRDataSource * source, Modes::type mode) +otb::ogr::DataSource::New(otb::OGRVersionProxy::GDALDatasetType * source, Modes::type mode) { Pointer res = new DataSource(source, mode); res->UnRegister(); @@ -271,7 +271,7 @@ otb::ogr::Layer otb::ogr::DataSource::CreateLayer( if (m_OpenMode == Modes::Read) { otb::ogr::Layer l = GetLayerChecked(name); // will throw if not existing - itkGenericOutputMacro(<< "Requesting layer creation in read-only OGRDataSource. Returning the existing layer"); + itkGenericOutputMacro(<< "Requesting layer creation in read-only GDALDataset. Returning the existing layer"); return l; } @@ -292,9 +292,9 @@ otb::ogr::Layer otb::ogr::DataSource::CreateLayer( OGRLayer * ol = m_DataSource->CreateLayer( name.c_str(), poSpatialRef, eGType, otb::ogr::StringListConverter(papszOptions).to_ogr()); if (!ol) - { + { itkGenericExceptionMacro(<< "Failed to create the layer <"<<name - << "> in the OGRDataSource file <" << m_DataSource->GetName() + << "> in the GDALDataset file <" << GetDatasetDescription() <<">: " << CPLGetLastErrorMsg()); } @@ -317,7 +317,7 @@ otb::ogr::Layer otb::ogr::DataSource::CreateLayer( if (!ol) { itkGenericExceptionMacro(<< "Failed to create the layer <"<<name - << "> in the OGRDataSource file <" << m_DataSource->GetName() + << "> in the GDALDataset file <" << GetDatasetDescription() <<">: " << CPLGetLastErrorMsg()); } @@ -329,7 +329,7 @@ otb::ogr::Layer otb::ogr::DataSource::CreateLayer( case Modes::Update_LayerCreateOnly: { // The layer exists. Returns it - // It will be non-modifiable if already existing at OGRDataSource creation time + // It will be non-modifiable if already existing at GDALDataset creation time if (layer) { return layer; @@ -341,7 +341,7 @@ otb::ogr::Layer otb::ogr::DataSource::CreateLayer( if (!ol) { itkGenericExceptionMacro(<< "Failed to create the layer <"<<name - << "> in the OGRDataSource file <" << m_DataSource->GetName() + << "> in the GDALDataset file <" << GetDatasetDescription() <<">: " << CPLGetLastErrorMsg()); } @@ -351,11 +351,11 @@ otb::ogr::Layer otb::ogr::DataSource::CreateLayer( break; default : assert(false && "Should never be there"); - itkGenericExceptionMacro(<< "OGRDataSource opening mode not supported"); + itkGenericExceptionMacro(<< "GDALDataset opening mode not supported"); break; } - itkGenericExceptionMacro(<< "OGRDataSource opening mode not supported"); + itkGenericExceptionMacro(<< "GDALDataset opening mode not supported"); return Layer(0, false); // keep compiler happy } @@ -369,11 +369,11 @@ otb::ogr::Layer otb::ogr::DataSource::CopyLayer( switch (m_OpenMode) { case Modes::Invalid: - assert(false && "Invalid OGRDataSource opening mode"); - itkGenericExceptionMacro(<< "Invalid OGRDataSource opening mode"); + assert(false && "Invalid GDALDataset opening mode"); + itkGenericExceptionMacro(<< "Invalid GDALDataset opening mode"); break; case Modes::Read: - itkGenericExceptionMacro(<< "OGRDataSource is opened in Read mode : cannot create a layer"); + itkGenericExceptionMacro(<< "GDALDataset is opened in Read mode : cannot create a layer"); break; default: break; @@ -382,10 +382,10 @@ otb::ogr::Layer otb::ogr::DataSource::CopyLayer( OGRLayer * l0 = &srcLayer.ogr(); OGRLayer * ol = m_DataSource->CopyLayer(l0, newName.c_str(), papszOptions); if (!ol) - { + { itkGenericExceptionMacro(<< "Failed to copy the layer <" << srcLayer.GetName() << "> into the new layer <" <<newName - << "> in the OGRDataSource file <" << m_DataSource->GetName() + << "> in the GDALDataset file <" << GetDatasetDescription() <<">: " << CPLGetLastErrorMsg()); } const bool modifiable = true; @@ -400,14 +400,14 @@ void otb::ogr::DataSource::DeleteLayer(size_t i) switch (m_OpenMode) { case Modes::Invalid: - assert(false && "Invalid OGRDataSource opening mode"); - itkGenericExceptionMacro(<< "Invalid OGRDataSource opening mode"); + assert(false && "Invalid GDALDataset opening mode"); + itkGenericExceptionMacro(<< "Invalid GDALDataset opening mode"); break; case Modes::Read: - itkGenericExceptionMacro(<< "OGRDataSource is opened in Read mode : cannot delete a layer"); + itkGenericExceptionMacro(<< "GDALDataset is opened in Read mode : cannot delete a layer"); break; case Modes::Update_LayerCreateOnly: - itkGenericExceptionMacro(<< "OGRDataSource is opened in Update_LayerCreateOnly mode : cannot delete a layer"); + itkGenericExceptionMacro(<< "GDALDataset is opened in Update_LayerCreateOnly mode : cannot delete a layer"); break; default: break; @@ -415,15 +415,15 @@ void otb::ogr::DataSource::DeleteLayer(size_t i) const int nb_layers = GetLayersCount(); if (int(i) >= nb_layers) - { - itkExceptionMacro(<< "Cannot delete " << i << "th layer in the OGRDataSource <" - << m_DataSource->GetName() << "> as it contains only " << nb_layers << "layers."); + { + itkExceptionMacro(<< "Cannot delete " << i << "th layer in the GDALDataset <" + << GetDatasetDescription() << "> 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 OGRDataSource <" - << m_DataSource->GetName() << ">: " << CPLGetLastErrorMsg()); + itkExceptionMacro(<< "Cannot delete " << i << "th layer in the GDALDataset <" + << GetDatasetDescription() << ">: " << CPLGetLastErrorMsg()); } } @@ -478,9 +478,9 @@ 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() << ">: " + << "> in the GDALDataset <" << GetDatasetDescription() << ">: " << CPLGetLastErrorMsg()); } return 0; // keep compiler happy @@ -491,15 +491,15 @@ otb::ogr::Layer otb::ogr::DataSource::GetLayerChecked(size_t i) assert(m_DataSource && "Datasource not initialized"); const int nb_layers = GetLayersCount(); if (int(i) >= nb_layers) - { - itkExceptionMacro(<< "Cannot fetch " << i << "th layer in the OGRDataSource <" - << m_DataSource->GetName() << "> as it contains only " << nb_layers << "layers."); + { + itkExceptionMacro(<< "Cannot fetch " << i << "th layer in the GDALDataset <" + << GetDatasetDescription() << "> 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 OGRDataSource <" - << m_DataSource->GetName() << ">: " << CPLGetLastErrorMsg()); + itkExceptionMacro( << "Unexpected error: cannot fetch " << i << "th layer in the GDALDataset <" + << GetDatasetDescription() << ">: " << CPLGetLastErrorMsg()); } return otb::ogr::Layer(layer_ptr, IsLayerModifiable(i)); } @@ -525,7 +525,7 @@ otb::ogr::Layer otb::ogr::DataSource::GetLayerChecked(std::string const& name) if (!layer_ptr) { itkExceptionMacro( << "Cannot fetch any layer named <" << name - << "> in the OGRDataSource <" << m_DataSource->GetName() << ">: " + << "> in the GDALDataset <" << GetDatasetDescription() << ">: " << CPLGetLastErrorMsg()); } return otb::ogr::Layer(layer_ptr, IsLayerModifiable(name)); @@ -550,10 +550,10 @@ otb::ogr::Layer otb::ogr::DataSource::ExecuteSQL( { #if defined(PREFER_EXCEPTION) itkExceptionMacro( << "Unexpected error: cannot execute the SQL request <" << statement - << "> in the OGRDataSource <" << m_DataSource->GetName() << ">: " << CPLGetLastErrorMsg()); + << "> in the GDALDataset <" << GetDatasetDescription() << ">: " << CPLGetLastErrorMsg()); #else // Cannot use the deleter made for result sets obtained from - // OGRDataSource::ExecuteSQL because it checks for non-nullity.... + // GDALDataset::ExecuteSQL because it checks for non-nullity.... // *sigh* return otb::ogr::Layer(0, modifiable); #endif @@ -693,16 +693,28 @@ 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"); - const OGRErr res= m_DataSource->SyncToDisk(); - if (res != OGRERR_NONE) + bool ret = otb::OGRVersionProxy::SyncToDisk(m_DataSource); + + if(!ret) { itkExceptionMacro( << "Cannot flush the pending of the OGRDataSource <" - << m_DataSource->GetName() << ">: " << CPLGetLastErrorMsg()); + << GetDatasetDescription() << ">: " << CPLGetLastErrorMsg()); } } + + +std::string otb::ogr::DataSource::GetDatasetDescription() const +{ + std::vector<std::string> files = otb::OGRVersionProxy::GetFileListAsStringVector(m_DataSource); + std::string description = ""; + for(std::vector<std::string>::const_iterator it = files.begin();it!=files.end();++it) + description+=(*it)+", "; + + return description; +} diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRLayerWrapper.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRLayerWrapper.cxx index 6942ef41a8b4f1916fd99592736d58468a4511f2..e917acd9f961da50f5c0bd2a1b11f7e0ec081750 100644 --- a/Modules/Adapters/GdalAdapters/src/otbOGRLayerWrapper.cxx +++ b/Modules/Adapters/GdalAdapters/src/otbOGRLayerWrapper.cxx @@ -27,10 +27,10 @@ #ifdef __GNUC__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" -#include "ogrsf_frmts.h" // OGRDataSource & OGRLayer +#include "gdal_priv.h"// GDALDataset #pragma GCC diagnostic pop #else -#include "ogrsf_frmts.h" // OGRDataSource & OGRLayer +#include "gdal_priv.h" // GDALDataset #endif #include "otbOGRDataSourceWrapper.h" @@ -60,8 +60,8 @@ otb::ogr::Layer::Layer(OGRLayer* layer, bool modifiable) { } -otb::ogr::Layer::Layer(OGRLayer* layer, OGRDataSource& 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" ); diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy1x.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy1x.cxx new file mode 100644 index 0000000000000000000000000000000000000000..b05ffbd8bac57fddbf7c992b6e11e0bb3e282bba --- /dev/null +++ b/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy1x.cxx @@ -0,0 +1,106 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "otbOGRVersionProxy.h" + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#include "ogrsf_frmts.h" +#pragma GCC diagnostic pop +#else +#include "ogrsf_frmts.h" +#endif + +namespace otb +{ + + +OGRVersionProxy::GDALDatasetType * OGRVersionProxy::Open(const char * filename, bool readOnly) +{ + return OGRSFDriverRegistrar::Open(filename,!readOnly); +} + +void OGRVersionProxy::Close(GDALDatasetType * dataset) +{ + OGRDataSource::DestroyDataSource(dataset); +} + +OGRVersionProxy::GDALDatasetType * OGRVersionProxy::Create(GDALDriverType * driver, const char * name) +{ + return driver->CreateDataSource(name); +} + +bool OGRVersionProxy::Delete(GDALDriverType * driver, const char * name) +{ + OGRErr ret = driver->DeleteDataSource(name); + + return (ret == OGRERR_NONE); +} + +OGRVersionProxy::GDALDriverType * OGRVersionProxy::GetDriverByName(const char * name) +{ + return OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(name); +} + +bool OGRVersionProxy::TestCapability(const GDALDriverType * driver, const GDALDatasetType *, const char * capability) +{ + // Class OGRDataSource is not const correct + return const_cast<GDALDriverType * >(driver)->TestCapability(capability); +} + +std::string OGRVersionProxy::GetDatasetClassName() +{ + return std::string("OGRDataSource"); +} + +std::string OGRVersionProxy::GetDriverClassName() +{ + return std::string("OGRSFDriver"); +} + +std::vector<std::string> OGRVersionProxy::GetFileListAsStringVector(GDALDatasetType * dataset) +{ + std::vector<std::string> ret; + + ret.push_back(std::string(dataset->GetName())); + + return ret; +} + +bool OGRVersionProxy::SyncToDisk(GDALDatasetType * dataset) +{ + const OGRErr res= dataset->SyncToDisk(); + + return (res == OGRERR_NONE); +} + +std::vector<std::string> OGRVersionProxy::GetAvailableDriversAsStringVector() +{ + std::vector<std::string> ret; + + int nbDrivers = OGRSFDriverRegistrar::GetRegistrar()->GetDriverCount(); + + for(int i = 0; i < nbDrivers;++i) + { + ret.push_back(OGRSFDriverRegistrar::GetRegistrar()->GetDriver(i)->GetName()); + } + + return ret; +} + +} // end namespace diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy2x.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy2x.cxx new file mode 100644 index 0000000000000000000000000000000000000000..0d5ff9407cc0dea10ec1ebba8c3caded51261ce3 --- /dev/null +++ b/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy2x.cxx @@ -0,0 +1,119 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "otbOGRVersionProxy.h" + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#include "gdal_priv.h" +#pragma GCC diagnostic pop +#else +#include "gdal_priv.h" +#endif + +namespace otb +{ + + +OGRVersionProxy::GDALDatasetType * OGRVersionProxy::Open(const char * filename, bool readOnly) +{ + return (GDALDatasetType *)GDALOpenEx(filename, (readOnly? GDAL_OF_READONLY : GDAL_OF_UPDATE) | GDAL_OF_VECTOR,NULL,NULL,NULL); +} + +void OGRVersionProxy::Close(GDALDatasetType * dataset) +{ + GDALClose(dataset); +} + +OGRVersionProxy::GDALDatasetType * OGRVersionProxy::Create(GDALDriverType * driver, const char * name) +{ + return driver->Create(name,0,0,0,GDT_Unknown,NULL); +} + +bool OGRVersionProxy::Delete(GDALDriverType * driver, const char * name) +{ + OGRErr ret = driver->Delete(name); + + return (ret == OGRERR_NONE); +} + +OGRVersionProxy::GDALDriverType * OGRVersionProxy::GetDriverByName(const char * name) +{ + return GetGDALDriverManager()->GetDriverByName(name); +} + +bool OGRVersionProxy::TestCapability(const GDALDriverType *, const GDALDatasetType * dataset, const char * capability) +{ + // Const correctness problem in GDALDataset class + return const_cast<GDALDatasetType *>(dataset)->TestCapability(capability); +} + +std::string OGRVersionProxy::GetDatasetClassName() +{ + return std::string("GDALDataset"); +} + +std::string OGRVersionProxy::GetDriverClassName() +{ + return std::string("GDALDriver"); +} + +std::vector<std::string> OGRVersionProxy::GetFileListAsStringVector(GDALDatasetType * dataset) +{ + std::vector<std::string> ret; + + char ** files = dataset->GetFileList(); + + std::string files_str=""; + + if(files) + { + unsigned int i = 0; + while(files[i]!=NULL) + { + ret.push_back(std::string(files[i])); + ++i; + } + CSLDestroy(files); + } + + return ret; +} + +bool OGRVersionProxy::SyncToDisk(GDALDatasetType * dataset) +{ + dataset->FlushCache(); + + return true; +} + +std::vector<std::string> OGRVersionProxy::GetAvailableDriversAsStringVector() +{ + std::vector<std::string> ret; + + int nbDrivers = GetGDALDriverManager()->GetDriverCount(); + + for(int i = 0; i < nbDrivers;++i) + { + ret.push_back(GDALGetDriverShortName(GetGDALDriverManager()->GetDriver(i))); + } + + return ret; +} + +} // end namespace diff --git a/Modules/Core/Common/src/otbConfigure.h.in b/Modules/Core/Common/src/otbConfigure.h.in index 29555ad72a89c3d8879787b6f87b5a8c77aca532..38ac5b8a48a1df786fac99d4f7d03352fa51dfcc 100644 --- a/Modules/Core/Common/src/otbConfigure.h.in +++ b/Modules/Core/Common/src/otbConfigure.h.in @@ -17,3 +17,4 @@ /* Show developper debug messages */ #cmakedefine OTB_SHOW_ALL_MSG_DEBUG +#cmakedefine OTB_USE_GDAL_20 diff --git a/Modules/IO/IOGDAL/include/otbOGRIOHelper.h b/Modules/IO/IOGDAL/include/otbOGRIOHelper.h index baf02bd4ee213ec10f1c7d4c3591afd8d767effe..f84829cb9e1f13aa0b4945d1614366c57bbdcf89 100644 --- a/Modules/IO/IOGDAL/include/otbOGRIOHelper.h +++ b/Modules/IO/IOGDAL/include/otbOGRIOHelper.h @@ -21,8 +21,9 @@ #include <vector> #include "otbVectorData.h" +#include "otbOGRVersionProxy.h" -class OGRDataSource; +class GDALDataset; class OGRGeometryCollection; class OGRLayer; class OGRSpatialReference; @@ -63,14 +64,14 @@ public: unsigned int ProcessNodeWrite(InternalTreeNodeType * source, - OGRDataSource * m_DataSource, + OGRVersionProxy::GDALDatasetType * m_DataSource, OGRGeometryCollection * ogrCollection, OGRLayer * ogrCurrentLayer, OGRSpatialReference * oSRS); /** Return a list of OGRLayer * */ std::vector<OGRLayer*> ConvertDataTreeNodeToOGRLayers(InternalTreeNodeType * source, - OGRDataSource * dummyDatasource, + OGRVersionProxy::GDALDatasetType * dummyDatasource, OGRLayer* ogrCurrentLayer, OGRSpatialReference * oSRS); diff --git a/Modules/IO/IOGDAL/include/otbOGRVectorDataIO.h b/Modules/IO/IOGDAL/include/otbOGRVectorDataIO.h index f7ae24d3dac2171d7a1ecabc928546a98fa6538a..b56ef6d1b96ef3e87f55060de0aa0215aac34de5 100644 --- a/Modules/IO/IOGDAL/include/otbOGRVectorDataIO.h +++ b/Modules/IO/IOGDAL/include/otbOGRVectorDataIO.h @@ -23,7 +23,7 @@ #include "otbVectorDataIOBase.h" #include "otbVectorData.h" -class OGRDataSource; +#include "otbOGRVersionProxy.h" namespace otb { @@ -116,7 +116,7 @@ private: std::string GetOGRDriverName(std::string name) const; - OGRDataSource * m_DataSource; + OGRVersionProxy::GDALDatasetType * m_DataSource; }; diff --git a/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx b/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx index 4216a78fa1613a42be710769db43a657ac8a25ff..df01b6f1e8bbe140add450276a609675bc66613f 100644 --- a/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx +++ b/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx @@ -649,7 +649,7 @@ void OGRIOHelper unsigned int OGRIOHelper ::ProcessNodeWrite(InternalTreeNodeType * source, - OGRDataSource * m_DataSource, + OGRVersionProxy::GDALDatasetType * m_DataSource, OGRGeometryCollection * ogrCollection, OGRLayer * ogrCurrentLayer, OGRSpatialReference * oSRS) @@ -1025,7 +1025,7 @@ unsigned int OGRIOHelper **/ std::vector<OGRLayer*> OGRIOHelper ::ConvertDataTreeNodeToOGRLayers(InternalTreeNodeType * source, - OGRDataSource * inMemoryDataSource, + OGRVersionProxy::GDALDatasetType * inMemoryDataSource, OGRLayer* ogrCurrentLayer, OGRSpatialReference * oSRS) { @@ -1034,8 +1034,8 @@ std::vector<OGRLayer*> OGRIOHelper if (inMemoryDataSource == NULL) { const char * driverName = "Memory"; - OGRSFDriver * ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName); - inMemoryDataSource = ogrDriver->CreateDataSource("tempDataSource",NULL); + OGRVersionProxy::GDALDriverType * ogrDriver = OGRVersionProxy::GetDriverByName(driverName); + inMemoryDataSource = OGRVersionProxy::Create(ogrDriver,"tempDataSource"); } std::vector<OGRLayer*> ogrLayerVector; diff --git a/Modules/IO/IOGDAL/src/otbOGRVectorDataIO.cxx b/Modules/IO/IOGDAL/src/otbOGRVectorDataIO.cxx index cd2b744bda57b013c9c54cb9b0a6935386aab6da..5c4b543394eaa4d5d09ce88c06f32aba171f80e4 100644 --- a/Modules/IO/IOGDAL/src/otbOGRVectorDataIO.cxx +++ b/Modules/IO/IOGDAL/src/otbOGRVectorDataIO.cxx @@ -46,7 +46,7 @@ OGRVectorDataIO::~OGRVectorDataIO() { if (m_DataSource != NULL) { - OGRDataSource::DestroyDataSource(m_DataSource); + GDALClose(m_DataSource); } } @@ -54,13 +54,15 @@ OGRVectorDataIO::~OGRVectorDataIO() bool OGRVectorDataIO::CanReadFile(const char* filename) const { - OGRDataSource * poDS = OGRSFDriverRegistrar::Open(filename, FALSE); + otb::OGRVersionProxy::GDALDatasetType * poDS = OGRVersionProxy::Open(filename, true); + if (poDS == NULL) { + std::cerr<<"Can not read file "<<filename<<" with GDALOpen"<<std::endl; return false; } // std::cout << poDS->GetDriver()->GetName() << std::endl; - OGRDataSource::DestroyDataSource(poDS); + OGRVersionProxy::Close(poDS); return true; } @@ -88,10 +90,10 @@ OGRVectorDataIO if (m_DataSource != NULL) { - OGRDataSource::DestroyDataSource(m_DataSource); + OGRVersionProxy::Close(m_DataSource); } - m_DataSource = OGRSFDriverRegistrar::Open(this->m_FileName.c_str(), FALSE); + m_DataSource = OGRVersionProxy::Open(this->m_FileName.c_str(),true); if (m_DataSource == NULL) { @@ -176,7 +178,7 @@ OGRVectorDataIO } // end For each layer - OGRDataSource::DestroyDataSource(m_DataSource); + GDALClose(m_DataSource); m_DataSource = NULL; } @@ -194,7 +196,7 @@ bool OGRVectorDataIO::CanWriteFile(const char* filename) const } -void OGRVectorDataIO::Write(const itk::DataObject* datag, char ** papszOptions) +void OGRVectorDataIO::Write(const itk::DataObject* datag, char ** /** unused */) { itk::TimeProbe chrono; chrono.Start(); @@ -208,8 +210,8 @@ void OGRVectorDataIO::Write(const itk::DataObject* datag, char ** papszOptions) //Find first the OGR driver - OGRSFDriver * ogrDriver = - OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(this->GetOGRDriverName(this->m_FileName).data()); + OGRVersionProxy::GDALDriverType * ogrDriver = + OGRVersionProxy::GetDriverByName(this->GetOGRDriverName(this->m_FileName).data()); if (ogrDriver == NULL) { @@ -219,25 +221,25 @@ void OGRVectorDataIO::Write(const itk::DataObject* datag, char ** papszOptions) // free an existing previous data source, if any if (m_DataSource != NULL) { - OGRDataSource::DestroyDataSource(m_DataSource); + OGRVersionProxy::Close(m_DataSource); } // Erase the dataSource if already exist //TODO investigate the possibility of giving the option OVERWRITE=YES to the CreateDataSource method - OGRDataSource * poDS = OGRSFDriverRegistrar::Open(this->m_FileName.c_str(), TRUE); + OGRVersionProxy::GDALDatasetType * poDS = OGRVersionProxy::Open(this->m_FileName.c_str(),false); if (poDS != NULL) { //Erase the data if possible - if (poDS->GetDriver()->TestCapability(ODrCDeleteDataSource)) + if (OGRVersionProxy::TestCapability(ogrDriver,poDS,ODrCDeleteDataSource)) { //Delete datasource - poDS->GetDriver()->DeleteDataSource(this->m_FileName.c_str()); + OGRVersionProxy::Delete(ogrDriver,m_FileName.c_str()); } } - OGRDataSource::DestroyDataSource(poDS); + OGRVersionProxy::Close(poDS); // m_DataSource = OGRSFDriverRegistrar::Open(this->m_FileName.c_str(), TRUE); - m_DataSource = ogrDriver->CreateDataSource(this->m_FileName.c_str(), papszOptions); + m_DataSource = OGRVersionProxy::Create(ogrDriver,this->m_FileName.c_str()); // check the created data source if (m_DataSource == NULL) @@ -292,7 +294,7 @@ void OGRVectorDataIO::Write(const itk::DataObject* datag, char ** papszOptions) otbMsgDevMacro( << "layerKept " << layerKept ); (void)layerKept; // keep compiler happy - OGRDataSource::DestroyDataSource(m_DataSource); + GDALClose(m_DataSource); m_DataSource = NULL; if (oSRS != NULL) diff --git a/Modules/IO/TestKernel/src/otbTestHelper.cxx b/Modules/IO/TestKernel/src/otbTestHelper.cxx index ab256ff3ef14e56f136b288279f2453a8af87264..db856132111a06c522c11ce2293240f17bfd4e6c 100644 --- a/Modules/IO/TestKernel/src/otbTestHelper.cxx +++ b/Modules/IO/TestKernel/src/otbTestHelper.cxx @@ -27,6 +27,8 @@ #include <algorithm> #include <string> +#include "ogrsf_frmts.h" + #include "itksys/SystemTools.hxx" #include "itksys/Directory.hxx" #include "itksys/RegularExpression.hxx" @@ -37,6 +39,9 @@ #include "otbDifferenceImageFilter.h" #include "otbPrintableImageFilter.h" #include "otbStreamingShrinkImageFilter.h" +#include "otbOGRVersionProxy.h" + +#include "otbConfigure.h" #define ITK_TEST_DIMENSION_MAX 6 @@ -44,16 +49,6 @@ #include "cpl_conv.h" #include "cpl_string.h" #include "cpl_multiproc.h" -#include "ogr_api.h" - -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wshadow" -#include "ogrsf_frmts.h" -#pragma GCC diagnostic pop -#else -#include "ogrsf_frmts.h" -#endif #define otbPrintDiff(comment, refStr, testStr) \ std::cout << " ---- '" << comment << "' checking ---------------------------" << std::endl; \ @@ -1074,29 +1069,27 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b /* -------------------------------------------------------------------- */ /* Open data source. */ /* -------------------------------------------------------------------- */ - OGRDataSource *ref_poDS = NULL; - OGRSFDriver * ref_poDriver = NULL; + otb::OGRVersionProxy::GDALDatasetType *ref_poDS = NULL; + otb::OGRVersionProxy::GDALDriverType * ref_poDriver = NULL; //OGRGeometry * ref_poSpatialFilter = NULL; - OGRDataSource *test_poDS = NULL; - OGRSFDriver * test_poDriver = NULL; + otb::OGRVersionProxy::GDALDatasetType *test_poDS = NULL; + otb::OGRVersionProxy::GDALDriverType * test_poDriver = NULL; //OGRGeometry * test_poSpatialFilter = NULL; - OGRRegisterAll(); - - ref_poDS = OGRSFDriverRegistrar::Open(ref_pszDataSource, !bReadOnly, &ref_poDriver); + ref_poDS = otb::OGRVersionProxy::Open(ref_pszDataSource, false); if (ref_poDS == NULL && !bReadOnly) { - ref_poDS = OGRSFDriverRegistrar::Open(ref_pszDataSource, FALSE, &ref_poDriver); + ref_poDS = otb::OGRVersionProxy::Open(ref_pszDataSource, true); if (ref_poDS != NULL && m_ReportErrors) { std::cout << "Had to open REF data source read-only.\n"; bReadOnly = TRUE; } } - test_poDS = OGRSFDriverRegistrar::Open(test_pszDataSource, !bReadOnly, &test_poDriver); + test_poDS = otb::OGRVersionProxy::Open(ref_pszDataSource, bReadOnly); if (test_poDS == NULL && !bReadOnly) { - test_poDS = OGRSFDriverRegistrar::Open(test_pszDataSource, FALSE, &test_poDriver); + test_poDS = otb::OGRVersionProxy::Open(ref_pszDataSource, bReadOnly); if (test_poDS != NULL && m_ReportErrors) { std::cout << "Had to open REF data source read-only.\n"; @@ -1108,51 +1101,66 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b /* -------------------------------------------------------------------- */ if (ref_poDS == NULL) { - OGRSFDriverRegistrar *ref_poR = OGRSFDriverRegistrar::GetRegistrar(); - + if (m_ReportErrors) - std::cout << "FAILURE:\n" - "Unable to open REF datasource `" << ref_pszDataSource << "' with the following drivers." << std::endl; - for (int iDriver = 0; iDriver < ref_poR->GetDriverCount(); ++iDriver) + std::cout << "FAILURE:\n" "Unable to open REF datasource `" << ref_pszDataSource << "' with the following drivers." << std::endl; + + std::vector<std::string> drivers = OGRVersionProxy::GetAvailableDriversAsStringVector(); + + for (std::vector<std::string>::const_iterator it = drivers.begin();it!=drivers.end();++it) { - std::cout << " -> " << ref_poR->GetDriver(iDriver)->GetName() << std::endl; + std::cout << " -> " << *it << std::endl; } return (1); } + ref_poDriver = test_poDS->GetDriver(); CPLAssert(ref_poDriver != NULL); if (test_poDS == NULL) { - OGRSFDriverRegistrar *test_poR = OGRSFDriverRegistrar::GetRegistrar(); - if (m_ReportErrors) std::cout << "FAILURE:\n" "Unable to open TEST datasource `" << test_pszDataSource << "' with the following drivers." << std::endl; - for (int iDriver = 0; iDriver < test_poR->GetDriverCount(); ++iDriver) + + std::vector<std::string> drivers = OGRVersionProxy::GetAvailableDriversAsStringVector(); + + for (std::vector<std::string>::const_iterator it = drivers.begin();it!=drivers.end();++it) { - std::cout << " -> " << test_poR->GetDriver(iDriver)->GetName() << std::endl; + std::cout << " -> " << *it << std::endl; } return (1); } + test_poDriver = test_poDS->GetDriver(); CPLAssert(test_poDriver != NULL); /* -------------------------------------------------------------------- */ /* Some information messages. */ /* -------------------------------------------------------------------- */ - otbCheckStringValue("INFO: using driver", ref_poDriver->GetName(), test_poDriver->GetName(), nbdiff, m_ReportErrors); - - // std::string strRefName(ref_poDS->GetName()); - // std::string strTestName(test_poDS->GetName()); - // if (strRefName != strTestName) - // { - // if (!m_ReportErrors) - // { - // otbPrintDiff("WARNING: INFO: Internal data source name poDS->GetName() were different", - // strRefName, - // strTestName); - // } - // } + otbCheckStringValue("INFO: using driver", GDALGetDriverShortName(ref_poDriver), GDALGetDriverShortName(test_poDriver), nbdiff, m_ReportErrors); + + // TODO: Improve this check as it will stop as soon as one of the + // list ends (i.e. it does not guarantee that all files are present) + std::vector<std::string> refFileList = otb::OGRVersionProxy::GetFileListAsStringVector(ref_poDS); + std::vector<std::string> testFileList = otb::OGRVersionProxy::GetFileListAsStringVector(test_poDS); + + unsigned int fileId = 0; + while (fileId < refFileList.size() && fileId < testFileList.size()) + { + std::string strRefName(refFileList[fileId]); + std::string strTestName(testFileList[fileId]); + if (strRefName != strTestName) + { + if (!m_ReportErrors) + { + otbPrintDiff("WARNING: INFO: Internal data source files were different", + strRefName, + strTestName); + } + } + ++fileId; + } + /* -------------------------------------------------------------------- */ /* Process each data source layer. */ /* -------------------------------------------------------------------- */ @@ -1240,8 +1248,8 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b /* -------------------------------------------------------------------- */ /* Close down. */ /* -------------------------------------------------------------------- */ - OGRDataSource::DestroyDataSource( ref_poDS ); - OGRDataSource::DestroyDataSource( test_poDS ); + GDALClose( ref_poDS ); + GDALClose( test_poDS ); return (nbdiff != 0) ? 1 : 0; } @@ -1256,7 +1264,7 @@ void TestHelper::DumpOGRFeature(FILE* fpOut, OGRFeature* feature, char** papszOp return; } - fprintf(fpOut, "OGRFeature:%ld\n", feature->GetFID()); + fprintf(fpOut, "OGRFeature:%lld\n", feature->GetFID()); const char* pszDisplayFields = CSLFetchNameValue(papszOptions, "DISPLAY_FIELDS"); @@ -1374,6 +1382,8 @@ void TestHelper::DumpOGRGeometry(FILE* fp, OGRGeometry* geometry, const char * p } case wkbLinearRing: break; + default: + break; } } else if (pszDisplayGeometry == NULL || CSLTestBoolean(pszDisplayGeometry) || diff --git a/Modules/ThirdParty/GDAL/gdalVersionTest.cxx b/Modules/ThirdParty/GDAL/gdalVersionTest.cxx index 981cf8fe1c572a7cfcdffbf0040d3a5f699eb008..076a144dd5f368c6cc673983dd09f85aafb2f979 100644 --- a/Modules/ThirdParty/GDAL/gdalVersionTest.cxx +++ b/Modules/ThirdParty/GDAL/gdalVersionTest.cxx @@ -55,7 +55,7 @@ int main(int argc, char * argv[]) } - if ( (UIntVect[0]<MAJOR) || (UIntVect[1]<MINOR) ) + if ( (UIntVect[0]==MAJOR && UIntVect[1]<MINOR) || (UIntVect[0]<MAJOR) ) { cout << "WARNING : Version of GDAL must be >= " << MAJOR << "." << MINOR << " : " << UIntVect[0] << "." << UIntVect[1] << " detected)." << endl; return 1; diff --git a/Modules/ThirdParty/GDAL/otb-module-init.cmake b/Modules/ThirdParty/GDAL/otb-module-init.cmake index 7222581bcc0a0766809697b5dce4fd4c731c23cb..74e86291e8f3c1e468b4d740a83eb3a129a2d218 100644 --- a/Modules/ThirdParty/GDAL/otb-module-init.cmake +++ b/Modules/ThirdParty/GDAL/otb-module-init.cmake @@ -23,7 +23,7 @@ if(GDAL_CONFIG_CHECKING) #------------------- TESTS --------------------- # Version of GDAL try_run(RUN_RESULT_VERSION COMPILE_RESULT_VERSION ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/Modules/ThirdParty/GDAL/gdalVersionTest.cxx CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:PATH=${GDAL_INCLUDE_DIR}" "-DLINK_LIBRARIES:STRING=${GDAL_LIBRARY}" ARGS ${TEMP}/gdalVersion.txt ${MIN_MAJOR_VERSION} ${MIN_MINOR_VERSION}) - + # Has OGR try_compile(GDAL_HAS_OGR ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/Modules/ThirdParty/GDAL/gdalOGRTest.cxx CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:PATH=${GDAL_INCLUDE_DIR}" "-DLINK_LIBRARIES:STRING=${GDAL_LIBRARY}") @@ -85,6 +85,15 @@ if(GDAL_CONFIG_CHECKING) file(READ "${TEMP}/gdalVersion.txt" DETECTED_VERSION) message(WARNING "Version of GDAL must be >= " ${MIN_MAJOR_VERSION} "." ${MIN_MINOR_VERSION} " : " ${DETECTED_VERSION} " detected.") set(GDAL_QUALIFIES FALSE) + else((${RUN_RESULT_VERSION} EQUAL 1)) + file(READ "${TEMP}/gdalVersion.txt" DETECTED_VERSION) + string(SUBSTRING ${DETECTED_VERSION} 0 2 VER2) + if(${VER2} EQUAL "2.") + message("-- Gdal >= 2.0.0 detected") + set(OTB_USE_GDAL_20 true CACHE INTERNAL "True if GDAL >= 2.0.0 has been detected" FORCE ) + else(${VER2} EQUAL "2.") + set(OTB_USE_GDAL_20 false CACHE INTERNAL "True if GDAL >= 2.0.0 has been detected" FORCE ) + endif() endif() if (NOT GDAL_HAS_OGR)