OTB  5.0.0
Orfeo Toolbox
otbVectorDataProjectionFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbVectorDataProjectionFilter_txx
19 #define __otbVectorDataProjectionFilter_txx
20 
22 #include "itkProgressReporter.h"
23 #include "itkMetaDataObject.h"
24 #include "otbMetaDataKey.h"
25 #include "itkTimeProbe.h"
26 
27 namespace otb
28 {
32 template <class TInputVectorData, class TOutputVectorData>
35 {
36  m_InputProjectionRef.clear();
37  m_OutputProjectionRef.clear();
38  m_InputKeywordList.Clear();
39  m_OutputKeywordList.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>
49 void
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>
63 void
65 ::SetInputSpacing(const double spacing[2])
66 {
67  SpacingType s(spacing);
68  this->SetInputSpacing(s);
69 }
70 
71 //----------------------------------------------------------------------------
72 template <class TInputVectorData, class TOutputVectorData>
73 void
75 ::SetInputSpacing(const float spacing[2])
76 {
77  itk::Vector<float, 2> sf(spacing);
78  SpacingType s;
79  s.CastFrom(sf);
80  this->SetInputSpacing(s);
81 }
82 
83 //----------------------------------------------------------------------------
84 template <class TInputVectorData, class TOutputVectorData>
85 void
87 ::SetInputOrigin(const double origin[2])
88 {
89  OriginType p(origin);
90  this->SetInputOrigin(p);
91 }
92 
93 //----------------------------------------------------------------------------
94 template <class TInputVectorData, class TOutputVectorData>
95 void
97 ::SetInputOrigin(const float origin[2])
98 {
99  itk::Point<float, 2> of(origin);
100  OriginType p;
101  p.CastFrom(of);
102  this->SetInputOrigin(p);
103 }
104 
105 //----------------------------------------------------------------------------
106 template <class TInputVectorData, class TOutputVectorData>
107 void
110 {
111  itkDebugMacro("setting Spacing to " << spacing);
112  if (this->m_OutputSpacing != spacing)
113  {
114  this->m_OutputSpacing = spacing;
115  this->Modified();
116  }
117 }
118 
119 //----------------------------------------------------------------------------
120 template <class TInputVectorData, class TOutputVectorData>
121 void
123 ::SetOutputSpacing(const double spacing[2])
124 {
125  SpacingType s(spacing);
126  this->SetOutputSpacing(s);
127 }
128 
129 //----------------------------------------------------------------------------
130 template <class TInputVectorData, class TOutputVectorData>
131 void
133 ::SetOutputSpacing(const float spacing[2])
134 {
135  itk::Vector<float, 2> sf(spacing);
136  SpacingType s;
137  s.CastFrom(sf);
138  this->SetOutputSpacing(s);
139 }
140 
141 //----------------------------------------------------------------------------
142 template <class TInputVectorData, class TOutputVectorData>
143 void
145 ::SetOutputOrigin(const double origin[2])
146 {
147  OriginType p(origin);
148  this->SetOutputOrigin(p);
149 }
150 
151 //----------------------------------------------------------------------------
152 template <class TInputVectorData, class TOutputVectorData>
153 void
155 ::SetOutputOrigin(const float origin[2])
156 {
157  itk::Point<float, 2> of(origin);
158  OriginType p;
159  p.CastFrom(of);
160  this->SetOutputOrigin(p);
161 }
162 
163 template <class TInputVectorData, class TOutputVectorData>
164 void
167 {
168  Superclass::GenerateOutputInformation();
169 
170  OutputVectorDataPointer output = this->GetOutput();
171  itk::MetaDataDictionary& dict = output->GetMetaDataDictionary();
172 
173  itk::EncapsulateMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
174 
175 }
176 
180 template <class TInputVectorData, class TOutputVectorData>
184 {
185 
186  itk::Point<double, 2> point;
187  point = m_Transform->TransformPoint(pointCoord);
188  return point;
189 }
190 
194 template <class TInputVectorData, class TOutputVectorData>
198 {
199  typedef typename InputLineType::VertexListType::ConstPointer VertexListConstPointerType;
200  typedef typename InputLineType::VertexListConstIteratorType VertexListConstIteratorType;
201  VertexListConstPointerType vertexList = line->GetVertexList();
202  VertexListConstIteratorType it = vertexList->Begin();
203  typename OutputLineType::Pointer newLine = OutputLineType::New();
204  while (it != vertexList->End())
205  {
206  itk::Point<double, 2> point;
208  typename InputLineType::VertexType pointCoord = it.Value();
209  point = m_Transform->TransformPoint(pointCoord);
210  index[0] = point[0];
211  index[1] = point[1];
212 // otbMsgDevMacro(<< "Converting: " << it.Value() << " -> " << pointCoord << " -> " << point << " -> " << index);
213  newLine->AddVertex(index);
214  ++it;
215  }
217 
218  return newLine;
219 }
220 
224 template <class TInputVectorData, class TOutputVectorData>
228 {
229  typedef typename InputPolygonType::VertexListType::ConstPointer VertexListConstPointerType;
230  typedef typename InputPolygonType::VertexListConstIteratorType VertexListConstIteratorType;
231  VertexListConstPointerType vertexList = polygon->GetVertexList();
232  VertexListConstIteratorType it = vertexList->Begin();
233  typename OutputPolygonType::Pointer newPolygon = OutputPolygonType::New();
234  while (it != vertexList->End())
235  {
236  itk::Point<double, 2> point;
238  typename InputPolygonType::VertexType pointCoord = it.Value();
239  point = m_Transform->TransformPoint(pointCoord);
240  index[0] = point[0];
241  index[1] = point[1];
242  newPolygon->AddVertex(index);
243  ++it;
244  }
245  return newPolygon;
246 }
248 
252 template <class TInputVectorData, class TOutputVectorData>
256 {
257 
258  OutputPolygonListPointerType newPolygonList = OutputPolygonListType::New();
259  for (typename InputPolygonListType::ConstIterator it = polygonList->Begin();
260  it != polygonList->End(); ++it)
261  {
262  newPolygonList->PushBack(this->ProcessPolygon(it.Get()));
263  }
264  return newPolygonList;
265 }
266 
270 template <class TInputVectorData, class TOutputVectorData>
271 void
274 {
275 
276 // otbMsgDevMacro(<< "Information to instanciate transform (VectorDataProjectionFilter): ");
277 // otbMsgDevMacro(<< " * Input Origin: " << m_InputOrigin);
278 // otbMsgDevMacro(<< " * Input Spacing: " << m_InputSpacing);
279 // otbMsgDevMacro(<< " * Input keyword list: "
280 // << ((m_InputKeywordList.GetSize() == 0)?"Empty":"Full"));
281 // otbMsgDevMacro(<< " * Input projection: " << m_InputProjectionRef);
282 // otbMsgDevMacro(<< " * Output keyword list: "
283 // << ((m_OutputKeywordList.GetSize() == 0)?"Empty":"Full"));
284 // otbMsgDevMacro(<< " * Output projection: " << m_OutputProjectionRef);
285 // otbMsgDevMacro(<< " * Output Origin: " << m_OutputOrigin);
286 // otbMsgDevMacro(<< " * Output Spacing: " << m_OutputSpacing);
287 
288  m_Transform = InternalTransformType::New();
289 
290  InputVectorDataPointer input = this->GetInput();
291  const itk::MetaDataDictionary& inputDict = input->GetMetaDataDictionary();
292 
293  OutputVectorDataPointer output = this->GetOutput();
294  itk::MetaDataDictionary& outputDict = output->GetMetaDataDictionary();
295 
296 // m_Transform->SetInputDictionary(input->GetMetaDataDictionary());
297  m_Transform->SetInputDictionary(inputDict);
298  m_Transform->SetOutputDictionary(output->GetMetaDataDictionary());
299 
300  m_Transform->SetInputProjectionRef(m_InputProjectionRef);
301  m_Transform->SetOutputProjectionRef(m_OutputProjectionRef);
302  m_Transform->SetInputKeywordList(m_InputKeywordList);
303  m_Transform->SetOutputKeywordList(m_OutputKeywordList);
304  m_Transform->SetInputSpacing(m_InputSpacing);
305  m_Transform->SetInputOrigin(m_InputOrigin);
306  m_Transform->SetOutputSpacing(m_OutputSpacing);
307  m_Transform->SetOutputOrigin(m_OutputOrigin);
308 
309  m_Transform->InstanciateTransform();
310  // retrieve the output projection ref
311  // if it is not specified and end up being geographic,
312  // only the m_Transform will know
313  m_OutputProjectionRef = m_Transform->GetOutputProjectionRef();
314 
315  //If the projection information for the output is provided, propagate it
316 
317  if (m_OutputKeywordList.GetSize() != 0)
318  {
319  itk::EncapsulateMetaData<ImageKeywordlist>(outputDict, MetaDataKey::OSSIMKeywordlistKey, m_OutputKeywordList);
320  }
321  if (!m_OutputProjectionRef.empty())
322  {
323  itk::EncapsulateMetaData<std::string>(outputDict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
324  }
325  output->SetSpacing(m_OutputSpacing);
326  output->SetOrigin(m_OutputOrigin);
327 
328 }
329 
333 template <class TInputVectorData, class TOutputVectorData>
334 void
337 {
338  this->AllocateOutputs();
339  InputVectorDataPointer inputPtr = this->GetInput();
340  OutputVectorDataPointer outputPtr = this->GetOutput();
342 
343  //Instanciate the transform
344  this->InstanciateTransform();
345 
346  typedef typename OutputVectorDataType::DataTreePointerType OutputDataTreePointerType;
347  OutputDataTreePointerType tree = outputPtr->GetDataTree();
348 
349 // Get the input tree root
350  InputInternalTreeNodeType * inputRoot = const_cast<InputInternalTreeNodeType *>(inputPtr->GetDataTree()->GetRoot());
351 
352  // Create the output tree root
353  typedef typename OutputVectorDataType::DataNodePointerType OutputDataNodePointerType;
354  OutputDataNodePointerType newDataNode = OutputDataNodeType::New();
355  newDataNode->SetNodeType(inputRoot->Get()->GetNodeType());
356  newDataNode->SetNodeId(inputRoot->Get()->GetNodeId());
357  typename OutputInternalTreeNodeType::Pointer outputRoot = OutputInternalTreeNodeType::New();
358  outputRoot->Set(newDataNode);
359  tree->SetRoot(outputRoot);
360 
361  // Start recursive processing
362  itk::TimeProbe chrono;
363  chrono.Start();
364  this->ProcessNode(inputRoot, outputRoot);
365  chrono.Stop();
366  otbMsgDevMacro(<< "VectoDataProjectionFilter: features Processed in " << chrono.GetMean() << " seconds.");
367 }
368 
369 } // end namespace otb
370 
371 #endif
void CastFrom(const Point< TCoordRepB, NPointDimension > &pa)
virtual void SetInputSpacing(const SpacingType &spacing)
MeanType GetMean(void) const
virtual OutputPointType ProcessPoint(InputPointType point) const
virtual OutputLinePointerType ProcessLine(InputLinePointerType line) const
virtual void SetOutputSpacing(const SpacingType &spacing)
TOutputVectorData::Pointer OutputVectorDataPointer
OutputPolygonListType::Pointer OutputPolygonListPointerType
virtual void SetInputOrigin(OriginType _arg)
void CastFrom(const Vector< TCoordRepB, NVectorDimension > &pa)
virtual OutputPolygonListPointerType ProcessPolygonList(InputPolygonListPointerType polygonList) const
InputVectorDataType::DataTreeType::TreeNodeType InputInternalTreeNodeType
OutputPolygonListType::Pointer OutputPolygonListPointerType
virtual void SetOutputOrigin(OriginType _arg)
InputPolygonListType::Pointer InputPolygonListPointerType
TInputVectorData::ConstPointer InputVectorDataPointer
virtual OutputPolygonPointerType ProcessPolygon(InputPolygonPointerType polygon) const
char const * ProjectionRefKey
char const * OSSIMKeywordlistKey
OutputPolygonType::Pointer OutputPolygonPointerType
#define otbMsgDevMacro(x)
Definition: otbMacro.h:95
OutputDataNodeType::PointType OutputPointType