coordinateconversion.cc 6.1 KB
Newer Older
1
2

#include "radixgeo/coordinateconversion.hh"
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
3
4
#include <algorithm>

5
6
namespace radix
{
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
7
8
9
10
11
12
13
14
15
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
const std::array<char, 22> CoordinateConversion::UTMZones::letters = {
    'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
    'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z'};

const std::array<short, 22> CoordinateConversion::UTMZones::degrees = {
    -90, -84, -72, -64, -56, -48, -40, -32, -24, -16, -8,
    0,   8,   16,  24,  32,  40,  48,  56,  64,  72,  84};

const std::array<char, 11> CoordinateConversion::UTMZones::neg_letters = {
    'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M'};

const std::array<short, 11> CoordinateConversion::UTMZones::neg_degrees = {
    -90, -84, -72, -64, -56, -48, -40, -32, -24, -16, -8};

const std::array<char, 11> CoordinateConversion::UTMZones::pos_letters = {
    'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z'};

const std::array<short, 11> CoordinateConversion::UTMZones::pos_degrees = {
    0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 84};

short CoordinateConversion::UTMZones::latZoneDegree(char letter)
{
  auto itr = std::find(letters.begin(), letters.end(), letter);
  if (itr == letters.end()) return -100;
  return *itr;
}

short CoordinateConversion::UTMZones::lonZone(double longitude)
{
  double longZone = 0;
  if (longitude < 0.0)
  {
    longZone = ((180.0 + longitude) / 6) + 1;
  }
  else
  {
    longZone = (longitude / 6) + 31;
  }
  return short(longZone);
}

char CoordinateConversion::UTMZones::latZone(double latitude)
{
  short latIndex = -2;
  short lat      = short(latitude);

  if (lat >= 0)
  {
    size_t len = pos_letters.size();
    for (size_t i = 0; i < len; i++)
    {
      if (lat == pos_degrees[i])
      {
        latIndex = i;
        break;
      }

      if (lat > pos_degrees[i])
      {
        continue;
      }
      else
      {
        latIndex = i - 1;
        break;
      }
    }
  }
  else
  {
    size_t len = neg_letters.size();
    for (size_t i = 0; i < len; i++)
    {
      if (lat == neg_degrees[i])
      {
        latIndex = i;
        break;
      }

      if (lat < neg_degrees[i])
      {
        latIndex = i - 1;
        break;
      }
      else
      {
        continue;
      }
    }
  }

  if (latIndex == -1)
  {
    latIndex = 0;
  }
  if (lat >= 0)
  {
    if (latIndex == -2)
    {
      latIndex = pos_letters.size() - 1;
    }
    return pos_letters[latIndex];
  }
  else
  {
    if (latIndex == -2)
    {
      latIndex = neg_letters.size() - 1;
    }
    return neg_letters[latIndex];
  }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
205
206
207
208
209
210
211
212
}

double CoordinateConversion::LatLon2UTM::northing(double latitude) const
{
  double northing = m_K1 + m_K2 * m_p * m_p + m_K3 * std::pow(m_p, 4.);
  if (latitude < 0.0)
  {
    northing = 10000000. + northing;
  }
  return northing;
}

double CoordinateConversion::LatLon2UTM::easting() const
{
  return 500000. + (m_K4 * m_p + m_K5 * std::pow(m_p, 3.));
}

CoordinateConversion::UTMCoordinate CoordinateConversion::LatLon2UTM::toUTM(
    double latitude, double longitude)
{
  CoordinateConversion::validate(latitude, longitude);
  UTMCoordinate utm;
  init(latitude, longitude);
  utm.longitude_zone = UTMZones::lonZone(longitude);
  utm.lattitude_zone = UTMZones::latZone(latitude);

  utm.easting  = easting();
  utm.northing = northing(latitude);
  return utm;
}

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)
{
  CoordinateConversion::validate(point.first, point.second);
}

void CoordinateConversion::validate(double latitude, double longitude)
{
213
  radix_tagged_line("Validating (" << latitude << "," << longitude << ")");
214
215
216
217
218
219
220
221
222
223
224
  if (latitude < -90.0 || latitude > 90.0 || longitude < -180.0 ||
      longitude >= 180.0)
  {
    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());
  }
}
// UTMZones::latZone
225
}  // namespace radix