boat
C++23 geospatial library
Loading...
Searching...
No Matches
fetch.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_GUI_FETCH_HPP
4#define BOAT_GUI_FETCH_HPP
5
6#include <boat/db/catalog.hpp>
8#include <boat/gui/detail/geometry.hpp>
9#include <boat/gui/detail/tile.hpp>
10#include <boat/gui/variant.hpp>
11
12namespace boat::gui {
13
15 std::move_only_function<db::catalog&() const> catalog;
17 std::shared_ptr<caches::cache> cache;
18 size_t cache_key;
20 int limit = 512;
21 std::stop_token stop;
22};
23
24namespace detail {
25
26using caches::detail::get_or_invoke;
27
28std::generator<variant> vectors(layer_query const& q)
29{
30 namespace bgi = boost::geometry::index;
31 auto cells = std::max<>(1, int(std::sqrt(2 * q.limit)));
32 auto tbl = get_or_invoke(q.cache.get(), q.cache_key, [&] {
33 return q.catalog().get_table(q.layer.schema_name, q.layer.table_name);
34 });
35 auto& col = q.layer.column_name;
36 auto it = std::ranges::find(tbl.columns, col, &db::column::column_name);
37 check(it != tbl.columns.end(), col);
38 auto crs = geometry::make_srs_variant(it->epsg, it->proj4);
39 auto inv =
41 auto done = bgi::rtree<geometry::cartesian::box, bgi::rstar<4>>{};
42 for (auto& box : geometry::make_boxes(q.footprint, crs)) {
43 if (q.stop.stop_requested())
44 co_return;
45 if (done.qbegin(bgi::contains(box)) != done.qend())
46 continue;
47 auto a = box.min_corner(), b = box.max_corner();
48 auto vec = get_or_invoke(
49 q.cache.get(),
50 std::tuple{q.cache_key, a.x(), a.y(), b.x(), b.y(), q.limit},
51 [&] {
52 auto rs = q.catalog().select(tbl, {{col}, col, box, q.limit});
53 auto ret = vector{};
54 for (auto&& wkb : rs | db::view<blob>)
55 blob_view{wkb} >> ret.geoms.emplace_back();
56 ret.partial = ret.geoms.size() >= q.limit;
57 if (ret.partial)
58 ret.geoms = sample(std::move(ret.geoms), box, cells);
59 ret.geoms =
60 inv(std::move(ret.geoms)).value_or(decltype(ret.geoms){});
61 return ret;
62 });
63 if (!vec.partial)
64 done.insert(box);
65 if (!vec.geoms.empty())
66 co_yield std::move(vec);
67 }
68}
69
70std::generator<variant> rasters(layer_query const& q)
71{
72 auto& footprint = q.footprint;
73 if (footprint.empty())
74 co_return;
75 auto r = get_or_invoke(q.cache.get(), q.cache_key, [&] {
76 return q.catalog().get_raster(q.layer);
77 });
78 auto meters = std::next(footprint.begin(), footprint.size() > 1)->first;
79 auto mat = affine(r);
80 auto crs = geometry::make_srs_variant(r.epsg, r.proj4);
81 auto uncached = std::vector<tile>{};
82 for (auto& t :
83 tiles(footprint.begin()->second, meters, r.extent, mat, crs)) {
84 auto any =
85 q.cache ? q.cache->get(std::pair{q.cache_key, t}) : std::any{};
86 if (!any.has_value()) {
87 uncached.push_back(t);
88 continue;
89 }
90 co_yield raster{
91 std::any_cast<decltype(raster::rgba)>(std::move(any)),
92 mat * t.affine(r.extent),
93 crs};
94 }
95 if (uncached.empty() || q.stop.stop_requested())
96 co_return;
97 for (auto [t, img] : q.catalog().read(r, std::move(uncached))) {
98 auto rgba = gil::to<decltype(raster::rgba)>(const_view(img));
99 if (q.cache)
100 q.cache->put(std::pair{q.cache_key, t}, rgba);
101 co_yield raster{std::move(rgba), mat * t.affine(r.extent), crs};
102 if (q.stop.stop_requested())
103 co_return;
104 }
105}
106
107} // namespace detail
108
111inline std::generator<variant> fetch(layer_query const& q)
112{
113 return q.layer.raster ? detail::rasters(q) : detail::vectors(q);
114}
115
116} // namespace boat::gui
117
118#endif // BOAT_GUI_FETCH_HPP
constexpr auto view
Columns must match the field count and order; scalars need one column.
Definition rowset.hpp:27
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 make_boxes(geographic::grid const &footprint, srs_spec auto const &crs)
Definition raster.hpp:79
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
T to(any_image_view img)
Definition gil.hpp:55
Definition cache.hpp:9
std::generator< variant > fetch(layer_query const &q)
Definition fetch.hpp:111
Definition blob.hpp:13
Definition catalog.hpp:14
std::string column_name
Definition meta.hpp:29
Definition meta.hpp:18
std::string column_name
Definition meta.hpp:21
bool raster
Definition meta.hpp:22
std::map< double, multi_point > grid
Definition vocabulary.hpp:73
Definition fetch.hpp:14
db::layer layer
Definition fetch.hpp:16
geometry::geographic::grid footprint
Definition fetch.hpp:19
std::move_only_function< db::catalog &() const > catalog
Definition fetch.hpp:15
int limit
Definition fetch.hpp:20
std::shared_ptr< caches::cache > cache
Definition fetch.hpp:17
std::stop_token stop
Definition fetch.hpp:21
size_t cache_key
Unique within the cache.
Definition fetch.hpp:18
Definition variant.hpp:21
Definition variant.hpp:11