Libosmium: include/osmium/osm/area.hpp Source File

Libosmium 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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS 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
50namespace 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::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::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::NodeRefList
NodeRefList(osmium::item_type itemtype) noexcept
Definition node_ref_list.hpp:65
osmium::OSMObject::OSMObject
OSMObject(osmium::memory::item_size_type size, osmium::item_type type)
Definition object.hpp:102
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 osm_object_builder.hpp:428
osmium::memory::CollectionIterator::data
unsigned char * data() const noexcept
Definition collection.hpp:92
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::area
Code related to the building of areas (multipolygons) from relations.
Definition assembler.hpp:55
osmium::builder
Classes for building OSM objects and other items in buffers.
Definition attr.hpp:62
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
Definition item_type.hpp:53
osmium::item_type::relation_member_list
@ relation_member_list
Definition item_type.hpp:55
osmium::item_type::way_node_list
@ way_node_list
Definition item_type.hpp:54
osmium::item_type::changeset_discussion
@ changeset_discussion
Definition item_type.hpp:59
osmium::item_type::relation_member_list_with_full_members
@ relation_member_list_with_full_members
Definition item_type.hpp:56
osmium::item_type::relation
@ relation
Definition item_type.hpp:50
osmium::item_type::node
@ node
Definition item_type.hpp:48
osmium::item_type::changeset
@ changeset
Definition item_type.hpp:52
osmium::item_type::area
@ area
Definition item_type.hpp:51
osmium::item_type::undefined
@ undefined
Definition item_type.hpp:47
osmium::item_type::way
@ way
Definition item_type.hpp:49
osmium::item_type::outer_ring
@ outer_ring
Definition item_type.hpp:57
osmium::item_type::inner_ring
@ inner_ring
Definition item_type.hpp:58
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