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 | 51 | if (!crop.IsFinite()) { | |
| 38 | 2 | throw std::invalid_argument("The crop must be finite"); | |
| 39 | } | ||
| 40 | 49 | if (!(crop.minX < crop.maxX && crop.minY < crop.maxY)) { | |
| 41 | 7 | throw std::invalid_argument("The crop must have minX < maxX and minY < maxY"); | |
| 42 | } | ||
| 43 | 42 | const PixelWindow cells = PixelWindowIn(lattice, crop); | |
| 44 | 42 | if (cells.Empty()) { | |
| 45 | 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 | 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 | 104 | m_wrapColumns(wrapColumns), m_across(CeilDiv(lattice.width, m_reader.BlockWidth())), | |
| 66 | 208 | m_blocks( | |
| 67 | 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 | 362791 | row0, height, false, m_lattice.height, m_window.rowBegin, m_window.rowEnd); | |
| 84 | 362791 | const std::size_t blockWidth = m_reader.BlockWidth(); | |
| 85 | 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 | 1340239 | for (const auto& entry : held) { | |
| 90 | 945669 | if (entry.first == key) { | |
| 91 | 738184 | return *entry.second; | |
| 92 | } | ||
| 93 | } | ||
| 94 | 789384 | held.emplace_back(key, m_blocks.Get(key, [&] { return Decode(bx, by); })); | |
| 95 | 394570 | return *held.back().second; | |
| 96 | 362791 | }; | |
| 97 | 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 | 5888337 | for (std::size_t c = 0; c < width; ++c) { | |
| 103 | 4889552 | const std::size_t bx = cols[c] / blockWidth; | |
| 104 | 4889552 | if (current == nullptr || bx != currentBx) { | |
| 105 | 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 | 244 | const bool identity = m_scaling.Identity(); | |
| 119 | 119625 | for (float& value : block.Values()) { | |
| 120 | 119381 | if (block.IsMissing(value)) { | |
| 121 | 99 | value = kMissing; | |
| 122 | 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 | 523 | : GeoTiffSource(path, std::move(options), ReadGeoTiffInfo(path)) {} | |
| 143 | |||
| 144 | 178 | GeoTiffSource::GeoTiffSource(std::filesystem::path path, GeoTiffSourceOptions options, | |
| 145 | 178 | GeoTiffInfo info) | |
| 146 | 178 | : RasterHeightsSource(info.grid, CoveredCells(info.grid, options.crop), info.geoKeys), | |
| 147 | 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 | 163 | if (band >= m_info.bands) { | |
| 150 | 3 | throw std::invalid_argument("Band " + std::to_string(band) + " is missing; the file has " + | |
| 151 | 2 | std::to_string(m_info.bands) + | |
| 152 | 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 | 162 | if ((scale && !std::isfinite(*scale)) || (offset && !std::isfinite(*offset))) { | |
| 157 | 2 | throw std::invalid_argument("Scale and offset must be finite"); | |
| 158 | } | ||
| 159 | 161 | detail::ValidateRasterSourceOptions(SamplingOptions(m_options)); | |
| 160 | 159 | m_scaling = {scale.value_or(m_info.bandScaling[band].scale), | |
| 161 | 159 | offset.value_or(m_info.bandScaling[band].offset)}; | |
| 162 | 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 | 104 | const RasterGrid& lattice = Lattice(); | |
| 168 | 104 | const bool wraps = detail::WrapsLongitude(m_info.geoKeys, lattice); | |
| 169 | 208 | PixelWindow window = detail::NativeWindow( | |
| 170 | 170 | lattice, (sampling.upsampler ? sampling.upsampler : DefaultUpsampler())->Reach(), | |
| 171 | Cells()); | ||
| 172 | 104 | if (wraps) { | |
| 173 | 6 | window.colBegin = 0; | |
| 174 | 6 | window.colEnd = lattice.width; | |
| 175 | } | ||
| 176 | 104 | std::optional<float> noData; | |
| 177 | 104 | if (m_info.noData) { | |
| 178 | 85 | noData = m_scaling.Identity() ? static_cast<float>(*m_info.noData) | |
| 179 | : std::numeric_limits<float>::quiet_NaN(); | ||
| 180 | } | ||
| 181 | 208 | m_heights = std::make_shared<const detail::RasterHeights>( | |
| 182 | 208 | std::make_shared<GeoTiffPosts>(m_path, m_options.band, m_scaling, lattice, window, | |
| 183 | 104 | wraps, m_options.cacheBytes), | |
| 184 | 208 | lattice, noData, sampling, Cells(), m_path.string(), wraps); | |
| 185 | 104 | }); | |
| 186 | 385798 | return *m_heights; | |
| 187 | } | ||
| 188 | |||
| 189 | } // namespace terraingen | ||
| 190 |