OTB  10.0.0
Orfeo Toolbox
otbVectorDataIntoImageProjectionFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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>
34 {
35  m_OutputSpacing.Fill(1);
36  m_OutputOrigin.Fill(0);
37  m_UseOutputSpacingAndOriginFromImage = false;
39 
40  m_VdExtractFilter = VectorDataExtractROIType::New();
41  m_VdProjFilter = VectorDataProjectionFilterType::New();
42 
43  m_VdProjFilter->SetInput(m_VdExtractFilter->GetOutput());
44 }
45 
46 template <class TInputVectorData, class TInputImage>
48 {
49  m_UseOutputSpacingAndOriginFromImage = flag;
50 }
51 
52 //----------------------------------------------------------------------------
53 template <class TInputVectorData, class TOutputVectorData>
55 {
56  itkDebugMacro("setting Spacing to " << spacing);
57  if (this->m_OutputSpacing != spacing)
58  {
59  this->m_OutputSpacing = spacing;
60  this->Modified();
61  }
62 }
63 
64 //----------------------------------------------------------------------------
65 template <class TInputVectorData, class TOutputVectorData>
67 {
68  SpacingType s(spacing);
69  this->SetOutputSpacing(s);
70 }
71 
72 //----------------------------------------------------------------------------
73 template <class TInputVectorData, class TOutputVectorData>
75 {
76  itk::Vector<float, 2> sf(spacing);
77  SpacingType s;
78  s.CastFrom(sf);
79  this->SetOutputSpacing(s);
80 }
81 
82 //----------------------------------------------------------------------------
83 template <class TInputVectorData, class TOutputVectorData>
85 {
86  OriginType p(origin);
87  this->SetOutputOrigin(p);
88 }
89 
90 //----------------------------------------------------------------------------
91 template <class TInputVectorData, class TOutputVectorData>
93 {
94  itk::Point<float, 2> of(origin);
95  OriginType p;
96  p.CastFrom(of);
97  this->SetOutputOrigin(p);
98 }
99 
100 
104 template <class TInputVectorData, class TInputImage>
106 {
107  m_VdExtractFilter->SetInput(this->GetInput());
108 
109  typedef typename ImageType::PointType PointType;
110  typedef typename ImageType::SizeType SizeType;
111 
112  if (m_InputImage.IsNull())
113  {
114  itkExceptionMacro("Invalid input image.");
115  }
116 
117  /*std::cout << "Spacing of the input image: "<< m_InputImage->GetSignedSpacing() << std::endl;
118  std::cout << "Origin of the input image: "<< m_InputImage->GetOrigin() << std::endl;
119  std::cout << "Size of the input image: "<< m_InputImage->GetLargestPossibleRegion() << std::endl;
120  std::cout << "ProjRef of the input image: "<< m_InputImage->GetProjectionRef() << std::endl;
121  std::cout << "ProjRef of the input vector data: "<< this->GetInput()->GetProjectionRef() << std::endl; */
122 
123  // Get the index of the corner of the image
124  PointType pul, pur, pll, plr;
125  itk::ContinuousIndex<double, 2> ul(m_InputImage->GetLargestPossibleRegion().GetIndex());
126  ul[0] += -0.5;
127  ul[1] += -0.5;
128 
129  itk::ContinuousIndex<double, 2> ur(ul);
130  itk::ContinuousIndex<double, 2> ll(ul);
131  itk::ContinuousIndex<double, 2> lr(ul);
132 
133  SizeType size = m_InputImage->GetLargestPossibleRegion().GetSize();
134  ur[0] += size[0];
135  lr[0] += size[0];
136  lr[1] += size[1];
137  ll[1] += size[1];
138 
139  // std::cout << "bounding box of the input image (pixel): "<< ur << ", " << ul << ", " << lr << ", " << ll << std::endl;
140 
141  // Transform to physical point
142  m_InputImage->TransformContinuousIndexToPhysicalPoint(ul, pul);
143  m_InputImage->TransformContinuousIndexToPhysicalPoint(ur, pur);
144  m_InputImage->TransformContinuousIndexToPhysicalPoint(ll, pll);
145  m_InputImage->TransformContinuousIndexToPhysicalPoint(lr, plr);
146  // std::cout << "bounding box of the input image (physical): "<< pur << ", " << pul << ", " << plr << ", " << pll << std::endl;
147 
148  // Build the cartographic region
149  RemoteSensingRegionType rsRegion;
150  typename RemoteSensingRegionType::IndexType rsOrigin;
151  typename RemoteSensingRegionType::SizeType rsSize;
152  rsOrigin[0] = std::min(pul[0], plr[0]);
153  rsOrigin[1] = std::min(pul[1], plr[1]);
154  rsSize[0] = std::abs(pul[0] - plr[0]);
155  rsSize[1] = std::abs(pul[1] - plr[1]);
156 
157  rsRegion.SetOrigin(rsOrigin);
158  rsRegion.SetSize(rsSize);
159 
160  if (m_InputImage->GetProjectionRef().empty() || boost::algorithm::istarts_with(m_InputImage->GetProjectionRef(), "LOCAL_CS"))
161  {
162  rsRegion.SetImageMetadata(m_InputImage->GetImageMetadata());
163  }
164  else
165  {
166  rsRegion.SetRegionProjection(m_InputImage->GetProjectionRef());
167  }
168 
169  // std::cout << "remote sensing region origin and size (physical): " << rsOrigin << ", " << rsSize << std::endl;
170  // std::cout << "remote sensing region ProjRef: " << rsRegion.GetRegionProjection() << std::endl;
171 
172  // Set the cartographic region to the extract roi filter
173  m_VdExtractFilter->SetRegion(rsRegion);
174 
175  // Reproject VectorData in image projection
176  m_VdProjFilter->SetInputProjectionRef(this->GetInput()->GetProjectionRef());
177 
178  if (m_InputImage->GetProjectionRef().empty() || boost::algorithm::istarts_with(m_InputImage->GetProjectionRef(), "LOCAL_CS"))
179  {
180  m_VdProjFilter->SetOutputImageMetadata(&m_InputImage->GetImageMetadata());
181  }
182  else
183  {
184  m_VdProjFilter->SetOutputProjectionRef(m_InputImage->GetProjectionRef());
185  }
186 
187  if (m_UseOutputSpacingAndOriginFromImage)
188  {
189  m_VdProjFilter->SetOutputOrigin(m_InputImage->GetOrigin());
190  m_VdProjFilter->SetOutputSpacing(m_InputImage->GetSignedSpacing());
191  }
192  else
193  {
194  m_VdProjFilter->SetOutputOrigin(this->GetOutputOrigin());
195  m_VdProjFilter->SetOutputSpacing(this->GetOutputSpacing());
196  }
197 
198  m_VdProjFilter->Update();
199 
200  if (m_UseOutputSpacingAndOriginFromImage)
201  {
202  m_VdProjFilter->GetOutput()->SetProjectionRef("");
203  }
204 
205  this->GraftOutput(m_VdProjFilter->GetOutput());
206  this->UpdateProgress(1.0f);
207 }
208 
209 
210 } // end namespace otb
211 
212 #endif
void SetSize(const SizeType &size)
void SetRegionProjection(const std::string &projection)
void SetOrigin(const IndexType &index)
void SetImageMetadata(const ImageMetadata &imd)
virtual void SetOutputSpacing(const SpacingType &spacing)
virtual void SetOutputOrigin(OriginType _arg)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.