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
|
#pragma once
#include "indexer/cell_id.hpp"
#include "indexer/feature_covering.hpp"
#include "indexer/interval_index.hpp"
#include "indexer/locality_object.hpp"
#include "indexer/scales.hpp"
#include "coding/file_container.hpp"
#include "geometry/rect2d.hpp"
#include "base/geo_object_id.hpp"
#include <cstdint>
#include <functional>
#include <memory>
#include <set>
#include <string>
#include "defines.hpp"
namespace indexer
{
// Geometry index which stores base::GeoObjectId as object identifier.
// Used for geocoder server, stores only POIs and buildings which have address information.
// Based on IntervalIndex.
template <typename Reader, int DEPTH_LEVELS>
class LocalityIndex
{
public:
using ProcessObject = std::function<void(base::GeoObjectId const &)>;
LocalityIndex() = default;
explicit LocalityIndex(Reader const & reader)
{
m_intervalIndex = std::make_unique<IntervalIndex<Reader, uint64_t>>(reader);
}
void ForEachInRect(ProcessObject const & processObject, m2::RectD const & rect) const
{
covering::CoveringGetter cov(rect, covering::CoveringMode::ViewportWithLowLevels);
covering::Intervals const & intervals = cov.Get<DEPTH_LEVELS>(scales::GetUpperScale());
for (auto const & i : intervals)
{
m_intervalIndex->ForEach(
[&processObject](uint64_t storedId) {
processObject(LocalityObject::FromStoredId(storedId));
},
i.first, i.second);
}
}
// Applies |processObject| to at most |topSize| object closest to |center| with maximal distance |sizeM|.
// Closest objects are first. Due to perfomance reasons far objects have approximate order.
void ForClosestToPoint(ProcessObject const & processObject, m2::PointD const & center, double sizeM,
uint32_t topSize) const
{
auto const rect =
MercatorBounds::RectByCenterXYAndSizeInMeters(center, sizeM);
covering::CoveringGetter cov(rect, covering::CoveringMode::Spiral);
covering::Intervals const & intervals = cov.Get<DEPTH_LEVELS>(scales::GetUpperScale());
std::set<uint64_t> objects;
auto process = [topSize, &objects, &processObject](uint64_t storedId) {
if (objects.insert(storedId).second && objects.size() <= topSize)
processObject(LocalityObject::FromStoredId(storedId));
};
for (auto const & i : intervals)
{
if (objects.size() >= topSize)
return;
m_intervalIndex->ForEach(process, i.first, i.second);
}
}
private:
std::unique_ptr<IntervalIndex<Reader, uint64_t>> m_intervalIndex;
};
template <typename Reader>
using GeoObjectsIndex = LocalityIndex<Reader, kGeoObjectsDepthLevels>;
template <typename Reader>
using RegionsIndex = LocalityIndex<Reader, kRegionsDepthLevels>;
template <typename Reader>
struct GeoObjectsIndexBox
{
static constexpr const char * kFileTag = GEO_OBJECTS_INDEX_FILE_TAG;
using ReaderType = Reader;
using IndexType = GeoObjectsIndex<ReaderType>;
};
template <typename Reader>
struct RegionsIndexBox
{
static constexpr const char * kFileTag = REGIONS_INDEX_FILE_TAG;
using ReaderType = Reader;
using IndexType = RegionsIndex<ReaderType>;
};
template <typename IndexBox, typename Reader>
typename IndexBox::IndexType ReadIndex(std::string const & pathIndx)
{
FilesContainerR cont(pathIndx);
auto const offsetSize = cont.GetAbsoluteOffsetAndSize(IndexBox::kFileTag);
Reader reader(pathIndx);
typename IndexBox::ReaderType subReader(reader.CreateSubReader(offsetSize.first, offsetSize.second));
typename IndexBox::IndexType index(subReader);
return index;
}
} // namespace indexer
|