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 #include <unordered_map>
31 namespace otb
32 {
33 
40 struct DimapData
41 {
42  std::string mission;
43  std::string missionIndex;
44 
45  std::string ImageID;
46  std::string ProductionDate;
47  std::string AcquisitionDate;
48  std::string Instrument;
49  std::string InstrumentIndex;
50  std::string ProcessingLevel;
51  std::string SpectralProcessing;
52 
53  std::vector<std::string> BandIDs;
54 
55  std::vector<double> SunAzimuth;
56  std::vector<double> SunElevation;
57  std::vector<double> IncidenceAngle;
58  std::vector<double> ViewingAngle;
59  std::vector<double> AzimuthAngle;
60  std::vector<double> SceneOrientation;
61 
62  std::vector<double> PhysicalBias;
63  std::vector<double> PhysicalGain;
64 
65  std::vector<double> SolarIrradiance;
66 
67  // Optional
68  std::vector<double> AlongTrackIncidenceAngle;
69  std::vector<double> AcrossTrackIncidenceAngle;
70  std::vector<double> AlongTrackViewingAngle;
71  std::vector<double> AcrossTrackViewingAngle;
72  int StepCount;
73  std::string softwareVersion;
74  double SatAzimuth;
75 
76  //specific pneo sensors features
77  std::vector<std::string> LUTFileNames;
78  std::unordered_map<std::string, std::vector<double>> LUTs;
79  std::vector<double> RangeMin;
80  std::vector<double> RangeMax;
81 
82  // phr and pneo sensor characteristics
83  std::string TimeRangeStart;
84  std::string TimeRangeEnd;
85  std::string LinePeriod;
86  std::string SwathFirstCol;
87  std::string SwathLastCol;
88 };
89 
97 class OTBMetadata_EXPORT DimapMetadataHelper
98 {
99 public:
100 
101  DimapMetadataHelper() = default;
102 
103  const DimapData & GetDimapData() const
104  {
105  return m_Data;
106  }
107 
108 
111 
113  void ParseDimapV1(const MetadataSupplierInterface & mds, const std::string prefix);
114 
116  void ParseDimapV2(const MetadataSupplierInterface & mds, const std::string & prefix = "Dimap_Document.");
117 
118  void ParseSpot5Model(const MetadataSupplierInterface & mds, Spot5Param& spot5Param, const std::string & prefix = "Dimap_Document.");
120  void ParseDimapV3(const MetadataSupplierInterface & mds, const std::string & prefix = "Dimap_Document.");
121 
123  std::vector<double> parseLUTStringToArrays(std::string const & s);
124 
127 
130 
131 protected:
132 
133 private:
134 
135  template <class T>
137  const std::string & prefix,
138  const std::string & name,
139  std::vector<T> & dest)
140  {
141  dest.clear();
142 
143  std::vector<std::string> mdStr;
144 
145  bool hasValue = false;
146  auto value = mds.GetMetadataValue(prefix + "." + name, hasValue);
147 
148  if (hasValue)
149  {
150  mdStr.push_back(value);
151  }
152  else
153  {
154  hasValue = true;
155  unsigned int i = 1;
156  while(hasValue)
157  {
158  value = mds.GetMetadataValue(prefix + "_" + std::to_string(i) + "." + name, hasValue);
159  if (hasValue)
160  mdStr.push_back(value);
161  i++;
162  }
163  }
164 
165  if (mdStr.empty())
166  {
168  <<"Missing metadata '"<< prefix + name <<"'")
169  }
170 
171  for (const auto & elem : mdStr)
172  {
173  try
174  {
175  dest.push_back(boost::lexical_cast<T>(elem));
176  }
177  catch (boost::bad_lexical_cast&)
178  {
179  otbGenericExceptionMacro(MissingMetadataException,<<"Bad metadata value for '"<<prefix + name<<"', got: "<<elem)
180  }
181  }
182 
183  //dest.push_back( );
184  }
185 
186  // Non throwing version
187  template <class T>
189  const std::string & prefix,
190  const std::string & name,
191  std::vector<T> & dest,
192  std::vector<T> & defaultValue)
193  {
194  //TODO factorize
195  dest.clear();
196 
197  std::vector<std::string> mdStr;
198 
199  bool hasValue = false;
200  auto value = mds.GetMetadataValue(prefix + "." + name, hasValue);
201 
202  if (hasValue)
203  {
204  mdStr.push_back(value);
205  }
206  else
207  {
208  hasValue = true;
209  unsigned int i = 1;
210  while(hasValue)
211  {
212  value = mds.GetMetadataValue(prefix + "_" + std::to_string(i) + "." + name, hasValue);
213  if (hasValue)
214  mdStr.push_back(value);
215  i++;
216  }
217  }
218 
219  if (mdStr.empty())
220  {
221  dest = defaultValue;
222  }
223 
224  for (const auto & elem : mdStr)
225  {
226  try
227  {
228  dest.push_back(boost::lexical_cast<T>(elem));
229  }
230  catch (boost::bad_lexical_cast&)
231  {
232  dest = defaultValue;
233  }
234  }
235  }
236 
237  template <class T>
239  const std::string & prefix,
240  const std::string & name)
241  {
242  std::vector<T> vector;
243  ParseVector(mds, prefix, name, vector);
244  return vector[0];
245 
246  }
247 
248  double GetTime(const std::string& timeStr){
249  // Time stamps are in the format: "yyyy-mm-ddThh:mm:ss.ssssss"
250  const auto d = MetaData::ReadFormattedDate(timeStr);
251  return d.GetSecond() + 60.0 * (
252  d.GetMinute() + 60.0 * ( // Total NB of minutes
253  d.GetHour() + 24.0 * ( // Total NB of hours
254  d.GetDay() - 1.0 + 365.25 * ( // Total NB of days (-1 as day is not over)
255  d.GetYear() - 2002.0
256  )
257  )
258  )
259  );
260  }
261 
263 };
264 
265 
266 } // end namespace otb
267 
268 #endif
269 
Helper class to read dimap data from various sources (Dimap v1, dimap v2, Ossim geom file) and variou...
void ParseDimapV3(const MetadataSupplierInterface &mds, const std::string &prefix="Dimap_Document.")
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.")
void ParseLUT(const MetadataSupplierInterface &mds)
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)
std::vector< double > parseLUTStringToArrays(std::string const &s)
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::unordered_map< std::string, std::vector< double > > LUTs
std::string AcquisitionDate
std::vector< double > PhysicalBias
std::vector< double > AlongTrackViewingAngle
std::vector< std::string > LUTFileNames
std::vector< double > SunElevation
std::vector< double > SceneOrientation
std::vector< double > AlongTrackIncidenceAngle
std::vector< double > RangeMin
std::vector< double > RangeMax
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.