Libosmium  2.20.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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 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
39
40#include <cassert>
41#include <cstdint>
42
43namespace 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
88 inline constexpr uint32_t mercy_to_tiley(uint32_t zoom, double y) noexcept {
89 return static_cast<uint32_t>(detail::clamp<int32_t>(
90 static_cast<int32_t>((detail::max_coordinate_epsg3857 - y) / tile_extent_in_zoom(zoom)),
91 0, num_tiles_in_zoom(zoom) - 1));
92 }
93
97 struct Tile {
98
99 enum {
100 max_zoom = 30U
101 };
102
104 uint32_t x;
105
107 uint32_t y;
108
110 uint32_t z;
111
120 explicit Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept :
121 x(tx),
122 y(ty),
123 z(zoom) {
124 assert(zoom <= max_zoom);
125 assert(x < num_tiles_in_zoom(zoom));
126 assert(y < num_tiles_in_zoom(zoom));
127 }
128
137 explicit Tile(uint32_t zoom, const osmium::Location& location) :
138 z(zoom) {
139 assert(zoom <= max_zoom);
140 assert(location.valid());
141 const auto coordinates = lonlat_to_mercator(location);
142 x = mercx_to_tilex(zoom, coordinates.x);
143 y = mercy_to_tiley(zoom, coordinates.y);
144 }
145
154 explicit Tile(uint32_t zoom, const osmium::geom::Coordinates& coordinates) :
155 z(zoom) {
156 assert(zoom <= max_zoom);
157 x = mercx_to_tilex(zoom, coordinates.x);
158 y = mercy_to_tiley(zoom, coordinates.y);
159 }
160
166 bool valid() const noexcept {
167 if (z > max_zoom) {
168 return false;
169 }
170 const auto max = num_tiles_in_zoom(z);
171 return x < max && y < max;
172 }
173
174 }; // struct Tile
175
177 inline bool operator==(const Tile& lhs, const Tile& rhs) noexcept {
178 return lhs.z == rhs.z && lhs.x == rhs.x && lhs.y == rhs.y;
179 }
180
181 inline bool operator!=(const Tile& lhs, const Tile& rhs) noexcept {
182 return !(lhs == rhs);
183 }
184
188 inline bool operator<(const Tile& lhs, const Tile& rhs) noexcept {
189 if (lhs.z < rhs.z) {
190 return true;
191 }
192 if (lhs.z > rhs.z) {
193 return false;
194 }
195 if (lhs.x < rhs.x) {
196 return true;
197 }
198 if (lhs.x > rhs.x) {
199 return false;
200 }
201 return lhs.y < rhs.y;
202 }
203
204 } // namespace geom
205
206} // namespace osmium
207
208#endif // OSMIUM_GEOM_TILE_HPP
Definition: location.hpp:271
constexpr bool valid() const noexcept
Definition: location.hpp:352
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:188
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:88
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:97
uint32_t x
x coordinate
Definition: tile.hpp:104
uint32_t z
Zoom level.
Definition: tile.hpp:110
Tile(uint32_t zoom, const osmium::geom::Coordinates &coordinates)
Definition: tile.hpp:154
Tile(uint32_t zoom, const osmium::Location &location)
Definition: tile.hpp:137
uint32_t y
y coordinate
Definition: tile.hpp:107
@ max_zoom
Definition: tile.hpp:100
Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept
Definition: tile.hpp:120
bool valid() const noexcept
Definition: tile.hpp:166