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: 18a58b743e146bdb0ca5549ccbea6d674a9b111f (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
#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 "geometry/rect2d.hpp"

#include "base/osm_id.hpp"

#include <cstdint>
#include <functional>
#include <memory>
#include <set>

namespace indexer
{
// Geometry index which stores osm::Id 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(osm::Id const &)>;

  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>;
}  // namespace indexer