Libosmium: include/osmium/tags/filter.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
filter.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_TAGS_FILTER_HPP
2#define OSMIUM_TAGS_FILTER_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/memory/collection.hpp>
37#include <osmium/osm/tag.hpp>
38
39#include <cstddef>
40#include <cstring>
41#include <string>
42#include <type_traits>
43#include <vector>
44
45namespace osmium {
46
47 namespace tags {
48
49 template <typename TKey>
50 struct match_key {
51 bool operator()(const TKey& rule_key, const char* tag_key) const {
52 return rule_key == tag_key;
53 }
54 }; // struct match_key
55
56 template <>
57 struct match_key<std::string> {
58 bool operator()(const std::string& rule_key, const char* tag_key) const {
59 return !std::strcmp(rule_key.c_str(), tag_key);
60 }
61 }; // struct match_key
62
63 struct match_key_prefix {
64 bool operator()(const std::string& rule_key, const char* tag_key) const {
65 return rule_key.compare(0, std::string::npos, tag_key, 0, rule_key.size()) == 0;
66 }
67 }; // struct match_key_prefix
68
69 template <typename TValue>
70 struct match_value {
71 bool operator()(const TValue& rule_value, const char* tag_value) const {
72 return rule_value == tag_value;
73 }
74 }; // struct match_value
75
76 template <>
77 struct match_value<std::string> {
78 bool operator()(const std::string& rule_value, const char* tag_value) const {
79 return !std::strcmp(rule_value.c_str(), tag_value);
80 }
81 }; // struct match_value
82
83 template <>
84 struct match_value<void> {
85 bool operator()(const bool /*rule_value*/, const char* /*tag_value*/) const noexcept {
86 return true;
87 }
88 }; // struct match_value<void>
89
91 template <typename TKey, typename TValue = void, typename TKeyComp = match_key<TKey>, typename TValueComp = match_value<TValue>>
92 class Filter {
93
94 using key_type = TKey;
95 using value_type = std::conditional_t<std::is_void<TValue>::value, bool, TValue>;
96
97 struct Rule {
98 key_type key;
99 value_type value;
100 bool ignore_value;
101 bool result;
102
103 explicit Rule(bool r, bool ignore, key_type k, value_type v) :
104 key(std::move(k)),
105 value(std::move(v)),
106 ignore_value(ignore),
107 result(r) {
108 }
109
110 explicit Rule(bool r, bool ignore, key_type k) :
111 key(std::move(k)),
112 value(),
113 ignore_value(ignore),
114 result(r) {
115 }
116
117 }; // struct Rule
118
119 std::vector<Rule> m_rules;
120 bool m_default_result;
121
122 public:
123
124 using filter_type = Filter<TKey, TValue, TKeyComp, TValueComp>;
125 using argument_type = const osmium::Tag&;
126 using result_type = bool;
127 using iterator = osmium::memory::CollectionFilterIterator<filter_type, const osmium::Tag>;
128
129 explicit Filter(bool default_result = false) :
130 m_default_result(default_result) {
131 }
132
133 template <typename V = TValue, typename std::enable_if_t<!std::is_void<V>::value, int> = 0>
134 Filter& add(bool result, const key_type& key, const value_type& value) {
135 m_rules.emplace_back(result, false, key, value);
136 return *this;
137 }
138
139 Filter& add(bool result, const key_type& key) {
140 m_rules.emplace_back(result, true, key);
141 return *this;
142 }
143
144 bool operator()(const osmium::Tag& tag) const {
145 for (const Rule& rule : m_rules) {
146 if (TKeyComp()(rule.key, tag.key()) && (rule.ignore_value || TValueComp()(rule.value, tag.value()))) {
147 return rule.result;
148 }
149 }
150 return m_default_result;
151 }
152
158 size_t count() const noexcept {
159 return m_rules.size();
160 }
161
167 bool empty() const noexcept {
168 return m_rules.empty();
169 }
170
171 }; // class Filter
172
174 using KeyValueFilter = Filter<std::string, std::string>;
175
177 using KeyFilter = Filter<std::string>;
178
180 using KeyPrefixFilter = Filter<std::string, void, match_key_prefix>;
181
182 } // namespace tags
183
184} // namespace osmium
185
186#endif // OSMIUM_TAGS_FILTER_HPP
osmium::Tag
Definition tag.hpp:48
osmium::Tag::key
const char * key() const noexcept
Definition tag.hpp:86
osmium::Tag::value
const char * value() const noexcept
Definition tag.hpp:95
osmium::memory::CollectionFilterIterator
Definition collection.hpp:118
osmium::tags::Filter
Definition filter.hpp:92
osmium::tags::Filter< TKey, TValue, TKeyComp, TValueComp >::iterator
osmium::memory::CollectionFilterIterator< filter_type, const osmium::Tag > iterator
Definition filter.hpp:127
osmium::tags::Filter::Filter
Filter(bool default_result=false)
Definition filter.hpp:129
osmium::tags::Filter< TKey, TValue, TKeyComp, TValueComp >::m_default_result
bool m_default_result
Definition filter.hpp:120
osmium::tags::Filter::value_type
std::conditional_t< std::is_void< TValue >::value, bool, TValue > value_type
Definition filter.hpp:95
osmium::tags::Filter::empty
bool empty() const noexcept
Definition filter.hpp:167
osmium::tags::Filter< TKey, TValue, TKeyComp, TValueComp >::m_rules
std::vector< Rule > m_rules
Definition filter.hpp:119
osmium::tags::Filter::operator()
bool operator()(const osmium::Tag &tag) const
Definition filter.hpp:144
osmium::tags::Filter::filter_type
Filter< TKey, TValue, TKeyComp, TValueComp > filter_type
Definition filter.hpp:124
osmium::tags::Filter< TKey, TValue, TKeyComp, TValueComp >::result_type
bool result_type
Definition filter.hpp:126
osmium::tags::Filter::count
size_t count() const noexcept
Definition filter.hpp:158
osmium::tags::Filter::add
Filter & add(bool result, const key_type &key)
Definition filter.hpp:139
osmium::tags::Filter::add
Filter & add(bool result, const key_type &key, const value_type &value)
Definition filter.hpp:134
osmium::tags::Filter::key_type
TKey key_type
Definition filter.hpp:94
osmium::tags::Filter< TKey, TValue, TKeyComp, TValueComp >::argument_type
const osmium::Tag & argument_type
Definition filter.hpp:125
collection.hpp
osmium::tags
Code related to working with OSM tags.
Definition filter.hpp:47
osmium::tags::KeyFilter
Filter< std::string > KeyFilter
Definition filter.hpp:177
osmium::tags::KeyPrefixFilter
Filter< std::string, void, match_key_prefix > KeyPrefixFilter
Definition filter.hpp:180
osmium::tags::KeyValueFilter
Filter< std::string, std::string > KeyValueFilter
Definition filter.hpp:174
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:654
osmium::tags::Filter::Rule::key
key_type key
Definition filter.hpp:98
osmium::tags::Filter::Rule::value
value_type value
Definition filter.hpp:99
osmium::tags::Filter::Rule::Rule
Rule(bool r, bool ignore, key_type k, value_type v)
Definition filter.hpp:103
osmium::tags::Filter::Rule::ignore_value
bool ignore_value
Definition filter.hpp:100
osmium::tags::Filter::Rule::result
bool result
Definition filter.hpp:101
osmium::tags::Filter::Rule::Rule
Rule(bool r, bool ignore, key_type k)
Definition filter.hpp:110
osmium::tags::match_key< std::string >::operator()
bool operator()(const std::string &rule_key, const char *tag_key) const
Definition filter.hpp:58
osmium::tags::match_key_prefix
Definition filter.hpp:63
osmium::tags::match_key_prefix::operator()
bool operator()(const std::string &rule_key, const char *tag_key) const
Definition filter.hpp:64
osmium::tags::match_key
Definition filter.hpp:50
osmium::tags::match_key::operator()
bool operator()(const TKey &rule_key, const char *tag_key) const
Definition filter.hpp:51
osmium::tags::match_value< std::string >::operator()
bool operator()(const std::string &rule_value, const char *tag_value) const
Definition filter.hpp:78
osmium::tags::match_value< void >::operator()
bool operator()(const bool, const char *) const noexcept
Definition filter.hpp:85
osmium::tags::match_value
Definition filter.hpp:70
osmium::tags::match_value::operator()
bool operator()(const TValue &rule_value, const char *tag_value) const
Definition filter.hpp:71
tag.hpp