OTB  9.0.0
Orfeo Toolbox
otbMetadataSupplierInterface.hxx
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 otbMetadataSupplierInterface_hxx
22 #define otbMetadataSupplierInterface_hxx
23 
25 
26 namespace otb
27 {
28 
29 
30 
31 template <typename T> T MetadataSupplierInterface::GetAs(T const& defaultValue, std::string const& path, int band) const
32 {
33  bool hasValue;
34  std::string ret = GetMetadataValue(path, hasValue, band);
35  if (!hasValue)
36  {
37  return defaultValue;
38  }
39  boost::trim(ret);
40  try
41  {
42  return boost::lexical_cast<T>(ret);
43  }
44  catch (boost::bad_lexical_cast&)
45  {
46  return defaultValue;
47  }
48 }
49 
50 template <typename T> T MetadataSupplierInterface::GetAs(std::string const& path, int band) const
51 {
52  bool hasValue;
53  std::string ret = GetMetadataValue(path, hasValue, band);
54  if (!hasValue)
55  {
56  otbGenericExceptionMacro(MissingMetadataException,<<"Missing metadata '"<<path<<"'")
57  }
58  boost::trim(ret);
59  try
60  {
61  return boost::lexical_cast<T>(ret);
62  }
63  catch (boost::bad_lexical_cast&)
64  {
65  otbGenericExceptionMacro(MissingMetadataException,<<"Bad metadata value for '"<<path<<"', got: "<<ret)
66  }
67 }
68 
69 template < typename T> std::vector<T> MetadataSupplierInterface::GetAsVector(std::string const& path, char sep, int size, int band) const
70 {
71  bool hasValue;
72  std::string ret = GetMetadataValue(path, hasValue, band);
73  if (!hasValue)
74  {
75  otbGenericExceptionMacro(MissingMetadataException,<<"Missing metadata '"<<path<<"'")
76  }
77  string_view value(ret);
78  string_view filt_value = rstrip(lstrip(value,"[ "), "] ");
79  std::vector<T> output;
80  typedef part_range<splitter_on_delim> range_type;
81  const range_type parts = split_on(filt_value, sep);
82  for (auto const& part : parts)
83  {
84  // TODO: check if we can use lexical_cast on a string_view
85  std::string strPart = to<std::string>(part, "casting string_view to std::string");
86  if (strPart.empty())
87  {
88  continue;
89  }
90  try
91  {
92  output.push_back(boost::lexical_cast<T>(strPart));
93  }
94  catch (boost::bad_lexical_cast&)
95  {
96  otbGenericExceptionMacro(MissingMetadataException,<<"Bad metadata vector element in '"<<path<<"', got :"<<part)
97  }
98  }
99  if ((size >= 0) && (output.size() != (size_t)size))
100  {
101  otbGenericExceptionMacro(MissingMetadataException,<<"Bad number of elements in vector '"<<path<<"', expected "<<size<< ", got "<<output.size())
102  }
103  return output;
104 }
105 
106 } // end namespace otb
107 
108 #endif
otb::part_range
Definition: otbStringUtilities.h:435
otb::split_on
part_range< splitter_on_delim > split_on(String const &str, char delim)
Definition: otbStringUtilities.h:489
otb::lstrip
OTBCommon_EXPORT string_view lstrip(string_view const &v, string_view const &c)
returns a string_view with the leading characters removed
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MetadataSupplierInterface::GetMetadataValue
virtual std::string GetMetadataValue(std::string const &path, bool &hasValue, int band=-1) const =0
otbGenericExceptionMacro
#define otbGenericExceptionMacro(T, x)
Definition: otbMacro.h:144
otb::rstrip
OTBCommon_EXPORT string_view rstrip(string_view const &v, string_view const &c)
returns a string_view with the ending characters removed
otbMetadataSupplierInterface.h
otb::MissingMetadataException
Exception to be used when metadata parsing fails.
Definition: otbMissingMetadataException.h:36
otb::string_view
Definition: otbStringUtilities.h:58
otb::MetadataSupplierInterface::GetAsVector
std::vector< T > GetAsVector(std::string const &path, char sep=' ', int size=-1, int band=-1) const
Definition: otbMetadataSupplierInterface.hxx:69
otb::MetadataSupplierInterface::GetAs
T GetAs(T const &defaultValue, std::string const &path, int band=-1) const
Definition: otbMetadataSupplierInterface.hxx:31