OTB  10.0.0
Orfeo Toolbox
otbVectorDataProjectionFilter.hxx
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 otbVectorDataProjectionFilter_hxx
22 #define otbVectorDataProjectionFilter_hxx
23 
25 #include "itkProgressReporter.h"
26 #include "itkMetaDataObject.h"
27 #include "otbMetaDataKey.h"
28 #include "otbStopwatch.h"
29 
30 namespace otb
31 {
35 template <class TInputVectorData, class TOutputVectorData>
37 {
38  m_InputProjectionRef.clear();
39  m_OutputProjectionRef.clear();
40  m_InputSpacing.Fill(1);
41  m_InputOrigin.Fill(0);
42  m_OutputSpacing.Fill(1);
43  m_OutputOrigin.Fill(0);
44 }
46 
47 //----------------------------------------------------------------------------
48 template <class TInputVectorData, class TOutputVectorData>
50 {
51  itkDebugMacro("setting Spacing to " << spacing);
52  if (this->m_InputSpacing != spacing)
53  {
54  this->m_InputSpacing = spacing;
55  this->Modified();
56  }
57 }
58 
59 //----------------------------------------------------------------------------
60 template <class TInputVectorData, class TOutputVectorData>
62 {
63  SpacingType s(spacing);
64  this->SetInputSpacing(s);
65 }
66 
67 //----------------------------------------------------------------------------
68 template <class TInputVectorData, class TOutputVectorData>
70 {
71  itk::Vector<float, 2> sf(spacing);
72  SpacingType s;
73  s.CastFrom(sf);
74  this->SetInputSpacing(s);
75 }
76 
77 //----------------------------------------------------------------------------
78 template <class TInputVectorData, class TOutputVectorData>
80 {
81  OriginType p(origin);
82  this->SetInputOrigin(p);
83 }
84 
85 //----------------------------------------------------------------------------
86 template <class TInputVectorData, class TOutputVectorData>
88 {
89  itk::Point<float, 2> of(origin);
90  OriginType p;
91  p.CastFrom(of);
92  this->SetInputOrigin(p);
93 }
94 
95 //----------------------------------------------------------------------------
96 template <class TInputVectorData, class TOutputVectorData>
98 {
99  itkDebugMacro("setting Spacing to " << spacing);
100  if (this->m_OutputSpacing != spacing)
101  {
102  this->m_OutputSpacing = spacing;
103  this->Modified();
104  }
105 }
106 
107 //----------------------------------------------------------------------------
108 template <class TInputVectorData, class TOutputVectorData>
110 {
111  SpacingType s(spacing);
112  this->SetOutputSpacing(s);
113 }
114 
115 //----------------------------------------------------------------------------
116 template <class TInputVectorData, class TOutputVectorData>
118 {
119  itk::Vector<float, 2> sf(spacing);
120  SpacingType s;
121  s.CastFrom(sf);
122  this->SetOutputSpacing(s);
123 }
124 
125 //----------------------------------------------------------------------------
126 template <class TInputVectorData, class TOutputVectorData>
128 {
129  OriginType p(origin);
130  this->SetOutputOrigin(p);
131 }
132 
133 //----------------------------------------------------------------------------
134 template <class TInputVectorData, class TOutputVectorData>
136 {
137  itk::Point<float, 2> of(origin);
138  OriginType p;
139  p.CastFrom(of);
140  this->SetOutputOrigin(p);
141 }
142 
143 template <class TInputVectorData, class TOutputVectorData>
145 {
146  Superclass::GenerateOutputInformation();
147 
148  OutputVectorDataPointer output = this->GetOutput();
149  itk::MetaDataDictionary& dict = output->GetMetaDataDictionary();
150 
151  itk::EncapsulateMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
152 }
153 
157 template <class TInputVectorData, class TOutputVectorData>
160 {
161 
162  itk::Point<double, 2> point;
163  point = m_Transform->TransformPoint(pointCoord);
164  return point;
165 }
166 
170 template <class TInputVectorData, class TOutputVectorData>
173 {
174  typedef typename InputLineType::VertexListType::ConstPointer VertexListConstPointerType;
175  typedef typename InputLineType::VertexListConstIteratorType VertexListConstIteratorType;
176  VertexListConstPointerType vertexList = line->GetVertexList();
177  VertexListConstIteratorType it = vertexList->Begin();
178  typename OutputLineType::Pointer newLine = OutputLineType::New();
179  while (it != vertexList->End())
180  {
181  itk::Point<double, 2> point;
182  itk::ContinuousIndex<double, 2> index;
183  typename InputLineType::VertexType pointCoord = it.Value();
184  point = m_Transform->TransformPoint(pointCoord);
185  index[0] = point[0];
186  index[1] = point[1];
187  // otbMsgDevMacro(<< "Converting: " << it.Value() << " -> " << pointCoord << " -> " << point << " -> " << index);
188  newLine->AddVertex(index);
189  ++it;
190  }
192 
193  return newLine;
194 }
195 
199 template <class TInputVectorData, class TOutputVectorData>
202 {
203  typedef typename InputPolygonType::VertexListType::ConstPointer VertexListConstPointerType;
204  typedef typename InputPolygonType::VertexListConstIteratorType VertexListConstIteratorType;
205  VertexListConstPointerType vertexList = polygon->GetVertexList();
206  VertexListConstIteratorType it = vertexList->Begin();
207  typename OutputPolygonType::Pointer newPolygon = OutputPolygonType::New();
208  while (it != vertexList->End())
209  {
210  itk::Point<double, 2> point;
211  itk::ContinuousIndex<double, 2> index;
212  typename InputPolygonType::VertexType pointCoord = it.Value();
213  point = m_Transform->TransformPoint(pointCoord);
214  index[0] = point[0];
215  index[1] = point[1];
216  newPolygon->AddVertex(index);
217  ++it;
218  }
219  return newPolygon;
220 }
222 
226 template <class TInputVectorData, class TOutputVectorData>
229 {
230 
231  OutputPolygonListPointerType newPolygonList = OutputPolygonListType::New();
232  for (typename InputPolygonListType::ConstIterator it = polygonList->Begin(); it != polygonList->End(); ++it)
233  {
234  newPolygonList->PushBack(this->ProcessPolygon(it.Get()));
235  }
236  return newPolygonList;
237 }
238 
242 template <class TInputVectorData, class TOutputVectorData>
244 {
245  m_Transform = InternalTransformType::New();
246 
247  InputVectorDataPointer input = this->GetInput();
248  const itk::MetaDataDictionary& inputDict = input->GetMetaDataDictionary();
249 
250  OutputVectorDataPointer output = this->GetOutput();
251  itk::MetaDataDictionary& outputDict = output->GetMetaDataDictionary();
252 
253  m_Transform->SetInputImageMetadata(m_InputImageMetadata);
254  m_Transform->SetOutputImageMetadata(m_OutputImageMetadata);
255 
256  if (m_InputProjectionRef.empty())
257  {
258  itk::ExposeMetaData<std::string>(inputDict, MetaDataKey::ProjectionRefKey, m_InputProjectionRef);
259  }
260 
261  m_Transform->SetInputProjectionRef(m_InputProjectionRef);
262  m_Transform->SetOutputProjectionRef(m_OutputProjectionRef);
263  m_Transform->SetInputSpacing(m_InputSpacing);
264  m_Transform->SetInputOrigin(m_InputOrigin);
265  m_Transform->SetOutputSpacing(m_OutputSpacing);
266  m_Transform->SetOutputOrigin(m_OutputOrigin);
267 
268  m_Transform->InstantiateTransform();
269  // retrieve the output projection ref
270  // if it is not specified and end up being geographic,
271  // only the m_Transform will know
272  m_OutputProjectionRef = m_Transform->GetOutputProjectionRef();
273 
274  // If the projection information for the output is provided, propagate it
275  if (!m_OutputProjectionRef.empty())
276  {
277  itk::EncapsulateMetaData<std::string>(outputDict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
278  }
279  output->SetSpacing(m_OutputSpacing);
280  output->SetOrigin(m_OutputOrigin);
281 }
282 
286 template <class TInputVectorData, class TOutputVectorData>
288 {
289  this->AllocateOutputs();
290  InputVectorDataPointer inputPtr = this->GetInput();
291  OutputVectorDataPointer outputPtr = this->GetOutput();
293 
294  // Instantiate the transform
295  this->InstantiateTransform();
296 
297  // Create the output tree root
298  // typedef typename OutputVectorDataType::DataNodePointerType OutputDataNodePointerType;
299  // OutputDataNodePointerType outputRoot = OutputDataNodeType::New();
300  // outputRoot->SetNodeType(inputPtr->GetRoot()->GetNodeType());
301  // outputRoot->SetNodeId(inputPtr->GetRoot()->GetNodeId());
302  // outputPtr->SetRoot(outputRoot);
303 
304  // Start recursive processing
306  this->ProcessNode(inputPtr,inputPtr->GetRoot(),outputPtr,outputPtr->GetRoot());
307  chrono.Stop();
308  otbMsgDevMacro(<< "VectoDataProjectionFilter: features processed in " << chrono.GetElapsedMilliseconds() << " ms.");
309 }
310 
311 } // end namespace otb
312 
313 #endif
Stopwatch timer.
Definition: otbStopwatch.h:42
static Stopwatch StartNew()
DurationType GetElapsedMilliseconds() const
virtual void SetOutputOrigin(OriginType _arg)
virtual void SetInputSpacing(const SpacingType &spacing)
OutputPolygonPointerType ProcessPolygon(InputPolygonPointerType polygon) const override
virtual void SetOutputSpacing(const SpacingType &spacing)
virtual void SetInputOrigin(OriginType _arg)
OutputPointType ProcessPoint(InputPointType point) const override
OutputPolygonListType::Pointer OutputPolygonListPointerType
OutputPolygonType::Pointer OutputPolygonPointerType
OutputPolygonListPointerType ProcessPolygonList(InputPolygonListPointerType polygonList) const override
OutputDataNodeType::PointType OutputPointType
OutputLinePointerType ProcessLine(InputLinePointerType line) const override
TOutputVectorData::Pointer OutputVectorDataPointer
TInputVectorData::ConstPointer InputVectorDataPointer
InputPolygonListType::Pointer InputPolygonListPointerType
OutputPolygonListType::Pointer OutputPolygonListPointerType
OTBMetadata_EXPORT char const * ProjectionRefKey
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
#define otbMsgDevMacro(x)
Definition: otbMacro.h:116