src/raster/geotiff/geotiff.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/raster/geotiff/geotiff.h" | ||
| 4 | |||
| 5 | #include "raster/geotiff/tiff_format.h" | ||
| 6 | |||
| 7 | #include "util/checks.h" | ||
| 8 | |||
| 9 | #include <cmath> | ||
| 10 | #include <cstdlib> | ||
| 11 | #include <initializer_list> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | /// One key of a GeoKeyDirectory: its value inline (`location` 0) or at index `value` of the tag | ||
| 18 | /// `location`. | ||
| 19 | struct GeoKeyEntry { | ||
| 20 | std::uint16_t key; | ||
| 21 | std::uint16_t location; | ||
| 22 | std::uint16_t value; | ||
| 23 | }; | ||
| 24 | |||
| 25 | /// A GeoKeyDirectory of version 1.1.0 holding `entries`, each of count 1. | ||
| 26 | 26 | std::vector<std::uint16_t> GeoKeyDirectory(std::initializer_list<GeoKeyEntry> entries) { | |
| 27 | 26 | std::vector<std::uint16_t> directory = {1, 1, 0, static_cast<std::uint16_t>(entries.size())}; | |
| 28 | 234 | for (const GeoKeyEntry& entry : entries) { | |
| 29 | 208 | directory.insert(directory.end(), {entry.key, entry.location, 1, entry.value}); | |
| 30 | } | ||
| 31 | 26 | return directory; | |
| 32 | } | ||
| 33 | |||
| 34 | /// The value of attribute `name` in the XML start tag `tag`, if present. | ||
| 35 | 242 | std::optional<std::string> XmlAttribute(const std::string& tag, const std::string& name) { | |
| 36 | 484 | const std::string key = " " + name + "=\""; | |
| 37 | 242 | const std::size_t start = tag.find(key); | |
| 38 | 242 | if (start == std::string::npos) { | |
| 39 | 3 | return std::nullopt; | |
| 40 | } | ||
| 41 | 239 | const std::size_t first = start + key.size(); | |
| 42 | 239 | const std::size_t last = tag.find('"', first); | |
| 43 | 239 | if (last == std::string::npos) { | |
| 44 | ✗ | return std::nullopt; | |
| 45 | } | ||
| 46 | 478 | return tag.substr(first, last - first); | |
| 47 | 242 | } | |
| 48 | |||
| 49 | } // namespace | ||
| 50 | |||
| 51 | 1931 | std::optional<std::uint16_t> detail::ShortGeoKey(const GeoKeys& keys, std::uint16_t key) { | |
| 52 | 1931 | const std::vector<std::uint16_t>& directory = keys.directory; | |
| 53 | 1931 | if (directory.size() < 4) { | |
| 54 | 857 | return std::nullopt; | |
| 55 | } | ||
| 56 | 1074 | const std::size_t count = directory[3]; | |
| 57 | 1877 | for (std::size_t k = 0; k < count && 4 + 4 * k + 3 < directory.size(); ++k) { | |
| 58 | 1732 | const std::size_t at = 4 + 4 * k; | |
| 59 | 1732 | if (directory[at] == key && directory[at + 1] == 0) { | |
| 60 | 929 | return directory[at + 3]; | |
| 61 | } | ||
| 62 | } | ||
| 63 | 145 | return std::nullopt; | |
| 64 | } | ||
| 65 | |||
| 66 | 914 | bool GeoKeys::PixelIsPoint() const { | |
| 67 | 914 | return detail::ShortGeoKey(*this, detail::kGtRasterTypeGeoKey) == detail::kRasterPixelIsPoint; | |
| 68 | } | ||
| 69 | |||
| 70 | 1014 | bool GeoKeys::IsGeographic() const { | |
| 71 | 1014 | return detail::ShortGeoKey(*this, detail::kGtModelTypeGeoKey) == detail::kModelTypeGeographic; | |
| 72 | } | ||
| 73 | |||
| 74 | 800 | std::vector<BandScaling> detail::ParseGdalBandScaling(const std::string& xml, std::size_t bands) { | |
| 75 | 800 | std::vector<BandScaling> scaling(bands); | |
| 76 | 800 | std::size_t position = 0; | |
| 77 | 921 | while ((position = xml.find("<Item", position)) != std::string::npos) { | |
| 78 | 122 | const std::size_t close = xml.find('>', position); | |
| 79 | 122 | const std::size_t end = xml.find("</Item>", close); | |
| 80 | 122 | if (close == std::string::npos || end == std::string::npos) { | |
| 81 | break; | ||
| 82 | } | ||
| 83 | 121 | const std::string tag = xml.substr(position, close - position); | |
| 84 | 121 | const std::string text = xml.substr(close + 1, end - close - 1); | |
| 85 | 121 | position = end; | |
| 86 | 242 | const auto sample = XmlAttribute(tag, "sample"); | |
| 87 | 242 | const auto role = XmlAttribute(tag, "role"); | |
| 88 | 121 | if (!sample || !role || (*role != "scale" && *role != "offset")) { | |
| 89 | 3 | continue; | |
| 90 | } | ||
| 91 | 118 | char* parsed = nullptr; | |
| 92 | 118 | const unsigned long long band = std::strtoull(sample->c_str(), &parsed, 10); | |
| 93 | 118 | if (parsed == sample->c_str() || band >= bands) { | |
| 94 | 2 | continue; | |
| 95 | } | ||
| 96 | 116 | const double value = std::strtod(text.c_str(), &parsed); | |
| 97 | 116 | if (parsed == text.c_str() || !std::isfinite(value)) { | |
| 98 | 1 | continue; | |
| 99 | } | ||
| 100 | 115 | (*role == "scale" ? scaling[band].scale : scaling[band].offset) = value; | |
| 101 | 121 | } | |
| 102 | 800 | return scaling; | |
| 103 | } | ||
| 104 | |||
| 105 | 27 | GeoKeys GeographicSphereKeys(double radius) { | |
| 106 | 27 | using namespace detail; | |
| 107 | 27 | RequirePositive(radius, "A sphere's radius"); | |
| 108 | 26 | GeoKeys keys; | |
| 109 | 26 | keys.directory = GeoKeyDirectory({ | |
| 110 | {kGtModelTypeGeoKey, 0, kModelTypeGeographic}, | ||
| 111 | {kGtRasterTypeGeoKey, 0, kRasterPixelIsArea}, | ||
| 112 | {kGeographicTypeGeoKey, 0, kUserDefined}, | ||
| 113 | {kGeogGeodeticDatumGeoKey, 0, kUserDefined}, | ||
| 114 | {kGeogAngularUnitsGeoKey, 0, kAngularDegree}, | ||
| 115 | {kGeogEllipsoidGeoKey, 0, kUserDefined}, | ||
| 116 | {kGeogSemiMajorAxisGeoKey, kGeoDoubleParams, 0}, | ||
| 117 | {kGeogSemiMinorAxisGeoKey, kGeoDoubleParams, 1}, | ||
| 118 | 26 | }); | |
| 119 | 26 | keys.doubleParams = {radius, radius}; | |
| 120 | 26 | return keys; | |
| 121 | } | ||
| 122 | |||
| 123 | } // namespace terraingen | ||
| 124 |