Libosmium: include/osmium/geom/tile.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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-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/geom/coordinates.hpp>
37#include <osmium/geom/mercator_projection.hpp>
38#include <osmium/osm/location.hpp>
39
40#include <cassert>
41#include <cstdint>
42
43namespace osmium {
44
45 namespace geom {
46
47 namespace detail {
48
49 constexpr int32_t clamp(int32_t value, int32_t min, int32_t max) {
50 if (value < min) {
51 return min;
52 }
53 return max < value ? max : value;
54 }
55
56 } // namespace detail
57
62 constexpr uint32_t num_tiles_in_zoom(uint32_t zoom) noexcept {
63 return 1U << zoom;
64 }
65
70 constexpr double tile_extent_in_zoom(uint32_t zoom) noexcept {
71 return detail::max_coordinate_epsg3857 * 2 / num_tiles_in_zoom(zoom);
72 }
73
79 constexpr uint32_t mercx_to_tilex(uint32_t zoom, double x) noexcept {
80 return static_cast<uint32_t>(detail::clamp(
81 static_cast<int32_t>((x + detail::max_coordinate_epsg3857) / tile_extent_in_zoom(zoom)),
82 0,
83 static_cast<int32_t>(num_tiles_in_zoom(zoom) - 1)));
84 }
85
91 constexpr uint32_t mercy_to_tiley(uint32_t zoom, double y) noexcept {
92 return static_cast<uint32_t>(detail::clamp(
93 static_cast<int32_t>((detail::max_coordinate_epsg3857 - y) / tile_extent_in_zoom(zoom)),
94 0,
95 static_cast<int32_t>(num_tiles_in_zoom(zoom) - 1)));
96 }
97
101 struct Tile {
102
103 enum {
104 max_zoom = 30U
105 };
106
108 uint32_t x;
109
111 uint32_t y;
112
114 uint32_t z;
115
124 explicit Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept :
125 x(tx),
126 y(ty),
127 z(zoom) {
128 assert(zoom <= max_zoom);
129 assert(x < num_tiles_in_zoom(zoom));
130 assert(y < num_tiles_in_zoom(zoom));
131 }
132
141 explicit Tile(uint32_t zoom, const osmium::Location& location) :
142 z(zoom) {
143 assert(zoom <= max_zoom);
144 assert(location.valid());
145 const auto coordinates = lonlat_to_mercator(location);
146 x = mercx_to_tilex(zoom, coordinates.x);
147 y = mercy_to_tiley(zoom, coordinates.y);
148 }
149
158 explicit Tile(uint32_t zoom, const osmium::geom::Coordinates& coordinates) :
159 z(zoom) {
160 assert(zoom <= max_zoom);
161 x = mercx_to_tilex(zoom, coordinates.x);
162 y = mercy_to_tiley(zoom, coordinates.y);
163 }
164
170 bool valid() const noexcept {
171 if (z > max_zoom) {
172 return false;
173 }
174 const auto max = num_tiles_in_zoom(z);
175 return x < max && y < max;
176 }
177
178 }; // struct Tile
179
181 inline bool operator==(const Tile& lhs, const Tile& rhs) noexcept {
182 return lhs.z == rhs.z && lhs.x == rhs.x && lhs.y == rhs.y;
183 }
184
185 inline bool operator!=(const Tile& lhs, const Tile& rhs) noexcept {
186 return !(lhs == rhs);
187 }
188
192 inline bool operator<(const Tile& lhs, const Tile& rhs) noexcept {
193 if (lhs.z < rhs.z) {
194 return true;
195 }
196 if (lhs.z > rhs.z) {
197 return false;
198 }
199 if (lhs.x < rhs.x) {
200 return true;
201 }
202 if (lhs.x > rhs.x) {
203 return false;
204 }
205 return lhs.y < rhs.y;
206 }
207
208 } // namespace geom
209
210} // namespace osmium
211
212#endif // OSMIUM_GEOM_TILE_HPP
osmium::Location
Definition location.hpp:279
osmium::Location::valid
constexpr bool valid() const noexcept
Definition location.hpp:385
coordinates.hpp
location.hpp
mercator_projection.hpp
osmium::geom
Everything related to geometry handling.
Definition coordinates.hpp:46
osmium::geom::operator!=
bool operator!=(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition coordinates.hpp:149
osmium::geom::mercx_to_tilex
constexpr uint32_t mercx_to_tilex(uint32_t zoom, double x) noexcept
Definition tile.hpp:79
osmium::geom::lonlat_to_mercator
Coordinates lonlat_to_mercator(const Coordinates &c)
Definition mercator_projection.hpp:123
osmium::geom::tile_extent_in_zoom
constexpr double tile_extent_in_zoom(uint32_t zoom) noexcept
Definition tile.hpp:70
osmium::geom::operator==
bool operator==(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition coordinates.hpp:139
osmium::geom::operator<
bool operator<(const Tile &lhs, const Tile &rhs) noexcept
Definition tile.hpp:192
osmium::geom::num_tiles_in_zoom
constexpr uint32_t num_tiles_in_zoom(uint32_t zoom) noexcept
Definition tile.hpp:62
osmium::geom::mercy_to_tiley
constexpr uint32_t mercy_to_tiley(uint32_t zoom, double y) noexcept
Definition tile.hpp:91
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::geom::Coordinates
Definition coordinates.hpp:48
osmium::geom::Coordinates::y
double y
Definition coordinates.hpp:51
osmium::geom::Coordinates::x
double x
Definition coordinates.hpp:50
osmium::geom::Tile
Definition tile.hpp:101
osmium::geom::Tile::x
uint32_t x
x coordinate
Definition tile.hpp:108
osmium::geom::Tile::z
uint32_t z
Zoom level.
Definition tile.hpp:114
osmium::geom::Tile::Tile
Tile(uint32_t zoom, const osmium::geom::Coordinates &coordinates)
Definition tile.hpp:158
osmium::geom::Tile::Tile
Tile(uint32_t zoom, const osmium::Location &location)
Definition tile.hpp:141
osmium::geom::Tile::max_zoom
@ max_zoom
Definition tile.hpp:104
osmium::geom::Tile::y
uint32_t y
y coordinate
Definition tile.hpp:111
osmium::geom::Tile::Tile
Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept
Definition tile.hpp:124
osmium::geom::Tile::valid
bool valid() const noexcept
Definition tile.hpp:170