diff --git a/Modules/Adapters/GdalAdapters/include/otbOGRHelpers.h b/Modules/Adapters/GdalAdapters/include/otbOGRHelpers.h index e693c942805efa8abfaed49e6cf12c77240e86a2..c490e9ce62204b80440594294c129ccaf5a9691b 100644 --- a/Modules/Adapters/GdalAdapters/include/otbOGRHelpers.h +++ b/Modules/Adapters/GdalAdapters/include/otbOGRHelpers.h @@ -27,6 +27,7 @@ #include <cassert> #include <boost/range/begin.hpp> #include <boost/range/end.hpp> +#include "ogr_feature.h" #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-local-typedefs" @@ -43,6 +44,8 @@ #include "OTBGdalAdaptersExport.h" +class GDALDataset; + namespace otb { namespace ogr @@ -105,6 +108,35 @@ private: std::vector<char const*> m_raw; }; +/** + * Return the list of available drivers. + * + * \return A vector of string containing the list of available drivers. +*/ +OTBGdalAdapters_EXPORT +std::vector<std::string> GetAvailableDriversAsStringVector(); + +/** + * Return the list of files composing the dataset. + * + * \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. +*/ + +OTBGdalAdapters_EXPORT +std::vector<std::string> GetFileListAsStringVector(GDALDataset * dataset); + +/** + * Returns true if the field 'index' is set and not-null in the given feature + * + * Before gdal 2.2, it calls OGRFeature::IsFieldSet(). + * After gdal 2.2, it calls OGRFeature::IsFieldSetAndNotNull() +*/ +OTBGdalAdapters_EXPORT +bool IsFieldSetAndNotNull(OGRFeature *feat, int index); + } // ogr namespace } // end namespace otb diff --git a/Modules/Adapters/GdalAdapters/include/otbOGRVersionProxy.h b/Modules/Adapters/GdalAdapters/include/otbOGRVersionProxy.h index d26689c360b066a01c3e4c4b2cd8f0bbead00b44..5021d43978abce232eea6580e5d2526516fe8f8f 100644 --- a/Modules/Adapters/GdalAdapters/include/otbOGRVersionProxy.h +++ b/Modules/Adapters/GdalAdapters/include/otbOGRVersionProxy.h @@ -76,30 +76,6 @@ namespace version_proxy * * \return A vector of string containing the list of files. */ - - OTBGdalAdapters_EXPORT - std::vector<std::string> GetFileListAsStringVector(GDALDataset * 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. - */ - OTBGdalAdapters_EXPORT - std::vector<std::string> GetAvailableDriversAsStringVector(); - - /** - * Returns true if the field 'index' is set and not-null in the given feature - * - * Before gdal 2.2, it calls OGRFeature::IsFieldSet(). - * After gdal 2.2, it calls OGRFeature::IsFieldSetAndNotNull() - */ - OTBGdalAdapters_EXPORT - bool IsFieldSetAndNotNull(OGRFeature *feat, int index); } } diff --git a/Modules/Adapters/GdalAdapters/src/CMakeLists.txt b/Modules/Adapters/GdalAdapters/src/CMakeLists.txt index 0f73c07a03e044b6c89d6a19b5df6fc6b9ae4851..a93505302f0b4ff3d4a62d5e986bbe0d6dff60ba 100644 --- a/Modules/Adapters/GdalAdapters/src/CMakeLists.txt +++ b/Modules/Adapters/GdalAdapters/src/CMakeLists.txt @@ -30,6 +30,7 @@ set(OTBGdalAdapters_SRC otbOGRDataSourceWrapper.cxx otbOGRVersionProxy.cxx otbOGRExtendedFilenameToOptions.cxx + otbOGRHelpers.cxx ) add_library(OTBGdalAdapters ${OTBGdalAdapters_SRC}) diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx index 966d975634999c1b894e8b0d29a68d1ed2a81fdc..10bcbbcd09983fad362a90efe28ea4c66750be5d 100644 --- a/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx +++ b/Modules/Adapters/GdalAdapters/src/otbOGRDataSourceWrapper.cxx @@ -829,7 +829,7 @@ void otb::ogr::DataSource::SyncToDisk() std::string otb::ogr::DataSource::GetDatasetDescription() const { std::vector<std::string> files = - otb::ogr::version_proxy::GetFileListAsStringVector( m_DataSource ); + otb::ogr::GetFileListAsStringVector( m_DataSource ); std::string description = ""; for( std::vector<std::string>::const_iterator it = files.begin() ; it!=files.end() ; ++it ) diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRFieldWrapper.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRFieldWrapper.cxx index 49f95f053546c6f59190ef88ae6d28cee0f19110..7e12f398ba63b8c79bcb3848bffb68aacc138b39 100644 --- a/Modules/Adapters/GdalAdapters/src/otbOGRFieldWrapper.cxx +++ b/Modules/Adapters/GdalAdapters/src/otbOGRFieldWrapper.cxx @@ -167,7 +167,7 @@ std::ostream & otb::ogr::Field::UncheckedPrintSelf( bool otb::ogr::Field::UncheckedHasBeenSet() const { - return otb::ogr::version_proxy::IsFieldSetAndNotNull(m_Feature.get(), m_index); + return otb::ogr::IsFieldSetAndNotNull(m_Feature.get(), m_index); } void otb::ogr::Field::UncheckedUnset() const diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRHelpers.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRHelpers.cxx new file mode 100644 index 0000000000000000000000000000000000000000..8bb2e064520c19b355cf76f6f4d787b0ff5e8031 --- /dev/null +++ b/Modules/Adapters/GdalAdapters/src/otbOGRHelpers.cxx @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES) + * + * This file is part of Orfeo Toolbox + * + * https://www.orfeo-toolbox.org/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "otbOGRHelpers.h" + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#include "gdal_priv.h" +#include "ogr_feature.h" +#pragma GCC diagnostic pop +#else +#include "gdal_priv.h" +#include "ogr_feature.h" +#endif // __GNUC__ || __clang__ + +namespace otb +{ +namespace ogr +{ + +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; +} + +namespace raii +{ +// This class is used in the next function, so as to prevent any +// resource 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(GDALDataset * 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; +} + +/*----------------------[GDAL 2.2 change on IsFieldSet()]---------------------*/ +bool IsFieldSetAndNotNull(OGRFeature *feat, int index) +{ +#if GDAL_VERSION_NUM<2020000 + return feat->IsFieldSet(index); +#else + return feat->IsFieldSetAndNotNull(index); +#endif +} + +} +} // end namespaces \ No newline at end of file diff --git a/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy.cxx b/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy.cxx index 7c4b1e44e25060aa9342b857877114c654b16420..479ecaf705ea6e4c35dd07f1a03052a21a27dda2 100644 --- a/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy.cxx +++ b/Modules/Adapters/GdalAdapters/src/otbOGRVersionProxy.cxx @@ -42,96 +42,12 @@ #include "ogr_feature.h" #endif // __GNUC__ || __clang__ -namespace otb +namespace otbscon { namespace ogr { namespace version_proxy { - -#if GDAL_VERSION_NUM>=2000000 -namespace raii -{ -// This class is used in the next function, so as to prevent any -// resource 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; -}; -} -#endif - -std::vector<std::string> GetFileListAsStringVector(GDALDataset * dataset) -{ - std::vector<std::string> ret; -#if GDAL_VERSION_NUM<2000000 - ret.push_back(std::string(dataset->GetName())); -#else - 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; - } - } -#endif - return ret; -} - -std::vector<std::string> GetAvailableDriversAsStringVector() -{ - std::vector<std::string> ret; -#if GDAL_VERSION_NUM<2000000 - int nbDrivers = OGRSFDriverRegistrar::GetRegistrar()->GetDriverCount(); - - for(int i = 0; i < nbDrivers;++i) - { - ret.push_back(OGRSFDriverRegistrar::GetRegistrar()->GetDriver(i)->GetName()); - } -#else - int nbDrivers = GetGDALDriverManager()->GetDriverCount(); - - for(int i = 0; i < nbDrivers;++i) - { - ret.push_back(GDALGetDriverShortName(GetGDALDriverManager()->GetDriver(i))); - } -#endif - return ret; -} - -/*----------------------[GDAL 2.2 change on IsFieldSet()]---------------------*/ -bool IsFieldSetAndNotNull(OGRFeature *feat, int index) -{ -#if GDAL_VERSION_NUM<2020000 - return feat->IsFieldSet(index); -#else - return feat->IsFieldSetAndNotNull(index); -#endif -} - } } } // end namespace diff --git a/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx b/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx index 2f3ddebb4aff8043b7d818e1f89b0e25ede1e62c..0bf6f0644d9536a302cc54d191e4de4bf27d90fa 100644 --- a/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx +++ b/Modules/IO/IOGDAL/src/otbOGRIOHelper.cxx @@ -209,7 +209,7 @@ void OGRIOHelper otb::VectorDataKeywordlist kwl; for (int fieldNum = 0; fieldNum < feature->GetFieldCount(); ++fieldNum) { - if (ogr::version_proxy::IsFieldSetAndNotNull(feature, fieldNum)) + if (ogr::IsFieldSetAndNotNull(feature, fieldNum)) { kwl.AddField(feature->GetFieldDefnRef(fieldNum), feature->GetRawFieldRef(fieldNum)); } diff --git a/Modules/IO/TestKernel/src/otbTestHelper.cxx b/Modules/IO/TestKernel/src/otbTestHelper.cxx index 310d9a10f1accea2db4899878b18fbf869c6b1bd..8fa2f19aa5a51b3ccdc33608a5ea8c0bef7185bb 100644 --- a/Modules/IO/TestKernel/src/otbTestHelper.cxx +++ b/Modules/IO/TestKernel/src/otbTestHelper.cxx @@ -49,6 +49,7 @@ #include "otbPrintableImageFilter.h" #include "otbStreamingShrinkImageFilter.h" #include "otbOGRVersionProxy.h" +#include "otbOGRHelpers.h" #include "otbConfigure.h" @@ -1786,7 +1787,7 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b { 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(); + std::vector<std::string> drivers = ogr::GetAvailableDriversAsStringVector(); for (std::vector<std::string>::const_iterator it = drivers.begin();it!=drivers.end();++it) { @@ -1804,7 +1805,7 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b { 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(); + std::vector<std::string> drivers = ogr::GetAvailableDriversAsStringVector(); for (std::vector<std::string>::const_iterator it = drivers.begin();it!=drivers.end();++it) { @@ -1823,8 +1824,8 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b // 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); + std::vector<std::string> refFileList = otb::ogr::GetFileListAsStringVector(ref_poDS); + std::vector<std::string> testFileList = otb::ogr::GetFileListAsStringVector(test_poDS); unsigned int fileId = 0; @@ -1961,7 +1962,7 @@ void TestHelper::DumpOGRFeature(FILE* fpOut, OGRFeature* feature, char** papszOp poFDefn->GetNameRef(), OGRFieldDefn::GetFieldTypeName(poFDefn->GetType())); - if (ogr::version_proxy::IsFieldSetAndNotNull(feature, iField)) fprintf(fpOut, "%s\n", feature->GetFieldAsString(iField)); + if (ogr::IsFieldSetAndNotNull(feature, iField)) fprintf(fpOut, "%s\n", feature->GetFieldAsString(iField)); else fprintf(fpOut, "(null)\n"); }