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

filter_elements.cpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce87770a6993811a542bc9ee60b671449757be5e (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "generator/filter_elements.hpp"

#include "base/logging.hpp"

#include <algorithm>
#include <fstream>
#include <iterator>

namespace
{
template <typename T>
class Set
{
public:
  bool Add(T const & key)
  {
    if (Contains(key))
      return false;

    m_vec.push_back(key);
    return true;
  }

  bool Contains(T const & key) const
  {
    return std::find(std::begin(m_vec), std::end(m_vec), key) != std::end(m_vec);
  }

private:
  std::vector<T> m_vec;
};
}  // namespace

namespace generator
{
bool FilterData::IsMatch(Tags const & elementTags, Tags const & tags)
{
  auto const fn = [&](OsmElement::Tag const & t)
  {
    auto const pred = [&](OsmElement::Tag const & tag) { return tag.key == t.key; };
    auto const it = std::find_if(std::begin(elementTags), std::end(elementTags), pred);
    return it == std::end(elementTags) ? false : t.value == "*" || it->value == t.value;
  };

  return std::all_of(std::begin(tags), std::end(tags), fn);
}

void FilterData::AddSkippedId(uint64_t id)
{
  m_skippedIds.insert(id);
}

void FilterData::AddSkippedTags(Tags const & tags)
{
  m_rulesStorage.push_back(tags);
  for (auto const & t : tags)
    m_skippedTags.emplace(t.key, m_rulesStorage.back());
}

bool FilterData::NeedSkipWithId(uint64_t id) const
{
  return m_skippedIds.find(id) != std::end(m_skippedIds);
}

bool FilterData::NeedSkipWithTags(Tags const & tags) const
{
  Set<Tags const *> s;
  for (auto const & tag : tags)
  {
    auto const t = m_skippedTags.equal_range(tag.key);
    for (auto it = t.first; it != t.second; ++it)
    {
      Tags const & t = it->second;
      if (s.Add(&t) && IsMatch(tags, t))
        return true;
    }
  }

  return false;
}

bool FilterElements::ParseSection(json_t * json, FilterData & fdata)
{
  if (!json_is_object(json))
    return false;

  char const * key = nullptr;
  json_t * value = nullptr;
  json_object_foreach(json, key, value)
  {
    if (std::strcmp("ids", key) == 0 && !ParseIds(value, fdata))
      return false;
    else if (std::strcmp("tags", key) == 0 && !ParseTags(value, fdata))
      return false;

    return true;
  }

  return true;
}

bool FilterElements::ParseIds(json_t * json, FilterData & fdata)
{
  if (!json_is_array(json))
    return false;

  size_t const sz = json_array_size(json);
  for (size_t i = 0; i < sz; ++i)
  {
    auto const * o = json_array_get(json, i);
    if (!json_is_integer(o))
      return false;

    auto const val = json_integer_value(o);
    if (val < 0)
      return false;

    fdata.AddSkippedId(static_cast<uint64_t>(val));
  }

  return true;
}

bool FilterElements::ParseTags(json_t * json, FilterData & fdata)
{
  if (!json_is_array(json))
    return false;

  size_t const sz = json_array_size(json);
  for (size_t i = 0; i < sz; ++i)
  {
    auto * o = json_array_get(json, i);
    if (!json_is_object(o))
      return false;

    char const * key = nullptr;
    json_t * value = nullptr;
    FilterData::Tags tags;
    json_object_foreach(o, key, value)
    {
      if (!json_is_string(value))
        return false;

      auto const val = json_string_value(value);
      tags.emplace_back(key, val);
    }

    if (!tags.empty())
      fdata.AddSkippedTags(tags);
  }

  return true;
}

FilterElements::FilterElements(std::string const & filename)
{
  std::ifstream stream(filename);
  std::string str((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
  if (!ParseString(str))
    LOG(LERROR, ("Cannot parse file", filename));
}

bool FilterElements::NeedSkip(OsmElement const & element) const
{
  switch (element.type)
  {
  case OsmElement::EntityType::Node: return NeedSkip(element, m_nodes);
  case OsmElement::EntityType::Way: return NeedSkip(element, m_ways);
  case OsmElement::EntityType::Relation: return NeedSkip(element, m_relations);
  default: return false;
  }
}

bool FilterElements::NeedSkip(OsmElement const & element,  FilterData const & fdata) const
{
  return fdata.NeedSkipWithId(element.id) || fdata.NeedSkipWithTags(element.Tags());
}

bool FilterElements::ParseString(std::string const & str)
{
  base::Json json(str);
  if (!json_is_object(json.get()))
    return false;

  char const * key = nullptr;
  json_t * value = nullptr;
  json_object_foreach(json.get(), key, value)
  {
    bool res = true;
    if (std::strcmp("node", key) == 0)
      res = ParseSection(value, m_nodes);
    else if (std::strcmp("way", key) == 0)
      res = ParseSection(value, m_ways);
    else if (std::strcmp("relation", key) == 0)
      res = ParseSection(value, m_relations);

    if (!res)
      return false;
  }

  return true;
}
}  // namespace generator