boat
C++23 geospatial library
Loading...
Searching...
No Matches
raster.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_GEOMETRY_RASTER_HPP
4#define BOAT_GEOMETRY_RASTER_HPP
5
7#include <boat/geometry/detail/fibonacci.hpp>
9#include <boost/qvm/map_vec_mat.hpp>
10
11namespace boat::geometry {
12
15 int width,
16 int height,
17 matrix const& mat,
18 srs_spec auto const& crs,
19 size_t num_points)
20{
21 auto tf = transformation(crs);
22 auto fwd = transform(srs_forward(tf), mat_inverse(mat));
23 auto inv = transform(mat_forward(mat), srs_inverse(tf));
24 auto sentinel = [&](auto& ll) {
25 auto xy = fwd(ll).transform(cartesian{});
26 return !xy || !boost::geometry::covered_by(
27 *xy, cartesian::box{{}, {width * 1., height * 1.}});
28 };
29 auto points =
30 inv(box_area_interpolate(geographic::box{{}, {width * 1., height * 1.}},
31 num_points) |
32 std::ranges::to<geographic::multi_point>())
33 .value_or(geographic::multi_point{});
34 auto ret = geographic::grid{};
35 for (auto fib : fibonacci_levels) {
36 auto indices = std::unordered_set<size_t>{};
37 for (auto& p : points)
38 for (auto i : fib.nearests(p, sentinel)) {
39 if (!indices.insert(i).second)
40 break;
41 if (indices.size() > points.size() * 2)
42 return ret;
43 }
44 if (indices.empty())
45 continue;
46 auto& lvl = ret[numbers::earth::sqrt_area / std::sqrt(fib.num_points)];
47 lvl.resize(indices.size());
48 for (auto [i, j] : indices | std::views::enumerate)
49 lvl[i] = fib[j];
50 }
51 return ret;
52}
53
55inline matrix affine(int width, int height, cartesian::segment const& mid_pixel)
56{
57 namespace qvm = boost::qvm;
58 auto const& [a, b] = mid_pixel;
59 auto scale = boost::geometry::distance(a, b);
60 return qvm::translation_mat(qvm::vec{{a.x(), a.y()}}) *
61 qvm::rotz_mat<3>(.5 * numbers::pi - boost::geometry::azimuth(a, b)) *
62 qvm::diag_mat(qvm::vec{{scale, scale, 1.}}) *
63 qvm::translation_mat(-qvm::vec{{width * .5, height * .5}}) *
64 qvm::diag_mat(qvm::vec{{1., -1., 1.}}) *
65 qvm::translation_mat(-qvm::vec{{0., height * 1.}});
66}
67
69inline matrix affine(int width, int height, cartesian::box const& mbr)
70{
71 return boost::qvm::inverse(
72 boost::geometry::strategy::transform::
73 map_transformer<double, 2, 2, true, false>{mbr, width, height}
74 .matrix());
75}
76
77} // namespace boat::geometry
78
79#endif // BOAT_GEOMETRY_RASTER_HPP
Definition vocabulary.hpp:37
Definition algorithm.hpp:9
bool transform(T1 const &geom1, T2 &geom2, Strategy const &strategy)
Definition transform.hpp:76
geographic::grid geographic_interpolate(int width, int height, matrix const &mat, srs_spec auto const &crs, size_t num_points)
Approximates a raster footprint with points from a fixed global grid.
Definition raster.hpp:14
auto mat_inverse(matrix const &v)
Definition transform.hpp:70
boost::geometry::strategy::transform::matrix_transformer< double, 2, 2 > mat_forward
Definition transform.hpp:67
matrix affine(int width, int height, cartesian::segment const &mid_pixel)
Maps pixel coordinates to spatial coordinates.
Definition raster.hpp:55
auto srs_forward(T const &v)
Definition transform.hpp:56
auto transformation(srs_spec auto const &a, srs_spec auto const &b)
Creates a transformation from source a to target b.
Definition transform.hpp:39
d2< boost::geometry::cs::cartesian > cartesian
Definition vocabulary.hpp:112
auto srs_inverse(T const &v)
Definition transform.hpp:62
auto box_area_interpolate(T const &mbr, size_t num_points)
Definition algorithm.hpp:19
boost::qvm::mat< double, 3, 3 > matrix
Definition vocabulary.hpp:114
model::multi_point< point > multi_point
Definition vocabulary.hpp:78
model::segment< point > segment
Definition vocabulary.hpp:82
std::map< double, multi_point > grid
Definition vocabulary.hpp:83
model::box< point > box
Definition vocabulary.hpp:81