Loading...
Searching...
No Matches
multipolygon_manager.hpp
Go to the documentation of this file.
#ifndef OSMIUM_AREA_MULTIPOLYGON_MANAGER_HPP
#define OSMIUM_AREA_MULTIPOLYGON_MANAGER_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/area/stats.hpp>
#include <osmium/osm/item_type.hpp>
#include <osmium/osm/relation.hpp>
#include <osmium/osm/tag.hpp>
#include <osmium/osm/way.hpp>
#include <osmium/relations/manager_util.hpp>
#include <osmium/relations/members_database.hpp>
#include <osmium/relations/relations_database.hpp>
#include <osmium/relations/relations_manager.hpp>
#include <osmium/storage/item_stash.hpp>
#include <osmium/tags/taglist.hpp>
#include <osmium/tags/tags_filter.hpp>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <vector>
namespace osmium {
namespace area {
template <typename TAssembler>
class MultipolygonManager : public osmium::relations::RelationsManager<MultipolygonManager<TAssembler>, false, true, false> {
using assembler_config_type = typename TAssembler::config_type;
const assembler_config_type m_assembler_config;
area_stats m_stats;
osmium::TagsFilter m_filter;
public:
explicit MultipolygonManager(assembler_config_type assembler_config, osmium::TagsFilter filter = osmium::TagsFilter{true}) :
m_assembler_config(std::move(assembler_config)),
m_filter(std::move(filter)) {
}
const area_stats& stats() const noexcept {
return m_stats;
}
bool new_relation(const osmium::Relation& relation) const {
const char* type = relation.tags().get_value_by_key("type");
// ignore relations without "type" tag
if (type == nullptr) {
return false;
}
if (((!std::strcmp(type, "multipolygon")) || (!std::strcmp(type, "boundary"))) && osmium::tags::match_any_of(relation.tags(), m_filter)) {
return std::any_of(relation.members().cbegin(), relation.members().cend(), [](const RelationMember& member) {
return member.type() == osmium::item_type::way;
});
}
return false;
}
void complete_relation(const osmium::Relation& relation) {
std::vector<const osmium::Way*> ways;
ways.reserve(relation.members().size());
for (const auto& member : relation.members()) {
if (member.ref() != 0) {
ways.push_back(this->get_member_way(member.ref()));
assert(ways.back() != nullptr);
}
}
try {
TAssembler assembler{m_assembler_config};
assembler(relation, ways, this->buffer());
m_stats += assembler.stats();
} catch (const osmium::invalid_location&) {
// XXX ignore
}
}
void after_way(const osmium::Way& way) {
// you need at least 4 nodes to make up a polygon
if (way.nodes().size() <= 3) {
return;
}
try {
if (!way.nodes().front().location() || !way.nodes().back().location()) {
throw osmium::invalid_location{"invalid location"};
}
if (way.ends_have_same_location()) {
if (way.tags().has_tag("area", "no")) {
return;
}
if (osmium::tags::match_none_of(way.tags(), m_filter)) {
return;
}
TAssembler assembler{m_assembler_config};
assembler(way, this->buffer());
m_stats += assembler.stats();
this->possibly_flush();
}
} catch (const osmium::invalid_location&) {
// XXX ignore
}
}
}; // class MultipolygonManager
} // namespace area
} // namespace osmium
#endif // OSMIUM_AREA_MULTIPOLYGON_MANAGER_HPP
osmium::RelationMember
Definition relation.hpp:56
osmium::Relation
Definition relation.hpp:161
osmium::TagsFilterBase< bool >
osmium::Way
Definition way.hpp:72
osmium::area::MultipolygonManager
Definition multipolygon_manager.hpp:76
osmium::area::MultipolygonManager::m_assembler_config
const assembler_config_type m_assembler_config
Definition multipolygon_manager.hpp:79
osmium::area::MultipolygonManager::stats
const area_stats & stats() const noexcept
Definition multipolygon_manager.hpp:105
osmium::area::MultipolygonManager::new_relation
bool new_relation(const osmium::Relation &relation) const
Definition multipolygon_manager.hpp:113
osmium::area::MultipolygonManager::m_stats
area_stats m_stats
Definition multipolygon_manager.hpp:81
osmium::area::MultipolygonManager::assembler_config_type
typename TAssembler::config_type assembler_config_type
Definition multipolygon_manager.hpp:78
osmium::area::MultipolygonManager::complete_relation
void complete_relation(const osmium::Relation &relation)
Definition multipolygon_manager.hpp:135
osmium::area::MultipolygonManager::MultipolygonManager
MultipolygonManager(assembler_config_type assembler_config, osmium::TagsFilter filter=osmium::TagsFilter{true})
Definition multipolygon_manager.hpp:96
osmium::area::MultipolygonManager::m_filter
osmium::TagsFilter m_filter
Definition multipolygon_manager.hpp:83
osmium::area::MultipolygonManager::after_way
void after_way(const osmium::Way &way)
Definition multipolygon_manager.hpp:154
osmium::relations::RelationsManagerBase::possibly_flush
void possibly_flush()
Flush the output buffer if it is full.
Definition relations_manager.hpp:274
osmium::relations::RelationsManagerBase::buffer
osmium::memory::Buffer & buffer() noexcept
Access the output buffer.
Definition relations_manager.hpp:259
osmium::relations::RelationsManagerBase::get_member_way
const osmium::Way * get_member_way(osmium::object_id_type id) const noexcept
Definition relations_manager.hpp:213
osmium::relations::RelationsManager
Definition relations_manager.hpp:305
item_stash.hpp
item_type.hpp
manager_util.hpp
members_database.hpp
osmium::osm_entity_bits::area
@ area
Definition entity_bits.hpp:72
osmium::tags::match_none_of
bool match_none_of(const osmium::TagList &tag_list, const TFilter &filter)
Definition taglist.hpp:60
osmium::tags::match_any_of
bool match_any_of(const osmium::TagList &tag_list, const TFilter &filter)
Definition taglist.hpp:50
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::item_type::relation
@ relation
osmium::item_type::way
@ way
std
Definition location.hpp:555
relation.hpp
relations_database.hpp
relations_manager.hpp
stats.hpp
osmium::area::area_stats
Definition stats.hpp:49
osmium::invalid_location
Definition location.hpp:53
tag.hpp
taglist.hpp
tags_filter.hpp
way.hpp