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

cities_boundaries_table.cpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c33a43946571ddc853567f2bb0a8c8c38e531d2 (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
#include "search/cities_boundaries_table.hpp"

#include "search/categories_cache.hpp"
#include "search/localities_source.hpp"
#include "search/mwm_context.hpp"
#include "search/utils.hpp"

#include "indexer/cities_boundaries_serdes.hpp"

#include "coding/reader.hpp"

#include "base/assert.hpp"
#include "base/cancellable.hpp"
#include "base/logging.hpp"

#include <algorithm>

using namespace indexer;
using namespace std;

namespace search
{
// CitiesBoundariesTable::Boundaries ---------------------------------------------------------------
bool CitiesBoundariesTable::Boundaries::HasPoint(m2::PointD const & p) const
{
  return any_of(m_boundaries.begin(), m_boundaries.end(),
                [&](CityBoundary const & b) { return b.HasPoint(p, m_eps); });
}

// CitiesBoundariesTable ---------------------------------------------------------------------------
bool CitiesBoundariesTable::Load(Index const & index)
{
  auto handle = FindWorld(index);
  if (!handle.IsAlive())
  {
    LOG(LERROR, ("Can't find world map file"));
    return false;
  }

  MwmContext context(move(handle));
  auto const localities = CategoriesCache(LocalitiesSource{}, my::Cancellable{}).Get(context);

  auto const & cont = context.m_value.m_cont;

  if (!cont.IsExist(CITIES_BOUNDARIES_FILE_TAG))
  {
    LOG(LWARNING, ("No cities boundaries table in the world map."));
    return false;
  }

  vector<vector<CityBoundary>> all;
  double precision;

  try
  {
    auto reader = cont.GetReader(CITIES_BOUNDARIES_FILE_TAG);
    ReaderSource<ReaderPtr<ModelReader>> source(reader);
    CitiesBoundariesSerDes::Deserialize(source, all, precision);
  }
  catch (Reader::Exception const & e)
  {
    LOG(LERROR, ("Can't read cities boundaries table from the world map:", e.Msg()));
    return false;
  }

  ASSERT_EQUAL(all.size(), localities.PopCount(), ());
  if (all.size() != localities.PopCount())
  {
    LOG(LERROR,
        ("Wrong number of boundaries, expected:", localities.PopCount(), "actual:", all.size()));
    return false;
  }

  m_table.clear();
  m_eps = precision;
  size_t boundary = 0;
  localities.ForEach([&](uint64_t fid) {
    ASSERT_LESS(boundary, all.size(), ());
    m_table[base::asserted_cast<uint32_t>(fid)] = move(all[boundary]);
    ++boundary;
  });
  ASSERT_EQUAL(boundary, all.size(), ());
  return true;
}

bool CitiesBoundariesTable::Get(uint32_t fid, Boundaries & bs) const
{
  auto it = m_table.find(fid);
  if (it == m_table.end())
    return false;
  bs = Boundaries(it->second, m_eps);
  return true;
}
}  // namespace search