OTB  10.0.0
Orfeo Toolbox
otbGenericRSTransform.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 otbGenericRSTransform_hxx
22 #define otbGenericRSTransform_hxx
23 
24 #include "otbGenericRSTransform.h"
25 #include "otbMacro.h"
26 #include "otbRPCForwardTransform.h"
27 #include "otbRPCInverseTransform.h"
28 
29 #include "otbSpatialReference.h"
30 
31 #include "ogr_spatialref.h"
33 
34 namespace otb
35 {
36 
37 template <class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
39 {
40  m_InputProjectionRef.clear();
41  m_OutputProjectionRef.clear();
42  m_InputSpacing.Fill(1);
43  m_InputOrigin.Fill(0);
44  m_OutputSpacing.Fill(1);
45  m_OutputOrigin.Fill(0);
46 
47  m_InputImd = nullptr;
48  m_OutputImd = nullptr;
49  m_Transform = nullptr;
50  m_InputTransform = nullptr;
51  m_OutputTransform = nullptr;
52  m_TransformUpToDate = false;
53  m_TransformAccuracy = Projection::UNKNOWN;
54 }
55 
56 template <class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
57 const typename GenericRSTransform<TScalarType, NInputDimensions, NOutputDimensions>::TransformType*
58 GenericRSTransform<TScalarType, NInputDimensions, NOutputDimensions>::GetTransform() const
59 {
60  itkDebugMacro("returning MapProjection address " << this->m_Transform);
61  if ((!m_TransformUpToDate) || (m_Transform.IsNull()))
62  {
63  itkExceptionMacro(<< "m_Transform not up-to-date, call InstantiateTransform() first");
64  }
65 
66  return this->m_Transform;
67 }
68 
72 template <class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
73 void GenericRSTransform<TScalarType, NInputDimensions, NOutputDimensions>::InstantiateTransform(void)
74 {
75  m_Transform = TransformType::New();
76 
77  if (m_InputProjectionRef.empty() && m_InputImd != nullptr && m_InputImd->HasProjectedGeometry())
78  {
79  m_InputProjectionRef = m_InputImd->GetProjectionWKT();
80  }
81 
82  otbMsgDevMacro(<< "Information to instantiate transform: ");
83  otbMsgDevMacro(<< " * Input Origin: " << m_InputOrigin);
84  otbMsgDevMacro(<< " * Input Spacing: " << m_InputSpacing);
85  otbMsgDevMacro(<< " * Input metadata: " << ((m_InputImd == nullptr) ? "Empty" : "Full"));
86  otbMsgDevMacro(<< " * Input projection: " << m_InputProjectionRef);
87  otbMsgDevMacro(<< " * Output metadata: " << ((m_OutputImd == nullptr) ? "Empty" : "Full"));
88  otbMsgDevMacro(<< " * Output projection: " << m_OutputProjectionRef);
89  otbMsgDevMacro(<< " * Output Origin: " << m_OutputOrigin);
90  otbMsgDevMacro(<< " * Output Spacing: " << m_OutputSpacing);
91 
92  // Make sure that the state is clean:
93  m_InputTransform = nullptr;
94  m_OutputTransform = nullptr;
95 
96  bool inputTransformIsSensor = false;
97  bool inputTransformIsMap = false;
98  bool outputTransformIsSensor = false;
99  bool outputTransformIsMap = false;
100 
101 /*****************************
102  // Set the input transformation
103 
104 /*****************************
105 
106  // First, try to make a geo transform
107  if (!m_InputProjectionRef.empty()) // map projection
108  {
109  typedef otb::GenericMapProjection<TransformDirection::INVERSE, ScalarType, InputSpaceDimension, InputSpaceDimension> InverseMapProjectionType;
110  typename InverseMapProjectionType::Pointer mapTransform = InverseMapProjectionType::New();
111 
112  mapTransform->SetWkt(m_InputProjectionRef);
113  if (mapTransform->IsProjectionDefined())
114  {
115  m_InputTransform = mapTransform.GetPointer();
116  inputTransformIsMap = true;
117  otbMsgDevMacro(<< "Input projection set to map transform: " << m_InputTransform);
118  }
119  }
120 
121  // If not, try to make a sensor model
122  if ((m_InputTransform.IsNull()) && (m_InputImd != nullptr))
123  {
124  auto sensorModel = otb::SensorTransformFactory::GetInstance().CreateTransform
125  <double, InputSpaceDimension, OutputSpaceDimension>(*m_InputImd,TransformDirection::FORWARD);
126 
127  if (sensorModel != nullptr)
128  {
129  m_InputTransform = sensorModel.GetPointer();
130  inputTransformIsSensor = true;
131  otbMsgDevMacro(<< "Input projection set to RPC model.");
132  }
133  }
134 
135 /*****************************
136  // Set the output transformation
137