Libosmium  2.15.6
Fast and flexible C++ library for working with OpenStreetMap data
area.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_OSM_AREA_HPP
2 #define OSMIUM_OSM_AREA_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2020 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/memory/item.hpp>
39 #include <osmium/osm/box.hpp>
40 #include <osmium/osm/item_type.hpp>
42 #include <osmium/osm/object.hpp>
43 #include <osmium/osm/types.hpp>
45 
46 #include <cassert>
47 #include <cstdlib>
48 #include <iterator>
49 #include <utility>
50 
51 namespace osmium {
52 
53  namespace builder {
54  template <typename TDerived, typename T>
55  class OSMObjectBuilder;
56  } // namespace builder
57 
61  class OuterRing : public NodeRefList {
62 
63  public:
64 
66 
67  constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
68  return t == itemtype;
69  }
70 
73  }
74 
75  }; // class OuterRing
76 
77 
81  class InnerRing : public NodeRefList {
82 
83  public:
84 
86 
87  constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
88  return t == itemtype;
89  }
90 
93  }
94 
95  }; // class InnerRing
96 
97 
106  osmium::object_id_type area_id = std::abs(id) * 2;
108  ++area_id;
109  }
110  return id < 0 ? -area_id : area_id;
111  }
112 
120  return id / 2;
121  }
122 
126  class Area : public OSMObject {
127 
128  template <typename TDerived, typename T>
130 
131  Area() :
132  OSMObject(sizeof(Area), osmium::item_type::area) {
133  }
134 
135  public:
136 
138 
139  constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
140  return t == itemtype;
141  }
142 
149  bool from_way() const noexcept {
150  return (positive_id() & 0x1U) == 0;
151  }
152 
158  osmium::object_id_type orig_id() const noexcept {
159  return osmium::area_id_to_object_id(id());
160  }
161 
169  std::pair<size_t, size_t> num_rings() const {
170  std::pair<size_t, size_t> counter{0, 0};
171 
172  for (const auto& item : *this) {
173  switch (item.type()) {
175  ++counter.first;
176  break;
178  ++counter.second;
179  break;
181  // ignore tags
182  break;
193  assert(false && "Children of Area can only be outer/inner_ring and tag_list.");
194  break;
195  }
196  }
197 
198  return counter;
199  }
200 
205  bool is_multipolygon() const {
206  return num_rings().first > 1;
207  }
208 
219  return it.cast<const osmium::InnerRing>();
220  }
221 
232  return std::next(it).cast<const osmium::InnerRing>();
233  }
234 
241  return subitems<const osmium::OuterRing>();
242  }
243 
252  return osmium::memory::ItemIteratorRange<const osmium::InnerRing>{outer_range.cbegin().data(), std::next(outer_range.cbegin()).data()};
253  }
254 
260  osmium::Box envelope() const noexcept {
261  osmium::Box box;
262  for (const auto& ring : outer_rings()) {
263  box.extend(ring.envelope());
264  }
265  return box;
266  }
267 
268  }; // class Area
269 
270 
271 } // namespace osmium
272 
273 #endif // OSMIUM_OSM_AREA_HPP
osmium::builder::OSMObjectBuilder
Definition: osm_object_builder.hpp:402
osmium::Area::envelope
osmium::Box envelope() const noexcept
Definition: area.hpp:260
osmium::Area::inner_ring_cend
OSMIUM_DEPRECATED osmium::memory::ItemIterator< const osmium::InnerRing > inner_ring_cend(const osmium::memory::ItemIterator< const osmium::OuterRing > &it) const
Definition: area.hpp:231
osmium::OSMObject
Definition: object.hpp:64
osmium::memory::Item::next
unsigned char * next() noexcept
Definition: item.hpp:155
item_iterator.hpp
item.hpp
osmium::item_type::outer_ring
@ outer_ring
osmium::InnerRing::itemtype
static constexpr osmium::item_type itemtype
Definition: area.hpp:85
OSMIUM_DEPRECATED
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:51
osmium::Area::num_rings
std::pair< size_t, size_t > num_rings() const
Definition: area.hpp:169
osmium::Box
Definition: box.hpp:49
node_ref_list.hpp
osmium::InnerRing
Definition: area.hpp:81
types.hpp
osmium::Area::inner_ring_cbegin
OSMIUM_DEPRECATED osmium::memory::ItemIterator< const osmium::InnerRing > inner_ring_cbegin(const osmium::memory::ItemIterator< const osmium::OuterRing > &it) const
Definition: area.hpp:218
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
osmium::builder::Builder::item
osmium::memory::Item & item() const
Definition: builder.hpp:90
osmium::Area::outer_rings
osmium::memory::ItemIteratorRange< const osmium::OuterRing > outer_rings() const
Definition: area.hpp:240
osmium::memory::ItemIteratorRange::cbegin
const_iterator cbegin() const noexcept
Definition: item_iterator.hpp:203
box.hpp
osmium::OSMObject::positive_id
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:127
osmium::item_type::tag_list
@ tag_list
osmium::item_type::way_node_list
@ way_node_list
osmium::item_type::relation_member_list
@ relation_member_list
osmium::Area::orig_id
osmium::object_id_type orig_id() const noexcept
Definition: area.hpp:158
osmium::item_type::area
@ area
osmium::item_type::relation_member_list_with_full_members
@ relation_member_list_with_full_members
osmium::OuterRing::OuterRing
OuterRing()
Definition: area.hpp:71
osmium::Area::from_way
bool from_way() const noexcept
Definition: area.hpp:149
osmium::Area::inner_rings
osmium::memory::ItemIteratorRange< const osmium::InnerRing > inner_rings(const osmium::OuterRing &outer) const
Definition: area.hpp:250
osmium::NodeRefList
Definition: node_ref_list.hpp:52
compatibility.hpp
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::item_type::node
@ node
osmium::object_id_to_area_id
osmium::object_id_type object_id_to_area_id(osmium::object_id_type id, osmium::item_type type) noexcept
Definition: area.hpp:105
osmium::item_type::undefined
@ undefined
osmium::memory::ItemIterator::cast
ItemIterator< T > cast() const noexcept
Definition: item_iterator.hpp:97
osmium::area_id_to_object_id
osmium::object_id_type area_id_to_object_id(osmium::object_id_type id) noexcept
Definition: area.hpp:119
osmium::memory::ItemIterator
Definition: item_iterator.hpp:59
collection.hpp
osmium::Area::Area
Area()
Definition: area.hpp:131
osmium::Area::is_multipolygon
bool is_multipolygon() const
Definition: area.hpp:205
osmium::InnerRing::is_compatible_to
constexpr static bool is_compatible_to(osmium::item_type t) noexcept
Definition: area.hpp:87
osmium::item_type::changeset_discussion
@ changeset_discussion
osmium::item_type::changeset
@ changeset
osmium::Box::extend
Box & extend(const Location &location) noexcept
Definition: box.hpp:100
osmium::memory::Item::type
item_type type() const noexcept
Definition: item.hpp:171
osmium::Area::is_compatible_to
constexpr static bool is_compatible_to(osmium::item_type t) noexcept
Definition: area.hpp:139
osmium::OuterRing::is_compatible_to
constexpr static bool is_compatible_to(osmium::item_type t) noexcept
Definition: area.hpp:67
osmium::Area
Definition: area.hpp:126
osmium::item_type::way
@ way
osmium::Area::itemtype
static constexpr osmium::item_type itemtype
Definition: area.hpp:137
osmium::OuterRing
Definition: area.hpp:61
osmium::InnerRing::InnerRing
InnerRing()
Definition: area.hpp:91
osmium::OuterRing::itemtype
static constexpr osmium::item_type itemtype
Definition: area.hpp:65
osmium::item_type::relation
@ relation
object.hpp
item_type.hpp
osmium::memory::CollectionIterator::data
unsigned char * data() const noexcept
Definition: collection.hpp:91
osmium::osm_entity_bits::type
type
Definition: entity_bits.hpp:63
osmium::item_type
item_type
Definition: item_type.hpp:43
osmium::memory::ItemIteratorRange
Definition: item_iterator.hpp:175
osmium::item_type::inner_ring
@ inner_ring