OTB  9.0.0
Orfeo Toolbox
otbGenericMapProjection.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 otbGenericMapProjection_hxx
22 #define otbGenericMapProjection_hxx
23 
25 #include "otbMacro.h"
26 
27 namespace otb
28 {
29 
30 template <TransformDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
32  : Superclass(ParametersDimension), m_MapProjection()
33 {
34 }
35 
36 template <TransformDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
38 {
39 }
40 
41 template <TransformDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
43 {
44  if (m_MapProjection)
45  {
46  // Use partial template specialisation instead ?
47  if (DirectionOfMapping == TransformDirection::FORWARD)
48  {
49  return m_MapProjection->GetTargetSpatialReference().ToWkt();
50  }
51  else
52  {
53  return m_MapProjection->GetSourceSpatialReference().ToWkt();
54  }
55  }
56  return "";
57 }
58 
59 template <TransformDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
61 {
63  SpatialReference wktSpatialReference = SpatialReference::FromDescription(projectionRefWkt);
64 
65 #if GDAL_VERSION_NUM >= 3000000
66  wgs84.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
67  wktSpatialReference.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
68 #endif
69 
70  if (DirectionOfMapping == TransformDirection::INVERSE)
71  {
72  std::unique_ptr<CoordinateTransformation> newMapProjection(new CoordinateTransformation(wktSpatialReference, wgs84));
73 
74  if (newMapProjection)
75  m_MapProjection.swap(newMapProjection);
76  }
77  else
78  {
79  std::unique_ptr<CoordinateTransformation> newMapProjection(new CoordinateTransformation(wgs84, wktSpatialReference));
80 
81  if (newMapProjection)
82  m_MapProjection.swap(newMapProjection);
83  }
84 
85  this->Modified();
86 }
87 
88 template <TransformDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
91 {
92  OutputPointType outputPoint;
93 
94  // Can be collapsed
95  if (DirectionOfMapping == TransformDirection::INVERSE)
96  {
97  double lon, lat, h;
98  double z = 0.0;
99  if (InputPointType::PointDimension == 3)
100  z = point[2];
101 
102  std::tie(lon, lat, h) = m_MapProjection->Transform(std::make_tuple(point[0], point[1], z));
103 
104  outputPoint[0] = lon;
105  outputPoint[1] = lat;
106  if (OutputPointType::PointDimension == 3)
107  outputPoint[2] = h;
108  }
109  if (DirectionOfMapping == TransformDirection::FORWARD)
110  {
111 
112  double x, y, z;
113  double h = 0.0;
114  if (InputPointType::PointDimension == 3)
115  h = point[2];
116  std::tie(x, y, z) = m_MapProjection->Transform(std::make_tuple(point[0], point[1], h));
117  outputPoint[0] = x;
118  outputPoint[1] = y;
119  if (OutputPointType::PointDimension == 3)
120  outputPoint[2] = z;
121  }
122 
123  return outputPoint;
124 }
125 
126 
127 template <TransformDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
129 {
130  return m_MapProjection != nullptr;
131 }
132 
133 template <TransformDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
135 {
136  Superclass::PrintSelf(os, indent);
137  os << indent << *m_MapProjection.get() << std::endl;
138 }
139 
140 } // namespace otb
141 
142 #endif
otb::GenericMapProjection::SetWkt
virtual void SetWkt(const std::string &projectionRefWkt)
Definition: otbGenericMapProjection.hxx:60
otb::CoordinateTransformation
This class is a wrapper around OGRCoordinateTransformation.
Definition: otbCoordinateTransformation.h:77
otb::GenericMapProjection::IsProjectionDefined
bool IsProjectionDefined() const
Definition: otbGenericMapProjection.hxx:128
otbGenericMapProjection.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::GenericMapProjection::~GenericMapProjection
~GenericMapProjection() override
Definition: otbGenericMapProjection.hxx:37
otbMacro.h
otb::SpatialReference
This class is a wrapper around OGRSpatialReference.
Definition: otbSpatialReference.h:79
otb::GenericMapProjection::GetWkt
virtual std::string GetWkt()
Definition: otbGenericMapProjection.hxx:42
otb::GenericMapProjection::OutputPointType
itk::Point< ScalarType, NOutputDimensions > OutputPointType
Definition: otbGenericMapProjection.h:63
otb::GenericMapProjection::TransformPoint
OutputPointType TransformPoint(const InputPointType &point) const override
Definition: otbGenericMapProjection.hxx:90
otb::Transform
Class to overload method passed to virtual pure in ITK V4.
Definition: otbTransform.h:39
otb::GenericMapProjection::InputPointType
itk::Point< ScalarType, NInputDimensions > InputPointType
Definition: otbGenericMapProjection.h:62
otb::GenericMapProjection::GenericMapProjection
GenericMapProjection()
Definition: otbGenericMapProjection.hxx:31
otb::TransformDirection::INVERSE
@ INVERSE
otb::TransformDirection::FORWARD
@ FORWARD
otb::SpatialReference::FromWGS84
static SpatialReference FromWGS84()
otb::SpatialReference::FromDescription
static SpatialReference FromDescription(const std::string &sr_description)
otb::GenericMapProjection::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbGenericMapProjection.hxx:134