OTB  6.7.0
Orfeo Toolbox
otbVectorData.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 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_txx
22 #define otbVectorData_txx
23 
24 #include "otbVectorData.h"
26 #include "otbMetaDataKey.h"
27 
28 namespace otb
29 {
30 
31 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
34 {
35  m_DataTree = DataTreeType::New();
36  DataNodePointerType root = DataNodeType::New();
37  root->SetNodeId("Root");
38  m_DataTree->SetRoot(root);
39  m_Origin.Fill(0);
40  m_Spacing.Fill(1);
41 }
42 
43 template<class TPrecision, unsigned int VDimension, class TValuePrecision>
44 void
46 ::SetProjectionRef(const std::string& projectionRef)
47 {
48  itk::MetaDataDictionary& dict = this->GetMetaDataDictionary();
49 
50  itk::EncapsulateMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, projectionRef);
51  this->Modified();
52 }
53 
54 template<class TPrecision, unsigned int VDimension, class TValuePrecision>
55 std::string
58 {
59  const itk::MetaDataDictionary& dict = this->GetMetaDataDictionary();
60 
61  std::string projectionRef;
62  itk::ExposeMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, projectionRef);
63 
64  return projectionRef;
65 }
66 
67 template<class TPrecision, unsigned int VDimension, class TValuePrecision>
68 void
70 ::SetSpacing(const SpacingType& spacing)
71 {
72  itkDebugMacro("setting Spacing to " << spacing);
73  if (this->m_Spacing != spacing)
74  {
75  this->m_Spacing = spacing;
76  this->Modified();
77  }
78 }
79 
80 template<class TPrecision, unsigned int VDimension, class TValuePrecision>
81 void
83 ::SetSpacing(const double spacing[2])
84 {
85  SpacingType s(spacing);
86  this->SetSpacing(s);
87 }
88 
89 template<class TPrecision, unsigned int VDimension, class TValuePrecision>
90 void
92 ::SetSpacing(const float spacing[2])
93 {
94  itk::Vector<float, 2> sf(spacing);
95  SpacingType s;
96  s.CastFrom(sf);
97  this->SetSpacing(s);
98 }
99 
100 template<class TPrecision, unsigned int VDimension, class TValuePrecision>
101 void
103 ::SetOrigin(const double origin[2])
104 {
105  OriginType p(origin);
106  this->SetOrigin(p);
107 }
108 
109 template<class TPrecision, unsigned int VDimension, class TValuePrecision>
110 void
112 ::SetOrigin(const float origin[2])
113 {
114  itk::Point<float, 2> of(origin);
115  OriginType p;
116  p.CastFrom(of);
117  this->SetOrigin(p);
118 }
119 
120 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
121 bool
124 {
125  return m_DataTree->Clear();
126 }
127 
128 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
129 int
131 ::Size() const
132 {
133  return m_DataTree->Count();
134 }
135 
136 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
137 void
139 ::PrintSelf(std::ostream& os, itk::Indent indent) const
140 {
141  Superclass::PrintSelf(os, indent);
142  os << std::endl;
143 
145  it.GoToBegin();
146 
147  while (!it.IsAtEnd())
148  {
150  bool goesOn = true;
151  while (itParent.HasParent() && goesOn)
152  {
153  os << indent;
154  goesOn = itParent.GoToParent();
155  }
156  os << "+" << it.Get()->GetNodeTypeAsString() << std::endl;
157  ++it;
158  }
159 }
160 
161 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
162 void
165 {
166  // call the superclass' implementation
167  Superclass::Graft( data );
168 
169  if ( data )
170  {
171  // Attempt to cast data to an Image
172  const Self * vdData;
173 
174  try
175  {
176  vdData = dynamic_cast<const Self *>( data );
177  }
178  catch( ... )
179  {
180  return;
181  }
182 
183  if ( vdData )
184  {
185  // Copy all the needed data : DataTree, spacing, origin and
186  // Projection Ref
187  m_DataTree = const_cast<DataTreeType*> (vdData->GetDataTree());
188  this->SetSpacing(vdData->GetSpacing());
189  this->SetOrigin(vdData->GetOrigin());
190  this->SetProjectionRef(vdData->GetProjectionRef());
191  }
192  else
193  {
194  // pointer could not be cast back down
195  itkExceptionMacro( << "otb::VectorData::Graft() cannot cast "
196  << typeid(data).name() << " to "
197  << typeid(const Self *).name() );
198  }
199  }
200 }
201 } // end namespace otb
202 
203 #endif
void CastFrom(const Point< TCoordRepB, NPointDimension > &pa)
virtual void SetProjectionRef(const std::string &projectionRef)
virtual bool Clear()
void Graft(const itk::DataObject *data) override
virtual bool HasParent() const
virtual void SetOrigin(OriginType _arg)
OTBOSSIMAdapters_EXPORT char const * ProjectionRefKey
virtual const ValueType & Get() const
virtual int Size() const
void CastFrom(const Vector< TCoordRepB, NVectorDimension > &pa)
bool IsAtEnd(void) const
virtual bool GoToParent()
virtual void SetSpacing(const SpacingType &spacing)
virtual std::string GetProjectionRef() const
void PrintSelf(std::ostream &os, itk::Indent indent) const override