Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
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-2023 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 
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>
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 
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),
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),
77  {
78  m_location_handler.ignore_errors();
79  if (m_with_areas) {
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  return m_reader.close();
121  }
122 
123  bool eof() const {
124  return m_reader.eof();
125  }
126 
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
Definition: flex_reader.hpp:58
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
osmium::memory::Buffer read()
Definition: reader.hpp:444
osmium::io::Header header()
Definition: reader.hpp:418
bool eof() const
Definition: reader.hpp:498
void close()
Definition: reader.hpp:384
void read_relations(TIter begin, TIter end)
Definition: collector.hpp:547
HandlerPass2 & handler(const callback_func_type &callback=nullptr)
Definition: collector.hpp:479
type
Definition: entity_bits.hpp:63
@ node
Definition: entity_bits.hpp:68
@ nwr
node, way, or relation object
Definition: entity_bits.hpp:71
@ relation
Definition: entity_bits.hpp:70
@ nothing
Definition: entity_bits.hpp:67
@ 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