Loading...
Searching...
No Matches
tile.hpp
Go to the documentation of this file.
#ifndef OSMIUM_GEOM_TILE_HPP
#define OSMIUM_GEOM_TILE_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/geom/coordinates.hpp>
#include <osmium/geom/mercator_projection.hpp>
#include <osmium/osm/location.hpp>
#include <cassert>
#include <cstdint>
namespace osmium {
namespace geom {
namespace detail {
constexpr int32_t clamp(int32_t value, int32_t min, int32_t max) {
if (value < min) {
return min;
}
return max < value ? max : value;
}
} // namespace detail
constexpr uint32_t num_tiles_in_zoom(uint32_t zoom) noexcept {
return 1U << zoom;
}
constexpr double tile_extent_in_zoom(uint32_t zoom) noexcept {
return detail::max_coordinate_epsg3857 * 2 / num_tiles_in_zoom(zoom);
}
constexpr uint32_t mercx_to_tilex(uint32_t zoom, double x) noexcept {
return static_cast<uint32_t>(detail::clamp(
static_cast<int32_t>((x + detail::max_coordinate_epsg3857) / tile_extent_in_zoom(zoom)),
0,
static_cast<int32_t>(num_tiles_in_zoom(zoom) - 1)));
}
constexpr uint32_t mercy_to_tiley(uint32_t zoom, double y) noexcept {
return static_cast<uint32_t>(detail::clamp(
static_cast<int32_t>((detail::max_coordinate_epsg3857 - y) / tile_extent_in_zoom(zoom)),
0,
static_cast<int32_t>(num_tiles_in_zoom(zoom) - 1)));
}
struct Tile {
enum {
max_zoom = 30U
};
uint32_t x;
uint32_t y;
uint32_t z;
explicit Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept :
x(tx),
y(ty),
z(zoom) {
assert(zoom <= max_zoom);
assert(x < num_tiles_in_zoom(zoom));
assert(y < num_tiles_in_zoom(zoom));
}
explicit Tile(uint32_t zoom, const osmium::Location& location) :
z(zoom) {
assert(zoom <= max_zoom);
assert(location.valid());
const auto coordinates = lonlat_to_mercator(location);
x = mercx_to_tilex(zoom, coordinates.x);
y = mercy_to_tiley(zoom, coordinates.y);
}
explicit Tile(uint32_t zoom, const osmium::geom::Coordinates& coordinates) :
z(zoom) {
assert(zoom <= max_zoom);
x = mercx_to_tilex(zoom, coordinates.x);
y = mercy_to_tiley(zoom, coordinates.y);
}
bool valid() const noexcept {
if (z > max_zoom) {
return false;
}
const auto max = num_tiles_in_zoom(z);
return x < max && y < max;
}
}; // struct Tile
inline bool operator==(const Tile& lhs, const Tile& rhs) noexcept {
return lhs.z == rhs.z && lhs.x == rhs.x && lhs.y == rhs.y;
}
inline bool operator!=(const Tile& lhs, const Tile& rhs) noexcept {
return !(lhs == rhs);
}
inline bool operator<(const Tile& lhs, const Tile& rhs) noexcept {
if (lhs.z < rhs.z) {
return true;
}
if (lhs.z > rhs.z) {
return false;
}
if (lhs.x < rhs.x) {
return true;
}
if (lhs.x > rhs.x) {
return false;
}
return lhs.y < rhs.y;
}
} // namespace geom
} // namespace osmium
#endif // OSMIUM_GEOM_TILE_HPP
osmium::Location
Definition location.hpp:271
osmium::Location::valid
constexpr bool valid() const noexcept
Definition location.hpp:352
coordinates.hpp
location.hpp
mercator_projection.hpp
detail
Definition attr.hpp:342
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::y
uint32_t y
y coordinate
Definition tile.hpp:111
osmium::geom::Tile::max_zoom
@ max_zoom
Definition tile.hpp:104
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