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
6#include <boat/geometry/detail/fibonacci.hpp>
8#include <boost/qvm/map_vec_mat.hpp>
9
10namespace boat::geometry {
11
13inline matrix affine(raster::point ext, cartesian::segment const& mid_pixel)
14{
15 namespace qvm = boost::qvm;
16 auto&& [a, b] = mid_pixel;
17 auto scale = boost::geometry::distance(a, b);
18 return qvm::translation_mat(qvm::vec{{a.x(), a.y()}}) *
19 qvm::rotz_mat<3>(.5 * numbers::pi - boost::geometry::azimuth(a, b)) *
20 qvm::diag_mat(qvm::vec{{scale, -scale, 1.}}) *
21 qvm::translation_mat(-qvm::vec{{ext.x() * .5, ext.y() * .5}});
22}
23
26 raster::point ext,
27 geographic::point const& mid,
28 double resolution,
29 auto const& crs)
30{
31 if constexpr (projection_or_transformation<decltype(crs)>)
32 return affine(ext, mid, resolution, transform(forward(crs)));
33 else if constexpr (srs_spec<decltype(crs)>)
34 return affine(ext, mid, resolution, transformation(crs));
35 else {
36 auto a = cartesian{}(*crs(mid));
37 auto b = cartesian{}(*crs(add_meters(mid, resolution, 0.)));
38 return affine(ext, {a, b});
39 }
40}
41
44 raster::point ext,
45 matrix const& mat,
46 srs_spec auto const& crs,
47 double meters)
48{
49 auto inv = transform(forward(mat), backward(transformation(crs)));
50 auto x = ext.x() * .5, y = ext.y() * .5;
51 auto px = inv(geographic::segment{{x, y}, {x + 1., y + 1.}});
52 if (!px)
53 return {};
54 auto area = 1LL * ext.x() * ext.y();
55 auto n = size_t(area * .5 * std::pow(length(*px) / meters, 2)) + 1;
56 auto mbr = geographic::box{{}, geographic{}(ext)};
57 auto points = inv(box_interpolate(mbr, size_t(2 * std::sqrt(n)) + 1, n));
58 if (!points)
59 return {};
60 auto ret = geographic::grid{};
61 for (auto fib : fibonacci_levels) {
62 auto step = fib.step();
63 if (step < meters * numbers::inv_sqrt_2)
64 break;
65 auto pred = [&](auto& ll) { return distance(ll, *points) <= step; };
66 auto indices = std::unordered_set<size_t>{};
67 for (auto& p : *points)
68 for (auto i : fib.nearests(p, pred)) {
69 if (!indices.insert(i).second)
70 break;
71 ret[step].push_back(fib[i]);
72 }
73 }
74 return ret;
75}
76
79auto make_boxes(geographic::grid const& footprint, srs_spec auto const& crs)
80{
81 auto ret = std::vector<cartesian::box>{};
82 auto fwd = transform(forward(transformation(crs)));
83 auto add = [&](geographic::box const& ll) {
84 auto a = ll.min_corner(), b = ll.max_corner();
85 if (a.x() <= -180. || b.x() >= 180. || a.y() <= -90. || b.y() >= 90.)
86 return;
87 if (auto xy = fwd(ll).transform(cartesian{}))
88 ret.push_back(*xy);
89 };
90 for (auto& lvl : footprint | std::views::reverse) {
91 auto d = lvl.first * numbers::inv_sqrt_2;
92 if (d >= numbers::earth::sqrt_area / 4)
93 continue;
94 for (auto& p : lvl.second) {
95 auto x = p.x(), y = p.y();
96 auto dx = d * meter(y), dy = d * meter();
97 add({{x - dx, y - dy}, {x + dx, y + dy}});
98 }
99 }
100 return ret;
101}
102
103} // namespace boat::geometry
104
105#endif // BOAT_GEOMETRY_RASTER_HPP
Definition transform.hpp:29
Definition algorithm.hpp:10
d2< double, cs::geographic< boost::geometry::degree > > geographic
Definition vocabulary.hpp:103
bool transform(T1 const &geom1, T2 &geom2, Strategy const &strategy)
Collections succeed if any element succeeds; failed elements are omitted.
Definition transform.hpp:89
auto forward(matrix const &v)
Definition transform.hpp:64
auto make_boxes(geographic::grid const &footprint, srs_spec auto const &crs)
Definition raster.hpp:79
matrix affine(raster::point ext, cartesian::segment const &mid_pixel)
Maps pixels to spatial coordinates.
Definition raster.hpp:13
geographic::grid geographic_interpolate(raster::point ext, matrix const &mat, srs_spec auto const &crs, double meters)
Samples a fixed grid.
Definition raster.hpp:43
multi_point auto box_interpolate(T const &mbr, size_t edge_points, size_t area_points)
Definition algorithm.hpp:14
auto backward(matrix const &v)
Definition transform.hpp:76
auto transformation(srs_spec auto const &a, srs_spec auto const &b)
Definition transform.hpp:48
geographic::point add_meters(geographic::point const &p, double eastward, double northward)
Definition algorithm.hpp:90
constexpr auto meter
Approximate degrees per meter of latitude, or longitude at a given latitude.
Definition algorithm.hpp:57
d2< double, cs::cartesian > cartesian
Definition vocabulary.hpp:102
boost::qvm::mat< double, 3, 3 > matrix
Definition vocabulary.hpp:104
model::box< point > box
Definition vocabulary.hpp:71
std::map< double, multi_point > grid
Definition vocabulary.hpp:73
model::segment< point > segment
Definition vocabulary.hpp:72
model::d2::point_xy< int, cs::cartesian > point
Definition vocabulary.hpp:65