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
|
#include "locality_finder.hpp"
#include "../indexer/ftypes_matcher.hpp"
#include "../indexer/features_vector.hpp"
#include "../geometry/distance_on_sphere.hpp"
namespace search
{
double const MAX_RADIUS_CITY = 30000.0;
class DoLoader
{
public:
DoLoader(LocalityFinder const & finder, FeaturesVector const & loader, LocalityFinder::Cache & cache)
: m_finder(finder), m_loader(loader), m_cache(cache)
{
}
void operator() (uint32_t id) const
{
FeatureType ft;
m_loader.Get(id, ft);
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;
}
if (m_cache.m_loaded.count(id) > 0)
return; // already loaded
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(rect, population, id, name);
m_cache.m_tree.Add(item, item.GetLimitRect());
m_cache.m_loaded.insert(id);
}
private:
LocalityFinder const & m_finder;
FeaturesVector const & m_loader;
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() (LocalityItem const & item)
{
m2::PointD const c = item.m_rect.Center();
double const d = ms::DistanceOnEarth(MercatorBounds::YToLat(c.y),
MercatorBounds::XToLon(c.x),
MercatorBounds::YToLat(m_point.y),
MercatorBounds::XToLon(m_point.x));
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(m2::RectD const & rect, uint32_t population, ID id, string const & name)
: m_rect(rect), m_name(name), m_population(population), m_id(id)
{
}
LocalityFinder::LocalityFinder(Index const * pIndex)
: m_pIndex(pIndex), m_lang(0)
{
}
void LocalityFinder::CorrectMinimalRect(m2::RectD & rect) const
{
m2::RectD const rlt = MercatorBounds::RectByCenterXYAndSizeInMeters(rect.LeftTop(), MAX_RADIUS_CITY);
m2::RectD const rrb = MercatorBounds::RectByCenterXYAndSizeInMeters(rect.RightBottom(), MAX_RADIUS_CITY);
rect = m2::RectD(MercatorBounds::ClampX(rlt.minX()),
MercatorBounds::ClampY(rrb.minY()),
MercatorBounds::ClampX(rrb.maxX()),
MercatorBounds::ClampY(rlt.maxY()));
}
void LocalityFinder::RecreateCache(Cache & cache, m2::RectD rect) const
{
vector<MwmInfo> mwmInfo;
m_pIndex->GetMwmInfo(mwmInfo);
cache.Clear();
CorrectMinimalRect(rect);
covering::CoveringGetter cov(rect, covering::ViewportWithLowLevels);
for (MwmSet::MwmId mwmId = 0; mwmId < mwmInfo.size(); ++mwmId)
{
typedef feature::DataHeader HeaderT;
Index::MwmLock mwmLock(*m_pIndex, mwmId);
MwmValue * pMwm = mwmLock.GetValue();
if (pMwm && pMwm->GetHeader().GetType() == HeaderT::world)
{
HeaderT const & header = pMwm->GetHeader();
int const scale = header.GetLastScale(); // scales::GetUpperWorldScale()
covering::IntervalsT const & interval = cov.Get(scale);
ScaleIndex<ModelReaderPtr> index(pMwm->m_cont.GetReader(INDEX_FILE_TAG), pMwm->m_factory);
FeaturesVector loader(pMwm->m_cont, header);
cache.m_rect = rect;
for (size_t i = 0; i < interval.size(); ++i)
{
DoLoader doLoader(*this, loader, cache);
index.ForEachInIntervalAndScale(doLoader, interval[i].first, interval[i].second, scale);
}
}
}
}
void LocalityFinder::SetViewportByIndex(m2::RectD const & rect, size_t idx)
{
ASSERT_LESS(idx, (size_t)MAX_VIEWPORT_COUNT, ());
RecreateCache(m_cache[idx], rect);
}
void LocalityFinder::GetLocalityInViewport(const m2::PointD & pt, string & name) const
{
for (size_t i = 0; i < MAX_VIEWPORT_COUNT; ++i)
m_cache[i].GetLocality(pt, name);
}
void LocalityFinder::GetLocalityCreateCache(const m2::PointD & pt, string & name) const
{
// search in temporary caches and find most unused cache
size_t minUsageIdx = 0;
size_t minUsage = numeric_limits<size_t>::max();
for (size_t idx = 0; idx < MAX_CACHE_TMP_COUNT; ++idx)
{
Cache const & cache = m_cache_tmp[idx];
cache.GetLocality(pt, name);
if (!name.empty())
return;
if (cache.m_usage < minUsage)
{
minUsage = cache.m_usage;
minUsageIdx = idx;
}
}
Cache & cache = m_cache_tmp[minUsageIdx];
RecreateCache(cache, MercatorBounds::RectByCenterXYAndSizeInMeters(pt, MAX_RADIUS_CITY));
cache.GetLocality(pt, name);
}
void LocalityFinder::ClearCacheAll()
{
for (size_t i = 0; i < MAX_VIEWPORT_COUNT; ++i)
ClearCache(i);
for (size_t i = 0; i < MAX_CACHE_TMP_COUNT; ++i)
m_cache_tmp[i].Clear();
}
void LocalityFinder::ClearCache(size_t idx)
{
ASSERT_LESS(idx, (size_t)MAX_VIEWPORT_COUNT, ());
m_cache[idx].Clear();
}
void LocalityFinder::Cache::Clear()
{
m_usage = 0;
m_tree.Clear();
m_loaded.clear();
}
void LocalityFinder::Cache::GetLocality(m2::PointD const & pt, string & name) const
{
if (!m_rect.IsPointInside(pt))
return;
++m_usage;
m_tree.ForEachInRect(m2::RectD(pt, pt), DoSelectLocality(name, pt));
}
}
|