include/terraingen/sources/geotiff_source.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Heights read from a band of a GeoTIFF DEM. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/raster/geotiff/geotiff.h" | ||
| 8 | #include "terraingen/raster/resample.h" | ||
| 9 | #include "terraingen/sources/raster_source.h" | ||
| 10 | #include "terraingen/sources/source.h" | ||
| 11 | #include "terraingen/sources/upsampling.h" | ||
| 12 | |||
| 13 | #include <cstddef> | ||
| 14 | #include <filesystem> | ||
| 15 | #include <memory> | ||
| 16 | #include <mutex> | ||
| 17 | #include <optional> | ||
| 18 | |||
| 19 | namespace terraingen { | ||
| 20 | |||
| 21 | /// The bytes of decoded tiles or strips a `GeoTiffSource` keeps by default: 512 MiB. | ||
| 22 | inline constexpr std::size_t kDefaultGeoTiffCacheBytes = std::size_t{512} << 20; | ||
| 23 | |||
| 24 | /// How a `GeoTiffSource` reads its band. | ||
| 25 | 641 | struct GeoTiffSourceOptions { | |
| 26 | std::size_t band = 0; ///< Zero-based band index. | ||
| 27 | std::optional<double> scale{}; ///< Replaces the band's scale when set; finite. | ||
| 28 | std::optional<double> offset{}; ///< Replaces the band's offset when set; finite. | ||
| 29 | /// The box in CRS units the source covers: only the cells of its grid that meet it. Unset | ||
| 30 | /// covers the whole file. | ||
| 31 | std::optional<Bounds2d> crop{}; | ||
| 32 | /// The cell size in CRS units the file's heights resolve, for a DEM that was upsampled or | ||
| 33 | /// smoothed before it was saved: `ResolutionAt` gives it in place of the cells' size. Unset | ||
| 34 | /// uses the cells'. | ||
| 35 | std::optional<double> effectivePixelSize{}; | ||
| 36 | /// How the cells become heights between them and at sample spacings finer than the cells; | ||
| 37 | /// null for cubic interpolation (`DefaultUpsampler`). | ||
| 38 | std::shared_ptr<const Upsampler> upsampler{}; | ||
| 39 | /// The most bytes of decoded tiles or strips kept for reuse; the one read last is kept | ||
| 40 | /// whatever the budget. | ||
| 41 | std::size_t cacheBytes = kDefaultGeoTiffCacheBytes; | ||
| 42 | |||
| 43 | /// Memberwise equality, the upsamplers compared by identity. | ||
| 44 | 2 | bool operator==(const GeoTiffSourceOptions& other) const { | |
| 45 | 4 | return band == other.band && scale == other.scale && offset == other.offset && | |
| 46 | 2 | crop == other.crop && effectivePixelSize == other.effectivePixelSize && | |
| 47 | 3 | upsampler == other.upsampler && cacheBytes == other.cacheBytes; | |
| 48 | } | ||
| 49 | 1 | bool operator!=(const GeoTiffSourceOptions& other) const { | |
| 50 | 1 | return !(*this == other); | |
| 51 | } | ||
| 52 | }; | ||
| 53 | |||
| 54 | /// Heights from one band of a GeoTIFF in a projected CRS, or in longitude and latitude (a | ||
| 55 | /// geographic CRS) for a terrain on a body, served as a `RasterSource` serves its raster; samples | ||
| 56 | /// take the band's scale and offset, or `scale` and `offset`. The file is decoded a tile or strip | ||
| 57 | /// at a time as heights are asked of them and kept in a cache of `cacheBytes`, so a small area of | ||
| 58 | /// a file too large to hold in memory reads only the blocks around it; a `crop` gives the same | ||
| 59 | /// bits over its grid as the whole file. A file in longitude and latitude whose columns span 360 | ||
| 60 | /// degrees wraps in longitude as a `RasterSource` does. | ||
| 61 | class GeoTiffSource final : public detail::RasterHeightsSource { | ||
| 62 | public: | ||
| 63 | /// Reads the file's header. The grid is the file's, clipped to the cells that meet `crop` | ||
| 64 | /// when set, and the projection the file's. | ||
| 65 | /// @throws GeoTiffError if the file cannot be read. | ||
| 66 | /// @throws std::invalid_argument for a missing band, a `scale` or `offset` that is not finite, | ||
| 67 | /// an `effectivePixelSize` that is not positive and finite, or a `crop` that is not finite, is | ||
| 68 | /// empty (`minX` >= `maxX` or `minY` >= `maxY`) or misses the file. | ||
| 69 | explicit GeoTiffSource(std::filesystem::path path, GeoTiffSourceOptions options = {}); | ||
| 70 | |||
| 71 | protected: | ||
| 72 | /// Opens the file for reading blocks on first use. | ||
| 73 | /// @throws GeoTiffError if the file cannot be read. | ||
| 74 | const detail::RasterHeights& Heights() const override; | ||
| 75 | |||
| 76 | private: | ||
| 77 | /// The source of `path` with the header `info` read from it. | ||
| 78 | GeoTiffSource(std::filesystem::path path, GeoTiffSourceOptions options, GeoTiffInfo info); | ||
| 79 | |||
| 80 | std::filesystem::path m_path; | ||
| 81 | GeoTiffSourceOptions m_options; | ||
| 82 | GeoTiffInfo m_info; | ||
| 83 | BandScaling m_scaling; | ||
| 84 | mutable std::once_flag m_loaded; | ||
| 85 | mutable std::shared_ptr<const detail::RasterHeights> m_heights; | ||
| 86 | }; | ||
| 87 | |||
| 88 | } // namespace terraingen | ||
| 89 |