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

country_info.cpp « storage - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3f7e9dfb6767249753c3d0bf62a39f9c06d0a7d (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
#include "country_info.hpp"
#include "country_polygon.hpp"
#include "country.hpp"

#include "../indexer/geometry_serialization.hpp"

#include "../geometry/region2d.hpp"

#include "../coding/read_write_utils.hpp"

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


namespace storage
{
  /*
  class LessCountryDef
  {
    bool CompareStrings(string const & s1, string const & s2) const
    {
      // Do this stuff because of 'Guinea-Bissau.mwm' goes before 'Guinea.mwm'
      // in file system (and in PACKED_POLYGONS_FILE also).
      size_t n = min(s1.size(), s2.size());
      return lexicographical_compare(s1.begin(), s1.begin() + n,
                                     s2.begin(), s2.begin() + n);
    }

  public:
    bool operator() (CountryDef const & r1, string const & r2) const
    {
      return CompareStrings(r1.m_name, r2);
    }
    bool operator() (string const & r1, CountryDef const & r2) const
    {
      return CompareStrings(r1, r2.m_name);
    }
    bool operator() (CountryDef const & r1, CountryDef const & r2) const
    {
      return CompareStrings(r1.m_name, r2.m_name);
    }
  };
  */

  CountryInfoGetter::CountryInfoGetter(ModelReaderPtr polyR, ModelReaderPtr countryR)
    : m_reader(polyR), m_cache(3)
  {
    ReaderSource<ModelReaderPtr> src(m_reader.GetReader(PACKED_POLYGONS_INFO_TAG));
    rw::Read(src, m_countries);

/*
    // We can't change the order of countries.
#ifdef DEBUG
    LessCountryDef check;
    for (size_t i = 0; i < m_countries.size() - 1; ++i)
      ASSERT ( !check(m_countries[i+1], m_countries[i]),
               (m_countries[i].m_name, m_countries[i+1].m_name) );
#endif
*/

    string buffer;
    countryR.ReadAsString(buffer);
    LoadCountryFile2CountryInfo(buffer, m_id2info);
  }

  template <class ToDo>
  void CountryInfoGetter::ForEachCountry(m2::PointD const & pt, ToDo & toDo) const
  {
    for (size_t i = 0; i < m_countries.size(); ++i)
      if (m_countries[i].m_rect.IsPointInside(pt))
        if (!toDo(i))
          return;
  }

  bool CountryInfoGetter::GetByPoint::operator() (size_t id)
  {
    vector<m2::RegionD> const & rgnV = m_info.GetRegions(id);

    for (size_t i = 0; i < rgnV.size(); ++i)
    {
      if (rgnV[i].Contains(m_pt))
      {
        m_res = id;
        return false;
      }
    }

    return true;
  }

  vector<m2::RegionD> const & CountryInfoGetter::GetRegions(size_t id) const
  {
    bool isFound = false;
    vector<m2::RegionD> & rgnV = m_cache.Find(id, isFound);

    if (!isFound)
    {
      rgnV.clear();

      // load regions from file
      ReaderSource<ModelReaderPtr> src(m_reader.GetReader(strings::to_string(id)));

      uint32_t const count = ReadVarUint<uint32_t>(src);
      for (size_t i = 0; i < count; ++i)
      {
        vector<m2::PointD> points;
        serial::LoadOuterPath(src, serial::CodingParams(), points);

        rgnV.push_back(m2::RegionD());
        rgnV.back().Assign(points.begin(), points.end());
      }
    }

    return rgnV;
  }

  string CountryInfoGetter::GetRegionFile(m2::PointD const & pt) const
  {
    GetByPoint doGet(*this, pt);
    ForEachCountry(pt, doGet);

    if (doGet.m_res != -1)
      return m_countries[doGet.m_res].m_name;
    else
      return string();
  }

  void CountryInfoGetter::GetRegionInfo(m2::PointD const & pt, CountryInfo & info) const
  {
    GetByPoint doGet(*this, pt);
    ForEachCountry(pt, doGet);

    if (doGet.m_res != -1)
      GetRegionInfo(m_countries[doGet.m_res].m_name, info);
  }

  void CountryInfoGetter::GetRegionInfo(string const & id, CountryInfo & info) const
  {
    map<string, CountryInfo>::const_iterator i = m_id2info.find(id);

    // Take into account 'minsk-pass'.
    if (i == m_id2info.end()) return;
    //ASSERT ( i != m_id2info.end(), () );

    info = i->second;

    if (info.m_name.empty())
      info.m_name = id;

    CountryInfo::FileName2FullName(info.m_name);
  }

  template <class ToDo> void CountryInfoGetter::ForEachCountry(string const & prefix, ToDo toDo) const
  {
    typedef vector<CountryDef>::const_iterator IterT;

    for (IterT i = m_countries.begin(); i != m_countries.end(); ++i)
    {
      if (i->m_name.find(prefix) == 0)
        toDo(*i);
    }

    /// @todo Store sorted list of polygons in PACKED_POLYGONS_FILE.
    /*
    pair<IterT, IterT> r = equal_range(m_countries.begin(), m_countries.end(), file, LessCountryDef());
    while (r.first != r.second)
    {
      toDo(r.first->m_name);
      ++r.first;
    }
    */
  }

  namespace
  {
    class DoCalcUSA
    {
      m2::RectD * m_rects;
    public:
      DoCalcUSA(m2::RectD * rects) : m_rects(rects) {}
      void operator() (CountryDef const & c)
      {
        if (c.m_name == "USA_Alaska")
          m_rects[1] = c.m_rect;
        else if (c.m_name == "USA_Hawaii")
          m_rects[2] = c.m_rect;
        else
          m_rects[0].Add(c.m_rect);
      }
    };

    void AddRect(m2::RectD & r, CountryDef const & c)
    {
      r.Add(c.m_rect);
    }
  }

  void CountryInfoGetter::CalcUSALimitRect(m2::RectD rects[3]) const
  {
    ForEachCountry("USA_", DoCalcUSA(rects));
  }

  m2::RectD CountryInfoGetter::CalcLimitRect(string const & prefix) const
  {
    m2::RectD r;
    ForEachCountry(prefix, bind(&AddRect, ref(r), _1));
    return r;
  }

  void CountryInfoGetter::GetMatchedRegions(string const & enName, IDSet & regions) const
  {
    for (size_t i = 0; i < m_countries.size(); ++i)
    {
      /// Match english name with region file name (they are equal in almost all cases).
      /// @todo Do it smarter in future.
      string s = m_countries[i].m_name;
      strings::AsciiToLower(s);
      if (s.find(enName) != string::npos)
        regions.push_back(i);
    }
  }

  bool CountryInfoGetter::IsBelongToRegion(m2::PointD const & pt, IDSet const & regions) const
  {
    GetByPoint doCheck(*this, pt);
    for (size_t i = 0; i < regions.size(); ++i)
      if (m_countries[regions[i]].m_rect.IsPointInside(pt) && !doCheck(regions[i]))
        return true;

    return false;
  }

  bool CountryInfoGetter::IsBelongToRegion(string const & fileName, IDSet const & regions) const
  {
    for (size_t i = 0; i < regions.size(); ++i)
    {
      if (m_countries[regions[i]].m_name == fileName)
        return true;
    }

    return false;
  }

namespace
{
  class DoFreeCacheMemory
  {
  public:
    void operator() (vector<m2::RegionD> & v) const
    {
      vector<m2::RegionD> emptyV;
      emptyV.swap(v);
    }
  };
}

  void CountryInfoGetter::ClearCaches() const
  {
    m_cache.ForEachValue(DoFreeCacheMemory());
    m_cache.Reset();
  }
}