boat
C++23 geospatial library
Loading...
Searching...
No Matches
tile.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_TILE_HPP
4#define BOAT_TILE_HPP
5
7#include <boost/pfr/ops_fields.hpp>
8#include <generator>
9
10namespace boat {
11
12struct tile {
13 int z, y, x;
14
15 static constexpr int size = 256;
16 static constexpr int half_size = size / 2;
17
18 friend auto operator<=>(tile const&, tile const&) = default;
19
20 friend auto hash_value(tile const& v) { return boost::pfr::hash_fields(v); }
21
24 {
25 auto n = std::max<>({ext.x(), ext.y(), size});
26 return std::bit_width(unsigned((n - 1) / size));
27 }
28
29 static int zoom(geometry::raster::point ext, double scale)
30 {
31 int lim = zmax(ext);
32 scale *= pow2(lim);
33 for (int i = lim; i > 0; --i)
34 if (scale >= pow2(i))
35 return i;
36 return 0;
37 }
38
40 static int scale(geometry::raster::point ext, int zoom)
41 {
42 return pow2(zmax(ext) - zoom);
43 }
44
45 static std::generator<tile> all(geometry::raster::point ext, int zoom)
46 {
47 if (ext.x() <= 0 || ext.y() <= 0)
48 co_return;
49 int tl = size * scale(ext, zoom);
50 auto nx = ext.x() / tl + (ext.x() % tl != 0);
51 auto ny = ext.y() / tl + (ext.y() % tl != 0);
52 for (int y{}; y < ny; ++y)
53 for (int x{}; x < nx; ++x)
54 co_yield {.z = zoom, .y = y, .x = x};
55 }
56
59 {
60 int tl = size * scale(ext, z);
61 return {
62 {std::clamp(x * tl, 0, ext.x()), std::clamp(y * tl, 0, ext.y())},
63 {std::clamp((x + 1) * tl, 0, ext.x()),
64 std::clamp((y + 1) * tl, 0, ext.y())}};
65 }
66
69 {
70 auto [x, y, w, h] = geometry::xywh(rect(ext));
71 auto px = scale(ext, z);
72 auto nx = w / px + (w % px != 0);
73 auto ny = h / px + (h % px != 0);
74 return geometry::matrix{
75 {{double(w) / nx, 0., double(x)},
76 {0., double(h) / ny, double(y)},
77 {0., 0., 1.}}};
78 }
79};
80
81} // namespace boat
82
83template <>
84struct std::hash<boat::tile> {
85 static auto operator()(boat::tile const& v) { return hash_value(v); }
86};
87
88#endif // BOAT_TILE_HPP
constexpr auto xywh
Definition vocabulary.hpp:120
boost::qvm::mat< double, 3, 3 > matrix
Definition vocabulary.hpp:104
Definition blob.hpp:11
model::box< point > box
Definition vocabulary.hpp:71
model::d2::point_xy< int, cs::cartesian > point
Definition vocabulary.hpp:65
Definition tile.hpp:12
friend auto operator<=>(tile const &, tile const &)=default
static constexpr int size
Maximum tile side in pixels.
Definition tile.hpp:15
static int zoom(geometry::raster::point ext, double scale)
Definition tile.hpp:29
int y
Definition tile.hpp:13
geometry::raster::box rect(geometry::raster::point ext) const
Clips the tile window to the raster extent.
Definition tile.hpp:58
static int scale(geometry::raster::point ext, int zoom)
Raster pixels per tile pixel.
Definition tile.hpp:40
static std::generator< tile > all(geometry::raster::point ext, int zoom)
Definition tile.hpp:45
friend auto hash_value(tile const &v)
Definition tile.hpp:20
auto affine(geometry::raster::point ext) const
Maps tile pixels to raster pixels.
Definition tile.hpp:68
int z
Definition tile.hpp:13
int x
Definition tile.hpp:13
static int zmax(geometry::raster::point ext)
One raster pixel per tile pixel.
Definition tile.hpp:23
static constexpr int half_size
Definition tile.hpp:16
static auto operator()(boat::tile const &v)
Definition tile.hpp:85