1 #ifndef OSMIUM_OSM_WAY_HPP
2 #define OSMIUM_OSM_WAY_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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/osm/box.hpp>
39 #include <osmium/osm/entity.hpp>
40 #include <osmium/osm/item_type.hpp>
41 #include <osmium/osm/node_ref.hpp>
42 #include <osmium/osm/node_ref_list.hpp>
43 #include <osmium/osm/object.hpp>
44 
45 namespace osmium {
46 
47     namespace builder {
48         template <typename TDerived, typename T>
49         class OSMObjectBuilder;
50     } // namespace builder
51 
52     /**
53      * List of node references (id and location) in a way.
54      */
55     class WayNodeList : public NodeRefList {
56 
57     public:
58 
59         static constexpr osmium::item_type itemtype = osmium::item_type::way_node_list;
60 
is_compatible_to(osmium::item_type t)61         constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
62             return t == itemtype;
63         }
64 
WayNodeList()65         WayNodeList() noexcept :
66             NodeRefList(itemtype) {
67         }
68 
69     }; // class WayNodeList
70 
71     static_assert(sizeof(WayNodeList) % osmium::memory::align_bytes == 0, "Class osmium::WayNodeList has wrong size to be aligned properly!");
72 
73     class Way : public OSMObject {
74 
75         template <typename TDerived, typename T>
76         friend class osmium::builder::OSMObjectBuilder;
77 
Way()78         Way() noexcept :
79             OSMObject(sizeof(Way), osmium::item_type::way) {
80         }
81 
82     public:
83 
84         static constexpr osmium::item_type itemtype = osmium::item_type::way;
85 
is_compatible_to(osmium::item_type t)86         constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
87             return t == itemtype;
88         }
89 
nodes()90         WayNodeList& nodes() {
91             return osmium::detail::subitem_of_type<WayNodeList>(begin(), end());
92         }
93 
nodes() const94         const WayNodeList& nodes() const {
95             return osmium::detail::subitem_of_type<const WayNodeList>(cbegin(), cend());
96         }
97 
98         /**
99          * Update all nodes in a way with the ID of the given NodeRef with the
100          * location of the given NodeRef.
101          */
update_node_location(const NodeRef & new_node_ref)102         void update_node_location(const NodeRef& new_node_ref) {
103             for (auto& node_ref : nodes()) {
104                 if (node_ref.ref() == new_node_ref.ref()) {
105                     node_ref.set_location(new_node_ref.location());
106                 }
107             }
108         }
109 
110         /**
111          * Checks whether the first and last node in the way have the
112          * same ID. The locations are not checked.
113          *
114          * Complexity: Constant.
115          *
116          * @pre @code !empty() @endcode
117          */
is_closed() const118         bool is_closed() const noexcept {
119             return nodes().is_closed();
120         }
121 
122         /**
123          * Checks whether the first and last node in the way have the
124          * same ID. The locations are not checked.
125          *
126          * Complexity: Constant.
127          *
128          * @pre @code !empty() @endcode
129          */
ends_have_same_id() const130         bool ends_have_same_id() const noexcept {
131             return nodes().ends_have_same_id();
132         }
133 
134         /**
135          * Checks whether the first and last node in the way have the
136          * same location. The IDs are not checked.
137          *
138          * Complexity: Constant.
139          *
140          * @pre @code !empty() @endcode
141          * @pre @code front().location() && back().location() @endcode
142          */
ends_have_same_location() const143         bool ends_have_same_location() const {
144             return nodes().ends_have_same_location();
145         }
146 
147         /**
148          * Calculate the envelope of this way. If the locations of the nodes
149          * are not set, the resulting box will be invalid.
150          *
151          * Complexity: Linear in the number of nodes.
152          */
envelope() const153         osmium::Box envelope() const noexcept {
154             return nodes().envelope();
155         }
156 
157     }; // class Way
158 
159     static_assert(sizeof(Way) % osmium::memory::align_bytes == 0, "Class osmium::Way has wrong size to be aligned properly!");
160 
161 } // namespace osmium
162 
163 #endif // OSMIUM_OSM_WAY_HPP
164