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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2025 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
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>
44#include <osmium/visitor.hpp>
45
46#include <string>
47#include <utility>
48#include <vector>
49
50namespace osmium {
51
55 namespace experimental {
56
57 template <typename TLocationHandler>
58 class FlexReader {
59
62
63 TLocationHandler& m_location_handler;
64
66 osmium::area::Assembler::config_type m_assembler_config;
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),
74 m_location_handler(location_handler),
75 m_reader(file, m_entities),
77 {
78 m_location_handler.ignore_errors();
79 if (m_with_areas) {
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 }
109 }
110 }
111
112 return buffer;
113 }
114
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
130
131 }; // class FlexReader
132
133 } // namespace experimental
134
135} // namespace osmium
136
137#endif // OSMIUM_EXPERIMENTAL_FLEX_READER_HPP
Definition multipolygon_collector.hpp:77
bool m_with_areas
Definition flex_reader.hpp:60
const osmium::area::MultipolygonCollector< osmium::area::Assembler > & collector() const
Definition flex_reader.hpp:127
osmium::io::Header header()
Definition flex_reader.hpp:115
osmium::area::Assembler::config_type m_assembler_config
Definition flex_reader.hpp:66
osmium::memory::Buffer read()
Definition flex_reader.hpp:94
osmium::io::Reader m_reader
Definition flex_reader.hpp:65
osmium::area::MultipolygonCollector< osmium::area::Assembler > m_collector
Definition flex_reader.hpp:67
TLocationHandler & m_location_handler
Definition flex_reader.hpp:63
void close()
Definition flex_reader.hpp:119
bool eof() const
Definition flex_reader.hpp:123
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
FlexReader(const char *filename, TLocationHandler &location_handler, osmium::osm_entity_bits::type entities=osmium::osm_entity_bits::nwr)
Definition flex_reader.hpp:90
osmium::osm_entity_bits::type m_entities
Definition flex_reader.hpp:61
FlexReader(const std::string &filename, TLocationHandler &location_handler, osmium::osm_entity_bits::type entities=osmium::osm_entity_bits::nwr)
Definition flex_reader.hpp:86
Definition file.hpp:72
Definition header.hpp:68
Definition reader.hpp:90
Code related to the building of areas (multipolygons) from relations.
Definition assembler.hpp:55
Experimental code that is not "officially" supported.
Definition flex_reader.hpp:55
Everything related to input and output of OSM data.
Definition bzip2_compression.hpp:86
Bit field for OSM entity types.
Definition entity_bits.hpp:46
type
Definition entity_bits.hpp:63
@ node
Definition entity_bits.hpp:68
@ nwr
node, way, or relation object
Definition entity_bits.hpp:74
@ relation
Definition entity_bits.hpp:71
@ way
Definition entity_bits.hpp:69
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
void apply(TIterator it, TIterator end, THandlers &&... handlers)
Definition visitor.hpp:326
@ node
Definition item_type.hpp:48
@ way
Definition item_type.hpp:49