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
|
#include "search/v2/features_layer_matcher.hpp"
#include "search/reverse_geocoder.hpp"
#include "indexer/scales.hpp"
#include "base/assert.hpp"
namespace search
{
namespace v2
{
// static
double const FeaturesLayerMatcher::kBuildingRadiusMeters = 50;
FeaturesLayerMatcher::FeaturesLayerMatcher(Index & index, my::Cancellable const & cancellable)
: m_context(nullptr)
, m_reverseGeocoder(index)
, m_nearbyStreetsCache("FeatureToNearbyStreets")
, m_matchingStreetsCache("BuildingToStreet")
, m_loader(scales::GetUpperScale(), ReverseGeocoder::kLookupRadiusM)
, m_cancellable(cancellable)
{
}
void FeaturesLayerMatcher::SetContext(MwmContext * context)
{
ASSERT(context, ());
if (m_context == context)
return;
m_context = context;
m_houseToStreetTable = HouseToStreetTable::Load(m_context->m_value);
ASSERT(m_houseToStreetTable, ());
m_loader.SetContext(context);
}
void FeaturesLayerMatcher::OnQueryFinished()
{
m_nearbyStreetsCache.ClearIfNeeded();
m_matchingStreetsCache.ClearIfNeeded();
m_loader.OnQueryFinished();
}
uint32_t FeaturesLayerMatcher::GetMatchingStreet(uint32_t houseId)
{
auto entry = m_matchingStreetsCache.Get(houseId);
if (!entry.second)
return entry.first;
FeatureType houseFeature;
GetByIndex(houseId, houseFeature);
entry.first = GetMatchingStreetImpl(houseId, houseFeature);
return entry.first;
}
uint32_t FeaturesLayerMatcher::GetMatchingStreet(uint32_t houseId, FeatureType & houseFeature)
{
auto entry = m_matchingStreetsCache.Get(houseId);
if (!entry.second)
return entry.first;
entry.first = GetMatchingStreetImpl(houseId, houseFeature);
return entry.first;
}
vector<FeaturesLayerMatcher::TStreet> const &
FeaturesLayerMatcher::GetNearbyStreets(uint32_t featureId)
{
auto entry = m_nearbyStreetsCache.Get(featureId);
if (!entry.second)
return entry.first;
FeatureType feature;
GetByIndex(featureId, feature);
GetNearbyStreetsImpl(feature, entry.first);
return entry.first;
}
vector<FeaturesLayerMatcher::TStreet> const &
FeaturesLayerMatcher::GetNearbyStreets(uint32_t featureId, FeatureType & feature)
{
auto entry = m_nearbyStreetsCache.Get(featureId);
if (!entry.second)
return entry.first;
GetNearbyStreetsImpl(feature, entry.first);
return entry.first;
}
void FeaturesLayerMatcher::GetNearbyStreetsImpl(FeatureType & feature, vector<TStreet> & streets)
{
m_reverseGeocoder.GetNearbyStreets(feature, streets);
for (size_t i = 0; i < streets.size(); ++i)
{
if (streets[i].m_distanceMeters > ReverseGeocoder::kLookupRadiusM)
{
streets.resize(i);
return;
}
}
}
uint32_t FeaturesLayerMatcher::GetMatchingStreetImpl(uint32_t houseId, FeatureType & houseFeature)
{
auto const & streets = GetNearbyStreets(houseId, houseFeature);
uint32_t streetId = kInvalidId;
uint32_t streetIndex;
if (!m_houseToStreetTable->Get(houseId, streetIndex))
streetIndex = streets.size();
if (streetIndex < streets.size() && streets[streetIndex].m_id.m_mwmId == m_context->m_id)
streetId = streets[streetIndex].m_id.m_index;
return streetId;
}
} // namespace v2
} // namespace search
|