Libosmium  2.6.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
node_locations_for_ways.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_HANDLER_NODE_LOCATIONS_FOR_WAYS_HPP
2 #define OSMIUM_HANDLER_NODE_LOCATIONS_FOR_WAYS_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 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 <type_traits>
37 
38 #include <osmium/handler.hpp>
39 #include <osmium/index/index.hpp>
41 #include <osmium/osm/location.hpp>
42 #include <osmium/osm/node.hpp>
43 #include <osmium/osm/node_ref.hpp>
44 #include <osmium/osm/types.hpp>
45 #include <osmium/osm/way.hpp>
46 
48 
49 namespace osmium {
50 
51  namespace handler {
52 
54 
63  template <typename TStoragePosIDs, typename TStorageNegIDs = dummy_type>
65 
66 
67 
68  public:
69 
70  typedef TStoragePosIDs index_pos_type;
71  typedef TStorageNegIDs index_neg_type;
72 
73  private:
74 
76  TStoragePosIDs& m_storage_pos;
77 
79  TStorageNegIDs& m_storage_neg;
80 
81  bool m_ignore_errors {false};
82 
83  bool m_must_sort {false};
84 
85  // It is okay to have this static dummy instance, even when using several threads,
86  // because it is read-only.
87  static dummy_type& get_dummy() {
88  static dummy_type instance;
89  return instance;
90  }
91 
92  public:
93 
94  explicit NodeLocationsForWays(TStoragePosIDs& storage_pos,
95  TStorageNegIDs& storage_neg = get_dummy()) :
96  m_storage_pos(storage_pos),
97  m_storage_neg(storage_neg) {
98  }
99 
102 
105 
106  ~NodeLocationsForWays() noexcept = default;
107 
108  void ignore_errors() {
109  m_ignore_errors = true;
110  }
111 
115  void node(const osmium::Node& node) {
116  m_must_sort = true;
117  const osmium::object_id_type id = node.id();
118  if (id >= 0) {
119  m_storage_pos.set(static_cast<osmium::unsigned_object_id_type>( id), node.location());
120  } else {
121  m_storage_neg.set(static_cast<osmium::unsigned_object_id_type>(-id), node.location());
122  }
123  }
124 
129  if (id >= 0) {
130  return m_storage_pos.get(static_cast<osmium::unsigned_object_id_type>( id));
131  } else {
132  return m_storage_neg.get(static_cast<osmium::unsigned_object_id_type>(-id));
133  }
134  }
135 
140  void way(osmium::Way& way) {
141  if (m_must_sort) {
142  m_storage_pos.sort();
143  m_storage_neg.sort();
144  m_must_sort = false;
145  }
146  bool error = false;
147  for (auto& node_ref : way.nodes()) {
148  try {
149  node_ref.set_location(get_node_location(node_ref.ref()));
150  if (!node_ref.location()) {
151  error = true;
152  }
153  } catch (osmium::not_found&) {
154  error = true;
155  }
156  }
157  if (error && !m_ignore_errors) {
158  throw osmium::not_found("location for one or more nodes not found in node location index");
159  }
160  }
161 
167  void clear() {
168  m_storage_pos.clear();
169  m_storage_neg.clear();
170  }
171 
172  }; // class NodeLocationsForWays
173 
174  } // namespace handler
175 
176 } // namespace osmium
177 
178 #endif // OSMIUM_HANDLER_NODE_LOCATIONS_FOR_WAYS_HPP
WayNodeList & nodes()
Definition: way.hpp:75
bool m_ignore_errors
Definition: node_locations_for_ways.hpp:81
osmium::index::map::Dummy< osmium::unsigned_object_id_type, osmium::Location > dummy_type
Definition: node_locations_for_ways.hpp:53
void way(osmium::Way &way)
Definition: node_locations_for_ways.hpp:140
bool m_must_sort
Definition: node_locations_for_ways.hpp:83
void ignore_errors()
Definition: node_locations_for_ways.hpp:108
Definition: handler.hpp:45
~NodeLocationsForWays() noexcept=default
static dummy_type & get_dummy()
Definition: node_locations_for_ways.hpp:87
Definition: way.hpp:65
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
TStoragePosIDs & m_storage_pos
Object that handles the actual storage of the node locations (with positive IDs). ...
Definition: node_locations_for_ways.hpp:76
Definition: dummy.hpp:53
TStorageNegIDs & m_storage_neg
Object that handles the actual storage of the node locations (with negative IDs). ...
Definition: node_locations_for_ways.hpp:79
Definition: location.hpp:79
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:110
osmium::Location location() const noexcept
Definition: node.hpp:61
NodeLocationsForWays & operator=(const NodeLocationsForWays &)=delete
Definition: node.hpp:47
Definition: index.hpp:50
NodeLocationsForWays(TStoragePosIDs &storage_pos, TStorageNegIDs &storage_neg=get_dummy())
Definition: node_locations_for_ways.hpp:94
TStorageNegIDs index_neg_type
Definition: node_locations_for_ways.hpp:71
Definition: node_locations_for_ways.hpp:64
void node(const osmium::Node &node)
Definition: node_locations_for_ways.hpp:115
osmium::Location get_node_location(const osmium::object_id_type id) const
Definition: node_locations_for_ways.hpp:128
void clear()
Definition: node_locations_for_ways.hpp:167
TStoragePosIDs index_pos_type
Definition: node_locations_for_ways.hpp:70