File: key_filter.cc

package info (click to toggle)
pyosmium 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: python: 4,400; cpp: 2,504; makefile: 20
file content (76 lines) | stat: -rw-r--r-- 1,641 bytes parent folder | download | duplicates (6)
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
/* SPDX-License-Identifier: BSD-2-Clause
 *
 * This file is part of pyosmium. (https://osmcode.org/pyosmium/)
 *
 * Copyright (C) 2024 Sarah Hoffmann <lonvia@denofr.de> and others.
 * For a full list of authors see the git log.
 */
#include <pybind11/pybind11.h>

#include <osmium/osm.hpp>

#include "base_filter.h"

namespace py = pybind11;

namespace {

class KeyFilter : public pyosmium::BaseFilter
{
public:
    KeyFilter(py::args args)
    {
        if (args.empty()) {
            throw py::type_error{"Need keys to filter on."};
        }

        m_keys.reserve(args.size());
        for (auto const &arg: args) {
            if (!py::isinstance<py::str>(arg)) {
                throw py::type_error{"Arguments must be strings."};
            }

            m_keys.push_back(arg.cast<std::string>());
        }
    }

    bool filter(osmium::OSMObject const *o) override
    {
        auto const &tags = o->tags();
        for (auto const &key: m_keys) {
            if (tags.has_key(key.c_str())) {
                return false;
            }
        }

        return true;
    }

    bool filter_changeset(pyosmium::PyOSMChangeset &o) override
    {
        auto const &tags = o.get()->tags();
        for (auto const &key: m_keys) {
            if (tags.has_key(key.c_str())) {
                return false;
            }
        }

        return true;
    }

private:
    std::vector<std::string> m_keys;
};

} // namespace

namespace pyosmium {

void init_key_filter(pybind11::module &m)
{
    py::class_<KeyFilter, pyosmium::BaseFilter, BaseHandler>(m, "KeyFilter")
        .def(py::init<py::args>())
    ;
}

} // namespace