diff --git a/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.h b/Modules/Adapters/GdalAdapters/include/otbOGRDataSourceWrapper.h index 0a37948fab58167d9426007187279e5468497bd8..ed6bf3db5189fb534dbcb54c513b4f99f4b999ff 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(ogr::version_proxy::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(ogr::version_proxy::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(); + ogr::version_proxy::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(ogr::version_proxy::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; + ogr::version_proxy::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..3ae9e89704c1abcd31eeff29bc4ee989fa295315 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::ogr::version_proxy::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..419b84723c8e580d9b4fefffd493943cf18424f7 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::ogr::version_proxy::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..1c4ac63e4b685b3fddcfdb2fedba65f07e55523f --- /dev/null +++ b/Modules/Adapters/GdalAdapters/include/otbOGRVersionProxy.h @@ -0,0 +1,183 @@ +/*========================================================================= + + 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 +{ +namespace ogr +{ +namespace version_proxy +{ + +/** + * This namespace holds proxy functions hiding interface changes in gdal 2.0 + * + * This namespace holds proxy functions 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 functions. + * + * Whenever GDALDataset and GDALDriver have to be used to open a + * vector dataset (or OGRDataSource an OGRSFDriver for gdal 1.x), one + * should use ogr::version_proxy types GDALDatasetType and + * GDALDriverType. + * + * See function documentation for details. + */ + + #ifdef OTB_USE_GDAL_20 + typedef GDALDataset GDALDatasetType; + typedef GDALDriver GDALDriverType; + #else + typedef OGRDataSource GDALDatasetType; + typedef OGRSFDriver GDALDriverType; +#endif + + /** + * This function 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. + */ + GDALDatasetType * Open(const char * filename, bool readOnly = true); + + /** + * This function 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. + */ + void Close(GDALDatasetType * dataset); + + /** + * This function 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. + */ + GDALDatasetType * Create(GDALDriverType * driver, const char * name); + + + /** + * This function physically deletes an existing dataset. + * + * Calls OGRDataSource::DeleteDataSource for gdal 1.x implementation + * and GDALDriver::Delete for gdal 2.x implementation. + * + * \param name Name of the dataset to destroy. + */ + bool Delete(const char * name); + + /** + * This function 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. + */ + GDALDriverType * GetDriverByName(const char * name); + + /** + * 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. + */ + 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) + */ + std::string GetDatasetClassName(); + + /** + * \return The name of the driver class behind the implementation + * (OGRSFDriver for gdal 1.x and GDALDriver for gdal 2.x) + */ + 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. + */ + 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. + */ + std::vector<std::string> GetAvailableDriversAsStringVector(); + +} +} +} // 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..891c22b662caa6df44cf8d1c0b04507dbc4289d2 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::ogr::version_proxy::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 + ogr::version_proxy::Close(m_DataSource); // void, noexcept } m_DataSource = source; } @@ -119,16 +118,15 @@ otb::ogr::DataSource::DataSource() { Drivers::Init(); - OGRSFDriver * d = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("Memory"); + ogr::version_proxy::GDALDriverType * d = ogr::version_proxy::GetDriverByName("Memory"); assert(d && "OGR Memory driver not found"); - m_DataSource = d->CreateDataSource("in-memory"); + m_DataSource = ogr::version_proxy::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::ogr::version_proxy::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); + ogr::version_proxy::GDALDatasetType * source = ogr::version_proxy::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,48 +157,30 @@ otb::ogr::DataSource::Pointer otb::ogr::DataSource::OpenDataSource(std::string c <<datasourceName<<">."); } - OGRSFDriver * d = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName); + ogr::version_proxy::GDALDriverType * d = ogr::version_proxy::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 = ogr::version_proxy::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); } void DeleteDataSource(std::string const& datasourceName) { - // Attempt to delete the datasource if it already exists - OGRDataSource * poDS = OGRSFDriverRegistrar::Open(datasourceName.c_str(), TRUE); - - if (poDS != NULL) + bool ret = otb::ogr::version_proxy::Delete(datasourceName.c_str()); + if (!ret) { - OGRSFDriver * ogrDriver = poDS->GetDriver(); - OGRDataSource::DestroyDataSource(poDS); - //Erase the data if possible - if (ogrDriver->TestCapability(ODrCDeleteDataSource)) - { - //Delete datasource - OGRErr ret = ogrDriver->DeleteDataSource(datasourceName.c_str()); - if (ret != OGRERR_NONE) - { - itkGenericExceptionMacro(<< "Deletion of data source " << datasourceName - << " failed: " << CPLGetLastErrorMsg()); - } - } - else - { - itkGenericExceptionMacro(<< "Cannot delete data source " << datasourceName); - } - } // if (poDS != NULL) + itkGenericExceptionMacro(<< "Deletion of data source " << datasourceName + << " failed: " << CPLGetLastErrorMsg()); + } } otb::ogr::DataSource::Pointer @@ -213,7 +193,14 @@ otb::ogr::DataSource::New(std::string const& datasourceName, Modes::type mode) Drivers::Init(); - if (mode == Modes::Overwrite) + ogr::version_proxy::GDALDatasetType * ds = ogr::version_proxy::Open(datasourceName.c_str(),true); + + bool ds_exists = (ds!=NULL); + + ogr::version_proxy::Close(ds); + + + if (ds_exists && mode == Modes::Overwrite) { DeleteDataSource(datasourceName); } @@ -223,7 +210,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::ogr::version_proxy::GDALDatasetType * source, Modes::type mode) { Pointer res = new DataSource(source, mode); res->UnRegister(); @@ -271,7 +258,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 +279,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 +304,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 +316,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 +328,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 +338,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 +356,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 +369,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 +387,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 +402,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 +465,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 +478,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 +512,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 +537,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 @@ -699,10 +686,22 @@ bool otb::ogr::DataSource::HasCapability(std::string const& capabilityName) cons void otb::ogr::DataSource::SyncToDisk() { assert(m_DataSource && "Datasource not initialized"); - const OGRErr res= m_DataSource->SyncToDisk(); - if (res != OGRERR_NONE) + bool ret = otb::ogr::version_proxy::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::ogr::version_proxy::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..9a9aa525bba80edc004bba95df091291a3fe38fe 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::ogr::version_proxy::GDALDatasetType& sourceInChargeOfLifeTime, bool modifiable) +: m_Layer(layer, boost::bind(&otb::ogr::version_proxy::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..f92ee6e1dfd4a1e1784421369a867b56c76441c3 --- /dev/null +++ b/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy1x.cxx @@ -0,0 +1,124 @@ +/*========================================================================= + + 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 +{ +namespace ogr +{ +namespace version_proxy +{ + +GDALDatasetType * Open(const char * filename, bool readOnly) +{ + return OGRSFDriverRegistrar::Open(filename,!readOnly); +} + +void Close(GDALDatasetType * dataset) +{ + OGRDataSource::DestroyDataSource(dataset); +} + +GDALDatasetType * Create(GDALDriverType * driver, const char * name) +{ + GDALDatasetType * ds = driver->CreateDataSource(name); + + if(ds) + ds->SetDriver(driver); + + return ds; +} + +bool Delete(const char * name) +{ + // Open dataset + GDALDatasetType * poDS = Open(name,false); + GDALDriverType * poDriver = NULL; + if(poDS) + { + poDriver = poDS->GetDriver(); + Close(poDS); + } + + if(poDriver && poDriver->TestCapability(ODrCDeleteDataSource)) + { + + OGRErr ret = poDriver->DeleteDataSource(name); + return (ret == OGRERR_NONE); + } + + return false; +} + +GDALDriverType * GetDriverByName(const char * name) +{ + return OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(name); +} + +std::string GetDatasetClassName() +{ + return std::string("OGRDataSource"); +} + +std::string GetDriverClassName() +{ + return std::string("OGRSFDriver"); +} + +std::vector<std::string> GetFileListAsStringVector(GDALDatasetType * dataset) +{ + std::vector<std::string> ret; + + ret.push_back(std::string(dataset->GetName())); + + return ret; +} + +bool SyncToDisk(GDALDatasetType * dataset) +{ + const OGRErr res= dataset->SyncToDisk(); + + return (res == OGRERR_NONE); +} + +std::vector<std::string> 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..725486f4b74758e60b880cf8d38906dff85a9ead --- /dev/null +++ b/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy2x.cxx @@ -0,0 +1,157 @@ +/*========================================================================= + + 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 +{ +namespace ogr +{ +namespace version_proxy +{ + +GDALDatasetType * Open(const char * filename, bool readOnly) +{ + return (GDALDatasetType *)GDALOpenEx(filename, (readOnly? GDAL_OF_READONLY : GDAL_OF_UPDATE) | GDAL_OF_VECTOR,NULL,NULL,NULL); +} + +void Close(GDALDatasetType * dataset) +{ + GDALClose(dataset); +} + +GDALDatasetType * Create(GDALDriverType * driver, const char * name) +{ + return driver->Create(name,0,0,0,GDT_Unknown,NULL); +} + +bool Delete(const char * name) +{ + // Open dataset + GDALDatasetType * poDS = otb::ogr::version_proxy::Open(name,false); + GDALDriverType * poDriver = NULL; + if(poDS) + { + poDriver = poDS->GetDriver(); + Close(poDS); + } + + if(poDriver) + { + OGRErr ret = poDriver->Delete(name); + + return (ret == OGRERR_NONE); + } + return false; +} + +GDALDriverType * GetDriverByName(const char * name) +{ + return GetGDALDriverManager()->GetDriverByName(name); +} + +std::string GetDatasetClassName() +{ + return std::string("GDALDataset"); +} + +std::string GetDriverClassName() +{ + return std::string("GDALDriver"); +} + +namespace raii +{ +// This class is used in the next function, so as to prevent any +// ressource leak on char ** returned by dataset->GetFileList() +class CharPPCapsule +{ +public: + CharPPCapsule(char ** in) + : m_P(in) + {} + + const char ** P() const + { + return const_cast<const char **>(m_P); + } + + ~CharPPCapsule() + { + if(m_P) + CSLDestroy(m_P); + } + +private: + char ** m_P; +}; +} + +std::vector<std::string> GetFileListAsStringVector(GDALDatasetType * dataset) +{ + std::vector<std::string> ret; + + raii::CharPPCapsule capsule(dataset->GetFileList()); + + std::string files_str=""; + + if(capsule.P()) + { + unsigned int i = 0; + while(capsule.P()[i]!=NULL) + { + ret.push_back(std::string(capsule.P()[i])); + ++i; + } + } + return ret; +} + +bool SyncToDisk(GDALDatasetType * dataset) +{ + dataset->FlushCache(); + + return true; +} + +std::vector<std::string> 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/Applications/AppSegmentation/app/otbSegmentation.cxx b/Modules/Applications/AppSegmentation/app/otbSegmentation.cxx index dfd43a4514a265c352445b999e6fedb307f74487..8015f13648a5a337870a91b3f22f6f47cdcfd6f4 100644 --- a/Modules/Applications/AppSegmentation/app/otbSegmentation.cxx +++ b/Modules/Applications/AppSegmentation/app/otbSegmentation.cxx @@ -675,7 +675,8 @@ private: //and recomputed FID for each features (without holes). //Note : the GetDriver() Method has not been encapsulated in otb::ogr::DataSource, //so we must access the OGR pointer by using .ogr() - std::string driverName(ogrDS->ogr().GetDriver()->GetName()); + + std::string driverName(ogrDS->ogr().GetDriver()->GetName()); if ( driverName.find("ESRI Shapefile") != std::string::npos) { otbAppLogINFO(<<"REPACK the Shapefile ..."<<std::endl); 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/Core/VectorDataBase/include/otbVectorDataKeywordlist.h b/Modules/Core/VectorDataBase/include/otbVectorDataKeywordlist.h index 8b16fb5d70e0b8c069e1562abfe47708a6658bc6..f55622ad5e041cf471703c546bc032e4c78100c9 100644 --- a/Modules/Core/VectorDataBase/include/otbVectorDataKeywordlist.h +++ b/Modules/Core/VectorDataBase/include/otbVectorDataKeywordlist.h @@ -141,8 +141,9 @@ public: /** Constructor */ VectorDataKeywordlist(); + /** Destructor */ - virtual ~VectorDataKeywordlist(); + ~VectorDataKeywordlist(); /** Constructor by copy (deep copy)*/ VectorDataKeywordlist(const Self& other); diff --git a/Modules/Core/VectorDataBase/src/otbVectorDataKeywordlist.cxx b/Modules/Core/VectorDataBase/src/otbVectorDataKeywordlist.cxx index 4404db5e7a58c0a065aa3cd342b61f208df8b275..9b5463d604ad411ba07a2c4dfc6804b91e07d2fc 100644 --- a/Modules/Core/VectorDataBase/src/otbVectorDataKeywordlist.cxx +++ b/Modules/Core/VectorDataBase/src/otbVectorDataKeywordlist.cxx @@ -20,6 +20,8 @@ #include "otbVectorDataKeywordlist.h" +#include "otbConfigure.h" + namespace otb { @@ -88,25 +90,39 @@ VectorDataKeywordlist { if (key.compare(m_FieldList[i].first->GetNameRef()) == 0) { - if (m_FieldList[i].first->GetType() == OFTString) + switch(m_FieldList[i].first->GetType()) + { + case OFTString: { return m_FieldList[i].second.String; } - if (m_FieldList[i].first->GetType() == OFTInteger) + case OFTInteger: { std::ostringstream ss; ss << std::setprecision(15) << m_FieldList[i].second.Integer; return ss.str(); } - if (m_FieldList[i].first->GetType() == OFTReal) +#ifdef OTB_USE_GDAL_20 + case OFTInteger64: + { + std::ostringstream ss; + ss << std::setprecision(15) << m_FieldList[i].second.Integer64; + return ss.str(); + } +#endif + case OFTReal: { std::ostringstream ss; ss << std::setprecision(15) << m_FieldList[i].second.Real; return ss.str(); } - itkExceptionMacro( - << "This type (" << m_FieldList[i].first->GetType() << - ") is not handled (yet) by GetFieldAsString(), please request for it"); + default: + { + itkExceptionMacro( + << "Type of field " << m_FieldList[i].first->GetNameRef() << " (" << m_FieldList[i].first->GetType() << + ") is not handled (yet) by GetFieldAsString(), please request for it"); + } + } } } return ""; @@ -120,24 +136,30 @@ VectorDataKeywordlist { if (key.compare(m_FieldList[i].first->GetNameRef()) == 0) { - if (m_FieldList[i].first->GetType() == OFTInteger) + switch (m_FieldList[i].first->GetType()) + { + case OFTInteger: { return (double)(m_FieldList[i].second.Integer); } - if (m_FieldList[i].first->GetType() == OFTReal) + case OFTReal: { return (double)(m_FieldList[i].second.Real); } - if (m_FieldList[i].first->GetType() == OFTString) + case OFTString: { std::istringstream is(m_FieldList[i].second.String); double value; is >> value; return value; } - itkExceptionMacro( - << "This type (" << m_FieldList[i].first->GetType() << - ") is not handled (yet) by GetFieldAsDouble(), please request for it"); + default: + { + itkExceptionMacro( + << "Type of field " << m_FieldList[i].first->GetNameRef() << " (" << m_FieldList[i].first->GetType() << + ") is not handled (yet) by GetFieldAsDouble(), please request for it"); + } + } } } return 0.; @@ -151,26 +173,47 @@ VectorDataKeywordlist { if (key.compare(m_FieldList[i].first->GetNameRef()) == 0) { - if (m_FieldList[i].first->GetType() == OFTInteger) + switch(m_FieldList[i].first->GetType()) + { + case OFTInteger: { - return (int)(m_FieldList[i].second.Integer); + return (int)(m_FieldList[i].second.Integer); + } +#ifdef OTB_USE_GDAL_20 + // Some fields that were OFTInteger with gdal 1.x are now + // exposed as OFTInteger64. So as to make the old code still + // work with the same data, here we downcast to Integer (if + // and only if no overflow occur). + case OFTInteger64: + { + if(m_FieldList[i].second.Integer64 > itk::NumericTraits<int>::max()) + { + itkExceptionMacro(<<"value "<<m_FieldList[i].second.Integer64<<" of field "<<m_FieldList[i].first->GetNameRef()<<" can not be safely casted to 32 bits integer"); + } + + return static_cast<int>(m_FieldList[i].second.Integer64); } - if (m_FieldList[i].first->GetType() == OFTReal) +#endif + case OFTReal: { return (int)(m_FieldList[i].second.Real); } - if (m_FieldList[i].first->GetType() == OFTString) + case OFTString: { std::istringstream is(m_FieldList[i].second.String); int value; is >> value; return value; } + default: + { itkExceptionMacro( - << "This type (" << m_FieldList[i].first->GetType() << + << "Type of field " << m_FieldList[i].first->GetNameRef() << " (" << m_FieldList[i].first->GetType() << ") is not handled (yet) by GetFieldAsInt(), please request for it"); } - } + } + } + } return 0.; } @@ -383,51 +426,11 @@ VectorDataKeywordlist::CopyFieldList(const Self& kwl) kwl.GetNthField(idx).second.Real); break; } - case OFTIntegerList: - { - std::cerr << "Type not handled for Integer conversion" <<std::endl; - break; - } - case OFTRealList: - { - std::cerr << "Type not handled for Integer conversion"<<std::endl; - break; - } - case OFTStringList: - { - std::cerr << "Type not handled for Integer conversion"<<std::endl; - break; - } - case OFTWideString: - { - std::cerr << "Type not handled for Integer conversion"<<std::endl; - break; - } - case OFTWideStringList: - { - std::cerr << "Type not handled for Integer conversion"<<std::endl; - break; - } - case OFTBinary: - { - std::cerr << "Type not handled for Integer conversion"<<std::endl; - break; - } - case OFTDate: - { - std::cerr << "Type not handled for Integer conversion"<<std::endl; - break; - } - case OFTTime: - { - std::cerr << "Type not handled for Integer conversion"<<std::endl; - break; - } - case OFTDateTime: + default: { std::cerr << "Type not handled for Integer conversion"<<std::endl; break; - } + } } } } @@ -447,21 +450,11 @@ VectorDataKeywordlist output << field.second.Integer; break; } - case OFTIntegerList: - { - output << "Type not handled for printing"; - break; - } case OFTReal: { output << field.second.Real; break; } - case OFTRealList: - { - std::cerr << "Type not handled for printing" << std::endl; - break; - } case OFTString: { if (field.second.String != NULL) @@ -470,26 +463,6 @@ VectorDataKeywordlist } break; } - case OFTStringList: - { - output << "Type not handled for printing"; - break; - } - case OFTWideString: - { - output << "Type not handled for printing"; - break; - } - case OFTWideStringList: - { - output << "Type not handled for printing"; - break; - } - case OFTBinary: - { - output << "Type not handled for printing"; - break; - } case OFTDate: { output << field.second.Date.Year << field.second.Date.Month << field.second.Date.Day; @@ -506,6 +479,16 @@ VectorDataKeywordlist << field.second.Date.Hour << field.second.Date.Minute << field.second.Date.Second; break; } +#ifdef OTB_USE_GDAL_20 + case OFTInteger64: + { + output << std::setprecision(15)<<field.second.Integer64; + break; + } +#endif + default: + output << "Type not handled for printing"; + break; } output << std::endl; return output.str(); @@ -524,21 +507,18 @@ VectorDataKeywordlist outField.second.Integer = field.second.Integer; break; } - case OFTIntegerList: - { - std::cerr << "OGR type not handled" << std::endl; - break; - } +#ifdef OTB_USE_GDAL_20 + case OFTInteger64: + { + outField.second.Integer64 = field.second.Integer64; + break; + } +#endif case OFTReal: { outField.second.Real = field.second.Real; break; } - case OFTRealList: - { - std::cerr << "OGR type not handled" << std::endl; - break; - } case OFTString: { if (field.second.String != NULL) @@ -548,26 +528,6 @@ VectorDataKeywordlist } break; } - case OFTStringList: - { - std::cerr << "OGR type not handled" << std::endl; - break; - } - case OFTWideString: - { - std::cerr << "OGR type not handled" << std::endl; - break; - } - case OFTWideStringList: - { - std::cerr << "OGR type not handled" << std::endl; - break; - } - case OFTBinary: - { - std::cerr << "OGR type not handled" << std::endl; - break; - } case OFTDate: { outField.second.Date.Year = field.second.Date.Year; @@ -592,6 +552,11 @@ VectorDataKeywordlist outField.second.Date.Second = field.second.Date.Second; break; } + default: + { + std::cerr << "OGR type not handled" << std::endl; + break; + } } return outField; } diff --git a/Modules/IO/IOGDAL/include/otbOGRIOHelper.h b/Modules/IO/IOGDAL/include/otbOGRIOHelper.h index baf02bd4ee213ec10f1c7d4c3591afd8d767effe..6df03e24d743bccc4c4c5cb2a2858a1c2bf0c0d4 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, + ogr::version_proxy::GDALDatasetType * m_DataSource, OGRGeometryCollection * ogrCollection, OGRLayer * ogrCurrentLayer, OGRSpatialReference * oSRS); /** Return a list of OGRLayer * */ std::vector<OGRLayer*> ConvertDataTreeNodeToOGRLayers(InternalTreeNodeType * source, - OGRDataSource * dummyDatasource, + ogr::version_proxy::GDALDatasetType * dummyDatasource, OGRLayer* ogrCurrentLayer, OGRSpatialReference * oSRS); diff --git a/Modules/IO/IOGDAL/include/otbOGRVectorDataIO.h b/Modules/IO/IOGDAL/include/otbOGRVectorDataIO.h index f7ae24d3dac2171d7a1ecabc928546a98fa6538a..45f89b75d23d1659d7d201bcaa5633d7e20f345e 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; + ogr::version_proxy::GDALDatasetType * m_DataSource; }; diff --git a/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx b/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx index 4216a78fa1613a42be710769db43a657ac8a25ff..6cc3890ac278ad4b67c9731e58aadea10694e3bb 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, + ogr::version_proxy::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, + ogr::version_proxy::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); + ogr::version_proxy::GDALDriverType * ogrDriver = ogr::version_proxy::GetDriverByName(driverName); + inMemoryDataSource = ogr::version_proxy::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..75da823e3f873c64a8d7b969de141a8acd2bd290 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); + otb::ogr::version_proxy::Close(m_DataSource); } } @@ -54,13 +54,15 @@ OGRVectorDataIO::~OGRVectorDataIO() bool OGRVectorDataIO::CanReadFile(const char* filename) const { - OGRDataSource * poDS = OGRSFDriverRegistrar::Open(filename, FALSE); + otb::ogr::version_proxy::GDALDatasetType * poDS = ogr::version_proxy::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); + ogr::version_proxy::Close(poDS); return true; } @@ -88,10 +90,10 @@ OGRVectorDataIO if (m_DataSource != NULL) { - OGRDataSource::DestroyDataSource(m_DataSource); + ogr::version_proxy::Close(m_DataSource); } - m_DataSource = OGRSFDriverRegistrar::Open(this->m_FileName.c_str(), FALSE); + m_DataSource = ogr::version_proxy::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()); + ogr::version_proxy::GDALDriverType * ogrDriver = + ogr::version_proxy::GetDriverByName(this->GetOGRDriverName(this->m_FileName).data()); if (ogrDriver == NULL) { @@ -219,25 +221,14 @@ 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); + ogr::version_proxy::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); - if (poDS != NULL) - { - //Erase the data if possible - if (poDS->GetDriver()->TestCapability(ODrCDeleteDataSource)) - { - //Delete datasource - poDS->GetDriver()->DeleteDataSource(this->m_FileName.c_str()); - } - } - OGRDataSource::DestroyDataSource(poDS); + ogr::version_proxy::Delete(this->m_FileName.c_str()); // m_DataSource = OGRSFDriverRegistrar::Open(this->m_FileName.c_str(), TRUE); - m_DataSource = ogrDriver->CreateDataSource(this->m_FileName.c_str(), papszOptions); + m_DataSource = ogr::version_proxy::Create(ogrDriver,this->m_FileName.c_str()); // check the created data source if (m_DataSource == NULL) @@ -292,7 +283,7 @@ void OGRVectorDataIO::Write(const itk::DataObject* datag, char ** papszOptions) otbMsgDevMacro( << "layerKept " << layerKept ); (void)layerKept; // keep compiler happy - OGRDataSource::DestroyDataSource(m_DataSource); + otb::ogr::version_proxy::Close(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..bf7996740b24b70b0bf6475636da3616b7f02dd3 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,33 +1069,33 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b /* -------------------------------------------------------------------- */ /* Open data source. */ /* -------------------------------------------------------------------- */ - OGRDataSource *ref_poDS = NULL; - OGRSFDriver * ref_poDriver = NULL; + otb::ogr::version_proxy::GDALDatasetType *ref_poDS = NULL; + otb::ogr::version_proxy::GDALDriverType * ref_poDriver = NULL; //OGRGeometry * ref_poSpatialFilter = NULL; - OGRDataSource *test_poDS = NULL; - OGRSFDriver * test_poDriver = NULL; + otb::ogr::version_proxy::GDALDatasetType *test_poDS = NULL; + otb::ogr::version_proxy::GDALDriverType * test_poDriver = NULL; //OGRGeometry * test_poSpatialFilter = NULL; - OGRRegisterAll(); - - ref_poDS = OGRSFDriverRegistrar::Open(ref_pszDataSource, !bReadOnly, &ref_poDriver); + ref_poDS = otb::ogr::version_proxy::Open(ref_pszDataSource, false); if (ref_poDS == NULL && !bReadOnly) { - ref_poDS = OGRSFDriverRegistrar::Open(ref_pszDataSource, FALSE, &ref_poDriver); + ref_poDS = otb::ogr::version_proxy::Open(ref_pszDataSource, true); + bReadOnly = TRUE; if (ref_poDS != NULL && m_ReportErrors) { - std::cout << "Had to open REF data source read-only.\n"; - bReadOnly = TRUE; + std::cout << "Had to open REF data source read-only."<<std::endl; } } - test_poDS = OGRSFDriverRegistrar::Open(test_pszDataSource, !bReadOnly, &test_poDriver); + test_poDS = otb::ogr::version_proxy::Open(ref_pszDataSource, bReadOnly); if (test_poDS == NULL && !bReadOnly) { - test_poDS = OGRSFDriverRegistrar::Open(test_pszDataSource, FALSE, &test_poDriver); + test_poDS = otb::ogr::version_proxy::Open(ref_pszDataSource, bReadOnly); + + bReadOnly = TRUE; + if (test_poDS != NULL && m_ReportErrors) { - std::cout << "Had to open REF data source read-only.\n"; - bReadOnly = TRUE; + std::cout << "Had to open TEST data source read-only."<<std::endl; } } /* -------------------------------------------------------------------- */ @@ -1108,51 +1103,69 @@ 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 << " -> " << ref_poR->GetDriver(iDriver)->GetName() << std::endl; + std::cout << "FAILURE:\n" "Unable to open REF datasource `" << ref_pszDataSource << "' with the following drivers." << std::endl; + + std::vector<std::string> drivers = ogr::version_proxy::GetAvailableDriversAsStringVector(); + + for (std::vector<std::string>::const_iterator it = drivers.begin();it!=drivers.end();++it) + { + std::cout << " -> " << *it << std::endl; + } } return (1); } + ref_poDriver = ref_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::cout << " -> " << test_poR->GetDriver(iDriver)->GetName() << std::endl; + std::cout << "FAILURE:\n""Unable to open TEST datasource `" << test_pszDataSource << "' with the following drivers." << std::endl; + + std::vector<std::string> drivers = ogr::version_proxy::GetAvailableDriversAsStringVector(); + + for (std::vector<std::string>::const_iterator it = drivers.begin();it!=drivers.end();++it) + { + 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::ogr::version_proxy::GetFileListAsStringVector(ref_poDS); + std::vector<std::string> testFileList = otb::ogr::version_proxy::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 +1253,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 +1269,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 +1387,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/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.h b/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.h index 32e8d621e92aedd98ebfe21bdac826257e191d20..ce68b0f989414ef4aee0971a5e8f17a744664a7f 100644 --- a/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.h +++ b/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.h @@ -27,10 +27,8 @@ #include "gdal.h" #include "gdal_alg.h" - -#include <ogrsf_frmts.h> -#include "ogr_api.h" #include "ogr_srs_api.h" +#include "otbOGRVersionProxy.h" namespace otb { @@ -142,7 +140,7 @@ protected: { if (m_OGRDataSourcePointer != NULL) { - OGRDataSource::DestroyDataSource(m_OGRDataSourcePointer); + ogr::version_proxy::Close(m_OGRDataSourcePointer); } } @@ -154,7 +152,7 @@ private: RasterizeVectorDataFilter(const Self&); //purposely not implemented void operator=(const Self&); //purposely not implemented - OGRDataSource* m_OGRDataSourcePointer; + ogr::version_proxy::GDALDatasetType * m_OGRDataSourcePointer; // Vector Of LayersH std::vector< OGRLayerH > m_SrcDataSetLayers; diff --git a/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.h b/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.h index 0e51efb881d85c78b643c04e36247b88ef8e6114..24aa6e71c6c1a343275e0829cd412c6c7eb74ca8 100644 --- a/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.h +++ b/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.h @@ -26,7 +26,7 @@ #include "gdal.h" #include "ogr_api.h" -#include <ogrsf_frmts.h> +#include "otbOGRVersionProxy.h" namespace otb { @@ -134,7 +134,7 @@ protected: if (m_OGRDataSourcePointer != NULL) { - OGRDataSource::DestroyDataSource(m_OGRDataSourcePointer); + ogr::version_proxy::Close(m_OGRDataSourcePointer); } } @@ -146,7 +146,7 @@ private: VectorDataToLabelImageFilter(const Self&); //purposely not implemented void operator=(const Self&); //purposely not implemented - OGRDataSource* m_OGRDataSourcePointer; + ogr::version_proxy::GDALDatasetType * m_OGRDataSourcePointer; // Vector Of OGRGeometyH std::vector< OGRGeometryH > m_SrcDataSetGeometries; 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)