Libosmium  2.17.0
Fast and flexible C++ library for working with OpenStreetMap data
tile.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_GEOM_TILE_HPP
2 #define OSMIUM_GEOM_TILE_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 
38 #include <osmium/osm/location.hpp>
39 
40 #include <cassert>
41 #include <cstdint>
42 
43 namespace osmium {
44 
45  namespace geom {
46 
47  namespace detail {
48 
49  template <typename T>
50  inline constexpr const T& clamp(const T& value, const T& min, const T& max) {
51  return value < min ? min : (max < value ? max : value);
52  }
53 
54  } // namespace detail
55 
60  inline constexpr uint32_t num_tiles_in_zoom(uint32_t zoom) noexcept {
61  return 1U << zoom;
62  }
63 
68  inline constexpr double tile_extent_in_zoom(uint32_t zoom) noexcept {
69  return detail::max_coordinate_epsg3857 * 2 / num_tiles_in_zoom(zoom);
70  }
71 
77  inline constexpr uint32_t mercx_to_tilex(uint32_t zoom, double x) noexcept {
78  return static_cast<uint32_t>(detail::clamp<int32_t>(
79  static_cast<int32_t>((x + detail::max_coordinate_epsg3857) / tile_extent_in_zoom(zoom)),
80  0, num_tiles_in_zoom(zoom) -1
81  ));
82  }
83 
89  inline constexpr uint32_t mercy_to_tiley(uint32_t zoom, double y) noexcept {
90  return static_cast<uint32_t>(detail::clamp<int32_t>(
91  static_cast<int32_t>((detail::max_coordinate_epsg3857 - y) / tile_extent_in_zoom(zoom)),
92  0, num_tiles_in_zoom(zoom) -1
93  ));
94  }
95 
99  struct Tile {
100 
101  enum {
102  max_zoom = 30U
103  };
104 
106  uint32_t x;
107 
109  uint32_t y;
110 
112  uint32_t z;
113 
122  explicit Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept :
123  x(tx),
124  y(ty),
125  z(zoom) {
126  assert(zoom <= max_zoom);
127  assert(x < num_tiles_in_zoom(zoom));
128  assert(y < num_tiles_in_zoom(zoom));
129  }
130 
139  explicit Tile(uint32_t zoom, const osmium::Location& location) :
140  z(zoom) {
141  assert(zoom <= max_zoom);
142  assert(location.valid());
143  const auto coordinates = lonlat_to_mercator(location);
144  x = mercx_to_tilex(zoom, coordinates.x);
145  y = mercy_to_tiley(zoom, coordinates.y);
146  }
147 
156  explicit Tile(uint32_t zoom, const osmium::geom::Coordinates& coordinates) :
157  z(zoom) {
158  assert(zoom <= max_zoom);
159  x = mercx_to_tilex(zoom, coordinates.x);
160  y = mercy_to_tiley(zoom, coordinates.y);
161  }
162 
168  bool valid() const noexcept {
169  if (z > max_zoom) {
170  return false;
171  }
172  const auto max = num_tiles_in_zoom(z);
173  return x < max && y < max;
174  }
175 
176  }; // struct Tile
177 
179  inline bool operator==(const Tile& lhs, const Tile& rhs) noexcept {
180  return lhs.z == rhs.z && lhs.x == rhs.x && lhs.y == rhs.y;
181  }
182 
183  inline bool operator!=(const Tile& lhs, const Tile& rhs) noexcept {
184  return !(lhs == rhs);
185  }
186 
190  inline bool operator<(const Tile& lhs, const Tile& rhs) noexcept {
191  if (lhs.z < rhs.z) {
192  return true;
193  }
194  if (lhs.z > rhs.z) {
195  return false;
196  }
197  if (lhs.x < rhs.x) {
198  return true;
199  }
200  if (lhs.x > rhs.x) {
201  return false;
202  }
203  return lhs.y < rhs.y;
204  }
205 
206  } // namespace geom
207 
208 } // namespace osmium
209 
210 #endif // OSMIUM_GEOM_TILE_HPP
Definition: location.hpp:271
constexpr bool valid() const noexcept
Definition: location.hpp:348
Definition: attr.hpp:342
bool operator!=(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:149
constexpr uint32_t mercx_to_tilex(uint32_t zoom, double x) noexcept
Definition: tile.hpp:77
Coordinates lonlat_to_mercator(const Coordinates &c)
Definition: mercator_projection.hpp:123
constexpr double tile_extent_in_zoom(uint32_t zoom) noexcept
Definition: tile.hpp:68
bool operator==(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:139
bool operator<(const Tile &lhs, const Tile &rhs) noexcept
Definition: tile.hpp:190
constexpr uint32_t num_tiles_in_zoom(uint32_t zoom) noexcept
Definition: tile.hpp:60
constexpr uint32_t mercy_to_tiley(uint32_t zoom, double y) noexcept
Definition: tile.hpp:89
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: coordinates.hpp:48
double y
Definition: coordinates.hpp:51
double x
Definition: coordinates.hpp:50
Definition: tile.hpp:99
uint32_t x
x coordinate
Definition: tile.hpp:106
uint32_t z
Zoom level.
Definition: tile.hpp:112
Tile(uint32_t zoom, const osmium::geom::Coordinates &coordinates)
Definition: tile.hpp:156
Tile(uint32_t zoom, const osmium::Location &location)
Definition: tile.hpp:139
uint32_t y
y coordinate
Definition: tile.hpp:109
@ max_zoom
Definition: tile.hpp:102
Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept
Definition: tile.hpp:122
bool valid() const noexcept
Definition: tile.hpp:168