OTB  6.7.0
Orfeo Toolbox
otbGenericMapProjection.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 otbGenericMapProjection_hxx
22 #define otbGenericMapProjection_hxx
23 
25 #include "otbMacro.h"
26 
27 namespace otb
28 {
29 
30 template<TransformDirection::TransformationDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions,
31  unsigned int NOutputDimensions>
33 ::GenericMapProjection() : Superclass(ParametersDimension), m_MapProjection()
34 {}
35 
36 template<TransformDirection::TransformationDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions,
37  unsigned int NOutputDimensions>
40 {
41 }
42 
43 template<TransformDirection::TransformationDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions,
44  unsigned int NOutputDimensions>
45 std::string
48 {
49  if(m_MapProjection)
50  {
51  // Use partial template specialisation instead ?
52  if(DirectionOfMapping == TransformDirection::FORWARD)
53  {
54  return m_MapProjection->GetTargetSpatialReference().ToWkt();
55  }
56  else
57  {
58  return m_MapProjection->GetSourceSpatialReference().ToWkt();
59  }
60  }
61  return "";
62 }
63 
64 template<TransformDirection::TransformationDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions,
65  unsigned int NOutputDimensions>
66 void
68 ::SetWkt(const std::string& projectionRefWkt)
69 {
71  SpatialReference wktSpatialReference = SpatialReference::FromDescription(projectionRefWkt);
72 
73 #if GDAL_VERSION_NUM >= 3000000
74  wgs84.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
75  wktSpatialReference.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
76 #endif
77 
78  if(DirectionOfMapping == TransformDirection::INVERSE)
79  {
80  std::unique_ptr<CoordinateTransformation> newMapProjection(new CoordinateTransformation(wktSpatialReference,wgs84));
81 
82  if(newMapProjection)
83  m_MapProjection.swap(newMapProjection);
84  }
85  else
86  {
87  std::unique_ptr<CoordinateTransformation> newMapProjection(new CoordinateTransformation(wgs84,wktSpatialReference));
88 
89  if(newMapProjection)
90  m_MapProjection.swap(newMapProjection);
91  }
92 
93  this->Modified();
94 }
95 
96 template<TransformDirection::TransformationDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions,
97  unsigned int NOutputDimensions>
100 ::TransformPoint(const InputPointType& point) const
101 {
102  OutputPointType outputPoint;
103 
104  // Can be collapsed
105  if (DirectionOfMapping == TransformDirection::INVERSE)
106  {
107  double lon, lat, h;
108  double z = 0.0;
109  if (InputPointType::PointDimension == 3) z = point[2];
110 
111  std::tie(lon,lat,h) = m_MapProjection->Transform(std::make_tuple(point[0], point[1], z));
112 
113  outputPoint[0] = lon;
114  outputPoint[1] = lat;
115  if (OutputPointType::PointDimension == 3) outputPoint[2] = h;
116 
117  }
118  if (DirectionOfMapping == TransformDirection::FORWARD)
119  {
120 
121  double x, y, z;
122  double h = 0.0;
123  if (InputPointType::PointDimension == 3) h = point[2];
124  std::tie(x,y,z) = m_MapProjection->Transform(std::make_tuple(point[0], point[1], h));
125  outputPoint[0] = x;
126  outputPoint[1] = y;
127  if (OutputPointType::PointDimension == 3) outputPoint[2] = z;
128  }
129 
130  return outputPoint;
131 }
132 
133 
134 template<TransformDirection::TransformationDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions,
135  unsigned int NOutputDimensions>
136 bool
139 {
140  return m_MapProjection != nullptr;
141 }
142 
143 template<TransformDirection::TransformationDirection TDirectionOfMapping, class TScalarType, unsigned int NInputDimensions,
144  unsigned int NOutputDimensions>
145 void
147 ::PrintSelf(std::ostream& os, itk::Indent indent) const
148 {
149  Superclass::PrintSelf(os, indent);
150  os << indent << *m_MapProjection.get() << std::endl;
151 }
152 
153 } // namespace otb
154 
155 #endif
This class is a wrapper around OGRCoordinateTransformation.
void PrintSelf(std::ostream &os, itk::Indent indent) const override
static SpatialReference FromDescription(const std::string &sr_description)
static SpatialReference FromWGS84()
This class is a wrapper around OGRSpatialReference.
OutputPointType TransformPoint(const InputPointType &point) const override
virtual void SetWkt(const std::string &projectionRefWkt)