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

Loading...
Searching...
No Matches
flex_reader.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_EXPERIMENTAL_FLEX_READER_HPP
2#define OSMIUM_EXPERIMENTAL_FLEX_READER_HPP
3
4/*
5
6 This file is part of Osmium (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
9
10 Boost Software License - Version 1.0 - August 17th, 2003
11
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/area/assembler.hpp>
37#include <osmium/area/multipolygon_collector.hpp>
38#include <osmium/handler/node_locations_for_ways.hpp> // IWYU pragma: keep
39#include <osmium/io/file.hpp>
40#include <osmium/io/header.hpp>
41#include <osmium/io/reader.hpp>
42#include <osmium/memory/buffer.hpp>
43#include <osmium/osm/entity_bits.hpp>
44#include <osmium/visitor.hpp>
45
46#include <string>
47#include <utility>
48#include <vector>
49
50 namespace osmium {
51
55 namespace experimental {
56
57 template <typename TLocationHandler>
58 class FlexReader {
59
60 bool m_with_areas;
61 osmium::osm_entity_bits::type m_entities;
62
63 TLocationHandler& m_location_handler;
64
65 osmium::io::Reader m_reader;
66 osmium::area::Assembler::config_type m_assembler_config;
67 osmium::area::MultipolygonCollector<osmium::area::Assembler> m_collector;
68
69 public:
70
71 explicit FlexReader(const osmium::io::File& file, TLocationHandler& location_handler, osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::nwr) :
72 m_with_areas((entities & osmium::osm_entity_bits::area) != 0),
73 m_entities((entities & ~osmium::osm_entity_bits::area) | (m_with_areas ? osmium::osm_entity_bits::node | osmium::osm_entity_bits::way : osmium::osm_entity_bits::nothing)),
74 m_location_handler(location_handler),
75 m_reader(file, m_entities),
76 m_collector(m_assembler_config)
77 {
78 m_location_handler.ignore_errors();
79 if (m_with_areas) {
80 osmium::io::Reader reader{file, osmium::osm_entity_bits::relation};
81 m_collector.read_relations(reader);
82 reader.close();
83 }
84 }
85
86 explicit FlexReader(const std::string& filename, TLocationHandler& location_handler, osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::nwr) :
87 FlexReader(osmium::io::File{filename}, location_handler, entities) {
88 }
89
90 explicit FlexReader(const char* filename, TLocationHandler& location_handler, osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::nwr) :
91 FlexReader(osmium::io::File{filename}, location_handler, entities) {
92 }
93
94 osmium::memory::Buffer read() {
95 osmium::memory::Buffer buffer = m_reader.read();
96
97 if (buffer) {
98 if (m_with_areas) {
99 std::vector<osmium::memory::Buffer> area_buffers;
100 osmium::apply(buffer, m_location_handler, m_collector.handler([&area_buffers](osmium::memory::Buffer&& area_buffer) {
101 area_buffers.push_back(std::move(area_buffer));
102 }));
103 for (const osmium::memory::Buffer& b : area_buffers) {
104 buffer.add_buffer(b);
105 buffer.commit();
106 }
107 } else if (m_entities & (osmium::osm_entity_bits::node | osmium::osm_entity_bits::way)) {
108 osmium::apply(buffer, m_location_handler);
109 }
110 }
111
112 return buffer;
113 }
114
115 osmium::io::Header header() {
116 return m_reader.header();
117 }
118
119 void close() {
120 m_reader.close();
121 }
122
123 bool eof() const {
124 return m_reader.eof();
125 }
126
127 const osmium::area::MultipolygonCollector<osmium::area::Assembler>& collector() const {
128 return m_collector;
129 }
130
131 }; // class FlexReader
132
133 } // namespace experimental
134
135} // namespace osmium
136
137#endif // OSMIUM_EXPERIMENTAL_FLEX_READER_HPP
assembler.hpp
buffer.hpp
osmium::area::MultipolygonCollector
Definition multipolygon_collector.hpp:77
osmium::experimental::FlexReader
Definition flex_reader.hpp:58
osmium::experimental::FlexReader::m_with_areas
bool m_with_areas
Definition flex_reader.hpp:60
osmium::experimental::FlexReader::collector
const osmium::area::MultipolygonCollector< osmium::area::Assembler > & collector() const
Definition flex_reader.hpp:127
osmium::experimental::FlexReader::header
osmium::io::Header header()
Definition flex_reader.hpp:115
osmium::experimental::FlexReader::m_assembler_config
osmium::area::Assembler::config_type m_assembler_config
Definition flex_reader.hpp:66
osmium::experimental::FlexReader::read
osmium::memory::Buffer read()
Definition flex_reader.hpp:94
osmium::experimental::FlexReader::m_reader
osmium::io::Reader m_reader
Definition flex_reader.hpp:65
osmium::experimental::FlexReader::m_collector
osmium::area::MultipolygonCollector< osmium::area::Assembler > m_collector
Definition flex_reader.hpp:67
osmium::experimental::FlexReader::m_location_handler
TLocationHandler & m_location_handler
Definition flex_reader.hpp:63
osmium::experimental::FlexReader::close
void close()
Definition flex_reader.hpp:119
osmium::experimental::FlexReader::eof
bool eof() const
Definition flex_reader.hpp:123
osmium::experimental::FlexReader::FlexReader
FlexReader(const osmium::io::File &file, TLocationHandler &location_handler, osmium::osm_entity_bits::type entities=osmium::osm_entity_bits::nwr)
Definition flex_reader.hpp:71
osmium::experimental::FlexReader::FlexReader
FlexReader(const char *filename, TLocationHandler &location_handler, osmium::osm_entity_bits::type entities=osmium::osm_entity_bits::nwr)
Definition flex_reader.hpp:90
osmium::experimental::FlexReader::m_entities
osmium::osm_entity_bits::type m_entities
Definition flex_reader.hpp:61
osmium::experimental::FlexReader::FlexReader
FlexReader(const std::string &filename, TLocationHandler &location_handler, osmium::osm_entity_bits::type entities=osmium::osm_entity_bits::nwr)
Definition flex_reader.hpp:86
osmium::io::File
Definition file.hpp:72
osmium::io::Header
Definition header.hpp:68
osmium::io::Reader
Definition reader.hpp:90
osmium::io::Reader::read
osmium::memory::Buffer read()
Definition reader.hpp:444
osmium::io::Reader::header
osmium::io::Header header()
Definition reader.hpp:418
osmium::io::Reader::eof
bool eof() const
Definition reader.hpp:498
osmium::io::Reader::close
void close()
Definition reader.hpp:384
osmium::relations::Collector::handler
HandlerPass2 & handler(const callback_func_type &callback=nullptr)
Definition collector.hpp:479
osmium::relations::Collector::read_relations
void read_relations(TIter begin, TIter end)
Definition collector.hpp:547
entity_bits.hpp
header.hpp
file.hpp
multipolygon_collector.hpp
osmium::osm_entity_bits::type
type
Definition entity_bits.hpp:63
osmium::osm_entity_bits::node
@ node
Definition entity_bits.hpp:68
osmium::osm_entity_bits::nwr
@ nwr
node, way, or relation object
Definition entity_bits.hpp:74
osmium::osm_entity_bits::relation
@ relation
Definition entity_bits.hpp:71
osmium::osm_entity_bits::way
@ way
Definition entity_bits.hpp:69
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::apply
void apply(TIterator it, TIterator end, THandlers &&... handlers)
Definition visitor.hpp:326
osmium::item_type::node
@ node
osmium::item_type::way
@ way
node_locations_for_ways.hpp
reader.hpp
visitor.hpp