OTB  9.0.0
Orfeo Toolbox
otbDimapMetadataHelper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbMetadataDataHelper_h
22 #define otbMetadataDataHelper_h
23 
24 #include "OTBMetadataExport.h"
26 
27 namespace otb
28 {
29 
36 struct DimapData
37 {
38  std::string mission;
39  std::string missionIndex;
40 
41  std::string ImageID;
42  std::string ProductionDate;
43  std::string AcquisitionDate;
44  std::string Instrument;
45  std::string InstrumentIndex;
46  std::string ProcessingLevel;
47  std::string SpectralProcessing;
48 
49  std::vector<std::string> BandIDs;
50 
51  std::vector<double> SunAzimuth;
52  std::vector<double> SunElevation;
53  std::vector<double> IncidenceAngle;
54  std::vector<double> ViewingAngle;
55  std::vector<double> AzimuthAngle;
56  std::vector<double> SceneOrientation;
57 
58  std::vector<double> PhysicalBias;
59  std::vector<double> PhysicalGain;
60 
61  std::vector<double> SolarIrradiance;
62 
63  // Optional
64  std::vector<double> AlongTrackIncidenceAngle;
65  std::vector<double> AcrossTrackIncidenceAngle;
66  std::vector<double> AlongTrackViewingAngle;
67  std::vector<double> AcrossTrackViewingAngle;
68  int StepCount;
69  std::string softwareVersion;
70  double SatAzimuth;
71 
72  // phr sensor characteristics
73  std::string TimeRangeStart;
74  std::string TimeRangeEnd;
75  std::string LinePeriod;
76  std::string SwathFirstCol;
77  std::string SwathLastCol;
78 };
79 
87 class OTBMetadata_EXPORT DimapMetadataHelper
88 {
89 public:
90 
91  DimapMetadataHelper() = default;
92 
93  const DimapData & GetDimapData() const
94  {
95  return m_Data;
96  }
97 
98 
100  void ParseGeom(const MetadataSupplierInterface & mds);
101 
103  void ParseDimapV1(const MetadataSupplierInterface & mds, const std::string prefix);
104 
106  void ParseDimapV2(const MetadataSupplierInterface & mds, const std::string & prefix = "Dimap_Document.");
107 
108 protected:
109 
110 private:
111 
112  template <class T>
114  const std::string & prefix,
115  const std::string & name,
116  std::vector<T> & dest)
117  {
118  dest.clear();
119 
120  std::vector<std::string> mdStr;
121 
122  bool hasValue = false;
123  auto value = mds.GetMetadataValue(prefix + "." + name, hasValue);
124 
125  if (hasValue)
126  {
127  mdStr.push_back(value);
128  }
129  else
130  {
131  hasValue = true;
132  unsigned int i = 1;
133  while(hasValue)
134  {
135  value = mds.GetMetadataValue(prefix + "_" + std::to_string(i) + "." + name, hasValue);
136  if (hasValue)
137  mdStr.push_back(value);
138  i++;
139  }
140  }
141 
142  if (mdStr.empty())
143  {
145  <<"Missing metadata '"<< prefix + name <<"'")
146  }
147 
148  for (const auto & elem : mdStr)
149  {
150  try
151  {
152  dest.push_back(boost::lexical_cast<T>(elem));
153  }
154  catch (boost::bad_lexical_cast&)
155  {
156  otbGenericExceptionMacro(MissingMetadataException,<<"Bad metadata value for '"<<prefix + name<<"', got: "<<elem)
157  }
158  }
159 
160  //dest.push_back( );
161  }
162 
163  // Non throwing version
164  template <class T>
166  const std::string & prefix,
167  const std::string & name,
168  std::vector<T> & dest,
169  std::vector<T> & defaultValue)
170  {
171  //TODO factorize
172  dest.clear();
173 
174  std::vector<std::string> mdStr;
175 
176  bool hasValue = false;
177  auto value = mds.GetMetadataValue(prefix + "." + name, hasValue);
178 
179  if (hasValue)
180  {
181  mdStr.push_back(value);
182  }
183  else
184  {
185  hasValue = true;
186  unsigned int i = 1;
187  while(hasValue)
188  {
189  value = mds.GetMetadataValue(prefix + "_" + std::to_string(i) + "." + name, hasValue);
190  if (hasValue)
191  mdStr.push_back(value);
192  i++;
193  }
194  }
195 
196  if (mdStr.empty())
197  {
198  dest = defaultValue;
199  }
200 
201  for (const auto & elem : mdStr)
202  {
203  try
204  {
205  dest.push_back(boost::lexical_cast<T>(elem));
206  }
207  catch (boost::bad_lexical_cast&)
208  {
209  dest = defaultValue;
210  }
211  }
212  }
213 
214  template <class T>
216  const std::string & prefix,
217  const std::string & name)
218  {
219  std::vector<T> vector;
220  ParseVector(mds, prefix, name, vector);
221  return vector[0];
222 
223  }
224 
226 };
227 
228 
229 } // end namespace otb
230 
231 #endif
232 
otb::DimapData::SwathLastCol
std::string SwathLastCol
Definition: otbDimapMetadataHelper.h:77
otb::DimapData::StepCount
int StepCount
Definition: otbDimapMetadataHelper.h:68
otb::DimapData::PhysicalBias
std::vector< double > PhysicalBias
Definition: otbDimapMetadataHelper.h:58
otb::DimapData::TimeRangeStart
std::string TimeRangeStart
Definition: otbDimapMetadataHelper.h:73
otb::DimapData::AcrossTrackViewingAngle
std::vector< double > AcrossTrackViewingAngle
Definition: otbDimapMetadataHelper.h:67
otb::DimapData::ProcessingLevel
std::string ProcessingLevel
Definition: otbDimapMetadataHelper.h:46
otb::DimapMetadataHelper::ParseVector
void ParseVector(const MetadataSupplierInterface &mds, const std::string &prefix, const std::string &name, std::vector< T > &dest, std::vector< T > &defaultValue)
Definition: otbDimapMetadataHelper.h:165
otb::DimapData::ImageID
std::string ImageID
Definition: otbDimapMetadataHelper.h:41
otb::DimapMetadataHelper::GetDimapData
const DimapData & GetDimapData() const
Definition: otbDimapMetadataHelper.h:93
otb::DimapData::missionIndex
std::string missionIndex
Definition: otbDimapMetadataHelper.h:39
otb::DimapData::SolarIrradiance
std::vector< double > SolarIrradiance
Definition: otbDimapMetadataHelper.h:61
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::DimapData::ViewingAngle
std::vector< double > ViewingAngle
Definition: otbDimapMetadataHelper.h:54
otb::DimapData::AlongTrackIncidenceAngle
std::vector< double > AlongTrackIncidenceAngle
Definition: otbDimapMetadataHelper.h:64
otb::MetadataSupplierInterface::GetMetadataValue
virtual std::string GetMetadataValue(std::string const &path, bool &hasValue, int band=-1) const =0
otb::DimapData::AzimuthAngle
std::vector< double > AzimuthAngle
Definition: otbDimapMetadataHelper.h:55
otb::DimapMetadataHelper
Helper class to read dimap data from various sources (Dimap v1, dimap v2, Ossim geom file) and variou...
Definition: otbDimapMetadataHelper.h:87
otb::DimapData::ProductionDate
std::string ProductionDate
Definition: otbDimapMetadataHelper.h:42
otb::DimapData::PhysicalGain
std::vector< double > PhysicalGain
Definition: otbDimapMetadataHelper.h:59
otb::DimapData::AlongTrackViewingAngle
std::vector< double > AlongTrackViewingAngle
Definition: otbDimapMetadataHelper.h:66
otb::DimapData::InstrumentIndex
std::string InstrumentIndex
Definition: otbDimapMetadataHelper.h:45
otb::DimapData::SceneOrientation
std::vector< double > SceneOrientation
Definition: otbDimapMetadataHelper.h:56
otb::MetadataSupplierInterface
Base class to access metadata information in files/images.
Definition: otbMetadataSupplierInterface.h:40
otb::DimapData::TimeRangeEnd
std::string TimeRangeEnd
Definition: otbDimapMetadataHelper.h:74
otb::DimapData::softwareVersion
std::string softwareVersion
Definition: otbDimapMetadataHelper.h:69
otb::DimapData::SunAzimuth
std::vector< double > SunAzimuth
Definition: otbDimapMetadataHelper.h:51
otb::DimapData::SunElevation
std::vector< double > SunElevation
Definition: otbDimapMetadataHelper.h:52
otbGenericExceptionMacro
#define otbGenericExceptionMacro(T, x)
Definition: otbMacro.h:144
otb::DimapData::AcquisitionDate
std::string AcquisitionDate
Definition: otbDimapMetadataHelper.h:43
otb::DimapMetadataHelper::GetSingleValueFromList
T GetSingleValueFromList(const MetadataSupplierInterface &mds, const std::string &prefix, const std::string &name)
Definition: otbDimapMetadataHelper.h:215
otb::DimapData::Instrument
std::string Instrument
Definition: otbDimapMetadataHelper.h:44
otb::DimapData::mission
std::string mission
Definition: otbDimapMetadataHelper.h:38
otb::DimapData::AcrossTrackIncidenceAngle
std::vector< double > AcrossTrackIncidenceAngle
Definition: otbDimapMetadataHelper.h:65
otbMetadataSupplierInterface.h
otb::MissingMetadataException
Exception to be used when metadata parsing fails.
Definition: otbMissingMetadataException.h:36
otb::DimapData::SpectralProcessing
std::string SpectralProcessing
Definition: otbDimapMetadataHelper.h:47
otb::DimapData::IncidenceAngle
std::vector< double > IncidenceAngle
Definition: otbDimapMetadataHelper.h:53
otb::DimapData::LinePeriod
std::string LinePeriod
Definition: otbDimapMetadataHelper.h:75
otb::DimapData
Struct containing metadata parsed from a Dimap product.
Definition: otbDimapMetadataHelper.h:36
otb::DimapData::SwathFirstCol
std::string SwathFirstCol
Definition: otbDimapMetadataHelper.h:76
otb::DimapData::SatAzimuth
double SatAzimuth
Definition: otbDimapMetadataHelper.h:70
otb::DimapMetadataHelper::ParseVector
void ParseVector(const MetadataSupplierInterface &mds, const std::string &prefix, const std::string &name, std::vector< T > &dest)
Definition: otbDimapMetadataHelper.h:113
otb::DimapMetadataHelper::m_Data
DimapData m_Data
Definition: otbDimapMetadataHelper.h:225
otb::DimapData::BandIDs
std::vector< std::string > BandIDs
Definition: otbDimapMetadataHelper.h:49