OTB  10.0.0
Orfeo Toolbox
otbDimapMetadataHelper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 #include "otbSpot5Metadata.h"
27 #include "otbDateTime.h"
28 
29 
30 namespace otb
31 {
32 
39 struct DimapData
40 {
41  std::string mission;
42  std::string missionIndex;
43 
44  std::string ImageID;
45  std::string ProductionDate;
46  std::string AcquisitionDate;
47  std::string Instrument;
48  std::string InstrumentIndex;
49  std::string ProcessingLevel;
50  std::string SpectralProcessing;
51 
52  std::vector<std::string> BandIDs;
53 
54  std::vector<double> SunAzimuth;
55  std::vector<double> SunElevation;
56  std::vector<double> IncidenceAngle;
57  std::vector<double> ViewingAngle;
58  std::vector<double> AzimuthAngle;
59  std::vector<double> SceneOrientation;
60 
61  std::vector<double> PhysicalBias;
62  std::vector<double> PhysicalGain;
63 
64  std::vector<double> SolarIrradiance;
65 
66  // Optional
67  std::vector<double> AlongTrackIncidenceAngle;
68  std::vector<double> AcrossTrackIncidenceAngle;
69  std::vector<double> AlongTrackViewingAngle;
70  std::vector<double> AcrossTrackViewingAngle;
71  int StepCount;
72  std::string softwareVersion;
73  double SatAzimuth;
74 
75  // phr sensor characteristics
76  std::string TimeRangeStart;
77  std::string TimeRangeEnd;
78  std::string LinePeriod;
79  std::string SwathFirstCol;
80  std::string SwathLastCol;
81 };
82 
90 class OTBMetadata_EXPORT DimapMetadataHelper
91 {
92 public:
93 
94  DimapMetadataHelper() = default;
95 
96  const DimapData & GetDimapData() const
97  {
98  return m_Data;
99  }
100 
101 
104 
106  void ParseDimapV1(const MetadataSupplierInterface & mds, const std::string prefix);
107 
109  void ParseDimapV2(const MetadataSupplierInterface & mds, const std::string & prefix = "Dimap_Document.");
110 
111  void ParseSpot5Model(const MetadataSupplierInterface & mds, Spot5Param& spot5Param, const std::string & prefix = "Dimap_Document.");
112 
113 protected:
114 
115 private:
116 
117  template <class T>
119  const std::string & prefix,
120  const std::string & name,
121  std::vector<T> & dest)
122  {
123  dest.clear();
124 
125  std::vector<std::string> mdStr;
126 
127  bool hasValue = false;
128  auto value = mds.GetMetadataValue(prefix + "." + name, hasValue);
129 
130  if (hasValue)
131  {
132  mdStr.push_back(value);
133  }
134  else
135  {
136  hasValue = true;
137  unsigned int i = 1;
138  while(hasValue)
139  {
140  value = mds.GetMetadataValue(prefix + "_" + std::to_string(i) + "." + name, hasValue);
141  if (hasValue)
142  mdStr.push_back(value);
143  i++;
144  }
145  }
146 
147  if (mdStr.empty())
148  {
150  <<"Missing metadata '"<< prefix + name <<"'")
151  }
152 
153  for (const auto & elem : mdStr)
154  {
155  try
156  {
157  dest.push_back(boost::lexical_cast<T>(elem));
158  }
159  catch (boost::bad_lexical_cast&)
160  {
161  otbGenericExceptionMacro(MissingMetadataException,<<"Bad metadata value for '"<<prefix + name<<"', got: "<<elem)
162  }
163  }
164 
165  //dest.push_back( );
166  }
167 
168  // Non throwing version
169  template <class T>
171  const std::string & prefix,
172  const std::string & name,
173  std::vector<T> & dest,
174  std::vector<T> & defaultValue)
175  {
176  //TODO factorize
177  dest.clear();
178 
179  std::vector<std::string> mdStr;
180 
181  bool hasValue = false;
182  auto value = mds.GetMetadataValue(prefix + "." + name, hasValue);
183 
184  if (hasValue)
185  {
186  mdStr.push_back(value);
187  }
188  else
189  {
190  hasValue = true;
191  unsigned int i = 1;
192  while(hasValue)
193  {
194  value = mds.GetMetadataValue(prefix + "_" + std::to_string(i) + "." + name, hasValue);
195  if (hasValue)
196  mdStr.push_back(value);
197  i++;
198  }
199  }
200 
201  if (mdStr.empty())
202  {
203  dest = defaultValue;
204  }
205 
206  for (const auto & elem : mdStr)
207  {
208  try
209  {
210  dest.push_back(boost::lexical_cast<T>(elem));
211  }
212  catch (boost::bad_lexical_cast&)
213  {
214  dest = defaultValue;
215  }
216  }
217  }
218 
219  template <class T>
221  const std::string & prefix,
222  const std::string & name)
223  {
224  std::vector<T> vector;
225  ParseVector(mds, prefix, name, vector);
226  return vector[0];
227 
228  }
229 
230  double GetTime(const std::string& timeStr){
231  // Time stamps are in the format: "yyyy-mm-ddThh:mm:ss.ssssss"
232  const auto d = MetaData::ReadFormattedDate(timeStr);
233  return d.GetSecond() + 60.0 * (
234  d.GetMinute() + 60.0 * ( // Total NB of minutes
235  d.GetHour() + 24.0 * ( // Total NB of hours
236  d.GetDay() - 1.0 + 365.25 * ( // Total NB of days (-1 as day is not over)
237  d.GetYear() - 2002.0
238  )
239  )
240  )
241  );
242  }
243 
245 };
246 
247 
248 } // end namespace otb
249 
250 #endif
251 
Helper class to read dimap data from various sources (Dimap v1, dimap v2, Ossim geom file) and variou...
void ParseVector(const MetadataSupplierInterface &mds, const std::string &prefix, const std::string &name, std::vector< T > &dest)
void ParseDimapV2(const MetadataSupplierInterface &mds, const std::string &prefix="Dimap_Document.")
T GetSingleValueFromList(const MetadataSupplierInterface &mds, const std::string &prefix, const std::string &name)
void ParseSpot5Model(const MetadataSupplierInterface &mds, Spot5Param &spot5Param, const std::string &prefix="Dimap_Document.")
void ParseDimapV1(const MetadataSupplierInterface &mds, const std::string prefix)
double GetTime(const std::string &timeStr)
void ParseGeom(const MetadataSupplierInterface &mds)
const DimapData & GetDimapData() const
void ParseVector(const MetadataSupplierInterface &mds, const std::string &prefix, const std::string &name, std::vector< T > &dest, std::vector< T > &defaultValue)
Base class to access metadata information in files/images.
virtual std::string GetMetadataValue(std::string const &path, bool &hasValue, int band=-1) const =0
Exception to be used when metadata parsing fails.
OTBMetadata_EXPORT TimePoint ReadFormattedDate(const std::string &dateStr, const std::string &format=details::timePointFormat)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
#define otbGenericExceptionMacro(T, x)
Definition: otbMacro.h:196
Struct containing metadata parsed from a Dimap product.
std::vector< double > PhysicalGain
std::vector< double > AcrossTrackViewingAngle
std::vector< double > SolarIrradiance
std::string ProcessingLevel
std::string AcquisitionDate
std::vector< double > PhysicalBias
std::vector< double > AlongTrackViewingAngle
std::vector< double > SunElevation
std::vector< double > SceneOrientation
std::vector< double > AlongTrackIncidenceAngle
std::vector< double > IncidenceAngle
std::string ProductionDate
std::string SpectralProcessing
std::string softwareVersion
std::string InstrumentIndex
std::vector< double > AzimuthAngle
std::vector< double > ViewingAngle
std::vector< double > AcrossTrackIncidenceAngle
std::string TimeRangeStart
std::vector< double > SunAzimuth
std::vector< std::string > BandIDs
Spot5 sensors parameters.