coordinateconversion.cc 8.92 KB
Newer Older
1
2

#include "radixgeo/coordinateconversion.hh"
3
#include "radixmath/constants.hh"
4

Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
5
6
#include <algorithm>

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

13
const std::array<short, 22> UTMZones::degrees = {
14
15
    {-90, -84, -72, -64, -56, -48, -40, -32, -24, -16, -8,
     0,   8,   16,  24,  32,  40,  48,  56,  64,  72,  84}};
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
16

17
const std::array<char, 11> UTMZones::neg_letters = {
18
    {'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M'}};
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
19

20
const std::array<short, 11> UTMZones::neg_degrees = {
21
    {-90, -84, -72, -64, -56, -48, -40, -32, -24, -16, -8}};
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
22

23
const std::array<char, 11> UTMZones::pos_letters = {
24
    {'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z'}};
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
25

26
const std::array<short, 11> UTMZones::pos_degrees = {
27
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 84}};
28
const std::array<char, 11> UTM2LatLon::southern_hemisphere = {
29
    {'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M'}};
30
short UTMZones::latZoneDegree(char letter)
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
31
32
33
34
35
36
{
  auto itr = std::find(letters.begin(), letters.end(), letter);
  if (itr == letters.end()) return -100;
  return *itr;
}

37
short UTMZones::lonZone(double longitude)
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
38
39
40
41
42
43
44
45
46
47
48
49
50
{
  double longZone = 0;
  if (longitude < 0.0)
  {
    longZone = ((180.0 + longitude) / 6) + 1;
  }
  else
  {
    longZone = (longitude / 6) + 31;
  }
  return short(longZone);
}

51
char UTMZones::latZone(double latitude)
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
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
{
  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];
  }
121
122
}

123
double LatLon2UTM::northing(double latitude) const
124
125
126
127
128
129
130
131
132
{
  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;
}

133
double LatLon2UTM::easting() const
134
135
136
137
{
  return 500000. + (m_K4 * m_p + m_K5 * std::pow(m_p, 3.));
}

138
UTMCoordinate LatLon2UTM::toUTM(double latitude, double longitude)
139
140
141
142
143
144
145
146
147
148
149
150
{
  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;
}

151
void LatLon2UTM::init(double latitude, double longitude)
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{
  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
  {
169
    var1 = ((int)(longitude / 6.)) + 31;
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 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);
}

213
214
215
216
217
218
219
220
std::string CoordinateConversion::toString(const UTMCoordinate& utm)
{
  char buff[120];
  sprintf(buff, "%02d %c %f %f", utm.longitude_zone, utm.lattitude_zone,
          utm.easting, utm.northing);
  return std::string(buff);
}

221
222
void CoordinateConversion::validate(double latitude, double longitude)
{
223
  radix_tagged_line("Validating (" << latitude << "," << longitude << ")");
224
225
226
227
228
229
230
231
232
233
  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());
  }
}
234

235
void UTM2LatLon::init(const UTMCoordinate& utm)
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{
  m_arc = utm.northing / m_k0;
  m_mu  = m_arc /
         (m_a * (1. - std::pow(m_e, 2.) / 4.0 - 3. * std::pow(m_e, 4.) / 64.0 -
                 5. * std::pow(m_e, 6.) / 256.0));

  m_ei = (1. - std::pow((1. - m_e * m_e), (1. / 2.0))) /
         (1. + std::pow((1. - m_e * m_e), (1. / 2.0)));

  m_ca = 3. * m_ei / 2. - 27. * std::pow(m_ei, 3.) / 32.0;

  m_cb   = 21. * std::pow(m_ei, 2.) / 16. - 55. * std::pow(m_ei, 4.) / 32.;
  m_cc   = 151. * std::pow(m_ei, 3.) / 96.;
  m_cd   = 1097. * std::pow(m_ei, 4.) / 512.;
  m_phi1 = m_mu + m_ca * std::sin(2. * m_mu) + m_cb * std::sin(4. * m_mu) +
           m_cc * std::sin(6. * m_mu) + m_cd * std::sin(8. * m_mu);

  m_n0 =
      m_a / std::pow((1. - std::pow((m_e * std::sin(m_phi1)), 2.)), (1. / 2.0));

  m_r0 = m_a * (1. - m_e * m_e) /
         std::pow((1. - std::pow((m_e * std::sin(m_phi1)), 2.)), (3. / 2.0));
  m_fact1 = m_n0 * std::tan(m_phi1) / m_r0;

  m_a1    = 500000. - utm.easting;
  m_dd0   = m_a1 / (m_n0 * m_k0);
  m_fact2 = m_dd0 * m_dd0 / 2.;

  m_t0    = std::pow(std::tan(m_phi1), 2.);
  m_Q0    = m_e1sq * std::pow(std::cos(m_phi1), 2.);
  m_fact3 = (5. + 3. * m_t0 + 10. * m_Q0 - 4. * m_Q0 * m_Q0 - 9. * m_e1sq) *
            std::pow(m_dd0, 4.) / 24.;

  m_fact4 = (61. + 90. * m_t0 + 298. * m_Q0 + 45. * m_t0 * m_t0 -
             252. * m_e1sq - 3. * m_Q0 * m_Q0) *
            std::pow(m_dd0, 6.) / 720.;

  //
  m_lof1 = m_a1 / (m_n0 * m_k0);
  m_lof2 = (1. + 2. * m_t0 + m_Q0) * std::pow(m_dd0, 3.) / 6.0;
  m_lof3 = (5. - 2. * m_Q0 + 28. * m_t0 - 3. * std::pow(m_Q0, 2.) +
            8. * m_e1sq + 24. * std::pow(m_t0, 2.)) *
           std::pow(m_dd0, 5.) / 120.;
  m_a2 = (m_lof1 - m_lof2 + m_lof3) / std::cos(m_phi1);
  m_a3 = m_a2 * 180. / radix::PI;
}

283
char UTM2LatLon::hemisphere(char latitude_zone)
284
285
286
287
288
289
290
291
292
293
294
295
296
{
  auto it = std::find(std::begin(southern_hemisphere),
                      std::end(southern_hemisphere), latitude_zone);
  if (it == southern_hemisphere.end())
  {
    return 'N';
  }
  else
  {
    return 'S';
  }
}

297
std::pair<double, double> UTM2LatLon::toLatLon(const UTMCoordinate& c)
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
{
  UTMCoordinate utm   = c;
  double latitude     = 0.;
  double longitude    = 0.;
  char utm_hemisphere = hemisphere(utm.lattitude_zone);
  // modify for Southern hemisphere
  if ('S' == utm_hemisphere)
  {
    utm.northing = 10000000 - utm.northing;
  }
  // initialize internal memory
  init(utm);
  latitude =
      180. * (m_phi1 - m_fact1 * (m_fact2 + m_fact3 + m_fact4)) / radix::PI;

  if (utm.longitude_zone > 0)
  {
    m_zoneCM = 6 * utm.longitude_zone - 183.0;
  }
  else
  {
    m_zoneCM = 3.0;
  }

  longitude = m_zoneCM - m_a3;
  if ('S' == utm_hemisphere)
  {
    latitude = -latitude;
  }

  return std::make_pair(latitude, longitude);
}

331
// UTMZones::latZone
332
}  // namespace radix