diff --git a/Modules/Core/Metadata/include/otbImageMetadata.h b/Modules/Core/Metadata/include/otbImageMetadata.h index ac8b9f2c574015598aef13d4e1d7a0e4350b7636..e951bab05ea154f68898367efd78843058f3d57d 100644 --- a/Modules/Core/Metadata/include/otbImageMetadata.h +++ b/Modules/Core/Metadata/include/otbImageMetadata.h @@ -224,7 +224,7 @@ public: std::string ToJSON(bool multiline=false) const; /** Import metadata from a string keywordlist. - * Will skip MDGeom::SensorGeometry). + * Will skip MDGeom::SensorGeometry. * Returns True if all keywords were parsed correctly. */ bool FromKeywordlist(const Keywordlist&); diff --git a/Modules/IO/IOGDAL/include/otbGDALImageIO.h b/Modules/IO/IOGDAL/include/otbGDALImageIO.h index c199eb751f2fd8ed589e6a9e97aec8e88551b060..0266c2bd9bb2d2f4748239c03416a566d00501c1 100644 --- a/Modules/IO/IOGDAL/include/otbGDALImageIO.h +++ b/Modules/IO/IOGDAL/include/otbGDALImageIO.h @@ -228,6 +228,11 @@ protected: /** Destructor.*/ ~GDALImageIO() override; + /** Set the metadata from a Keywordlist*/ + void KeywordlistToMetadata(ImageMetadataBase::Keywordlist, int band=-1); + /** Parses a GDAL Metadata string list to fill a Keywordlist*/ + void GDALMetadataToKeywordlist(const char* const* , ImageMetadataBase::Keywordlist &); + void PrintSelf(std::ostream& os, itk::Indent indent) const override; /** Read all information on the image*/ void InternalReadImageInformation(); diff --git a/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx b/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx index 0a6ad605b4d6d79fade494c70bb11f3ecce316dd..b93d8900679da2115ecbdeec855e307bc2e26e22 100644 --- a/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx +++ b/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx @@ -1830,26 +1830,15 @@ void GDALImageIO::ExportMetadata() // be prefixed by: MDGeomNames[MDGeom::SensorGeometry] + '.' ImageMetadataBase::Keywordlist kwl; m_Imd.ToKeywordlist(kwl); - for (const auto& kv : kwl) - { - SetAs(kv.first, kv.second); - } + KeywordlistToMetadata(kwl); int bIdx = 0; for (const auto& band : m_Imd.Bands) - { + { band.ToKeywordlist(kwl); - if (band.Has(MDNum::NoData)) - { - // remove from kwl as it is already handled before - kwl.erase(MetaData::MDNumNames.left.at(MDNum::NoData)); - } - for (const auto& kv : kwl) - { - SetAs(kv.first, kv.second, bIdx); - } + KeywordlistToMetadata(kwl, bIdx); ++bIdx; - } + } } void GDALImageIO::ImportMetadata() @@ -1859,6 +1848,80 @@ void GDALImageIO::ImportMetadata() // Keys Starting with: MDGeomNames[MDGeom::SensorGeometry] + '.' should // be decoded by the (future) SensorModelFactory. // Use ImageMetadataBase::FromKeywordlist to ingest the metadata + if (std::string(GetMetadataValue("METADATATYPE")) != "OTB") + return; + ImageMetadataBase::Keywordlist kwl; + GDALMetadataToKeywordlist(m_Dataset->GetDataSet()->GetMetadata(), kwl); + m_Imd.FromKeywordlist(kwl); + for (int band = 0 ; band < m_NbBands ; ++band) + { + kwl.clear(); + GDALMetadataToKeywordlist(m_Dataset->GetDataSet()->GetRasterBand(band+1)->GetMetadata(), kwl); + m_Imd.Bands[band].FromKeywordlist(kwl); + } +} + +void GDALImageIO::KeywordlistToMetadata(ImageMetadataBase::Keywordlist kwl, int band) +{ + for (const auto& kv : kwl) + { + if (kv.first == MetaData::MDGeomNames.left.at(MDGeom::SensorGeometry)) + { + SetMetadataValue("MDGeomNames[MDGeom::SensorGeometry].", kv.second.c_str(), band); + } + SetMetadataValue(kv.first.c_str(), kv.second.c_str(), band); + } +} + +void GDALImageIO::GDALMetadataToKeywordlist(const char* const* metadataList, ImageMetadataBase::Keywordlist &kwl) +{ + // The GDAL metadata string list is formatted as a “Name=value” list with the last pointer value being NULL. + for ( ; *metadataList != nullptr ; ++metadataList ) + { + std::string metadataLine = std::string(*metadataList); + // The key and the value are separated by the '=' symbol + std::string::size_type pos = metadataLine.find('='); + std::string fieldName = metadataLine.substr(0, pos); + std::string fieldValue = metadataLine.substr(pos+1); + if((fieldName.size() > 36) && (fieldName.substr(0, 36) == "MDGeomNames[MDGeom::SensorGeometry].")) + { + // TODO: Keys Starting with: MDGeomNames[MDGeom::SensorGeometry] + '.' should + // be decoded by the (future) SensorModelFactory. + } + else if (fieldName == MetaData::MDGeomNames.left.at(MDGeom::RPC)) + { + Projection::RPCParam rpcStruct; + rpcStruct.LineOffset = this->GetAs<double>("RPC/LINE_OFF"); + rpcStruct.SampleOffset = this->GetAs<double>("RPC/SAMP_OFF"); + rpcStruct.LatOffset = this->GetAs<double>("RPC/LAT_OFF"); + rpcStruct.LonOffset = this->GetAs<double>("RPC/LONG_OFF"); + rpcStruct.HeightOffset = this->GetAs<double>("RPC/HEIGHT_OFF"); + + rpcStruct.LineScale = this->GetAs<double>("RPC/LINE_SCALE"); + rpcStruct.SampleScale = this->GetAs<double>("RPC/SAMP_SCALE"); + rpcStruct.LatScale = this->GetAs<double>("RPC/LAT_SCALE"); + rpcStruct.LonScale = this->GetAs<double>("RPC/LONG_SCALE"); + rpcStruct.HeightScale = this->GetAs<double>("RPC/HEIGHT_SCALE"); + + std::vector<double> coeffs(20); + + coeffs = this->GetAsVector<double>("RPC/LINE_NUM_COEFF",' ',20); + std::copy(coeffs.begin(), coeffs.end(), rpcStruct.LineNum); + + coeffs = this->GetAsVector<double>("RPC/LINE_DEN_COEFF",' ',20); + std::copy(coeffs.begin(), coeffs.end(), rpcStruct.LineDen); + + coeffs = this->GetAsVector<double>("RPC/SAMP_NUM_COEFF",' ',20); + std::copy(coeffs.begin(), coeffs.end(), rpcStruct.SampleNum); + + coeffs = this->GetAsVector<double>("RPC/SAMP_DEN_COEFF",' ',20); + std::copy(coeffs.begin(), coeffs.end(), rpcStruct.SampleDen); + + m_Imd.Add(MDGeom::RPC, rpcStruct); + } + else + kwl.emplace(fieldName, fieldValue); + } } diff --git a/Modules/IO/IOGDAL/test/CMakeLists.txt b/Modules/IO/IOGDAL/test/CMakeLists.txt index ca4b3b01a385a691a1207e2b142b0870f17f56c3..2504e7180e63aced816277e701a9c7db0b99166d 100644 --- a/Modules/IO/IOGDAL/test/CMakeLists.txt +++ b/Modules/IO/IOGDAL/test/CMakeLists.txt @@ -31,6 +31,7 @@ otbGDALReadPxlComplex.cxx otbGDALImageIOTestCanRead.cxx otbMultiDatasetReadingInfo.cxx otbOGRVectorDataIOCanRead.cxx +otbGDALImageIOImportExportMetadata.cxx ) add_executable(otbIOGDALTestDriver ${OTBIOGDALTests}) @@ -128,6 +129,15 @@ otb_add_test(NAME ioTvGDALImageIOWriteMetadata COMMAND otbIOGDALTestDriver ${TEMP}/ioTvGDALImageIOWriteMetadata.hdr ${TEMP}/ioTvGDALImageIOWriteMetadata_Report.txt ) + +otb_add_test(NAME ioTvGDALImageIOImportExportMetadata_JPEG_99 COMMAND otbIOGDALTestDriver + --compare-ascii ${NOTOL} ${BASELINE_FILES}/ioTvGDALImageIOImportExportMetadata_JPEG_99.txt + ${TEMP}/ioTvGDALImageIOImportExportMetadata_JPEG_99.txt + otbGDALImageIOImportExportMetadata + ${INPUTDATA}/maur_rgb_24bpp.tif + ${TEMP}/ioTvGDALImageIOImportExportMetadata_JPEG_99.tif + ${TEMP}/ioTvGDALImageIOImportExportMetadata_JPEG_99.txt + ) otb_add_test(NAME ioTvGDALOverviewsBuilder_TIFF COMMAND otbIOGDALTestDriver otbGDALOverviewsBuilder diff --git a/Modules/IO/IOGDAL/test/otbGDALImageIOImportExportMetadata.cxx b/Modules/IO/IOGDAL/test/otbGDALImageIOImportExportMetadata.cxx new file mode 100644 index 0000000000000000000000000000000000000000..058af112b086ca62d592c673ca018f192c047898 --- /dev/null +++ b/Modules/IO/IOGDAL/test/otbGDALImageIOImportExportMetadata.cxx @@ -0,0 +1,53 @@ +/* + * 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 "itkMacro.h" +#include <iostream> + +#include "otbGDALImageIO.h" + +//struct sGDALImageIO : public GDALImageIO +//{ +// +//}; + +int otbGDALImageIOImportExportMetadata(int itkNotUsed(argc), char* argv[]) +{ + // Verify the number of parameters in the command line + const char* inputFilename = argv[1]; + const char* outputFilename = argv[2]; + const char* outputTextFilename = argv[3]; + + std::ofstream outfile(outputTextFilename); + +// otb::sGDALImageIO::Pointer io = otb::sGDALImageIO::New(); +// io->CanReadFile(inputFilename); +// io->ImportMetadata(); +// outfile << io->m_Imd; +// +// otb::sGDALImageIO::Pointer io2 = otb::sGDALImageIO::New(); +// io2.CarWriteFile(outputFilename); +// io2->m_Imd = io->m_Imd; +// io2.ExportMetadata(); + return 1; + + return EXIT_SUCCESS; +} diff --git a/Modules/IO/IOGDAL/test/otbIOGDALTestDriver.cxx b/Modules/IO/IOGDAL/test/otbIOGDALTestDriver.cxx index 04dd3d2846837d4296ca9106c21f52f1caa67c48..5458ac35a60dc97dea80a6f36e8b91db43066c6f 100644 --- a/Modules/IO/IOGDAL/test/otbIOGDALTestDriver.cxx +++ b/Modules/IO/IOGDAL/test/otbIOGDALTestDriver.cxx @@ -33,4 +33,5 @@ void RegisterTests() REGISTER_TEST(otbGDALImageIOTestCanRead); REGISTER_TEST(otbMultiDatasetReadingInfo); REGISTER_TEST(otbOGRVectorDataIOTestCanRead); + REGISTER_TEST(otbGDALImageIOImportExportMetadata); }