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

geourl_process.cpp « map - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f32c0c5907a52a46dd90d6b348de5b141c20e78 (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
#include "map/geourl_process.hpp"

#include "indexer/scales.hpp"
#include "indexer/mercator.hpp"

#include "coding/uri.hpp"

#include "base/string_utils.hpp"
#include "base/regexp.hpp"
#include "base/logging.hpp"

#include "std/bind.hpp"


namespace url_scheme
{
  const double INVALID_COORD = -1000.0;

  bool Info::IsValid() const
  {
    return (m_lat != INVALID_COORD && m_lon != INVALID_COORD);
  }

  void Info::Reset()
  {
    m_lat = m_lon = INVALID_COORD;
    m_zoom = scales::GetUpperScale();
  }

  void Info::SetZoom(double x)
  {
    if (x >= 0.0 && x <= scales::GetUpperScale())
      m_zoom = x;
  }

  bool Info::SetLat(double x)
  {
    if (MercatorBounds::ValidLat(x))
    {
      m_lat = x;
      return true;
    }
    return false;
  }

  bool Info::SetLon(double x)
  {
    if (MercatorBounds::ValidLon(x))
    {
      m_lon = x;
      return true;
    }
    return false;
  }

  int const LL_PRIORITY = 5;

  /// Priority for accepting coordinates if we have many choices.
  /// -1 - not initialized
  /// 0 - coordinates in path;
  /// x - priority for query type (greater is better)
  int GetCoordinatesPriority(string const & token)
  {
    if (token.empty())
      return 0;
    else if (token == "q")
      return 1;
    else if (token == "daddr")
      return 2;
    else if (token == "point")
      return 3;
    else if (token == "ll")
      return 4;
    else if (token == "lat" || token == "lon")
      return LL_PRIORITY;

    return -1;
  }

  class LatLonParser
  {
    regexp::RegExpT m_regexp;
    Info & m_info;
    int m_latPriority, m_lonPriority;

    class AssignCoordinates
    {
      LatLonParser & m_parser;
      int m_priority;

    public:
      AssignCoordinates(LatLonParser & parser, int priority)
        : m_parser(parser), m_priority(priority)
      {
      }

      void operator() (string const & token) const
      {
        double lat, lon;

        string::size_type n = token.find(',');
        ASSERT(n != string::npos, ());
        VERIFY(strings::to_double(token.substr(0, n), lat), ());

        n = token.find_first_not_of(", ", n);
        ASSERT(n != string::npos, ());
        VERIFY(strings::to_double(token.substr(n, token.size() - n), lon), ());

        if (m_parser.m_info.SetLat(lat) && m_parser.m_info.SetLon(lon))
          m_parser.m_latPriority = m_parser.m_lonPriority = m_priority;
      }
    };

  public:
    LatLonParser(Info & info)
      : m_info(info), m_latPriority(-1), m_lonPriority(-1)
    {
      regexp::Create("-?\\d+\\.?\\d*, *-?\\d+\\.?\\d*", m_regexp);
    }

    bool IsValid() const
    {
      return (m_latPriority == m_lonPriority && m_latPriority != -1);
    }

    void operator()(string const & key, string const & value)
    {
      if (key == "z" || key == "zoom")
      {
        double x;
        if (strings::to_double(value, x))
          m_info.SetZoom(x);
        return;
      }

      int const priority = GetCoordinatesPriority(key);
      if (priority == -1 || priority < m_latPriority || priority < m_lonPriority)
        return;

      if (priority != LL_PRIORITY)
      {
        regexp::ForEachMatched(value, m_regexp, AssignCoordinates(*this, priority));
      }
      else
      {
        double x;
        if (strings::to_double(value, x))
        {
          if (key == "lat")
          {
            if (m_info.SetLat(x))
              m_latPriority = priority;
          }
          else
          {
            ASSERT_EQUAL(key, "lon", ());
            if (m_info.SetLon(x))
              m_lonPriority = priority;
          }
        }
      }
    }
  };

  void ParseGeoURL(string const & s, Info & info)
  {
    url_scheme::Uri uri(s);
    if (!uri.IsValid())
      return;

    LatLonParser parser(info);
    parser(string(), uri.GetPath());
    uri.ForEachKeyValue(ref(parser));

    if (!parser.IsValid())
      info.Reset();
  }
}