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
|
#include "search/downloader_search_callback.hpp"
#include "search/result.hpp"
#include "editor/editable_feature_source.hpp"
#include "indexer/data_source.hpp"
#include "storage/country_info_getter.hpp"
#include "storage/storage.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include <set>
#include <string>
namespace
{
bool GetGroupCountryIdFromFeature(storage::Storage const & storage, FeatureType const & ft,
std::string & name)
{
int8_t const langIndices[] = {StringUtf8Multilang::kEnglishCode,
StringUtf8Multilang::kDefaultCode,
StringUtf8Multilang::kInternationalCode};
for (auto const langIndex : langIndices)
{
if (!ft.GetName(langIndex, name))
continue;
if (storage.IsInnerNode(name))
return true;
}
return false;
}
} // namespace
namespace search
{
DownloaderSearchCallback::DownloaderSearchCallback(Delegate & delegate,
DataSource const & dataSource,
storage::CountryInfoGetter const & infoGetter,
storage::Storage const & storage,
storage::DownloaderSearchParams params)
: m_delegate(delegate)
, m_dataSource(dataSource)
, m_infoGetter(infoGetter)
, m_storage(storage)
, m_params(move(params))
{
}
void DownloaderSearchCallback::operator()(search::Results const & results)
{
storage::DownloaderSearchResults downloaderSearchResults;
std::set<storage::DownloaderSearchResult> uniqueResults;
for (auto const & result : results)
{
if (!result.HasPoint())
continue;
if (result.GetResultType() != search::Result::Type::LatLon)
{
FeatureID const & fid = result.GetFeatureID();
DataSource::FeaturesLoaderGuard loader(m_dataSource, fid.m_mwmId,
EditableFeatureSourceFactory());
FeatureType ft;
if (!loader.GetFeatureByIndex(fid.m_index, ft))
{
LOG(LERROR, ("Feature can't be loaded:", fid));
continue;
}
ftypes::Type const type = ftypes::IsLocalityChecker::Instance().GetType(ft);
if (type == ftypes::COUNTRY || type == ftypes::STATE)
{
std::string groupFeatureName;
if (GetGroupCountryIdFromFeature(m_storage, ft, groupFeatureName))
{
storage::DownloaderSearchResult downloaderResult(groupFeatureName,
result.GetString() /* m_matchedName */);
if (uniqueResults.find(downloaderResult) == uniqueResults.end())
{
uniqueResults.insert(downloaderResult);
downloaderSearchResults.m_results.push_back(downloaderResult);
}
continue;
}
}
}
auto const & mercator = result.GetFeatureCenter();
storage::TCountryId const & countryId = m_infoGetter.GetRegionCountryId(mercator);
if (countryId == storage::kInvalidCountryId)
continue;
storage::DownloaderSearchResult downloaderResult(countryId,
result.GetString() /* m_matchedName */);
if (uniqueResults.find(downloaderResult) == uniqueResults.end())
{
uniqueResults.insert(downloaderResult);
downloaderSearchResults.m_results.push_back(downloaderResult);
}
}
downloaderSearchResults.m_query = m_params.m_query;
downloaderSearchResults.m_endMarker = results.IsEndMarker();
if (m_params.m_onResults)
{
auto onResults = m_params.m_onResults;
m_delegate.RunUITask(
[onResults, downloaderSearchResults]() { onResults(downloaderSearchResults); });
}
}
} // namespace search
|