Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions lib/tag_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@

#include <osmium/osm.hpp>

#include <map>
#include <set>
#include <string>
#include <string_view>

#include "base_filter.h"

namespace py = pybind11;

namespace {

struct Tag
{
Tag(std::string k, std::string v)
: key(std::move(k)), value(std::move(v))
{}

std::string key;
std::string value;
};

class TagFilter : public pyosmium::BaseFilter
{
public:
Expand All @@ -34,7 +29,6 @@ class TagFilter : public pyosmium::BaseFilter
throw py::type_error{"Need tags to filter on."};
}

m_tags.reserve(args.size());
for (auto const &arg: args) {
if (!py::isinstance<py::tuple>(arg)) {
throw py::type_error{"Each tag must be a tuple of (key, value)."};
Expand All @@ -45,37 +39,46 @@ class TagFilter : public pyosmium::BaseFilter
|| !py::isinstance<py::str>(tag[1])) {
throw py::type_error{"Each tag must be a tuple of (key, value)."};
}
m_tags.emplace_back(tag[0].cast<std::string>(),
tag[1].cast<std::string>());
m_tags[tag[0].cast<std::string>()].insert(tag[1].cast<std::string>());
}
}

bool filter(osmium::OSMObject const *o) override
{
auto const &tags = o->tags();
for (auto const &tag: m_tags) {
if (tags.has_tag(tag.key.c_str(), tag.value.c_str())) {
return false;
}
}

return true;
return !has_matching_tag(o->tags());
}

bool filter_changeset(pyosmium::PyOSMChangeset &o) override
{
auto const &tags = o.get()->tags();
for (auto const &tag: m_tags) {
if (tags.has_tag(tag.key.c_str(), tag.value.c_str())) {
return false;
return !has_matching_tag(o.get()->tags());
}

private:
bool has_matching_tag(osmium::TagList const &tags) const
{
// With a single key, looking it up directly is faster than
// walking all tags of the object.
if (m_tags.size() == 1) {
auto const &[key, values] = *m_tags.begin();
char const *value = tags.get_value_by_key(key.c_str());
return value && values.count(std::string_view{value}) > 0;
}

for (auto const &tag: tags) {
auto const it = m_tags.find(std::string_view{tag.key()});
if (it != m_tags.end()
&& it->second.count(std::string_view{tag.value()}) > 0
// Like TagList::has_tag(), only the first tag with a key counts.
&& tags.get_value_by_key(tag.key()) == tag.value()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this line do?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is there so that the filter behaves like has_tag(), which checks only the first tag with a given key. An OSM object can't have the same key twice, though, so it isn't needed. If you agree, I'll remove it, along with the two tests for duplicate keys.

return true;
}
}

return true;
return false;
}

private:
std::vector<Tag> m_tags;
// std::less<> allows lookups by string_view without copying.
std::map<std::string, std::set<std::string, std::less<>>, std::less<>> m_tags;
};

}
Expand Down
9 changes: 7 additions & 2 deletions test/test_tag_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def test_tag_filter_bad_arguments(kv):

@pytest.mark.parametrize('tags,filt', [('foo=bar', [('foo', 'bar')]),
('a=1,b=2', [('x', 'x'), ('a', '1')]),
('a=1,b=2', [('x', 'x'), ('b', '2')])
('a=1,b=2', [('x', 'x'), ('b', '2')]),
('a=1,b=2', [('a', '0'), ('a', '1'), ('a', '3')]),
('a=1,b=2', [('a', '2'), ('b', '2')])
])
def test_tag_filter_pass(opl_reader, tags, filt):
data = f"""\
Expand All @@ -46,7 +48,10 @@ def test_tag_filter_pass(opl_reader, tags, filt):


@pytest.mark.parametrize('tags,filt', [('foo=bar', [('foo', 'bars')]),
('a=1,b=2', [('x', 'x'), ('a', '2')])
('a=1,b=2', [('x', 'x'), ('a', '2')]),
('a=1,b=2', [('a', '2')]),
('a=1,a=2', [('a', '2')]),
('a=1,a=2', [('a', '2'), ('x', 'x')])
])
def test_tag_filter_fail(opl_reader, tags, filt):
data = f"""\
Expand Down