forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.cpp
More file actions
112 lines (91 loc) · 3.31 KB
/
tile.cpp
File metadata and controls
112 lines (91 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "tile.hpp"
#include "format.hpp"
#include <osmium/util/string.hpp>
#include <cstdlib>
std::string tile_t::to_zxy() const
{
return fmt::format("{}/{}/{}", zoom(), x(), y());
}
geom::point_t tile_t::to_tile_coords(geom::point_t p,
unsigned int pixel_extent) const noexcept
{
double const factor = static_cast<double>(pixel_extent) / extent();
return {(p.x() - xmin()) * factor, (p.y() - ymin()) * factor};
}
geom::point_t tile_t::to_world_coords(geom::point_t p,
unsigned int pixel_extent) const noexcept
{
double const factor = extent() / static_cast<double>(pixel_extent);
return {p.x() * factor + xmin(), p.y() * factor + ymin()};
}
geom::point_t tile_t::center() const noexcept
{
constexpr double MIDDLE = 0.5;
return to_world_coords({MIDDLE, MIDDLE}, 1);
}
namespace {
// Quadkey implementation uses bit interleaving code from
// https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2018/01/08/interleave.c
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
uint64_t interleave_uint32_with_zeros(uint32_t input) noexcept
{
uint64_t word = input;
word = (word ^ (word << 16U)) & 0x0000ffff0000ffffULL;
word = (word ^ (word << 8U)) & 0x00ff00ff00ff00ffULL;
word = (word ^ (word << 4U)) & 0x0f0f0f0f0f0f0f0fULL;
word = (word ^ (word << 2U)) & 0x3333333333333333ULL;
word = (word ^ (word << 1U)) & 0x5555555555555555ULL;
return word;
}
uint32_t deinterleave_lowuint32(uint64_t word) noexcept
{
word &= 0x5555555555555555ULL;
word = (word ^ (word >> 1U)) & 0x3333333333333333ULL;
word = (word ^ (word >> 2U)) & 0x0f0f0f0f0f0f0f0fULL;
word = (word ^ (word >> 4U)) & 0x00ff00ff00ff00ffULL;
word = (word ^ (word >> 8U)) & 0x0000ffff0000ffffULL;
word = (word ^ (word >> 16U)) & 0x00000000ffffffffULL;
return static_cast<uint32_t>(word);
}
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
uint32_t parse_num_with_max(std::string const &str, uint32_t max)
{
std::size_t pos = 0;
auto const value = std::stoul(str, &pos);
if (pos != str.size()) {
throw std::invalid_argument{"extra characters"};
}
if (value >= max) {
throw std::invalid_argument{"value to large"};
}
return static_cast<uint32_t>(value);
}
} // anonymous namespace
quadkey_t tile_t::quadkey() const noexcept
{
return quadkey_t{interleave_uint32_with_zeros(m_x) |
(interleave_uint32_with_zeros(m_y) << 1U)};
}
tile_t tile_t::from_quadkey(quadkey_t quadkey, uint32_t zoom) noexcept
{
return {zoom, deinterleave_lowuint32(quadkey.value()),
deinterleave_lowuint32(quadkey.value() >> 1U)};
}
tile_t tile_t::from_zxy(std::string const &zxy)
{
auto const p = osmium::split_string(zxy, '/');
if (p.size() != 3) {
throw fmt_error("Invalid tile '{}'.", zxy);
}
auto const zoom = parse_num_with_max(p[0], MAX_ZOOM);
uint32_t const max = 1UL << zoom;
return {zoom, parse_num_with_max(p[1], max), parse_num_with_max(p[2], max)};
}