./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
61 / 62
98.4%
Branches:
51 / 56
91.1%
Functions:
7 / 7
100.0%

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
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 26 times.
234 for (const GeoKeyEntry& entry : entries) {
29
1/1
✓ Branch 1 taken 208 times.
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
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 239 times.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 239 times.
239 if (last == std::string::npos) {
44 ✗ return std::nullopt;
45 }
46
1/1
✓ Branch 1 taken 239 times.
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
2/2
✓ Branch 0 taken 857 times.
✓ Branch 1 taken 1074 times.
1931 if (directory.size() < 4) {
54 857 return std::nullopt;
55 }
56 1074 const std::size_t count = directory[3];
57
3/4
✓ Branch 0 taken 1732 times.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1732 times.
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
3/4
✓ Branch 0 taken 929 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 929 times.
✓ Branch 3 taken 803 times.
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
2/2
✓ Branch 0 taken 635 times.
✓ Branch 1 taken 279 times.
914 return detail::ShortGeoKey(*this, detail::kGtRasterTypeGeoKey) == detail::kRasterPixelIsPoint;
68 }
69
70 1014 bool GeoKeys::IsGeographic() const {
71
2/2
✓ Branch 0 taken 292 times.
✓ Branch 1 taken 722 times.
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
2/2
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 799 times.
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
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 1 time.
122 if (close == std::string::npos || end == std::string::npos) {
81 break;
82 }
83
1/1
✓ Branch 1 taken 121 times.
121 const std::string tag = xml.substr(position, close - position);
84
1/1
✓ Branch 1 taken 121 times.
121 const std::string text = xml.substr(close + 1, end - close - 1);
85 121 position = end;
86
3/3
✓ Branch 1 taken 121 times.
✓ Branch 4 taken 121 times.
✓ Branch 7 taken 121 times.
242 const auto sample = XmlAttribute(tag, "sample");
87
4/4
✓ Branch 1 taken 121 times.
✓ Branch 4 taken 121 times.
✓ Branch 6 taken 119 times.
✓ Branch 7 taken 2 times.
242 const auto role = XmlAttribute(tag, "role");
88
7/8
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 119 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 58 times.
✓ Branch 5 taken 61 times.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 57 times.
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
4/4
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 1 time.
118 if (parsed == sample->c_str() || band >= bands) {
94 2 continue;
95 }
96 116 const double value = std::strtod(text.c_str(), &parsed);
97
3/4
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
116 if (parsed == text.c_str() || !std::isfinite(value)) {
98 1 continue;
99 }
100
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 57 times.
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
1/1
✓ Branch 1 taken 26 times.
26 GeoKeys keys;
109
1/1
✓ Branch 1 taken 26 times.
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
1/1
✓ Branch 1 taken 26 times.
26 keys.doubleParams = {radius, radius};
120 26 return keys;
121 }
122
123 } // namespace terraingen
124