boat
C++23 geospatial library
Loading...
Searching...
No Matches
transform.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_GEOMETRY_TRANSFORM_HPP
4#define BOAT_GEOMETRY_TRANSFORM_HPP
5
6#include <boat/detail/numbers.hpp>
8#include <boost/geometry/algorithms/transform.hpp>
9#include <boost/geometry/srs/epsg.hpp>
10#include <boost/geometry/srs/transformation.hpp>
11#include <boost/geometry/strategies/transform/matrix_transformers.hpp>
12#include <boost/geometry/strategies/transform/srs_transformer.hpp>
13#include <format>
14#include <optional>
15
16namespace boat::geometry {
17
18namespace srs = boost::geometry::srs;
19
20using srs_variant = std::variant<srs::epsg, srs::proj4>;
21
22template <class T>
23constexpr bool is_srs_spec = std::is_constructible_v<srs::projection<>, T>;
24
25template <class... Ts>
26constexpr bool is_srs_spec<std::variant<Ts...>> = (is_srs_spec<Ts> && ...);
27
28template <class T>
30
31template <class T>
33 specialized<T, srs::projection> || specialized<T, srs::transformation>;
34
35static auto const lonlat = srs::proj4{" +proj=lonlat +datum=WGS84 +no_defs"};
36
37inline auto ortho(geographic::point const& v)
38{
39 return srs::proj4{std::format(
40 " +proj=ortho +x_0=0 +y_0=0 +units=m +no_defs"
41 " +a={} +b={} +lat_0={} +lon_0={}",
42 numbers::earth::equatorial_radius,
43 numbers::earth::polar_radius,
44 v.y(),
45 v.x())};
46}
47
48auto transformation(srs_spec auto const& a, srs_spec auto const& b)
49{
50 if constexpr (specialized<decltype(a), std::variant>)
51 return std::visit([&](auto&& a) { return transformation(a, b); }, a);
52 else if constexpr (specialized<decltype(b), std::variant>)
53 return std::visit([&](auto&& b) { return transformation(a, b); }, b);
54 else
55 return srs::transformation<>(a, b);
56}
57
59auto transformation(srs_spec auto const& v)
60{
61 return transformation(lonlat, v);
62}
63
64inline auto forward(matrix const& v)
65{
66 return boost::geometry::strategy::transform::
67 matrix_transformer<double, 2, 2>{v};
68}
69
70template <projection_or_transformation T>
71auto forward(T const& v)
72{
73 return boost::geometry::strategy::transform::srs_forward_transformer<T>{v};
74}
75
76inline auto backward(matrix const& v)
77{
78 return forward(inverse(v));
79}
80
81template <projection_or_transformation T>
82auto backward(T const& v)
83{
84 return boost::geometry::strategy::transform::srs_inverse_transformer<T>{v};
85}
86
88template <tagged T1, same_tag<T1> T2, class Strategy>
89bool transform(T1 const& geom1, T2& geom2, Strategy const& strategy)
90{
91 return overloaded{
92 [&](single auto const& a, single auto& b) {
93 return boost::geometry::transform(a, b = {}, strategy);
94 },
95 [](this auto&& self, multi auto const& a, multi auto& b) -> bool {
96 b = {};
97 for (auto const& v : a)
98 if (!self(v, b.emplace_back()))
99 b.pop_back();
100 return !b.empty();
101 },
102 [](this auto&& self, dynamic auto const& a, dynamic auto& b) -> bool {
103 auto vis = [&]<class T>(T const& v) {
104 return self(v, b.template emplace<variant_index_v<T>>());
105 };
106 return std::visit(vis, a);
107 }}(geom1, geom2);
108}
109
111auto transform(auto const&... strategies)
112{
113 return [=]<tagged T>(T a) {
114 T b;
115 return (... && (b = std::move(a), transform(b, a, strategies)))
116 ? std::optional{std::move(a)}
117 : std::nullopt;
118 };
119}
120
122inline srs_variant make_srs_variant(int epsg, std::string const& proj4)
123{
124 if (epsg > 0)
125 return srs::epsg{epsg};
126 if (!proj4.empty())
127 return srs::proj4{proj4};
128 throw std::runtime_error("no SRS");
129}
130
131} // namespace boat::geometry
132
133#endif // BOAT_GEOMETRY_TRANSFORM_HPP
Definition vocabulary.hpp:34
Definition vocabulary.hpp:40
Definition vocabulary.hpp:58
Definition transform.hpp:29
Definition vocabulary.hpp:28
Definition algorithm.hpp:10
constexpr bool is_srs_spec
Definition transform.hpp:23
bool transform(T1 const &geom1, T2 &geom2, Strategy const &strategy)
Collections succeed if any element succeeds; failed elements are omitted.
Definition transform.hpp:89
std::variant< srs::epsg, srs::proj4 > srs_variant
Definition transform.hpp:20
auto forward(matrix const &v)
Definition transform.hpp:64
constexpr auto variant_index_v
Definition vocabulary.hpp:118
auto ortho(geographic::point const &v)
Definition transform.hpp:37
srs_variant make_srs_variant(int epsg, std::string const &proj4)
Prefers positive EPSG to PROJ.4; throws if neither is available.
Definition transform.hpp:122
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
boost::qvm::mat< double, 3, 3 > matrix
Definition vocabulary.hpp:104
model::d2::point_xy< double, cs::geographic< boost::geometry::degree > > point
Definition vocabulary.hpp:65