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

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

#include "search/mwm_context.hpp"

#include "indexer/ftypes_matcher.hpp"


namespace search
{

double const kMaxCityRadiusMeters = 30000.0;
double const kInflateRectMercator = 1.0E-3;

class DoLoader
{
public:
  DoLoader(LocalityFinder const & finder, LocalityFinder::Cache & cache)
    : m_finder(finder), m_cache(cache)
  {
  }

  void operator() (FeatureType & ft) const
  {
    if (ft.GetFeatureType() != feature::GEOM_POINT)
      return;

    using namespace ftypes;
    switch (IsLocalityChecker::Instance().GetType(ft))
    {
    case CITY:
    case TOWN:
      break;
    default:  // cache only cities and towns at this moment
      return;
    }

    uint32_t const id = ft.GetID().m_index;

    if (m_cache.m_loaded.count(id) > 0)
      return;

    uint32_t const population = ftypes::GetPopulation(ft);
    if (population == 0)
      return;

    double const radius = ftypes::GetRadiusByPopulation(population);
    m2::RectD const rect = MercatorBounds::RectByCenterXYAndSizeInMeters(ft.GetCenter(), radius);
    if (!rect.IsIntersect(m_cache.m_rect))
      return;

    // read item
    string name;
    if (!ft.GetName(m_finder.m_lang, name))
      if (!ft.GetName(0, name))
        return;

    LocalityItem item(population, id, name);
    m_cache.m_tree.Add(item, rect);
    m_cache.m_loaded.insert(id);
  }

private:
  LocalityFinder const & m_finder;
  LocalityFinder::Cache & m_cache;
};


class DoSelectLocality
{
public:
  DoSelectLocality(string & name, m2::PointD const & p)
    : m_name(name) , m_point(p), m_bestValue(numeric_limits<double>::max())
  {
  }

  void operator() (m2::RectD const & rect, LocalityItem const & item)
  {
    double const d = MercatorBounds::DistanceOnEarth(rect.Center(), m_point);
    double const value = ftypes::GetPopulationByRadius(d) / static_cast<double>(item.m_population);
    if (value < m_bestValue)
    {
      m_bestValue = value;
      m_name = item.m_name;
    }
  }

private:
  string & m_name;
  m2::PointD m_point;
  double m_bestValue;
};


LocalityItem::LocalityItem(uint32_t population, ID id, string const & name)
  : m_name(name), m_population(population), m_id(id)
{
}

string DebugPrint(LocalityItem const & item)
{
  stringstream ss;
  ss << "Name = " << item.m_name << "Population = " << item.m_population << "ID = " << item.m_id;
  return ss.str();
}


LocalityFinder::LocalityFinder(Index const * pIndex)
  : m_pIndex(pIndex), m_lang(0)
{
}

void LocalityFinder::UpdateCache(Cache & cache, m2::PointD const & pt) const
{
  m2::RectD rect = MercatorBounds::RectByCenterXYAndSizeInMeters(pt, kMaxCityRadiusMeters);
  if (cache.m_rect.IsRectInside(rect))
    return;

  rect.Add(cache.m_rect);
  rect.Inflate(kInflateRectMercator, kInflateRectMercator);

  vector<shared_ptr<MwmInfo>> mwmsInfo;
  m_pIndex->GetMwmsInfo(mwmsInfo);
  for (auto const & info : mwmsInfo)
  {
    Index::MwmHandle handle = m_pIndex->GetMwmHandleById(info);
    MwmValue const * value = handle.GetValue<MwmValue>();
    if (handle.IsAlive() && value->GetHeader().GetType() == feature::DataHeader::world)
    {
      cache.m_rect = rect;
      MwmContext(move(handle)).ForEachFeature(rect, DoLoader(*this, cache));
      break;
    }
  }
}

void LocalityFinder::GetLocality(m2::PointD const & pt, string & name)
{
  Cache * working = nullptr;

  // Find suitable cache that includes needed point.
  for (auto & cache : m_caches)
  {
    if (cache.m_rect.IsPointInside(pt))
    {
      working = &cache;
      break;
    }
  }

  if (working == nullptr)
  {
    // Find most unused cache.
    size_t minUsage = numeric_limits<size_t>::max();
    for (auto & cache : m_caches)
    {
      if (cache.m_usage < minUsage)
      {
        working = &cache;
        minUsage = cache.m_usage;
      }
    }

    ASSERT(working, ());
    working->Clear();
  }

  UpdateCache(*working, pt);
  working->GetLocality(pt, name);
}

void LocalityFinder::ClearCache()
{
  for (auto & cache : m_caches)
    cache.Clear();
}

void LocalityFinder::Cache::Clear()
{
  m_usage = 0;
  m_tree.Clear();
  m_loaded.clear();
  m_rect.MakeEmpty();
}

void LocalityFinder::Cache::GetLocality(m2::PointD const & pt, string & name)
{
  ++m_usage;
  m_tree.ForEachInRectEx(m2::RectD(pt, pt), DoSelectLocality(name, pt));
}

}