From e221f4572e40ced39df53f182c451df4aedcbffb Mon Sep 17 00:00:00 2001 From: Julien Osman <julien.osman@csgroup.eu> Date: Thu, 18 Jun 2020 10:37:15 +0200 Subject: [PATCH] ENH: Add new XMLMetadataSupplier with corresponding test --- .../Files/ioTuotbXMLMetadataSupplierTest.txt | 3 + .../Metadata/include/otbXMLMetadataSupplier.h | 77 +++++++++++++++++++ Modules/Core/Metadata/src/CMakeLists.txt | 2 + .../Metadata/src/otbXMLMetadataSupplier.cxx | 74 ++++++++++++++++++ Modules/Core/Metadata/test/CMakeLists.txt | 9 +++ .../Metadata/test/otbMetadataTestDriver.cxx | 1 + .../test/otbXMLMetadataSupplierTest.cxx | 37 +++++++++ 7 files changed, 203 insertions(+) create mode 100644 Data/Baseline/OTB/Files/ioTuotbXMLMetadataSupplierTest.txt create mode 100644 Modules/Core/Metadata/include/otbXMLMetadataSupplier.h create mode 100644 Modules/Core/Metadata/src/otbXMLMetadataSupplier.cxx create mode 100644 Modules/Core/Metadata/test/otbXMLMetadataSupplierTest.cxx diff --git a/Data/Baseline/OTB/Files/ioTuotbXMLMetadataSupplierTest.txt b/Data/Baseline/OTB/Files/ioTuotbXMLMetadataSupplierTest.txt new file mode 100644 index 0000000000..085afb8795 --- /dev/null +++ b/Data/Baseline/OTB/Files/ioTuotbXMLMetadataSupplierTest.txt @@ -0,0 +1,3 @@ +IW +670 + diff --git a/Modules/Core/Metadata/include/otbXMLMetadataSupplier.h b/Modules/Core/Metadata/include/otbXMLMetadataSupplier.h new file mode 100644 index 0000000000..a63db7aba3 --- /dev/null +++ b/Modules/Core/Metadata/include/otbXMLMetadataSupplier.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2005-2020 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. + */ + +#ifndef otbXMLMetadataSupplier_h +#define otbXMLMetadataSupplier_h + +#include <gdal_mdreader.h> +#include "OTBMetadataExport.h" +//#include "otb_tinyxml.h" +#include "otbMetadataSupplierInterface.h" +#include "otbStringUtilities.h" + + +namespace otb +{ + +/** \class XMLMetadataSupplier + * + * \brief Class to access metadata information in a XML file + * + * \ingroup OTBMetadata + */ +class OTBMetadata_EXPORT XMLMetadataSupplier + : public MetadataSupplierInterface, + public GDALMDReaderBase +{ +public: + XMLMetadataSupplier(const std::string &); + + /** Get the metadata value corresponding to a given path + * Returns NULL when path is not found + * If band >= 0, the metadata value is looked in the specified band*/ + const char * GetMetadataValue(const char * path, int band=1) const override; + + std::string GetResourceFile() const override; + + /** + * @brief Determine whether the input parameter correspond to the particular + * provider of remote sensing data completely + * @return True if all needed sources files found + */ + bool HasRequiredFiles() const override; + + /** + * @brief Get metadata file names. The caller become owner of returned list + * and have to free it via CSLDestroy. + * @return A file name list + */ + char** GetMetadataFiles() const override; + +private: + /** List of resource files */ + std::string m_FileName; + /** Dictionary containing the metadata */ + char** m_MetadataDic = nullptr; +}; + +} // end namespace otb + +#endif diff --git a/Modules/Core/Metadata/src/CMakeLists.txt b/Modules/Core/Metadata/src/CMakeLists.txt index 3444d8babf..9dfb9ee098 100644 --- a/Modules/Core/Metadata/src/CMakeLists.txt +++ b/Modules/Core/Metadata/src/CMakeLists.txt @@ -71,6 +71,8 @@ set(OTBMetadata_SRC otbImageMetadata.cxx otbGeometryMetadata.cxx + + otbXMLMetadataSupplier.cxx ) add_library(OTBMetadata ${OTBMetadata_SRC}) diff --git a/Modules/Core/Metadata/src/otbXMLMetadataSupplier.cxx b/Modules/Core/Metadata/src/otbXMLMetadataSupplier.cxx new file mode 100644 index 0000000000..dd216a7754 --- /dev/null +++ b/Modules/Core/Metadata/src/otbXMLMetadataSupplier.cxx @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2005-2020 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 "otbXMLMetadataSupplier.h" + +namespace otb +{ +XMLMetadataSupplier::XMLMetadataSupplier(const std::string & fileName) + : GDALMDReaderBase(nullptr, nullptr), m_FileName(fileName) +{ + CPLXMLNode* psNode = CPLParseXMLFile(m_FileName.c_str()); + if(psNode != nullptr) + { + //if(std::string(psNode->pszValue) == "?xml") +// if(EQUAL(psNode->pszValue, "?xml")) +// psNode = psNode->psNext; + m_MetadataDic = ReadXMLToList(psNode, m_MetadataDic); + } + else + { + otbLogMacro(Warning, <<"Unable to parse XML file " << fileName); + } + CPLDestroyXMLNode(psNode); +} + +const char * XMLMetadataSupplier::GetMetadataValue(const char * path, int band) const +{ + char** fetched; + fetched = CSLFetchNameValueMultiple(m_MetadataDic, path); + if (fetched == nullptr) + return nullptr; + std::ostringstream oss; + oss << *fetched; + ++fetched; + for ( ; *fetched != nullptr ; ++fetched ) + oss << " " << *fetched; + return oss.str().c_str(); +} + +std::string XMLMetadataSupplier::GetResourceFile() const +{ + return m_FileName; +} + +bool XMLMetadataSupplier::HasRequiredFiles() const +{ + return false; // TODO +} + +char** XMLMetadataSupplier::GetMetadataFiles() const +{ + char ** todo; + return todo; // TODO +} + + +} // end namespace otb diff --git a/Modules/Core/Metadata/test/CMakeLists.txt b/Modules/Core/Metadata/test/CMakeLists.txt index fbc08b5d25..94ccaaf17e 100644 --- a/Modules/Core/Metadata/test/CMakeLists.txt +++ b/Modules/Core/Metadata/test/CMakeLists.txt @@ -32,6 +32,7 @@ otbImageMetadataInterfaceTest2.cxx otbNoDataHelperTest.cxx otbSarCalibrationLookupDataTest.cxx otbImageMetadataTest.cxx +otbXMLMetadataSupplierTest.cxx ) add_executable(otbMetadataTestDriver ${OTBMetadataTests}) @@ -397,4 +398,12 @@ otb_add_test(NAME ioTuImageMetadataToFromKeywordlistTest COMMAND otbMetadataTest otbImageMetadataToFromKeywordlistTest ${TEMP}/ioTuImageMetadataToFromKeywordlistTest.txt ) + +otb_add_test(NAME ioTuotbXMLMetadataSupplierTest COMMAND otbMetadataTestDriver + --compare-ascii ${NOTOL} ${BASELINE_FILES}/ioTuotbXMLMetadataSupplierTest.txt + ${TEMP}/ioTuotbXMLMetadataSupplierTest.txt + otbXMLMetadataSupplierTest + /home/julien/Bureau/S1Product/S1A_IW_GRDH_1SDV_20170819T001029_20170819T001054_017985_01E2E8_F302.SAFE/annotation/calibration/calibration-s1a-iw-grd-vh-20170819t001029-20170819t001054-017985-01e2e8-002.xml + ${TEMP}/ioTuotbXMLMetadataSupplierTest.txt + ) \ No newline at end of file diff --git a/Modules/Core/Metadata/test/otbMetadataTestDriver.cxx b/Modules/Core/Metadata/test/otbMetadataTestDriver.cxx index 8dc9776459..899b0dfc33 100644 --- a/Modules/Core/Metadata/test/otbMetadataTestDriver.cxx +++ b/Modules/Core/Metadata/test/otbMetadataTestDriver.cxx @@ -32,4 +32,5 @@ void RegisterTests() REGISTER_TEST(otbNoDataHelperTest); REGISTER_TEST(otbSarCalibrationLookupDataTest); REGISTER_TEST(otbImageMetadataTest); + REGISTER_TEST(otbXMLMetadataSupplierTest); } diff --git a/Modules/Core/Metadata/test/otbXMLMetadataSupplierTest.cxx b/Modules/Core/Metadata/test/otbXMLMetadataSupplierTest.cxx new file mode 100644 index 0000000000..bae5008e4f --- /dev/null +++ b/Modules/Core/Metadata/test/otbXMLMetadataSupplierTest.cxx @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2020 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 "otbXMLMetadataSupplier.h" + +int otbXMLMetadataSupplierTest(int itkNotUsed(argc), char* argv[]) +{ + // Verify the number of parameters in the command line + const char* fileName = argv[1]; + const char* outputFilename = argv[2]; + otb::XMLMetadataSupplier mds(fileName); + + std::ofstream file; + file.open(outputFilename); + file << mds.GetMetadataValue("calibration.adsHeader.swath") << "\n"; + file << mds.GetMetadataValue("calibration.calibrationVectorList.calibrationVector_2.line") << "\n"; + file.close(); + + return EXIT_SUCCESS; +} -- GitLab