Libosmium  2.4.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 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 
37 #include <osmium/visitor.hpp>
40 
41 namespace osmium {
42 
46  namespace experimental {
47 
48  template <class TLocationHandler>
49  class FlexReader {
50 
53 
54  TLocationHandler& m_location_handler;
55 
59 
60  public:
61 
62  explicit FlexReader(const osmium::io::File& file, TLocationHandler& location_handler, osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::nwr) :
63  m_with_areas((entities & osmium::osm_entity_bits::area) != 0),
64  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)),
65  m_location_handler(location_handler),
66  m_reader(file, m_entities),
67  m_assembler_config(),
68  m_collector(m_assembler_config)
69  {
70  m_location_handler.ignore_errors();
71  if (m_with_areas) {
73  m_collector.read_relations(reader);
74  reader.close();
75  }
76  }
77 
78  explicit FlexReader(const std::string& filename, TLocationHandler& location_handler, osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::nwr) :
79  FlexReader(osmium::io::File(filename), location_handler, entities) {
80  }
81 
82  explicit FlexReader(const char* filename, TLocationHandler& location_handler, osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::nwr) :
83  FlexReader(osmium::io::File(filename), location_handler, entities) {
84  }
85 
87  osmium::memory::Buffer buffer = m_reader.read();
88 
89  if (buffer) {
90  if (m_with_areas) {
91  std::vector<osmium::memory::Buffer> area_buffers;
92  osmium::apply(buffer, m_location_handler, m_collector.handler([&area_buffers](osmium::memory::Buffer&& area_buffer) {
93  area_buffers.push_back(std::move(area_buffer));
94  }));
95  for (const osmium::memory::Buffer& b : area_buffers) {
96  buffer.add_buffer(b);
97  buffer.commit();
98  }
99  } else if (m_entities & (osmium::osm_entity_bits::node | osmium::osm_entity_bits::way)) {
100  osmium::apply(buffer, m_location_handler);
101  }
102  }
103 
104  return buffer;
105  }
106 
108  return m_reader.header();
109  }
110 
111  void close() {
112  return m_reader.close();
113  }
114 
115  bool eof() const {
116  return m_reader.eof();
117  }
118 
120  return m_collector;
121  }
122 
123  }; // class FlexReader
124 
125  } // namespace experimental
126 
127 } // namespace osmium
128 
129 #endif // OSMIUM_EXPERIMENTAL_FLEX_READER_HPP
osmium::memory::Buffer read()
Definition: reader.hpp:252
bool m_with_areas
Definition: flex_reader.hpp:51
type
Definition: entity_bits.hpp:60
TLocationHandler & m_location_handler
Definition: flex_reader.hpp:54
Definition: assembler.hpp:61
osmium::memory::Buffer read()
Definition: flex_reader.hpp:86
void close()
Definition: flex_reader.hpp:111
bool eof() const
Definition: reader.hpp:283
void apply(TIterator it, TIterator end, THandlers &...handlers)
Definition: visitor.hpp:236
osmium::io::Header header() const
Definition: flex_reader.hpp:107
FlexReader(const osmium::io::File &file, TLocationHandler &location_handler, osmium::osm_entity_bits::type entities=osmium::osm_entity_bits::nwr)
Definition: flex_reader.hpp:62
Definition: file.hpp:73
Definition: entity_bits.hpp:64
bool eof() const
Definition: flex_reader.hpp:115
node, way, or relation object
Definition: entity_bits.hpp:66
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
void add_buffer(const Buffer &buffer)
Definition: buffer.hpp:379
osmium::area::Assembler::config_type m_assembler_config
Definition: flex_reader.hpp:57
FlexReader(const char *filename, TLocationHandler &location_handler, osmium::osm_entity_bits::type entities=osmium::osm_entity_bits::nwr)
Definition: flex_reader.hpp:82
Definition: reader.hpp:75
osmium::area::MultipolygonCollector< osmium::area::Assembler > m_collector
Definition: flex_reader.hpp:58
osmium::io::Header header() const
Definition: reader.hpp:238
Definition: buffer.hpp:94
Definition: entity_bits.hpp:63
HandlerPass2 & handler(const callback_func_type &callback=nullptr)
Definition: collector.hpp:461
osmium::io::Reader m_reader
Definition: flex_reader.hpp:56
Definition: entity_bits.hpp:62
FlexReader(const std::string &filename, TLocationHandler &location_handler, osmium::osm_entity_bits::type entities=osmium::osm_entity_bits::nwr)
Definition: flex_reader.hpp:78
void read_relations(TIter begin, TIter end)
Definition: collector.hpp:478
Definition: entity_bits.hpp:65
Definition: header.hpp:49
const osmium::area::MultipolygonCollector< osmium::area::Assembler > & collector() const
Definition: flex_reader.hpp:119
osmium::osm_entity_bits::type m_entities
Definition: flex_reader.hpp:52
void close()
Definition: reader.hpp:212
Definition: flex_reader.hpp:49
size_t commit()
Definition: buffer.hpp:272