OTB  6.7.0
Orfeo Toolbox
otbDEMToImageGenerator.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 otbDEMToImageGenerator_hxx
22 #define otbDEMToImageGenerator_hxx
23 
24 #include "otbDEMToImageGenerator.h"
25 #include "otbMacro.h"
26 #include "itkProgressReporter.h"
27 
28 namespace otb
29 {
30 
31 template<class TDEMImage>
34 {
35  m_DEMHandler = DEMHandlerType::Instance();
36  m_OutputSpacing[0] = 0.0001;
37  m_OutputSpacing[1] = -0.0001;
38  m_OutputSize[0] = 1;
39  m_OutputSize[1] = 1;
40  m_OutputOrigin[0] = 0;
41  m_OutputOrigin[1] = 0;
42  m_DefaultUnknownValue = itk::NumericTraits<PixelType>::ZeroValue();
43  m_AboveEllipsoid = false;
44 
45  m_Transform = GenericRSTransformType::New();
46 }
47 
48 // GenerateOutputInformation method
49 template <class TDEMImage>
52 {
53  DEMImageType *output;
54  output = this->GetOutput(0);
55 
56  IndexType start;
57  start[0] = 0;
58  start[1] = 0;
59 
60  // Specify region parameters
61  OutputImageRegionType largestPossibleRegion;
62  largestPossibleRegion.SetSize(m_OutputSize);
63  largestPossibleRegion.SetIndex(start);
64 
65  output->SetLargestPossibleRegion(largestPossibleRegion);
66  output->SetSignedSpacing(m_OutputSpacing);
67  output->SetOrigin(m_OutputOrigin);
68 
69 
70 // Get the Output MetaData Dictionary
71  itk::MetaDataDictionary& dict = output->GetMetaDataDictionary();
72 
73  // Encapsulate the metadata set by the user
74  itk::EncapsulateMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey,
75  m_Transform->GetInputProjectionRef());
76 
77  if (this->GetOutputKeywordList().GetSize() > 0)
78  {
79  itk::EncapsulateMetaData<ImageKeywordlist>(dict, MetaDataKey::OSSIMKeywordlistKey,
80  m_Transform->GetInputKeywordList());
81  }
82 }
83 
84 // InstantiateTransform method
85 template <class TDEMImage>
88 {
89  m_Transform->InstantiateTransform();
90 }
91 
92 template <class TDEMImage>
95 {
96  InstantiateTransform();
97  DEMImagePointerType DEMImage = this->GetOutput();
98 
99  // allocate the output buffer
100  DEMImage->SetBufferedRegion(DEMImage->GetRequestedRegion());
101  DEMImage->Allocate();
102  DEMImage->FillBuffer(0);
103 }
104 
105 
106 template <class TDEMImage>
107 void
109 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
110  itk::ThreadIdType threadId)
111 {
112  DEMImagePointerType DEMImage = this->GetOutput();
113 
114  // Create an iterator that will walk the output region
115  ImageIteratorType outIt = ImageIteratorType(DEMImage, outputRegionForThread);
116 
117  // support progress methods/callbacks
118  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
119 
120  // Walk the output image, evaluating the height at each pixel
121  IndexType currentindex;
122  PointType phyPoint;
123  double height;
124  PointType geoPoint;
125 
126  for (outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt)
127  {
128  currentindex = outIt.GetIndex();
129  DEMImage->TransformIndexToPhysicalPoint(currentindex, phyPoint);
130 
131 
132  if(m_Transform.IsNotNull())
133  {
134  geoPoint = m_Transform->TransformPoint(phyPoint);
135  if(m_AboveEllipsoid)
136  {
137  height = m_DEMHandler->GetHeightAboveEllipsoid(geoPoint); // Altitude
138  // calculation
139  }
140  else
141  {
142  height = m_DEMHandler->GetHeightAboveMSL(geoPoint); // Altitude
143  // calculation
144  }
145  }
146  else
147  {
148  if(m_AboveEllipsoid)
149  {
150  height = m_DEMHandler->GetHeightAboveEllipsoid(phyPoint); // Altitude
151  // calculation
152  }
153  else
154  {
155  height = m_DEMHandler->GetHeightAboveMSL(phyPoint); // Altitude
156  // calculation
157  }
158  }
159 /* otbMsgDevMacro(<< "Index : (" << currentindex[0]<< "," << currentindex[1] << ") -> PhyPoint : ("
160  << phyPoint[0] << "," << phyPoint[1] << ") -> GeoPoint: ("
161  << geoPoint[0] << "," << geoPoint[1] << ") -> height" << height);
162 */
163 // otbMsgDevMacro(<< "height" << height);
164  // DEM sets a default value (-32768) at point where it doesn't have altitude information.
165  // OSSIM has chosen to change this default value in OSSIM_DBL_NAN (-4.5036e15).
166  if (!vnl_math_isnan(height))
167  {
168  // Fill the image
169  DEMImage->SetPixel(currentindex, static_cast<PixelType>(height));
170  }
171  else
172  {
173  // Back to the MNT default value
174  DEMImage->SetPixel(currentindex, m_DefaultUnknownValue);
175  }
176  progress.CompletedPixel();
177  }
178 }
179 
180 template <class TDEMImage>
181 void
183 ::PrintSelf(std::ostream& os, itk::Indent indent) const
184 {
185  Superclass::PrintSelf(os, indent);
186 
187  os << indent << "Output Spacing:" << m_OutputSpacing[0] << "," << m_OutputSpacing[1] << std::endl;
188  os << indent << "Output Origin:" << m_OutputOrigin[0] << "," << m_OutputOrigin[1] << std::endl;
189  os << indent << "Output Size:" << m_OutputSize[0] << "," << m_OutputSize[1] << std::endl;
190 }
191 
192 } // namespace otb
193 
194 #endif
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
void BeforeThreadedGenerateData() override
DEMImageType::Pointer DEMImagePointerType
static T ZeroValue()
OTBOSSIMAdapters_EXPORT char const * ProjectionRefKey
const IndexType & GetIndex() const
OutputImageType::PointType PointType
void PrintSelf(std::ostream &os, itk::Indent indent) const override
OutputImageType::IndexType IndexType
unsigned int ThreadIdType
Superclass::OutputImageRegionType OutputImageRegionType
OTBOSSIMAdapters_EXPORT char const * OSSIMKeywordlistKey