OTB  6.7.0
Orfeo Toolbox
otbChangeInformationImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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 otbChangeInformationImageFilter_hxx
22 #define otbChangeInformationImageFilter_hxx
23 
25 #include "itkMetaDataObject.h"
26 
27 namespace otb
28 {
29 
30 template< typename TInputImage >
31 void
33 ::SetChangeMetaData(const char *keyname, bool flag)
34 {
35  std::string key(keyname);
36  if (! key.empty())
37  {
38  if (flag)
39  {
40  m_ChangedKeys.insert(key);
41  }
42  else
43  {
44  std::set<std::string>::iterator pos = m_ChangedKeys.find(key);
45  if (pos != m_ChangedKeys.end())
46  {
47  m_ChangedKeys.erase(pos);
48  }
49  }
50  }
51 }
52 
53 template< typename TInputImage >
54 bool
56 ::GetChangeMetaData(const char *keyname)
57 {
58  std::string key(keyname);
59  if (! key.empty())
60  {
61  if (m_ChangedKeys.find(key) != m_ChangedKeys.end())
62  {
63  return true;
64  }
65  }
66  return false;
67 }
68 
69 template< typename TInputImage >
70 template<typename T>
71 void
73 ::SetOutputMetaData(const char *keyname, const T * value)
74 {
75  std::string key(keyname);
76  if (! key.empty())
77  {
78  // enable this key for metadata change
79  m_ChangedKeys.insert(key);
80  itk::MetaDataDictionary &dict = this->GetMetaDataDictionary();
81  if (value == nullptr)
82  {
83  // Remove meta-data from dictionary
84  dict.Erase(key);
85  }
86  else
87  {
88  // Set metadata in dictionary
89  const T &valueRef = (*value);
90  itk::EncapsulateMetaData<T>(dict,key,valueRef);
91  }
92  }
93 }
94 
95 template< typename TInputImage >
96 void
99 {
100  Superclass::GenerateOutputInformation();
101 
102  // Process the metadatas to be changed
103  itk::MetaDataDictionary &dict = this->GetMetaDataDictionary();
104  itk::MetaDataDictionary &outputDict = this->GetOutput()->GetMetaDataDictionary();
105  std::set<std::string>::iterator it = m_ChangedKeys.begin();
106  for ( ; it != m_ChangedKeys.end() ; ++it)
107  {
108  if (dict.HasKey(*it))
109  {
110  // Replace metadata in output dictionary
111  outputDict[*it] = dict[*it];
112  }
113  else
114  {
115  // Remove metadata from output dictionary
116  outputDict.Erase(*it);
117  }
118  }
119 }
120 
121 } // End of namespace OTB
122 
123 #endif
bool HasKey(const std::string &) const
void SetOutputMetaData(const char *keyname, const T *value)
void SetChangeMetaData(const char *keyname, bool flag)
bool Erase(const std::string &)