OTB  9.0.0
Orfeo Toolbox
otbVectorData.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 otbVectorData_hxx
22 #define otbVectorData_hxx
23 
24 #include "otbVectorData.h"
25 #include "itkPreOrderTreeIterator.h"
26 #include "otbMetaDataKey.h"
27 
28 namespace otb
29 {
30 
31 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
33 {
34  m_DataTree = DataTreeType::New();
35  DataNodePointerType root = DataNodeType::New();
36  root->SetNodeId("Root");
37  m_DataTree->SetRoot(root);
38  m_Origin.Fill(0);
39  m_Spacing.Fill(1);
40 }
41 
42 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
44 {
45  itk::MetaDataDictionary& dict = this->GetMetaDataDictionary();
46 
47  itk::EncapsulateMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, projectionRef);
48  this->Modified();
49 }
50 
51 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
53 {
54  const itk::MetaDataDictionary& dict = this->GetMetaDataDictionary();
55 
56  std::string projectionRef;
57  itk::ExposeMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, projectionRef);
58 
59  return projectionRef;
60 }
61 
62 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
64 {
65  itkDebugMacro("setting Spacing to " << spacing);
66  if (this->m_Spacing != spacing)
67  {
68  this->m_Spacing = spacing;
69  this->Modified();
70  }
71 }
72 
73 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
75 {
76  SpacingType s(spacing);
77  this->SetSpacing(s);
78 }
79 
80 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
82 {
83  itk::Vector<float, 2> sf(spacing);
84  SpacingType s;
85  s.CastFrom(sf);
86  this->SetSpacing(s);
87 }
88 
89 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
91 {
92  OriginType p(origin);
93  this->SetOrigin(p);
94 }
95 
96 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
98 {
99  itk::Point<float, 2> of(origin);
100  OriginType p;
101  p.CastFrom(of);
102  this->SetOrigin(p);
103 }
104 
105 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
107 {
108  return m_DataTree->Clear();
109 }
110 
111 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
113 {
114  return m_DataTree->Count();
115 }
116 
117 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
118 void VectorData<TPrecision, VDimension, TValuePrecision>::PrintSelf(std::ostream& os, itk::Indent indent) const
119 {
120  Superclass::PrintSelf(os, indent);
121  os << std::endl;
122 
123  itk::PreOrderTreeIterator<DataTreeType> it(m_DataTree);
124  it.GoToBegin();
125 
126  while (!it.IsAtEnd())
127  {
128  itk::PreOrderTreeIterator<DataTreeType> itParent = it;
129  bool goesOn = true;
130  while (itParent.HasParent() && goesOn)
131  {
132  os << indent;
133  goesOn = itParent.GoToParent();
134  }
135  os << "+" << it.Get()->GetNodeTypeAsString() << std::endl;
136  ++it;
137  }
138 }
139 
140 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
142 {
143  // call the superclass' implementation
144  Superclass::Graft(data);
145 
146  if (data)
147  {
148  // Attempt to cast data to an Image
149  const Self* vdData;
150 
151  try
152  {
153  vdData = dynamic_cast<const Self*>(data);
154  }
155  catch (...)
156  {
157  return;
158  }
159 
160  if (vdData)
161  {
162  // Copy all the needed data : DataTree, spacing, origin and
163  // Projection Ref
164  m_DataTree = const_cast<DataTreeType*>(vdData->GetDataTree());
165  this->SetSpacing(vdData->GetSpacing());
166  this->SetOrigin(vdData->GetOrigin());
167  this->SetProjectionRef(vdData->GetProjectionRef());
168  }
169  else
170  {
171  // pointer could not be cast back down
172  itkExceptionMacro(<< "otb::VectorData::Graft() cannot cast " << typeid(data).name() << " to " << typeid(const Self*).name());
173  }
174  }
175 }
176 } // end namespace otb
177 
178 #endif
otb::VectorData::GetDataTree
virtual const DataTreeType * GetDataTree() const
otb::VectorData::SpacingType
itk::Vector< double, 2 > SpacingType
Definition: otbVectorData.h:86
otb::VectorData
This class represents a hierarchy of vector data.
Definition: otbVectorData.h:58
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::VectorData::GetProjectionRef
virtual std::string GetProjectionRef() const
Definition: otbVectorData.hxx:52
otb::VectorData::Size
virtual int Size() const
Definition: otbVectorData.hxx:112
otb::VectorData::DataTreeType
itk::TreeContainer< DataNodePointerType > DataTreeType
Definition: otbVectorData.h:79
otb::VectorData::OriginType
itk::Point< double, 2 > OriginType
Definition: otbVectorData.h:87
otb::VectorData::VectorData
VectorData()
Definition: otbVectorData.hxx:32
otb::VectorData::SetOrigin
virtual void SetOrigin(OriginType _arg)
otbMetaDataKey.h
otbVectorData.h
otb::VectorData::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbVectorData.hxx:118
otb::VectorData::DataNodePointerType
DataNodeType::Pointer DataNodePointerType
Definition: otbVectorData.h:78
otb::VectorData::Graft
void Graft(const itk::DataObject *data) override
Definition: otbVectorData.hxx:141
otb::VectorData::SetProjectionRef
virtual void SetProjectionRef(const std::string &projectionRef)
Definition: otbVectorData.hxx:43
otb::VectorData::Clear
virtual bool Clear()
Definition: otbVectorData.hxx:106
otb::MetaDataKey::ProjectionRefKey
OTBMetadata_EXPORT char const * ProjectionRefKey
otb::VectorData::GetOrigin
virtual const OriginType & GetOrigin() const
otb::VectorData::GetSpacing
virtual const SpacingType & GetSpacing() const
otb::VectorData::SetSpacing
virtual void SetSpacing(const SpacingType &spacing)
Definition: otbVectorData.hxx:63