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

locality_index.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8665d291ffd81e473e9a2e16a47573c2de1a596 (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
#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 the objects located within |radiusM| meters from |center|.
  // Application to the closest objects and only to them is not guaranteed and the order
  // of the objects is not specified.
  // However, the method attempts to process objects that are closer to |center| first
  // and stop after |sizeHint| objects have been processed. For stability, if an object from
  // an index cell has been processed, all other objects from this cell will be processed too,
  // thus probably overflowing the |sizeHint| limit.
  void ForClosestToPoint(ProcessObject const & processObject, m2::PointD const & center,
                         double radiusM, uint32_t sizeHint) const
  {
    auto const rect =
        MercatorBounds::RectByCenterXYAndSizeInMeters(center, radiusM);
    covering::CoveringGetter cov(rect, covering::CoveringMode::Spiral);
    covering::Intervals const & intervals = cov.Get<DEPTH_LEVELS>(scales::GetUpperScale());

    std::set<uint64_t> objects;
    auto processAll = [&objects, &processObject](uint64_t storedId) {
      if (objects.insert(storedId).second)
        processObject(LocalityObject::FromStoredId(storedId));
    };

    auto process = [&](uint64_t storedId) {
      if (objects.size() < sizeHint)
        processAll(storedId);
    };

    CHECK_EQUAL(intervals.begin()->first, intervals.begin()->second - 1, ());
    auto cellDepth = covering::GetCodingDepth<DEPTH_LEVELS>(scales::GetUpperScale());
    auto bestCell = m2::CellId<DEPTH_LEVELS>::FromInt64(intervals.begin()->first, cellDepth);
    std::set<int64_t> bestCells;
    while (bestCell.Level() > 0)
    {
      bestCells.insert(bestCell.ToInt64(cellDepth));
      bestCell = bestCell.Parent();
    }

    for (auto const & i : intervals)
    {
      if (bestCells.find(i.first) != bestCells.end())
      {
        m_intervalIndex->ForEach(processAll, i.first, i.second);
      }
      else
      {
        m_intervalIndex->ForEach(process, i.first, i.second);
        if (objects.size() >= sizeHint)
          return;
      }
    }
  }

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