OTB  6.1.0
Orfeo Toolbox
otbVectorDataProjectionFilter.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 otbVectorDataProjectionFilter_txx
22 #define otbVectorDataProjectionFilter_txx
23 
25 #include "itkProgressReporter.h"
26 #include "itkMetaDataObject.h"
27 #include "otbMetaDataKey.h"
28 #include "itkTimeProbe.h"
29 
30 namespace otb
31 {
35 template <class TInputVectorData, class TOutputVectorData>
38 {
39  m_InputProjectionRef.clear();
40  m_OutputProjectionRef.clear();
41  m_InputKeywordList.Clear();
42  m_OutputKeywordList.Clear();
43  m_InputSpacing.Fill(1);
44  m_InputOrigin.Fill(0);
45  m_OutputSpacing.Fill(1);
46  m_OutputOrigin.Fill(0);
47 }
49 
50 //----------------------------------------------------------------------------
51 template <class TInputVectorData, class TOutputVectorData>
52 void
55 {
56  itkDebugMacro("setting Spacing to " << spacing);
57  if (this->m_InputSpacing != spacing)
58  {
59  this->m_InputSpacing = spacing;
60  this->Modified();
61  }
62 }
63 
64 //----------------------------------------------------------------------------
65 template <class TInputVectorData, class TOutputVectorData>
66 void
68 ::SetInputSpacing(const double spacing[2])
69 {
70  SpacingType s(spacing);
71  this->SetInputSpacing(s);
72 }
73 
74 //----------------------------------------------------------------------------
75 template <class TInputVectorData, class TOutputVectorData>
76 void
78 ::SetInputSpacing(const float spacing[2])
79 {
80  itk::Vector<float, 2> sf(spacing);
81  SpacingType s;
82  s.CastFrom(sf);
83  this->SetInputSpacing(s);
84 }
85 
86 //----------------------------------------------------------------------------
87 template <class TInputVectorData, class TOutputVectorData>
88 void
90 ::SetInputOrigin(const double origin[2])
91 {
92  OriginType p(origin);
93  this->SetInputOrigin(p);
94 }
95 
96 //----------------------------------------------------------------------------
97 template <class TInputVectorData, class TOutputVectorData>
98 void
100 ::SetInputOrigin(const float origin[2])
101 {
102  itk::Point<float, 2> of(origin);
103  OriginType p;
104  p.CastFrom(of);
105  this->SetInputOrigin(p);
106 }
107 
108 //----------------------------------------------------------------------------
109 template <class TInputVectorData, class TOutputVectorData>
110 void
113 {
114  itkDebugMacro("setting Spacing to " << spacing);
115  if (this->m_OutputSpacing != spacing)
116  {
117  this->m_OutputSpacing = spacing;
118  this->Modified();
119  }
120 }
121 
122 //----------------------------------------------------------------------------
123 template <class TInputVectorData, class TOutputVectorData>
124 void
126 ::SetOutputSpacing(const double spacing[2])
127 {
128  SpacingType s(spacing);
129  this->SetOutputSpacing(s);
130 }
131 
132 //----------------------------------------------------------------------------
133 template <class TInputVectorData, class TOutputVectorData>
134 void
136 ::SetOutputSpacing(const float spacing[2])
137 {
138  itk::Vector<float, 2> sf(spacing);
139  SpacingType s;
140  s.CastFrom(sf);
141  this->SetOutputSpacing(s);
142 }
143 
144 //----------------------------------------------------------------------------
145 template <class TInputVectorData, class TOutputVectorData>
146 void
148 ::SetOutputOrigin(const double origin[2])
149 {
150  OriginType p(origin);
151  this->SetOutputOrigin(p);
152 }
153 
154 //----------------------------------------------------------------------------
155 template <class TInputVectorData, class TOutputVectorData>
156 void
158 ::SetOutputOrigin(const float origin[2])
159 {
160  itk::Point<float, 2> of(origin);
161  OriginType p;
162  p.CastFrom(of);
163  this->SetOutputOrigin(p);
164 }
165 
166 template <class TInputVectorData, class TOutputVectorData>
167 void
170 {
171  Superclass::GenerateOutputInformation();
172 
173  OutputVectorDataPointer output = this->GetOutput();
174  itk::MetaDataDictionary& dict = output->GetMetaDataDictionary();
175 
176  itk::EncapsulateMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
177 
178 }
179 
183 template <class TInputVectorData, class TOutputVectorData>
187 {
188 
189  itk::Point<double, 2> point;
190  point = m_Transform->TransformPoint(pointCoord);
191  return point;
192 }
193 
197 template <class TInputVectorData, class TOutputVectorData>
201 {
202  typedef typename InputLineType::VertexListType::ConstPointer VertexListConstPointerType;
203  typedef typename InputLineType::VertexListConstIteratorType VertexListConstIteratorType;
204  VertexListConstPointerType vertexList = line->GetVertexList();
205  VertexListConstIteratorType it = vertexList->Begin();
206  typename OutputLineType::Pointer newLine = OutputLineType::New();
207  while (it != vertexList->End())
208  {
209  itk::Point<double, 2> point;
211  typename InputLineType::VertexType pointCoord = it.Value();
212  point = m_Transform->TransformPoint(pointCoord);
213  index[0] = point[0];
214  index[1] = point[1];
215 // otbMsgDevMacro(<< "Converting: " << it.Value() << " -> " << pointCoord << " -> " << point << " -> " << index);
216  newLine->AddVertex(index);
217  ++it;
218  }
220 
221  return newLine;
222 }
223 
227 template <class TInputVectorData, class TOutputVectorData>
231 {
232  typedef typename InputPolygonType::VertexListType::ConstPointer VertexListConstPointerType;
233  typedef typename InputPolygonType::VertexListConstIteratorType VertexListConstIteratorType;
234  VertexListConstPointerType vertexList = polygon->GetVertexList();
235  VertexListConstIteratorType it = vertexList->Begin();
236  typename OutputPolygonType::Pointer newPolygon = OutputPolygonType::New();
237  while (it != vertexList->End())
238  {
239  itk::Point<double, 2> point;
241  typename InputPolygonType::VertexType pointCoord = it.Value();
242  point = m_Transform->TransformPoint(pointCoord);
243  index[0] = point[0];
244  index[1] = point[1];
245  newPolygon->AddVertex(index);
246  ++it;
247  }
248  return newPolygon;
249 }
251 
255 template <class TInputVectorData, class TOutputVectorData>
259 {
260 
261  OutputPolygonListPointerType newPolygonList = OutputPolygonListType::New();
262  for (typename InputPolygonListType::ConstIterator it = polygonList->Begin();
263  it != polygonList->End(); ++it)
264  {
265  newPolygonList->PushBack(this->ProcessPolygon(it.Get()));
266  }
267  return newPolygonList;
268 }
269 
273 template <class TInputVectorData, class TOutputVectorData>
274 void
277 {
278 
279 // otbMsgDevMacro(<< "Information to instantiate transform (VectorDataProjectionFilter): ");
280 // otbMsgDevMacro(<< " * Input Origin: " << m_InputOrigin);
281 // otbMsgDevMacro(<< " * Input Spacing: " << m_InputSpacing);
282 // otbMsgDevMacro(<< " * Input keyword list: "
283 // << ((m_InputKeywordList.GetSize() == 0)?"Empty":"Full"));
284 // otbMsgDevMacro(<< " * Input projection: " << m_InputProjectionRef);
285 // otbMsgDevMacro(<< " * Output keyword list: "
286 // << ((m_OutputKeywordList.GetSize() == 0)?"Empty":"Full"));
287 // otbMsgDevMacro(<< " * Output projection: " << m_OutputProjectionRef);
288 // otbMsgDevMacro(<< " * Output Origin: " << m_OutputOrigin);
289 // otbMsgDevMacro(<< " * Output Spacing: " << m_OutputSpacing);
290 
291  m_Transform = InternalTransformType::New();
292 
293  InputVectorDataPointer input = this->GetInput();
294  const itk::MetaDataDictionary& inputDict = input->GetMetaDataDictionary();
295 
296  OutputVectorDataPointer output = this->GetOutput();
297  itk::MetaDataDictionary& outputDict = output->GetMetaDataDictionary();
298 
299 // m_Transform->SetInputDictionary(input->GetMetaDataDictionary());
300  m_Transform->SetInputDictionary(inputDict);
301  m_Transform->SetOutputDictionary(output->GetMetaDataDictionary());
302 
303  m_Transform->SetInputProjectionRef(m_InputProjectionRef);
304  m_Transform->SetOutputProjectionRef(m_OutputProjectionRef);
305  m_Transform->SetInputKeywordList(m_InputKeywordList);
306  m_Transform->SetOutputKeywordList(m_OutputKeywordList);
307  m_Transform->SetInputSpacing(m_InputSpacing);
308  m_Transform->SetInputOrigin(m_InputOrigin);
309  m_Transform->SetOutputSpacing(m_OutputSpacing);
310  m_Transform->SetOutputOrigin(m_OutputOrigin);
311 
312  m_Transform->InstantiateTransform();
313  // retrieve the output projection ref
314  // if it is not specified and end up being geographic,
315  // only the m_Transform will know
316  m_OutputProjectionRef = m_Transform->GetOutputProjectionRef();
317 
318  //If the projection information for the output is provided, propagate it
319 
320  if (m_OutputKeywordList.GetSize() != 0)
321  {
322  itk::EncapsulateMetaData<ImageKeywordlist>(outputDict, MetaDataKey::OSSIMKeywordlistKey, m_OutputKeywordList);
323  }
324  if (!m_OutputProjectionRef.empty())
325  {
326  itk::EncapsulateMetaData<std::string>(outputDict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
327  }
328  output->SetSpacing(m_OutputSpacing);
329  output->SetOrigin(m_OutputOrigin);
330 
331 }
332 
336 template <class TInputVectorData, class TOutputVectorData>
337 void
340 {
341  this->AllocateOutputs();
342  InputVectorDataPointer inputPtr = this->GetInput();
343  OutputVectorDataPointer outputPtr = this->GetOutput();
345 
346  //Instantiate the transform
347  this->InstantiateTransform();
348 
349  typedef typename OutputVectorDataType::DataTreePointerType OutputDataTreePointerType;
350  OutputDataTreePointerType tree = outputPtr->GetDataTree();
351 
352 // Get the input tree root
353  InputInternalTreeNodeType * inputRoot = const_cast<InputInternalTreeNodeType *>(inputPtr->GetDataTree()->GetRoot());
354 
355  // Create the output tree root
356  typedef typename OutputVectorDataType::DataNodePointerType OutputDataNodePointerType;
357  OutputDataNodePointerType newDataNode = OutputDataNodeType::New();
358  newDataNode->SetNodeType(inputRoot->Get()->GetNodeType());
359  newDataNode->SetNodeId(inputRoot->Get()->GetNodeId());
360  typename OutputInternalTreeNodeType::Pointer outputRoot = OutputInternalTreeNodeType::New();
361  outputRoot->Set(newDataNode);
362  tree->SetRoot(outputRoot);
363 
364  // Start recursive processing
365  itk::TimeProbe chrono;
366  chrono.Start();
367  this->ProcessNode(inputRoot, outputRoot);
368  chrono.Stop();
369  otbMsgDevMacro(<< "VectoDataProjectionFilter: features Processed in " << chrono.GetMean() << " seconds.");
370 }
371 
372 } // end namespace otb
373 
374 #endif
void CastFrom(const Point< TCoordRepB, NPointDimension > &pa)
virtual void SetInputSpacing(const SpacingType &spacing)
virtual MeanType GetMean() const
OutputLinePointerType ProcessLine(InputLinePointerType line) const ITK_OVERRIDE
OTBOSSIMAdapters_EXPORT char const * ProjectionRefKey
OutputPolygonListPointerType ProcessPolygonList(InputPolygonListPointerType polygonList) const ITK_OVERRIDE
virtual void SetOutputSpacing(const SpacingType &spacing)
TOutputVectorData::Pointer OutputVectorDataPointer
OutputPolygonListType::Pointer OutputPolygonListPointerType
OutputPointType ProcessPoint(InputPointType point) const ITK_OVERRIDE
virtual void Stop()
virtual void Start()
virtual void SetInputOrigin(OriginType _arg)
void CastFrom(const Vector< TCoordRepB, NVectorDimension > &pa)
InputVectorDataType::DataTreeType::TreeNodeType InputInternalTreeNodeType
OutputPolygonListType::Pointer OutputPolygonListPointerType
virtual void SetOutputOrigin(OriginType _arg)
InputPolygonListType::Pointer InputPolygonListPointerType
TInputVectorData::ConstPointer InputVectorDataPointer
OutputPolygonType::Pointer OutputPolygonPointerType
OutputPolygonPointerType ProcessPolygon(InputPolygonPointerType polygon) const ITK_OVERRIDE
OTBOSSIMAdapters_EXPORT char const * OSSIMKeywordlistKey
#define otbMsgDevMacro(x)
Definition: otbMacro.h:98
OutputDataNodeType::PointType OutputPointType