Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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-2025 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 <osmium/memory/collection.hpp>
37#include <osmium/memory/item.hpp>
38#include <osmium/memory/item_iterator.hpp>
39#include <osmium/osm/box.hpp>
40#include <osmium/osm/item_type.hpp>
41#include <osmium/osm/node_ref_list.hpp>
42#include <osmium/osm/object.hpp>
43#include <osmium/osm/types.hpp>
44
45#include <cassert>
46#include <cstdlib>
47#include <iterator>
48#include <utility>
49
50 namespace osmium {
51
52 namespace builder {
53 template <typename TDerived, typename T>
54 class OSMObjectBuilder;
55 } // namespace builder
56
60 class OuterRing : public NodeRefList {
61
62 public:
63
64 static constexpr osmium::item_type itemtype = osmium::item_type::outer_ring;
65
66 constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
67 return t == itemtype;
68 }
69
70 OuterRing() :
71 NodeRefList(itemtype) {
72 }
73
74 }; // class OuterRing
75
76
80 class InnerRing : public NodeRefList {
81
82 public:
83
84 static constexpr osmium::item_type itemtype = osmium::item_type::inner_ring;
85
86 constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
87 return t == itemtype;
88 }
89
90 InnerRing() :
91 NodeRefList(itemtype) {
92 }
93
94 }; // class InnerRing
95
96
104 inline osmium::object_id_type object_id_to_area_id(osmium::object_id_type id, osmium::item_type type) noexcept {
105 osmium::object_id_type area_id = std::abs(id) * 2;
106 if (type == osmium::item_type::relation) {
107 ++area_id;
108 }
109 return id < 0 ? -area_id : area_id;
110 }
111
118 inline osmium::object_id_type area_id_to_object_id(osmium::object_id_type id) noexcept {
119 return id / 2;
120 }
121
125 class Area : public OSMObject {
126
127 template <typename TDerived, typename T>
128 friend class osmium::builder::OSMObjectBuilder;
129
130 Area() :
131 OSMObject(sizeof(Area), osmium::item_type::area) {
132 }
133
134 public:
135
136 static constexpr osmium::item_type itemtype = osmium::item_type::area;
137
138 constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
139 return t == itemtype;
140 }
141
148 bool from_way() const noexcept {
149 return (positive_id() & 0x1U) == 0;
150 }
151
157 osmium::object_id_type orig_id() const noexcept {
158 return osmium::area_id_to_object_id(id());
159 }
160
168 std::pair<size_t, size_t> num_rings() const {
169 std::pair<size_t, size_t> counter{0, 0};
170
171 for (const auto& item : *this) {
172 switch (item.type()) {
173 case osmium::item_type::outer_ring:
174 ++counter.first;
175 break;
176 case osmium::item_type::inner_ring:
177 ++counter.second;
178 break;
179 case osmium::item_type::tag_list:
180 // ignore tags
181 break;
182 case osmium::item_type::undefined:
183 case osmium::item_type::node:
184 case osmium::item_type::way:
185 case osmium::item_type::relation:
186 case osmium::item_type::area:
187 case osmium::item_type::changeset:
188 case osmium::item_type::way_node_list:
189 case osmium::item_type::relation_member_list:
190 case osmium::item_type::relation_member_list_with_full_members:
191 case osmium::item_type::changeset_discussion:
192 assert(false && "Children of Area can only be outer/inner_ring and tag_list.");
193 break;
194 }
195 }
196
197 return counter;
198 }
199
204 bool is_multipolygon() const {
205 return num_rings().first > 1;
206 }
207
213 osmium::memory::ItemIteratorRange<const osmium::OuterRing> outer_rings() const {
214 return subitems<const osmium::OuterRing>();
215 }
216
223 osmium::memory::ItemIteratorRange<const osmium::InnerRing> inner_rings(const osmium::OuterRing& outer) const {
224 const osmium::memory::ItemIteratorRange<const osmium::OuterRing> outer_range{outer.data(), next()};
225 return osmium::memory::ItemIteratorRange<const osmium::InnerRing>{outer_range.cbegin().data(), std::next(outer_range.cbegin()).data()};
226 }
227
233 osmium::Box envelope() const noexcept {
234 osmium::Box box;
235 for (const auto& ring : outer_rings()) {
236 box.extend(ring.envelope());
237 }
238 return box;
239 }
240
241 }; // class Area
242
243
244} // namespace osmium
245
246#endif // OSMIUM_OSM_AREA_HPP
box.hpp
osmium::Area
Definition area.hpp:125
osmium::Area::inner_rings
osmium::memory::ItemIteratorRange< const osmium::InnerRing > inner_rings(const osmium::OuterRing &outer) const
Definition area.hpp:223
osmium::Area::num_rings
std::pair< size_t, size_t > num_rings() const
Definition area.hpp:168
osmium::Area::is_compatible_to
static constexpr bool is_compatible_to(osmium::item_type t) noexcept
Definition area.hpp:138
osmium::Area::from_way
bool from_way() const noexcept
Definition area.hpp:148
osmium::Area::itemtype
static constexpr osmium::item_type itemtype
Definition area.hpp:136
osmium::Area::Area
Area()
Definition area.hpp:130
osmium::Area::outer_rings
osmium::memory::ItemIteratorRange< const osmium::OuterRing > outer_rings() const
Definition area.hpp:213
osmium::Area::envelope
osmium::Box envelope() const noexcept
Definition area.hpp:233
osmium::Area::is_multipolygon
bool is_multipolygon() const
Definition area.hpp:204
osmium::Area::orig_id
osmium::object_id_type orig_id() const noexcept
Definition area.hpp:157
osmium::InnerRing
Definition area.hpp:80
osmium::InnerRing::itemtype
static constexpr osmium::item_type itemtype
Definition area.hpp:84
osmium::InnerRing::InnerRing
InnerRing()
Definition area.hpp:90
osmium::InnerRing::is_compatible_to
static constexpr bool is_compatible_to(osmium::item_type t) noexcept
Definition area.hpp:86
osmium::NodeRefList
Definition node_ref_list.hpp:52
osmium::OSMObject
Definition object.hpp:64
osmium::OSMObject::positive_id
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition object.hpp:123
osmium::OSMObject::subitems
osmium::memory::ItemIteratorRange< T > subitems()
Definition object.hpp:415
osmium::OuterRing
Definition area.hpp:60
osmium::OuterRing::itemtype
static constexpr osmium::item_type itemtype
Definition area.hpp:64
osmium::OuterRing::OuterRing
OuterRing()
Definition area.hpp:70
osmium::OuterRing::is_compatible_to
static constexpr bool is_compatible_to(osmium::item_type t) noexcept
Definition area.hpp:66
osmium::builder::OSMObjectBuilder
Definition way.hpp:49
osmium::memory::CollectionIterator::data
unsigned char * data() const noexcept
Definition collection.hpp:91
osmium::memory::ItemIteratorRange
Definition item_iterator.hpp:175
osmium::memory::ItemIteratorRange::cbegin
const_iterator cbegin() const noexcept
Definition item_iterator.hpp:203
osmium::memory::Item::next
unsigned char * next() noexcept
Definition item.hpp:155
collection.hpp
item.hpp
item_iterator.hpp
item_type.hpp
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
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:104
osmium::item_type
item_type
Definition item_type.hpp:45
osmium::item_type::tag_list
@ tag_list
osmium::item_type::relation_member_list
@ relation_member_list
osmium::item_type::way_node_list
@ way_node_list
osmium::item_type::changeset_discussion
@ changeset_discussion
osmium::item_type::relation_member_list_with_full_members
@ relation_member_list_with_full_members
osmium::item_type::relation
@ relation
osmium::item_type::node
@ node
osmium::item_type::changeset
@ changeset
osmium::item_type::area
@ area
osmium::item_type::undefined
@ undefined
osmium::item_type::way
@ way
osmium::item_type::outer_ring
@ outer_ring
osmium::item_type::inner_ring
@ inner_ring
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition types.hpp:45
osmium::area_id_to_object_id
osmium::object_id_type area_id_to_object_id(osmium::object_id_type id) noexcept
Definition area.hpp:118
node_ref_list.hpp
object.hpp
types.hpp