OTB  6.7.0
Orfeo Toolbox
otbVectorDataIntoImageProjectionFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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 otbVectorDataIntoImageProjectionFilter_hxx
22 #define otbVectorDataIntoImageProjectionFilter_hxx
23 
26 
27 namespace otb
28 {
32 template <class TInputVectorData, class TInputImage>
35 {
36  m_OutputSpacing.Fill(1);
37  m_OutputOrigin.Fill(0);
38  m_UseOutputSpacingAndOriginFromImage = false;
40 
41  m_VdExtractFilter = VectorDataExtractROIType::New();
42  m_VdProjFilter = VectorDataProjectionFilterType::New();
43 
44  m_VdProjFilter->SetInput(m_VdExtractFilter->GetOutput());
45 }
46 
47 template <class TInputVectorData, class TInputImage>
48 void
51 {
52  m_UseOutputSpacingAndOriginFromImage = flag;
53 }
54 
55 //----------------------------------------------------------------------------
56 template <class TInputVectorData, class TOutputVectorData>
57 void
60 {
61  itkDebugMacro("setting Spacing to " << spacing);
62  if (this->m_OutputSpacing != spacing)
63  {
64  this->m_OutputSpacing = spacing;
65  this->Modified();
66  }
67 }
68 
69 //----------------------------------------------------------------------------
70 template <class TInputVectorData, class TOutputVectorData>
71 void
73 ::SetOutputSpacing(const double spacing[2])
74 {
75  SpacingType s(spacing);
76  this->SetOutputSpacing(s);
77 }
78 
79 //----------------------------------------------------------------------------
80 template <class TInputVectorData, class TOutputVectorData>
81 void
83 ::SetOutputSpacing(const float spacing[2])
84 {
85  itk::Vector<float, 2> sf(spacing);
86  SpacingType s;
87  s.CastFrom(sf);
88  this->SetOutputSpacing(s);
89 }
90 
91 //----------------------------------------------------------------------------
92 template <class TInputVectorData, class TOutputVectorData>
93 void
95 ::SetOutputOrigin(const double origin[2])
96 {
97  OriginType p(origin);
98  this->SetOutputOrigin(p);
99 }
100 
101 //----------------------------------------------------------------------------
102 template <class TInputVectorData, class TOutputVectorData>
103 void
105 ::SetOutputOrigin(const float origin[2])
106 {
107  itk::Point<float, 2> of(origin);
108  OriginType p;
109  p.CastFrom(of);
110  this->SetOutputOrigin(p);
111 }
112 
113 
117 template <class TInputVectorData, class TInputImage>
118 void
121 {
122  m_VdExtractFilter->SetInput(this->GetInput());
123 
124  typedef typename ImageType::PointType PointType;
125  typedef typename ImageType::SizeType SizeType;
126 
127  if (m_InputImage.IsNull())
128  {
129  itkExceptionMacro("Invalid input image.");
130  }
131 
132  /*std::cout << "Spacing of the input image: "<< m_InputImage->GetSignedSpacing() << std::endl;
133  std::cout << "Origin of the input image: "<< m_InputImage->GetOrigin() << std::endl;
134  std::cout << "Size of the input image: "<< m_InputImage->GetLargestPossibleRegion() << std::endl;
135  std::cout << "ProjRef of the input image: "<< m_InputImage->GetProjectionRef() << std::endl;
136  std::cout << "ProjRef of the input vector data: "<< this->GetInput()->GetProjectionRef() << std::endl; */
137 
138  // Get the index of the corner of the image
139  PointType pul, pur, pll, plr;
140  itk::ContinuousIndex<double,2> ul(m_InputImage->GetLargestPossibleRegion().GetIndex());
141  ul[0] += -0.5;
142  ul[1] += -0.5;
143 
147 
148  SizeType size = m_InputImage->GetLargestPossibleRegion().GetSize();
149  ur[0] += size[0];
150  lr[0] += size[0];
151  lr[1] += size[1];
152  ll[1] += size[1];
153 
154  //std::cout << "bounding box of the input image (pixel): "<< ur << ", " << ul << ", " << lr << ", " << ll << std::endl;
155 
156  // Transform to physical point
157  m_InputImage->TransformContinuousIndexToPhysicalPoint(ul, pul);
158  m_InputImage->TransformContinuousIndexToPhysicalPoint(ur, pur);
159  m_InputImage->TransformContinuousIndexToPhysicalPoint(ll, pll);
160  m_InputImage->TransformContinuousIndexToPhysicalPoint(lr, plr);
161  //std::cout << "bounding box of the input image (physical): "<< pur << ", " << pul << ", " << plr << ", " << pll << std::endl;
162 
163  // Build the cartographic region
164  RemoteSensingRegionType rsRegion;
165  typename RemoteSensingRegionType::IndexType rsOrigin;
166  typename RemoteSensingRegionType::SizeType rsSize;
167  rsOrigin[0] = std::min(pul[0], plr[0]);
168  rsOrigin[1] = std::min(pul[1], plr[1]);
169  rsSize[0] = std::abs(pul[0] - plr[0]);
170  rsSize[1] = std::abs(pul[1] - plr[1]);
171 
172  rsRegion.SetOrigin(rsOrigin);
173  rsRegion.SetSize(rsSize);
174 
175  if (m_InputImage->GetProjectionRef().empty()
176  || boost::algorithm::istarts_with(m_InputImage->GetProjectionRef(), "LOCAL_CS") )
177  {
178  rsRegion.SetKeywordList(m_InputImage->GetImageKeywordlist());
179  }
180  else
181  {
182  rsRegion.SetRegionProjection(m_InputImage->GetProjectionRef());
183  }
184 
185  //std::cout << "remote sensing region origin and size (physical): " << rsOrigin << ", " << rsSize << std::endl;
186  //std::cout << "remote sensing region ProjRef: " << rsRegion.GetRegionProjection() << std::endl;
187 
188  // Set the cartographic region to the extract roi filter
189  m_VdExtractFilter->SetRegion(rsRegion);
190 
191  // Reproject VectorData in image projection
192  m_VdProjFilter->SetInputProjectionRef(this->GetInput()->GetProjectionRef());
193 
194  if (m_InputImage->GetProjectionRef().empty()
195  || boost::algorithm::istarts_with(m_InputImage->GetProjectionRef(), "LOCAL_CS") )
196  {
197  m_VdProjFilter->SetOutputKeywordList(m_InputImage->GetImageKeywordlist());
198  }
199  else
200  {
201  m_VdProjFilter->SetOutputProjectionRef(m_InputImage->GetProjectionRef());
202  }
203 
204  if (m_UseOutputSpacingAndOriginFromImage)
205  {
206  m_VdProjFilter->SetOutputOrigin(m_InputImage->GetOrigin());
207  m_VdProjFilter->SetOutputSpacing(m_InputImage->GetSignedSpacing());
208  }
209  else
210  {
211  m_VdProjFilter->SetOutputOrigin(this->GetOutputOrigin());
212  m_VdProjFilter->SetOutputSpacing(this->GetOutputSpacing());
213  }
214 
215  m_VdProjFilter->Update();
216 
217  /*
218  TreeIteratorType itVector(m_VdProjFilter->GetOutput()->GetDataTree());
219  itVector.GoToBegin();
220  while (!itVector.IsAtEnd())
221  {
222  if (itVector.Get()->IsPolygonFeature())
223  {
224  std::cout << "----------\n" <<itVector.Get()->GetNodeTypeAsString() << std::endl;
225  std::cout << "List of vertices: " << ": " <<std::endl;
226  for (unsigned int itPoints = 0; itPoints < itVector.Get()->GetPolygonExteriorRing()->GetVertexList()->Size(); itPoints++)
227  {
228  std::cout << "vertex[" << itPoints << "]: " << itVector.Get()->GetPolygonExteriorRing()->GetVertexList()->GetElement(itPoints) <<std::endl;
229  }
230  std::cout << "Polygon region: " << itVector.Get()->GetPolygonExteriorRing()->GetBoundingRegion() << std::endl;
231  }
232  ++itVector;
233  }
234  */
235 
236  if (m_UseOutputSpacingAndOriginFromImage)
237  {
238  m_VdProjFilter->GetOutput()->SetProjectionRef("");
239  }
240 
241  this->GraftOutput(m_VdProjFilter->GetOutput());
242  this->UpdateProgress(1.0f);
243 }
244 
245 
246 } // end namespace otb
247 
248 #endif
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:137
void CastFrom(const Point< TCoordRepB, NPointDimension > &pa)
void SetKeywordList(const ImageKeywordlist &kwl)
virtual void SetOutputSpacing(const SpacingType &spacing)
void SetOrigin(const IndexType &index)
void CastFrom(const Vector< TCoordRepB, NVectorDimension > &pa)
void SetRegionProjection(const std::string &projection)
void SetSize(const SizeType &size)
virtual void SetOutputOrigin(OriginType _arg)
VectorImageType::PointType PointType
Definition: mvdTypes.h:189