-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from lonvia/filters
Introduce filter handlers
- Loading branch information
Showing
20 changed files
with
456 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* SPDX-License-Identifier: BSD-2-Clause | ||
* | ||
* This file is part of pyosmium. (https://osmcode.org/pyosmium/) | ||
* | ||
* Copyright (C) 2024 Sarah Hoffmann <[email protected]> and others. | ||
* For a full list of authors see the git log. | ||
*/ | ||
#ifndef PYOSMIUM_BASE_FILTER_HPP | ||
#define PYOSMIUM_BASE_FILTER_HPP | ||
|
||
#include <osmium/osm.hpp> | ||
#include <osmium/osm/entity_bits.hpp> | ||
|
||
#include "base_handler.h" | ||
|
||
namespace pyosmium { | ||
|
||
class BaseFilter : public BaseHandler { | ||
public: | ||
bool node(osmium::Node const *n) override | ||
{ | ||
return (m_enabled_for & osmium::osm_entity_bits::node) | ||
&& filter_node(n); | ||
} | ||
|
||
bool way(osmium::Way *n) override | ||
{ | ||
return (m_enabled_for & osmium::osm_entity_bits::way) | ||
&& filter_way(n); | ||
} | ||
|
||
bool relation(osmium::Relation const *n) override | ||
{ | ||
return (m_enabled_for & osmium::osm_entity_bits::relation) | ||
&& filter_relation(n); | ||
} | ||
|
||
bool area(osmium::Area const *n) override | ||
{ | ||
return (m_enabled_for & osmium::osm_entity_bits::area) | ||
&& filter_area(n); | ||
} | ||
|
||
bool changeset(osmium::Changeset const *n) override | ||
{ | ||
return (m_enabled_for & osmium::osm_entity_bits::changeset) | ||
&& filter_changeset(n); | ||
} | ||
|
||
BaseFilter *enable_for(osmium::osm_entity_bits::type entities) | ||
{ | ||
m_enabled_for = entities; | ||
return this; | ||
} | ||
|
||
protected: | ||
virtual bool filter(osmium::OSMObject const *) { return false; } | ||
virtual bool filter_node(osmium::Node const *n) { return filter(n); } | ||
virtual bool filter_way(osmium::Way const *w) { return filter(w); } | ||
virtual bool filter_relation(osmium::Relation const *r) { return filter(r); } | ||
virtual bool filter_area(osmium::Area const *a) { return filter(a); } | ||
virtual bool filter_changeset(osmium::Changeset const *) { return false; } | ||
|
||
private: | ||
osmium::osm_entity_bits::type m_enabled_for = osmium::osm_entity_bits::all; | ||
|
||
}; | ||
|
||
|
||
void init_empty_tag_filter(pybind11::module &m); | ||
void init_key_filter(pybind11::module &m); | ||
|
||
} // namespace | ||
|
||
|
||
#endif //PYOSMIUM_BASE_FILTER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* SPDX-License-Identifier: BSD-2-Clause | ||
* | ||
* This file is part of pyosmium. (https://osmcode.org/pyosmium/) | ||
* | ||
* Copyright (C) 2024 Sarah Hoffmann <[email protected]> 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 EmptyTagFilter : public pyosmium::BaseFilter | ||
{ | ||
bool filter(osmium::OSMObject const *o) override | ||
{ | ||
return o->tags().empty(); | ||
} | ||
|
||
bool filter_changeset(osmium::Changeset const *o) override | ||
{ | ||
return o->tags().empty(); | ||
} | ||
|
||
}; | ||
|
||
} | ||
|
||
namespace pyosmium { | ||
|
||
void init_empty_tag_filter(pybind11::module &m) | ||
{ | ||
py::class_<EmptyTagFilter, pyosmium::BaseFilter, BaseHandler>(m, "EmptyTagFilter") | ||
.def(py::init<>()) | ||
; | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* SPDX-License-Identifier: BSD-2-Clause | ||
* | ||
* This file is part of pyosmium. (https://osmcode.org/pyosmium/) | ||
* | ||
* Copyright (C) 2024 Sarah Hoffmann <[email protected]> and others. | ||
* For a full list of authors see the git log. | ||
*/ | ||
#include <pybind11/pybind11.h> | ||
|
||
#include "base_filter.h" | ||
|
||
|
||
namespace py = pybind11; | ||
|
||
PYBIND11_MODULE(_filter, m) { | ||
py::class_<pyosmium::BaseFilter, BaseHandler>(m, "BaseFilter") | ||
.def("enable_for", &pyosmium::BaseFilter::enable_for, | ||
py::arg("entities"), | ||
"Set the OSM types this filter should be used for.") | ||
; | ||
|
||
pyosmium::init_empty_tag_filter(m); | ||
pyosmium::init_key_filter(m); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* SPDX-License-Identifier: BSD-2-Clause | ||
* | ||
* This file is part of pyosmium. (https://osmcode.org/pyosmium/) | ||
* | ||
* Copyright (C) 2024 Sarah Hoffmann <[email protected]> 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(osmium::Changeset 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; | ||
} | ||
|
||
private: | ||
std::vector<std::string> m_keys; | ||
}; | ||
|
||
} | ||
|
||
namespace pyosmium { | ||
|
||
void init_key_filter(pybind11::module &m) | ||
{ | ||
py::class_<KeyFilter, pyosmium::BaseFilter, BaseHandler>(m, "KeyFilter") | ||
.def(py::init<py::args>()) | ||
; | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.