OTB  7.2.0
Orfeo Toolbox
otbVectorDataProjectionFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2020 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_InputKeywordList.Clear();
41  m_OutputKeywordList.Clear();
42  m_InputSpacing.Fill(1);
43  m_InputOrigin.Fill(0);
44  m_OutputSpacing.Fill(1);
45  m_OutputOrigin.Fill(0);
46 }
48 
49 //----------------------------------------------------------------------------
50 template <class TInputVectorData, class TOutputVectorData>
52 {
53  itkDebugMacro("setting Spacing to " << spacing);
54  if (this->m_InputSpacing != spacing)
55  {
56  this->m_InputSpacing = spacing;
57  this->Modified();
58  }
59 }
60 
61 //----------------------------------------------------------------------------
62 template <class TInputVectorData, class TOutputVectorData>
64 {
65  SpacingType s(spacing);
66  this->SetInputSpacing(s);
67 }
68 
69 //----------------------------------------------------------------------------
70 template <class TInputVectorData, class TOutputVectorData>
72 {
73  itk::Vector<float, 2> sf(spacing);
74  SpacingType s;
75  s.CastFrom(sf);
76  this->SetInputSpacing(s);
77 }
78 
79 //----------------------------------------------------------------------------
80 template <class TInputVectorData, class TOutputVectorData>
82 {
83  OriginType p(origin);
84  this->SetInputOrigin(p);
85 }
86 
87 //----------------------------------------------------------------------------
88 template <class TInputVectorData, class TOutputVectorData>
90 {
91  itk::Point<float, 2> of(origin);
92  OriginType p;
93  p.CastFrom(of);
94  this->SetInputOrigin(p);
95 }
96 
97 //----------------------------------------------------------------------------
98 template <class TInputVectorData, class TOutputVectorData>
100 {
101  itkDebugMacro("setting Spacing to " << spacing);
102  if (this->m_OutputSpacing != spacing)
103  {
104  this->m_OutputSpacing = spacing;
105  this->Modified();
106  }
107 }
108 
109 //----------------------------------------------------------------------------
110 template <class TInputVectorData, class TOutputVectorData>
112 {
113  SpacingType s(spacing);
114  this->SetOutputSpacing(s);
115 }
116 
117 //----------------------------------------------------------------------------
118 template <class TInputVectorData, class TOutputVectorData>
120 {
121  itk::Vector<float, 2> sf(spacing);
122  SpacingType s;
123  s.CastFrom(sf);
124  this->SetOutputSpacing(s);
125 }
126 
127 //----------------------------------------------------------------------------
128 template <class TInputVectorData, class TOutputVectorData>
130 {
131  OriginType p(origin);
132  this->SetOutputOrigin(p);
133 }
134 
135 //----------------------------------------------------------------------------
136 template <class TInputVectorData, class TOutputVectorData>
138 {
139  itk::Point<float, 2> of(origin);
140  OriginType p;
141  p.CastFrom(of);
142  this->SetOutputOrigin(p);
143 }
144 
145 template <class TInputVectorData, class TOutputVectorData>
147 {
148  Superclass::GenerateOutputInformation();
149 
150  OutputVectorDataPointer output = this->GetOutput();
151  itk::MetaDataDictionary& dict = output->GetMetaDataDictionary();
152 
153  itk::EncapsulateMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
154 }
155 
159 template <class TInputVectorData, class TOutputVectorData>
162 {
163 
164  itk::Point<double, 2> point;
165  point = m_Transform->TransformPoint(pointCoord);
166  return point;
167 }
168 
172 template <class TInputVectorData, class TOutputVectorData>
175 {
176  typedef typename InputLineType::VertexListType::ConstPointer VertexListConstPointerType;
177  typedef typename InputLineType::VertexListConstIteratorType VertexListConstIteratorType;
178  VertexListConstPointerType vertexList = line->GetVertexList();
179  VertexListConstIteratorType it = vertexList->Begin();
180  typename OutputLineType::Pointer newLine = OutputLineType::New();
181  while (it != vertexList->End())
182  {
183  itk::Point<double, 2> point;
184  itk::ContinuousIndex<double, 2> index;
185  typename InputLineType::VertexType pointCoord = it.Value();
186  point = m_Transform->TransformPoint(pointCoord);
187  index[0] = point[0];
188  index[1] = point[1];
189  // otbMsgDevMacro(<< "Converting: " << it.Value() << " -> " << pointCoord << " -> " << point << " -> " << index);
190  newLine->AddVertex(index);
191  ++it;
192  }
194 
195  return newLine;
196 }
197 
201 template <class TInputVectorData, class TOutputVectorData>
204 {
205  typedef typename InputPolygonType::VertexListType::ConstPointer VertexListConstPointerType;
206  typedef typename InputPolygonType::VertexListConstIteratorType VertexListConstIteratorType;
207  VertexListConstPointerType vertexList = polygon->GetVertexList();
208  VertexListConstIteratorType it = vertexList->Begin();
209  typename OutputPolygonType::Pointer newPolygon = OutputPolygonType::New();
210  while (it != vertexList->End())
211  {
212  itk::Point<double, 2> point;
213  itk::ContinuousIndex<double, 2> index;
214  typename InputPolygonType::VertexType pointCoord = it.Value();
215  point = m_Transform->TransformPoint(pointCoord);
216  index[0] = point[0];
217  index[1] = point[1];
218  newPolygon->AddVertex(index);
219  ++it;
220  }
221  return newPolygon;
222 }
224 
228 template <class TInputVectorData, class TOutputVectorData>
231 {
232 
233  OutputPolygonListPointerType newPolygonList = OutputPolygonListType::New();
234  for (typename InputPolygonListType::ConstIterator it = polygonList->Begin(); it != polygonList->End(); ++it)
235  {
236  newPolygonList->PushBack(this->ProcessPolygon(it.Get()));
237  }
238  return newPolygonList;
239 }
240 
244 template <class TInputVectorData, class TOutputVectorData>
246 {
247 
248  // otbMsgDevMacro(<< "Information to instantiate transform (VectorDataProjectionFilter): ");
249  // otbMsgDevMacro(<< " * Input Origin: " << m_InputOrigin);
250  // otbMsgDevMacro(<< " * Input Spacing: " << m_InputSpacing);
251  // otbMsgDevMacro(<< " * Input keyword list: "
252  // << ((m_InputKeywordList.GetSize() == 0)?"Empty":"Full"));
253  // otbMsgDevMacro(<< " * Input projection: " << m_InputProjectionRef);
254  // otbMsgDevMacro(<< " * Output keyword list: "
255  // << ((m_OutputKeywordList.GetSize() == 0)?"Empty":"Full"));
256  // otbMsgDevMacro(<< " * Output projection: " << m_OutputProjectionRef);
257  // otbMsgDevMacro(<< " * Output Origin: " << m_OutputOrigin);
258  // otbMsgDevMacro(<< " * Output Spacing: " << m_OutputSpacing);
259 
260  m_Transform = InternalTransformType::New();
261 
262  InputVectorDataPointer input = this->GetInput();
263  const itk::MetaDataDictionary& inputDict = input->GetMetaDataDictionary();
264 
265  OutputVectorDataPointer output = this->GetOutput();
266  itk::MetaDataDictionary& outputDict = output->GetMetaDataDictionary();
267 
268  // m_Transform->SetInputDictionary(input->GetMetaDataDictionary());
269  m_Transform->SetInputDictionary(inputDict);
270  m_Transform->SetOutputDictionary(output->GetMetaDataDictionary());
271 
272  m_Transform->SetInputProjectionRef(m_InputProjectionRef);
273  m_Transform->SetOutputProjectionRef(m_OutputProjectionRef);
274  m_Transform->SetInputKeywordList(m_InputKeywordList);
275  m_Transform->SetOutputKeywordList(m_OutputKeywordList);
276  m_Transform->SetInputSpacing(m_InputSpacing);
277  m_Transform->SetInputOrigin(m_InputOrigin);
278  m_Transform->SetOutputSpacing(m_OutputSpacing);
279  m_Transform->SetOutputOrigin(m_OutputOrigin);
280 
281  m_Transform->InstantiateTransform();
282  // retrieve the output projection ref
283  // if it is not specified and end up being geographic,
284  // only the m_Transform will know
285  m_OutputProjectionRef = m_Transform->GetOutputProjectionRef();
286 
287  // If the projection information for the output is provided, propagate it
288 
289  if (m_OutputKeywordList.GetSize() != 0)
290  {
291  itk::EncapsulateMetaData<ImageKeywordlist>(outputDict, MetaDataKey::OSSIMKeywordlistKey, m_OutputKeywordList);
292  }
293  if (!m_OutputProjectionRef.empty())
294  {
295  itk::EncapsulateMetaData<std::string>(outputDict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
296  }
297  output->SetSpacing(m_OutputSpacing);
298  output->SetOrigin(m_OutputOrigin);
299 }
300 
304 template <class TInputVectorData, class TOutputVectorData>
306 {
307  this->AllocateOutputs();
308  InputVectorDataPointer inputPtr = this->GetInput();
309  OutputVectorDataPointer outputPtr = this->GetOutput();
311 
312  // Instantiate the transform
313  this->InstantiateTransform();
314 
315  typedef typename OutputVectorDataType::DataTreePointerType OutputDataTreePointerType;
316  OutputDataTreePointerType tree = outputPtr->GetDataTree();
317 
318  // Get the input tree root
319  InputInternalTreeNodeType* inputRoot = const_cast<InputInternalTreeNodeType*>(inputPtr->GetDataTree()->GetRoot());
320 
321  // Create the output tree root
322  typedef typename OutputVectorDataType::DataNodePointerType OutputDataNodePointerType;
323  OutputDataNodePointerType newDataNode = OutputDataNodeType::New();
324  newDataNode->SetNodeType(inputRoot->Get()->GetNodeType());
325  newDataNode->SetNodeId(inputRoot->Get()->GetNodeId());
326  typename OutputInternalTreeNodeType::Pointer outputRoot = OutputInternalTreeNodeType::New();
327  outputRoot->Set(newDataNode);
328  tree->SetRoot(outputRoot);
329 
330  // Start recursive processing
332  this->ProcessNode(inputRoot, outputRoot);
333  chrono.Stop();
334  otbMsgDevMacro(<< "VectoDataProjectionFilter: features processed in " << chrono.GetElapsedMilliseconds() << " ms.");
335 }
336 
337 } // end namespace otb
338 
339 #endif
virtual void SetInputSpacing(const SpacingType &spacing)
static Stopwatch StartNew()
Stopwatch timer.
Definition: otbStopwatch.h:41
DurationType GetElapsedMilliseconds() const
OutputPolygonPointerType ProcessPolygon(InputPolygonPointerType polygon) const override
virtual void SetOutputSpacing(const SpacingType &spacing)
TOutputVectorData::Pointer OutputVectorDataPointer
OutputPolygonListType::Pointer OutputPolygonListPointerType
OTBMetadata_EXPORT char const * OSSIMKeywordlistKey
virtual void SetInputOrigin(OriginType _arg)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
InputVectorDataType::DataTreeType::TreeNodeType InputInternalTreeNodeType
OutputPolygonListType::Pointer OutputPolygonListPointerType
virtual void SetOutputOrigin(OriginType _arg)
OutputPolygonListPointerType ProcessPolygonList(InputPolygonListPointerType polygonList) const override
InputPolygonListType::Pointer InputPolygonListPointerType
OutputLinePointerType ProcessLine(InputLinePointerType line) const override
TInputVectorData::ConstPointer InputVectorDataPointer
OutputPolygonType::Pointer OutputPolygonPointerType
OTBMetadata_EXPORT char const * ProjectionRefKey
OutputPointType ProcessPoint(InputPointType point) const override
#define otbMsgDevMacro(x)
Definition: otbMacro.h:64
OutputDataNodeType::PointType OutputPointType