OTB  9.0.0
Orfeo Toolbox
otbGeographicalDistance.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 otbGeographicalDistance_hxx
22 #define otbGeographicalDistance_hxx
23 
25 #include "otbMath.h"
26 
27 namespace otb
28 {
29 
30 template <class TVector>
32 {
33 }
34 
35 
36 template <class TVector>
38 {
39  // First check if vector length is sufficient
40  if (x.Size() < 2)
41  itkExceptionMacro(<< "Vector length must be at least 2 to compute geographical distance.");
42 
43  // Call evaluate implementation with the first point being the
44  // origin
45  VectorType origin(x);
46  origin[0] = this->GetOrigin()[0];
47  origin[1] = this->GetOrigin()[1];
48 
49  return this->Evaluate(origin, x);
50 }
51 
52 template <class TVector>
54 {
55  // First check if vector length is sufficient
56  if (x.Size() < 2 || y.Size() < 2)
57  itkExceptionMacro(<< "Vector length must be at least 2 to compute geographical distance.");
58 
59  // Build some const variables
60  const double One = itk::NumericTraits<double>::One;
61  const double Two = One + One;
62  const double Deg2Rad = CONST_PI / 180.;
63 
64  // Compute latitude and longitude differences
65  double dLat = (std::fabs(x[1] - y[1])) * Deg2Rad;
66  double dLon = (std::fabs(x[0] - y[0])) * Deg2Rad;
67 
68  // Compute dx in meters
69  double a = std::sin(dLat / Two) * std::sin(dLat / Two) + std::cos(y[1] * Deg2Rad) * std::cos(x[1] * Deg2Rad) * std::sin(dLon / Two) * std::sin(dLon / Two);
70  double c = Two * std::atan2(std::sqrt(a), std::sqrt(One - a));
71  double d = m_EarthRadius * c;
72 
73  // Return result
74  return d;
75 }
76 
77 template <class TVector>
78 void GeographicalDistance<TVector>::PrintSelf(std::ostream& os, itk::Indent indent) const
79 {
80  // Call superclass implementation
81  Superclass::PrintSelf(os, indent);
82 
83  // Earth radius
84  os << indent << "Earth radius: " << m_EarthRadius << std::endl;
85 }
86 } // End namespace otb
87 
88 #endif
otb::CONST_PI
constexpr double CONST_PI
Definition: otbMath.h:49
otbGeographicalDistance.h
otbMath.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::GeographicalDistance::GeographicalDistance
GeographicalDistance()
Definition: otbGeographicalDistance.hxx:31
otb::GeographicalDistance::Evaluate
double Evaluate(const VectorType &x) const override
Definition: otbGeographicalDistance.hxx:37
otb::GeographicalDistance::VectorType
TVector VectorType
Definition: otbGeographicalDistance.h:64
otb::GeographicalDistance::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbGeographicalDistance.hxx:78