coordinateconversion.cc 2.51 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
118
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];
  }
}  // UTMZones::latZone
119
}  // namespace radix