Welcome to mirror list, hosted at ThFree Co, Russian Federation.

latlon_match.cpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d7aa8f8f37987e42956c734480e5cccd74c42b4f (plain)
1
2
3
4
5
6
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
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
213
214
215
216
217
#include "search/latlon_match.hpp"

#include "base/macros.hpp"

#include "std/array.hpp"
#include "std/cmath.hpp"
#include "std/cstdlib.hpp"
#include "std/cstring.hpp"
#include "std/algorithm.hpp"
#include "std/utility.hpp"


namespace search
{

namespace
{

template <typename CharT> void SkipSpaces(CharT * & s)
{
  while (*s && (*s == ' ' || *s == '\t'))
    ++s;
}

template <typename CharT> void Skip(CharT * & s)
{
  while (*s && (*s == ' ' || *s == '\t' || *s == ',' || *s == ';' ||
                *s == ':' || *s == '.' || *s == '(' || *s == ')'))
    ++s;
}

}  // unnamed namespace

namespace
{

bool MatchDMSArray(char const * & s, char const * arr[], size_t count)
{
  for (size_t i = 0; i < count; ++i)
  {
    size_t const len = strlen(arr[i]);
    if (strncmp(s, arr[i], len) == 0)
    {
      s += len;
      return true;
    }
  }
  return false;
}

int GetDMSIndex(char const * & s)
{
  char const * arrDegree[] = { "*", "°" };
  char const * arrMinutes[] = { "\'", "’", "′" };
  char const * arrSeconds[] = { "\"", "”", "″", "\'\'", "’’", "′′" };

  if (MatchDMSArray(s, arrDegree, ARRAY_SIZE(arrDegree)))
      return 0;
  if (MatchDMSArray(s, arrSeconds, ARRAY_SIZE(arrSeconds)))
    return 2;
  if (MatchDMSArray(s, arrMinutes, ARRAY_SIZE(arrMinutes)))
    return 1;

  return -1;
}

void SkipNSEW(char const * & s, char const * (&arrPos) [4])
{
  Skip(s);

  int ind;
  switch (*s)
  {
  case 'N': case 'n': ind = 0; break;
  case 'S': case 's': ind = 1; break;
  case 'E': case 'e': ind = 2; break;
  case 'W': case 'w': ind = 3; break;
  default: return;
  }

  arrPos[ind] = s++;

  Skip(s);
}

}

bool MatchLatLonDegree(string const & query, double & lat, double & lon)
{
  // should be default initialization (0, false)
  array<pair<double, bool>, 6> v;

  int base = 0;

  // Positions of N, S, E, W symbols
  char const * arrPos[] = { 0, 0, 0, 0 };
  bool arrDegreeSymbol[] = { false, false };

  char const * s = query.c_str();
  while (true)
  {
    char const * s1 = s;
    SkipNSEW(s, arrPos);
    if (!*s)
    {
      // End of the string - check matching.
      break;
    }

    char * s2;
    double const x = strtod(s, &s2);
    if (s == s2)
    {
      // invalid token
      if (s == s1)
      {
        // Return error if there are no any delimiters.
        return false;
      }
      else
      {
        // Check matching if token is delimited.
        break;
      }
    }

    s = s2;
    SkipSpaces(s);

    int i = GetDMSIndex(s);
    bool degreeSymbol = true;
    if (i == -1)
    {
      // try to assign next possible value mark
      if (arrDegreeSymbol[base / 3])
      {
        if (!v[base + 1].second)
          i = 1;
        else
          i = 2;
      }
      else
      {
        i = 0;
        degreeSymbol = false;
      }
    }

    if (i == 0) // degrees
    {
      if (v[base].second)
      {
        if (base == 0)
          base += 3;
        else
        {
          // too many degree values
          return false;
        }
      }
      arrDegreeSymbol[base / 3] = degreeSymbol;
    }
    else  // minutes or seconds
    {
      if (x < 0.0 || x > 60.0 ||            // minutes or seconds should be in [0, 60] range
          v[base + i].second ||             // value already exists
          !v[base].second ||                // no degrees found for value
          (i == 2 && !v[base + 1].second))  // no minutes for seconds
      {
        return false;
      }
    }

    v[base + i].first = x;
    v[base + i].second = true;
  }

  if (!v[0].second || !v[3].second)
  {
    // degree should exist for both coordinates
    return false;
  }

  if ((arrPos[0] && arrPos[1]) || (arrPos[2] && arrPos[3]))
  {
    // control symbols should match only once
    return false;
  }

  // Calculate Lat, Lon with correct sign.
  lat = fabs(v[0].first) + v[1].first / 60.0 + v[2].first / 3600.0;
  if (v[0].first < 0.0) lat = -lat;

  lon = fabs(v[3].first) + v[4].first / 60.0 + v[5].first / 3600.0;
  if (v[3].first < 0.0) lon = -lon;

  if (max(arrPos[0], arrPos[1]) > max(arrPos[2], arrPos[3]))
    swap(lat, lon);

  if (arrPos[1]) lat = -lat;
  if (arrPos[3]) lon = -lon;

  // Valid input ranges for longitude are: [0, 360] or [-180, 180].
  // We do normilize it to [-180, 180].
  if (lon > 180.0)
  {
    if (lon > 360.0)
      return false;
    lon -= 360.0;
  }
  else if (lon < -180.0)
    return false;

  return (fabs(lat) <= 90.0);
}

} // search