OTB  9.0.0
Orfeo Toolbox
otbVectorDataProjectionFilter.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 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  typedef typename OutputVectorDataType::DataTreePointerType OutputDataTreePointerType;
298  OutputDataTreePointerType tree = outputPtr->GetDataTree();
299 
300  // Get the input tree root
301  InputInternalTreeNodeType* inputRoot = const_cast<InputInternalTreeNodeType*>(inputPtr->GetDataTree()->GetRoot());
302 
303  // Create the output tree root
304  typedef typename OutputVectorDataType::DataNodePointerType OutputDataNodePointerType;
305  OutputDataNodePointerType newDataNode = OutputDataNodeType::New();
306  newDataNode->SetNodeType(inputRoot->Get()->GetNodeType());
307  newDataNode->SetNodeId(inputRoot->Get()->GetNodeId());
308  typename OutputInternalTreeNodeType::Pointer outputRoot = OutputInternalTreeNodeType::New();
309  outputRoot->Set(newDataNode);
310  tree->SetRoot(outputRoot);
311 
312  // Start recursive processing
314  this->ProcessNode(inputRoot, outputRoot);
315  chrono.Stop();
316  otbMsgDevMacro(<< "VectoDataProjectionFilter: features processed in " << chrono.GetElapsedMilliseconds() << " ms.");
317 }
318 
319 } // end namespace otb
320 
321 #endif
otb::VectorDataProjectionFilter::SetOutputOrigin
virtual void SetOutputOrigin(OriginType _arg)
otb::VectorDataProjectionFilter::OutputPointType
OutputDataNodeType::PointType OutputPointType
Definition: otbVectorDataProjectionFilter.h:107
otb::VectorDataProjectionFilter::OutputLinePointerType
OutputLineType::Pointer OutputLinePointerType
Definition: otbVectorDataProjectionFilter.h:112
otb::VectorDataToVectorDataFilter::InputVectorDataPointer
TInputVectorData::ConstPointer InputVectorDataPointer
Definition: otbVectorDataToVectorDataFilter.h:61
otb::VectorDataProjectionFilter::GenerateData
void GenerateData(void) override
Definition: otbVectorDataProjectionFilter.hxx:287
otb::VectorDataToVectorDataFilter::OutputPolygonListPointerType
OutputPolygonListType::Pointer OutputPolygonListPointerType
Definition: otbVectorDataToVectorDataFilter.h:85
otb::VectorDataProjectionFilter::SetInputSpacing
virtual void SetInputSpacing(const SpacingType &spacing)
Definition: otbVectorDataProjectionFilter.hxx:49
otb::VectorDataProjectionFilter::VectorDataProjectionFilter
VectorDataProjectionFilter()
Definition: otbVectorDataProjectionFilter.hxx:36
otb::VectorDataProjectionFilter::OriginType
itk::Point< double, 2 > OriginType
Definition: otbVectorDataProjectionFilter.h:90
otb::VectorDataProjectionFilter::InstantiateTransform
virtual void InstantiateTransform(void)
Definition: otbVectorDataProjectionFilter.hxx:243
otb::VectorDataProjectionFilter::SetOutputSpacing
virtual void SetOutputSpacing(const SpacingType &spacing)
Definition: otbVectorDataProjectionFilter.hxx:97
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::VectorDataToVectorDataFilter::InputInternalTreeNodeType
InputVectorDataType::DataTreeType::TreeNodeType InputInternalTreeNodeType
Definition: otbVectorDataToVectorDataFilter.h:66
otb::VectorDataProjectionFilter::SpacingType
itk::Vector< double, 2 > SpacingType
Definition: otbVectorDataProjectionFilter.h:89
otb::VectorDataProjectionFilter::ProcessPolygon
OutputPolygonPointerType ProcessPolygon(InputPolygonPointerType polygon) const override
Definition: otbVectorDataProjectionFilter.hxx:201
otb::VectorDataToVectorDataFilter::InputPolygonListPointerType
InputPolygonListType::Pointer InputPolygonListPointerType
Definition: otbVectorDataToVectorDataFilter.h:76
otb::VectorDataToVectorDataFilter::InputPointType
InputDataNodeType::PointType InputPointType
Definition: otbVectorDataToVectorDataFilter.h:69
otb::Stopwatch
Stopwatch timer.
Definition: otbStopwatch.h:41
otb::VectorDataToVectorDataFilter::InputPolygonPointerType
InputPolygonType::Pointer InputPolygonPointerType
Definition: otbVectorDataToVectorDataFilter.h:75
otb::VectorDataProjectionFilter::OutputPolygonListPointerType
OutputPolygonListType::Pointer OutputPolygonListPointerType
Definition: otbVectorDataProjectionFilter.h:114
otb::Stopwatch::StartNew
static Stopwatch StartNew()
otbMetaDataKey.h
otb::VectorDataProjectionFilter::SetInputOrigin
virtual void SetInputOrigin(OriginType _arg)
otb::VectorDataProjectionFilter::GenerateOutputInformation
void GenerateOutputInformation(void) override
Definition: otbVectorDataProjectionFilter.hxx:144
otb::VectorDataToVectorDataFilter::InputLinePointerType
InputLineType::Pointer InputLinePointerType
Definition: otbVectorDataToVectorDataFilter.h:74
otb::VectorDataSource::OutputVectorDataPointer
TOutputVectorData::Pointer OutputVectorDataPointer
Definition: otbVectorDataSource.h:59
otb::Stopwatch::Stop
void Stop()
otbMsgDevMacro
#define otbMsgDevMacro(x)
Definition: otbMacro.h:64
otb::VectorDataProjectionFilter::ProcessPolygonList
OutputPolygonListPointerType ProcessPolygonList(InputPolygonListPointerType polygonList) const override
Definition: otbVectorDataProjectionFilter.hxx:228
otbVectorDataProjectionFilter.h
otb::VectorDataProjectionFilter::OutputPolygonPointerType
OutputPolygonType::Pointer OutputPolygonPointerType
Definition: otbVectorDataProjectionFilter.h:113
otb::Stopwatch::GetElapsedMilliseconds
DurationType GetElapsedMilliseconds() const
otb::VectorDataProjectionFilter::ProcessLine
OutputLinePointerType ProcessLine(InputLinePointerType line) const override
Definition: otbVectorDataProjectionFilter.hxx:172
otb::VectorDataProjectionFilter::ProcessPoint
OutputPointType ProcessPoint(InputPointType point) const override
Definition: otbVectorDataProjectionFilter.hxx:159
otb::MetaDataKey::ProjectionRefKey
OTBMetadata_EXPORT char const * ProjectionRefKey
otbStopwatch.h