./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
102 / 102
100.0%
Branches:
103 / 111
92.8%
Functions:
14 / 14
100.0%

src/sources/geotiff_source.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/sources/geotiff_source.h"
4
5 #include "sources/lattice.h"
6 #include "sources/raster_heights.h"
7 #include "terraingen/util/bits.h"
8 #include "terraingen/util/lru_cache.h"
9 #include "util/checks.h"
10
11 #include <algorithm>
12 #include <cmath>
13 #include <cstdint>
14 #include <limits>
15 #include <memory>
16 #include <mutex>
17 #include <stdexcept>
18 #include <string>
19 #include <utility>
20 #include <vector>
21
22 namespace terraingen {
23
24 namespace {
25
26 /// How the band's cells become heights.
27 264 RasterSourceOptions SamplingOptions(const GeoTiffSourceOptions& options) {
28 264 RasterSourceOptions sampling;
29 264 sampling.effectivePixelSize = options.effectivePixelSize;
30 264 sampling.upsampler = options.upsampler;
31 264 return sampling;
32 }
33
34 /// The cells of `lattice` that meet `crop`.
35 /// @throws std::invalid_argument for a crop that is not finite, is empty or misses the lattice.
36 51 PixelWindow CropCells(const RasterGrid& lattice, const Bounds2d& crop) {
37
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 49 times.
51 if (!crop.IsFinite()) {
38
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("The crop must be finite");
39 }
40
4/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 42 times.
49 if (!(crop.minX < crop.maxX && crop.minY < crop.maxY)) {
41
1/1
✓ Branch 2 taken 7 times.
7 throw std::invalid_argument("The crop must have minX < maxX and minY < maxY");
42 }
43 42 const PixelWindow cells = PixelWindowIn(lattice, crop);
44
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 5 times.
42 if (cells.Empty()) {
45
1/1
✓ Branch 2 taken 6 times.
6 throw std::invalid_argument("The crop misses the file");
46 }
47 36 return cells;
48 }
49
50 /// The cells of `lattice` a source covers: those that meet `crop`, or all without one.
51 /// @throws std::invalid_argument as `CropCells`.
52 178 PixelWindow CoveredCells(const RasterGrid& lattice, const std::optional<Bounds2d>& crop) {
53
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 127 times.
178 return crop ? CropCells(lattice, *crop) : WholeWindow(lattice);
54 }
55
56 /// The scaled posts of a band of a GeoTIFF, decoded a block at a time as reads reach them and
57 /// kept until the decoded blocks exceed the cache's bytes, those read longest ago going first.
58 /// Posts beyond `window` repeat its edge, as `RasterPosts` of the window read whole does.
59 class GeoTiffPosts final : public PostLattice {
60 public:
61 104 GeoTiffPosts(const std::filesystem::path& path, std::size_t band, BandScaling scaling,
62 const RasterGrid& lattice, const PixelWindow& window, bool wrapColumns,
63 std::size_t cacheBytes)
64 208 : m_reader(path, band), m_scaling(scaling), m_lattice(lattice), m_window(window),
65
3/3
✓ Branch 1 taken 104 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 103 times.
104 m_wrapColumns(wrapColumns), m_across(CeilDiv(lattice.width, m_reader.BlockWidth())),
66
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 103 times.
208 m_blocks(
67
3/3
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 103 times.
✓ Branch 3 taken 104 times.
105 std::max<std::size_t>(cacheBytes, 1),
68 208 [](const Raster<float>& block) { return block.Size() * sizeof(float); }, {}, 1) {}
69
70 494883 GeoTransform Transform() const override {
71 494883 return m_lattice.transform;
72 }
73
74 2 std::optional<PixelWindow> Extent() const override {
75 2 return m_window;
76 }
77
78 362791 void Read(std::int64_t col0, std::int64_t row0, std::size_t width, std::size_t height,
79 Span<float> heights) const override {
80 362791 const std::vector<std::size_t> cols = detail::PostIndices(
81 362791 col0, width, m_wrapColumns, m_lattice.width, m_window.colBegin, m_window.colEnd);
82 362791 const std::vector<std::size_t> rows = detail::PostIndices(
83
1/1
✓ Branch 1 taken 362791 times.
362791 row0, height, false, m_lattice.height, m_window.rowBegin, m_window.rowEnd);
84
1/1
✓ Branch 1 taken 362791 times.
362791 const std::size_t blockWidth = m_reader.BlockWidth();
85
1/1
✓ Branch 1 taken 362791 times.
362791 const std::size_t blockHeight = m_reader.BlockHeight();
86 362791 std::vector<std::pair<std::size_t, std::shared_ptr<const Raster<float>>>> held;
87 1495545 const auto block = [&](std::size_t bx, std::size_t by) -> const Raster<float>& {
88 1132754 const std::size_t key = by * m_across + bx;
89
2/2
✓ Branch 0 taken 945669 times.
✓ Branch 1 taken 394570 times.
1340239 for (const auto& entry : held) {
90
2/2
✓ Branch 0 taken 738184 times.
✓ Branch 1 taken 207485 times.
945669 if (entry.first == key) {
91 738184 return *entry.second;
92 }
93 }
94
2/2
✓ Branch 2 taken 394570 times.
✓ Branch 5 taken 394570 times.
789384 held.emplace_back(key, m_blocks.Get(key, [&] { return Decode(bx, by); }));
95 394570 return *held.back().second;
96 362791 };
97
2/2
✓ Branch 0 taken 998786 times.
✓ Branch 1 taken 362790 times.
1361576 for (std::size_t r = 0; r < height; ++r) {
98 998786 const std::size_t by = rows[r] / blockHeight;
99 998786 const std::size_t blockRow = rows[r] - by * blockHeight;
100 998786 const Raster<float>* current = nullptr;
101 998786 std::size_t currentBx = 0;
102
2/2
✓ Branch 0 taken 4889552 times.
✓ Branch 1 taken 998785 times.
5888337 for (std::size_t c = 0; c < width; ++c) {
103
2/2
✓ Branch 0 taken 1132751 times.
✓ Branch 1 taken 3756801 times.
4889552 const std::size_t bx = cols[c] / blockWidth;
104
2/2
✓ Branch 0 taken 1132751 times.
✓ Branch 1 taken 3756801 times.
4889552 if (current == nullptr || bx != currentBx) {
105
1/1
✓ Branch 1 taken 1132751 times.
1132751 current = &block(bx, by);
106 currentBx = bx;
107 }
108 4889552 heights[r * width + c] = (*current)(cols[c] - bx * blockWidth, blockRow);
109 }
110 }
111 362790 }
112
113 private:
114 /// Block (`bx`, `by`) scaled, NaN where it has no data.
115 244 Raster<float> Decode(std::size_t bx, std::size_t by) const {
116 244 Raster<float> block = m_reader.ReadBlock(bx, by);
117 244 constexpr float kMissing = std::numeric_limits<float>::quiet_NaN();
118
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 10 times.
244 const bool identity = m_scaling.Identity();
119
2/2
✓ Branch 0 taken 119381 times.
✓ Branch 1 taken 244 times.
119625 for (float& value : block.Values()) {
120
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 119282 times.
119381 if (block.IsMissing(value)) {
121 99 value = kMissing;
122
2/2
✓ Branch 0 taken 6856 times.
✓ Branch 1 taken 112426 times.
119282 } else if (!identity) {
123 6856 value = static_cast<float>(static_cast<double>(value) * m_scaling.scale +
124 6856 m_scaling.offset);
125 }
126 }
127 244 return block;
128 }
129
130 GeoTiffBlockReader m_reader;
131 BandScaling m_scaling;
132 RasterGrid m_lattice;
133 PixelWindow m_window;
134 bool m_wrapColumns = false;
135 std::size_t m_across = 0;
136 mutable LruCache<std::size_t, Raster<float>> m_blocks;
137 };
138
139 } // namespace
140
141 179 GeoTiffSource::GeoTiffSource(std::filesystem::path path, GeoTiffSourceOptions options)
142
5/6
✓ Branch 3 taken 178 times.
✓ Branch 6 taken 159 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 159 times.
✓ Branch 11 taken 7 times.
✓ Branch 12 taken 12 times.
523 : GeoTiffSource(path, std::move(options), ReadGeoTiffInfo(path)) {}
143
144 178 GeoTiffSource::GeoTiffSource(std::filesystem::path path, GeoTiffSourceOptions options,
145 178 GeoTiffInfo info)
146
1/1
✓ Branch 1 taken 163 times.
178 : RasterHeightsSource(info.grid, CoveredCells(info.grid, options.crop), info.geoKeys),
147
2/2
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 162 times.
356 m_path(std::move(path)), m_options(std::move(options)), m_info(std::move(info)) {
148 163 const std::size_t band = m_options.band;
149
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 162 times.
163 if (band >= m_info.bands) {
150
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
3 throw std::invalid_argument("Band " + std::to_string(band) + " is missing; the file has " +
151
1/1
✓ Branch 1 taken 1 time.
2 std::to_string(m_info.bands) +
152
2/3
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 1 time.
3 (m_info.bands == 1 ? " band" : " bands"));
153 }
154 162 const std::optional<double>& scale = m_options.scale;
155 162 const std::optional<double>& offset = m_options.offset;
156
8/8
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 155 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 158 times.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 2 times.
162 if ((scale && !std::isfinite(*scale)) || (offset && !std::isfinite(*offset))) {
157
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Scale and offset must be finite");
158 }
159
1/1
✓ Branch 2 taken 159 times.
161 detail::ValidateRasterSourceOptions(SamplingOptions(m_options));
160
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 154 times.
159 m_scaling = {scale.value_or(m_info.bandScaling[band].scale),
161
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 157 times.
159 offset.value_or(m_info.bandScaling[band].offset)};
162
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
171 }
163
164 385798 const detail::RasterHeights& GeoTiffSource::Heights() const {
165 385798 std::call_once(m_loaded, [this] {
166 104 const RasterSourceOptions sampling = SamplingOptions(m_options);
167
1/1
✓ Branch 1 taken 104 times.
104 const RasterGrid& lattice = Lattice();
168
1/1
✓ Branch 1 taken 104 times.
104 const bool wraps = detail::WrapsLongitude(m_info.geoKeys, lattice);
169
4/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 38 times.
✓ Branch 3 taken 104 times.
✓ Branch 6 taken 104 times.
208 PixelWindow window = detail::NativeWindow(
170
4/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 38 times.
✓ Branch 3 taken 38 times.
✓ Branch 6 taken 104 times.
170 lattice, (sampling.upsampler ? sampling.upsampler : DefaultUpsampler())->Reach(),
171 Cells());
172
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 98 times.
104 if (wraps) {
173 6 window.colBegin = 0;
174 6 window.colEnd = lattice.width;
175 }
176 104 std::optional<float> noData;
177
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 59 times.
104 if (m_info.noData) {
178
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 5 times.
85 noData = m_scaling.Identity() ? static_cast<float>(*m_info.noData)
179 : std::numeric_limits<float>::quiet_NaN();
180 }
181
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 38 times.
208 m_heights = std::make_shared<const detail::RasterHeights>(
182
1/1
✓ Branch 1 taken 104 times.
208 std::make_shared<GeoTiffPosts>(m_path, m_options.band, m_scaling, lattice, window,
183
1/1
✓ Branch 1 taken 104 times.
104 wraps, m_options.cacheBytes),
184
3/6
✓ Branch 1 taken 104 times.
✓ Branch 4 taken 104 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 104 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
208 lattice, noData, sampling, Cells(), m_path.string(), wraps);
185 104 });
186 385798 return *m_heights;
187 }
188
189 } // namespace terraingen
190