OTB  9.1.1
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 
103  void ParseGeom(const MetadataSupplierInterface & mds);
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 
otbSpot5Metadata.h
otb::DimapData::SwathLastCol
std::string SwathLastCol
Definition: otbDimapMetadataHelper.h:80
otb::DimapData::StepCount
int StepCount
Definition: otbDimapMetadataHelper.h:71
otb::DimapData::PhysicalBias
std::vector< double > PhysicalBias
Definition: otbDimapMetadataHelper.h:61
otb::DimapData::TimeRangeStart
std::string TimeRangeStart
Definition: otbDimapMetadataHelper.h:76
otb::DimapData::AcrossTrackViewingAngle
std::vector< double > AcrossTrackViewingAngle
Definition: otbDimapMetadataHelper.h:70
otb::DimapData::ProcessingLevel
std::string ProcessingLevel
Definition: otbDimapMetadataHelper.h:49
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:170
otbDateTime.h
otb::DimapData::ImageID
std::string ImageID
Definition: otbDimapMetadataHelper.h:44
otb::DimapMetadataHelper::GetDimapData
const DimapData & GetDimapData() const
Definition: otbDimapMetadataHelper.h:96
otb::DimapData::missionIndex
std::string missionIndex
Definition: otbDimapMetadataHelper.h:42
otb::DimapData::SolarIrradiance
std::vector< double > SolarIrradiance
Definition: otbDimapMetadataHelper.h:64
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::DimapData::ViewingAngle
std::vector< double > ViewingAngle
Definition: otbDimapMetadataHelper.h:57
otb::DimapData::AlongTrackIncidenceAngle
std::vector< double > AlongTrackIncidenceAngle
Definition: otbDimapMetadataHelper.h:67
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:58
otb::DimapMetadataHelper
Helper class to read dimap data from various sources (Dimap v1, dimap v2, Ossim geom file) and variou...
Definition: otbDimapMetadataHelper.h:90
otb::DimapData::ProductionDate
std::string ProductionDate
Definition: otbDimapMetadataHelper.h:45
otb::DimapData::PhysicalGain
std::vector< double > PhysicalGain
Definition: otbDimapMetadataHelper.h:62
otb::DimapData::AlongTrackViewingAngle
std::vector< double > AlongTrackViewingAngle
Definition: otbDimapMetadataHelper.h:69
otb::DimapData::InstrumentIndex
std::string InstrumentIndex
Definition: otbDimapMetadataHelper.h:48
otb::DimapData::SceneOrientation
std::vector< double > SceneOrientation
Definition: otbDimapMetadataHelper.h:59
otb::MetadataSupplierInterface
Base class to access metadata information in files/images.
Definition: otbMetadataSupplierInterface.h:40
otb::DimapData::TimeRangeEnd
std::string TimeRangeEnd
Definition: otbDimapMetadataHelper.h:77
otb::DimapData::softwareVersion
std::string softwareVersion
Definition: otbDimapMetadataHelper.h:72
otb::DimapData::SunAzimuth
std::vector< double > SunAzimuth
Definition: otbDimapMetadataHelper.h:54
otb::DimapData::SunElevation
std::vector< double > SunElevation
Definition: otbDimapMetadataHelper.h:55
otbGenericExceptionMacro
#define otbGenericExceptionMacro(T, x)
Definition: otbMacro.h:144
otb::DimapData::AcquisitionDate
std::string AcquisitionDate
Definition: otbDimapMetadataHelper.h:46
otb::DimapMetadataHelper::GetSingleValueFromList
T GetSingleValueFromList(const MetadataSupplierInterface &mds, const std::string &prefix, const std::string &name)
Definition: otbDimapMetadataHelper.h:220
otb::DimapData::Instrument
std::string Instrument
Definition: otbDimapMetadataHelper.h:47
otb::DimapData::mission
std::string mission
Definition: otbDimapMetadataHelper.h:41
otb::DimapData::AcrossTrackIncidenceAngle
std::vector< double > AcrossTrackIncidenceAngle
Definition: otbDimapMetadataHelper.h:68
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:50
otb::DimapData::IncidenceAngle
std::vector< double > IncidenceAngle
Definition: otbDimapMetadataHelper.h:56
otb::DimapData::LinePeriod
std::string LinePeriod
Definition: otbDimapMetadataHelper.h:78
otb::DimapMetadataHelper::GetTime
double GetTime(const std::string &timeStr)
Definition: otbDimapMetadataHelper.h:230
otb::MetaData::ReadFormattedDate
OTBMetadata_EXPORT TimePoint ReadFormattedDate(const std::string &dateStr, const std::string &format=details::timePointFormat)
otb::DimapData
Struct containing metadata parsed from a Dimap product.
Definition: otbDimapMetadataHelper.h:39
otb::Spot5Param
Spot5 sensors parameters.
Definition: otbSpot5Metadata.h:68
otb::DimapData::SwathFirstCol
std::string SwathFirstCol
Definition: otbDimapMetadataHelper.h:79
otb::DimapData::SatAzimuth
double SatAzimuth
Definition: otbDimapMetadataHelper.h:73
otb::DimapMetadataHelper::ParseVector
void ParseVector(const MetadataSupplierInterface &mds, const std::string &prefix, const std::string &name, std::vector< T > &dest)
Definition: otbDimapMetadataHelper.h:118
otb::DimapMetadataHelper::m_Data
DimapData m_Data
Definition: otbDimapMetadataHelper.h:244
otb::DimapData::BandIDs
std::vector< std::string > BandIDs
Definition: otbDimapMetadataHelper.h:52