coordinateconversion.hh 6.46 KB
Newer Older
1
2
3
4
#ifndef RADIX_RADIXGEO_COORDINATECONVERSION_HH_
#define RADIX_RADIXGEO_COORDINATECONVERSION_HH_

#include "radixcore/visibility.hh"
LEFEBVREJP email's avatar
LEFEBVREJP email committed
5
#include "radixmath/util.hh"
6

LEFEBVREJP email's avatar
LEFEBVREJP email committed
7
8
#include <cmath>
#include <memory>
9
10
#include <sstream>
#include <stdexcept>
LEFEBVREJP email's avatar
LEFEBVREJP email committed
11

12
13
14
15
namespace radix
{
class RADIX_PUBLIC CoordinateConversion
{
LEFEBVREJP email's avatar
LEFEBVREJP email committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 public:
  struct UTMCoordinate
  {
    // earth radius is ~40million meteres
    // int will cover this
    int easting;
    int northing;
    // 00 -> 60 longitude zones
    short longitude_zone;
    // Not officially part of UTM
    // but frequently used with UTM
    // C - > X, (A,B,Y,Z) are not used as they cover western and eastern sides
    // of the Antarctic and Arctic regions
    char lattitude_zone;
  };

  class UTMZones
  {
    static const std::array<char, 22> letters;

    static const std::array<short, 22> degrees;

    static const std::array<char, 11> neg_letters;

    static const std::array<short, 11> neg_degrees;

    static const std::array<char, 11> pos_letters;

    static const std::array<short, 11> pos_degrees;

   public:
    static short latZoneDegree(char letter);
    static short lonZone(double longitude);
    static char latZone(double latitude);
  };  // UTMZone

 protected:
  class LatLon2UTM
  {
   private:
    // equatorial radius
    double m_equatorialRadius = double(6378137);

    // polar radius
    double m_polarRadius = double(6356752.314);

    // flattening
    double m_flattening = double(
        0.00335281066474748);  // (equatorialRadius-polarRadius)/equatorialRadius;

    // inverse flattening 1/flattening
    double m_inverseFlattening = double(298.257223563);  // 1/flattening;

    // Mean radius
    double m_rm =
        std::pow(m_equatorialRadius * m_polarRadius, double(1.) / double(2.0));

    // scale factor
    double m_k0 = double(0.9996);

    // eccentricity
    double m_e = std::sqrt(
        double(1) - std::pow(m_polarRadius / m_equatorialRadius, double(2.)));

    double m_e1sq = m_e * m_e / (double(1) - m_e * m_e);

    double m_n = (m_equatorialRadius - m_polarRadius) /
                 (m_equatorialRadius + m_polarRadius);

    // r curv 1
    double m_rho = double(6368573.744);

    // r curv 2
    double m_nu = double(6389236.914);

    // Calculate Meridional Arc Length
    // Meridional Arc
    double m_S = double(5103266.421);

    double m_A0 = double(6367449.146);

    double m_B0 = double(16038.42955);

    double m_C0 = double(16.83261333);

    double m_D0 = double(0.021984404);

    double m_E0 = double(0.000312705);

    // Calculation Constants
    // Delta Long
    double m_p = double(-0.483084);

    double m_sin1 = double(4.84814E-06);

    // Coefficients for UTM Coordinates
    double m_K1 = double(5101225.115);

    double m_K2 = double(3750.291596);

    double m_K3 = double(1.397608151);

    double m_K4 = double(214839.3105);

    double m_K5 = double(-2.995382942);

    double m_A6 = double(-1.00541E-07);

    void init(double latitude, double longitude);
  };  // class LatLon2UTM

127
 public:
128
129
130
131
132
133
134
  /**
   * @brief validate Validates coordinate
   * @param latitude latitude coordinate
   * @param longitude longitude coordinate
   * Throws std::out_of_range exception if latitude is outside [-90.,90.] or
   * longitude must be [-180,180).
   */
LEFEBVREJP email's avatar
LEFEBVREJP email committed
135
  static void validate(double latitude, double longitude);
136
137
138
139
140
141
142
  /**
   * @brief validate Validates coordinate
   * @parm point where point.first is latitutde and point.second is longitude
   * Throws std::out_of_range exception if latitude is outside [-90.,90.] or
   * longitude must be [-180,180).
   */

LEFEBVREJP email's avatar
LEFEBVREJP email committed
143
  static void validate(const std::pair<double, double>& point);
144
145

};  // class CoordinateConversion
146

LEFEBVREJP email's avatar
LEFEBVREJP email committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
void CoordinateConversion::LatLon2UTM::init(double latitude, double longitude)
{
  latitude = radix::toRadians(latitude);
  m_rho    = m_equatorialRadius * (double(1.) - m_e * m_e) /
          std::pow(double(1.) - std::pow(m_e * std::sin(latitude), double(2)),
                   double(3.) / double(2.0));

  m_nu = m_equatorialRadius /
         std::pow(double(1) - std::pow(m_e * std::sin(latitude), double(2)),
                  (double(1) / double(2.0)));

  double var1;
  if (longitude < double(0.))
  {
    var1 = ((int)((180 + longitude) / double(6.))) + 1;
  }
  else
  {
    var1 = ((int)(longitude / 6)) + 31;
  }
  double var2 = (6 * var1) - 183;
  double var3 = longitude - var2;
  m_p         = var3 * 3600 / 10000;

  m_S = m_A0 * latitude - m_B0 * std::sin(double(2) * latitude) +
        m_C0 * std::sin(double(4) * latitude) -
        m_D0 * std::sin(double(6) * latitude) +
        m_E0 * std::sin(double(8) * latitude);

  m_K1 = m_S * m_k0;
  m_K2 = m_nu * std::sin(latitude) * std::cos(latitude) * std::pow(m_sin1, 2) *
         m_k0 * (100000000) / 2;
  m_K3 = ((std::pow(m_sin1, double(4)) * m_nu * std::sin(latitude) *
           std::pow(std::cos(latitude), double(3))) /
          double(24)) *
         (double(5) - std::pow(std::tan(latitude), double(2)) +
          double(9) * m_e1sq * std::pow(std::cos(latitude), double(2)) +
          double(4) * std::pow(m_e1sq, double(2)) *
              std::pow(std::cos(latitude), double(4))) *
         m_k0 * (10000000000000000L);

  m_K4 = m_nu * std::cos(latitude) * m_sin1 * m_k0 * double(10000);

  m_K5 = std::pow(m_sin1 * std::cos(latitude), double(3)) * (m_nu / double(6)) *
         (double(1) - std::pow(std::tan(latitude), double(2)) +
          m_e1sq * std::pow(std::cos(latitude), double(2))) *
         m_k0 * 1000000000000L;

  m_A6 = (std::pow(m_p * m_sin1, double(6)) * m_nu * std::sin(latitude) *
          std::pow(std::cos(latitude), double(5)) / double(720)) *
         (double(61) - double(58) * std::pow(std::tan(latitude), double(2)) +
          std::pow(std::tan(latitude), double(4)) +
          double(270) * m_e1sq * std::pow(std::cos(latitude), double(2)) -
          double(330) * m_e1sq * std::pow(std::sin(latitude), double(2))) *
         m_k0 * (1E+24);
}  // LatLon2UTM init

void CoordinateConversion::validate(const std::pair<double, double>& point)
205
206
207
208
{
  CoordinateConversion::validate(point.first, point.second);
}

LEFEBVREJP email's avatar
LEFEBVREJP email committed
209
void CoordinateConversion::validate(double latitude, double longitude)
210
{
LEFEBVREJP email's avatar
LEFEBVREJP email committed
211
212
  if (latitude < double(-90.0) || latitude > double(90.0) ||
      longitude < double(-180.0) || longitude >= double(180.0))
213
214
215
216
217
218
219
220
  {
    std::ostringstream oss;
    oss << "Invalid coordinate [" << latitude << "," << longitude
        << "].\nLatitude must be [-90,90] and longitude must be "
           "[-180,180).";
    throw std::out_of_range(oss.str());
  }
}
221
222
}  // namespace radix
#endif /** RADIX_RADIXGEO_COORDINATECONVERSION_HH_ */