1 #ifndef OSMIUM_OSM_BOX_HPP 2 #define OSMIUM_OSM_BOX_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/osm/location.hpp> 37 38 #include <cassert> 39 #include <iosfwd> 40 41 namespace osmium { 42 43 /** 44 * Bounding box. A box is defined by two locations (bottom left location 45 * and top right location) or, alternatively by four coordinates (minx, 46 * miny, maxx, and maxy). If both locations are undefined, the box is 47 * undefined, too. 48 */ 49 class Box { 50 51 osmium::Location m_bottom_left{}; 52 osmium::Location m_top_right{}; 53 54 public: 55 56 /** 57 * Create undefined Box. Use the extend() function 58 * to add actual bounds. 59 */ 60 constexpr Box() noexcept = default; 61 62 /** 63 * Create box from minimum and maximum coordinates in WGS84. 64 * 65 * @pre @code minx <= maxx && miny <= maxy @endcode 66 */ Box(double minx,double miny,double maxx,double maxy)67 Box(double minx, double miny, double maxx, double maxy) : 68 m_bottom_left(minx, miny), 69 m_top_right(maxx, maxy) { 70 assert(minx <= maxx && miny <= maxy); 71 } 72 73 /** 74 * Create box from bottom left and top right locations. 75 * 76 * @pre Either both locations must be defined or neither. 77 * @pre If both locations are defined, the 78 * bottom left location must actually be to the left and below 79 * the top right location. Same coordinates for bottom/top or 80 * left/right are also okay. 81 */ Box(const osmium::Location & bottom_left,const osmium::Location & top_right)82 Box(const osmium::Location& bottom_left, const osmium::Location& top_right) : 83 m_bottom_left(bottom_left), 84 m_top_right(top_right) { 85 assert( 86 (!!bottom_left && !!top_right) || 87 (bottom_left.x() <= top_right.x() && bottom_left.y() <= top_right.y())); 88 } 89 90 /** 91 * Extend this bounding box by the specified location. If the 92 * location is invalid, the bounding box is unchanged. If the 93 * box is undefined it will only contain the new location after 94 * this call. 95 * 96 * @param location The location we want to extend the box by. 97 * @returns A reference to this box. 98 */ extend(const Location & location)99 Box& extend(const Location& location) noexcept { 100 if (location.valid()) { 101 if (m_bottom_left) { 102 if (location.x() < m_bottom_left.x()) { 103 m_bottom_left.set_x(location.x()); 104 } 105 if (location.x() > m_top_right.x()) { 106 m_top_right.set_x(location.x()); 107 } 108 if (location.y() < m_bottom_left.y()) { 109 m_bottom_left.set_y(location.y()); 110 } 111 if (location.y() > m_top_right.y()) { 112 m_top_right.set_y(location.y()); 113 } 114 } else { 115 m_bottom_left = location; 116 m_top_right = location; 117 } 118 } 119 return *this; 120 } 121 122 /** 123 * Extend this bounding box by the specified box. If the 124 * specified box is undefined, the bounding box is unchanged. 125 * 126 * @param box The box to extend by. 127 * @returns A reference to this box. 128 */ extend(const Box & box)129 Box& extend(const Box& box) noexcept { 130 extend(box.bottom_left()); 131 extend(box.top_right()); 132 return *this; 133 } 134 135 /** 136 * Box is defined, ie. contains defined locations. 137 */ operator bool() const138 explicit constexpr operator bool() const noexcept { 139 return bool(m_bottom_left) && bool(m_top_right); 140 } 141 142 /** 143 * Box is valid, ie. defined and inside usual bounds 144 * (-180<=lon<=180, -90<=lat<=90). 145 */ valid() const146 constexpr bool valid() const noexcept { 147 return bottom_left().valid() && top_right().valid(); 148 } 149 150 /** 151 * Access bottom-left location. 152 */ bottom_left() const153 constexpr Location bottom_left() const noexcept { 154 return m_bottom_left; 155 } 156 157 /** 158 * Access bottom-left location. 159 */ bottom_left()160 Location& bottom_left() noexcept { 161 return m_bottom_left; 162 } 163 164 /** 165 * Access top-right location. 166 */ top_right() const167 constexpr Location top_right() const noexcept { 168 return m_top_right; 169 } 170 171 /** 172 * Access top-right location. 173 */ top_right()174 Location& top_right() noexcept { 175 return m_top_right; 176 } 177 178 /** 179 * Get left boundary. 180 * 181 * @pre @code valid() == true @encode 182 */ left() const183 double left() const noexcept { 184 assert(valid()); 185 return m_bottom_left.lon_without_check(); 186 } 187 188 /** 189 * Get right boundary. 190 * 191 * @pre @code valid() == true @encode 192 */ right() const193 double right() const noexcept { 194 assert(valid()); 195 return m_top_right.lon_without_check(); 196 } 197 198 /** 199 * Get top boundary. 200 * 201 * @pre @code valid() == true @encode 202 */ top() const203 double top() const noexcept { 204 assert(valid()); 205 return m_top_right.lat_without_check(); 206 } 207 208 /** 209 * Get bottom boundary. 210 * 211 * @pre @code valid() == true @encode 212 */ bottom() const213 double bottom() const noexcept { 214 assert(valid()); 215 return m_bottom_left.lat_without_check(); 216 } 217 218 /** 219 * Check whether the location is inside the box. 220 * 221 * @pre Location must be defined. 222 * @pre Box must be defined. 223 */ contains(const osmium::Location & location) const224 bool contains(const osmium::Location& location) const noexcept { 225 assert(bottom_left()); 226 assert(top_right()); 227 assert(location); 228 return location.x() >= bottom_left().x() && location.y() >= bottom_left().y() && 229 location.x() <= top_right().x() && location.y() <= top_right().y(); 230 } 231 232 /** 233 * Calculate size of the box in square degrees. 234 * 235 * Note that this measure isn't very useful if you want to know the 236 * real-world size of the bounding box! 237 * 238 * @throws osmium::invalid_location unless all coordinates are valid. 239 */ size() const240 double size() const { 241 return (m_top_right.lon() - m_bottom_left.lon()) * 242 (m_top_right.lat() - m_bottom_left.lat()); 243 } 244 245 }; // class Box 246 247 /** 248 * Boxes are equal if both locations are equal. Undefined boxes will 249 * compare equal. 250 */ operator ==(const Box & lhs,const Box & rhs)251 inline constexpr bool operator==(const Box& lhs, const Box& rhs) noexcept { 252 return lhs.bottom_left() == rhs.bottom_left() && 253 lhs.top_right() == rhs.top_right(); 254 } 255 256 /** 257 * Output a box to a stream. The format is "(LON, LAT, LON, LAT)" or 258 * "(undefined)" if the box is undefined. 259 * 260 * @returns Reference to basic_ostream given as first parameter. 261 */ 262 template <typename TChar, typename TTraits> operator <<(std::basic_ostream<TChar,TTraits> & out,const osmium::Box & box)263 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const osmium::Box& box) { 264 if (box) { 265 out << '('; 266 box.bottom_left().as_string_without_check(std::ostream_iterator<char>(out)); 267 out << ','; 268 box.top_right().as_string_without_check(std::ostream_iterator<char>(out)); 269 out << ')'; 270 } else { 271 out << "(undefined)"; 272 } 273 return out; 274 } 275 276 } // namespace osmium 277 278 #endif // OSMIUM_OSM_BOX_HPP 279