-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest_utils.cpp
More file actions
356 lines (288 loc) · 14 KB
/
Copy pathtest_utils.cpp
File metadata and controls
356 lines (288 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "test_utils.h"
#include <fstream> // std::ifstream, std::ofstream
#include <array>
#include <utility>
#include <memory>
#include <iostream>
#include <catch2/catch_all.hpp>
#include <bag_dataset.h>
#include <bag_metadata.h>
#include <bag_metadataprofiles.h>
#include <bag_simplelayer.h>
#include <bag_surfacecorrections.h>
#include <bag_surfacecorrectionsdescriptor.h>
using BAG::Dataset;
namespace TestUtils {
constexpr uint32_t kGridSize = 100;
constexpr uint32_t kSepSize = 3;
//! Helper to copy a source file to a destination file.
void copyFile(
const std::string& source,
const std::string& dest)
{
const std::ifstream in{source, std::ios::binary|std::ios::in};
std::ofstream out{dest, std::ios::binary|std::ios::out};
out << in.rdbuf();
}
std::pair<std::shared_ptr<BAG::Dataset>, std::string>
createBag(const std::string metadataFileName,
const std::string bagFileName) {
// Load sample metadata
BAG::Metadata metadata;
metadata.loadFromFile(metadataFileName);
// Create the dataset
constexpr uint64_t chunkSize = 100;
constexpr int compressionLevel = 1;
std::shared_ptr<BAG::Dataset> dataset = BAG::Dataset::create(bagFileName, std::move(metadata),
chunkSize, compressionLevel);
// Write the elevation layer, constructing bogus data as we do so.
auto elevationLayer = dataset->getSimpleLayer(Elevation);
// Set the min/max values (optional).
// NOTE: Layer::write() calls update min/max.
{
const std::array<float, 2> surfRange{-10.0f,
-10.0f - ((kGridSize - 1) * (kGridSize - 1) + kGridSize) / 10.0f};
auto pDescriptor = elevationLayer->getDescriptor();
pDescriptor->setMinMax(surfRange[0], surfRange[1]);
elevationLayer->writeAttributes();
}
std::string elevationLayerName = elevationLayer->getDescriptor()->getName();
// Write the data.
std::array<float, kGridSize> surf{};
for(uint32_t row=0; row<kGridSize; ++row)
{
for (uint32_t column=0; column<kGridSize; ++column)
surf[column] = ((column * row) % kGridSize) +
(column / static_cast<float>(kGridSize));
try
{
const auto* buffer = reinterpret_cast<uint8_t*>(surf.data());
constexpr uint32_t columnStart = 0;
constexpr uint32_t columnEnd = kGridSize - 1;
elevationLayer->write(row, columnStart, row, columnEnd, buffer);
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
FAIL(e.what());
}
}
// Write the uncertainty layer, constructing bogus data as we do so.
auto uncertaintyLayer = dataset->getSimpleLayer(Uncertainty);
// Set the min/max values (optional).
// NOTE: Layer::write() calls update min/max.
{
const std::array<float, 2> uncertRange{1.0f,
1.0f + ((kGridSize - 1) * (kGridSize - 1) + kGridSize) / 100.0f};
auto pDescriptor = uncertaintyLayer->getDescriptor();
pDescriptor->setMinMax(uncertRange[0], uncertRange[1]);
uncertaintyLayer->writeAttributes();
}
// Write the data.
{
std::array<float, kGridSize> uncert{};
for (uint32_t row = 0; row < kGridSize; ++row) {
for (uint32_t column = 0; column < kGridSize; ++column)
uncert[column] = ((column * row) % kGridSize) / 1000.0f;
try {
const auto *buffer = reinterpret_cast<uint8_t *>(uncert.data());
constexpr uint32_t columnStart = 0;
constexpr uint32_t columnEnd = kGridSize - 1;
uncertaintyLayer->write(row, columnStart, row, columnEnd, buffer);
}
catch (const std::exception &e) {
std::cerr << e.what() << '\n';
FAIL(e.what());
}
}
}
// Write the uncertainty layer, constructing bogus data as we do so.
uncertaintyLayer = dataset->getSimpleLayer(Uncertainty);
// Set the min/max values (optional).
// NOTE: Layer::write() calls update min/max.
{
const std::array<float, 2> uncertRange{1.0f,
1.0f + ((kGridSize - 1) * (kGridSize - 1) + kGridSize) / 100.0f};
auto pDescriptor = uncertaintyLayer->getDescriptor();
pDescriptor->setMinMax(uncertRange[0], uncertRange[1]);
uncertaintyLayer->writeAttributes();
}
// Write the data.
{
std::array<float, kGridSize> uncert{};
for (uint32_t row = 0; row < kGridSize; ++row) {
for (uint32_t column = 0; column < kGridSize; ++column)
uncert[column] = ((column * row) % kGridSize) / 1000.0f;
try {
const auto *buffer = reinterpret_cast<uint8_t *>(uncert.data());
constexpr uint32_t columnStart = 0;
constexpr uint32_t columnEnd = kGridSize - 1;
uncertaintyLayer->write(row, columnStart, row, columnEnd, buffer);
}
catch (const std::exception &e) {
std::cerr << e.what() << '\n';
FAIL(e.what());
}
}
}
return std::pair<std::shared_ptr<BAG::Dataset>, std::string>{dataset, elevationLayerName};
}
void create_NOAA_OCS_Metadata(const std::string& simpleLayerName,
std::shared_ptr<BAG::Dataset> dataset) {
try
{
constexpr uint64_t chunkSize = 100;
constexpr unsigned int compressionLevel = 1;
auto& compoundLayer = dataset->createGeorefMetadataLayer(
BAG::GeorefMetadataProfile::NOAA_OCS_2022_10_METADATA_PROFILE,
simpleLayerName, chunkSize, compressionLevel);
// At this point, all entries in the georeferenced metadata layer point to index 0,
// which is a no data value.
// Write a couple records.
using BAG::CompoundDataType;
BAG::Record record = BAG::CreateRecord_NOAA_OCS_2022_10(
false, // significant_features
true, // feature_least_depth
1234.567f, // feature_size
765.4321f, // feature_size_var
true, // coverage
false, // bathy_coverage
9.87f, // horizontal_uncert_fixed
1.23f, // horizontal_uncert_var
std::string{"2019-04-01 00:00:00.0Z"}, // survey_data_start
std::string{"2019-04-01 12:00:00.0Z"}, // survey_date_end
std::string("NOAA"), // source_institution
std::string("CD71EB77-5812-4735-B728-0DC1AE2A2F3B"), // source_survey_id
42, // source_survey_index
std::string("Creative Commons Zero Public Domain Dedication (CC0)"), // license_same
std::string("https://creativecommons.org/publicdomain/zero/1.0/") // license_url
);
auto& valueTable = compoundLayer.getValueTable();
// Store the new record in memory and in the BAG.
const auto firstRecordIndex = valueTable.addRecord(record);
record = BAG::CreateRecord_NOAA_OCS_2022_10(
true, // significant_features
false, // feature_least_depth
987.6f, // feature_size
6.789f, // feature_size_var
false, // coverage
true, // bathy_coverage
12345.67f, // horizontal_uncert_fixed
89.0f, // horizontal_uncert_var
std::string{"2019-04-02 00:00:00.0Z"}, // survey_data_start
std::string{"2019-04-02 12:00:00.0Z"}, // survey_date_end
std::string("NOAA"), // source_institution
std::string("15B46F99-1D94-4669-92D8-AA86F533B097"), // source_survey_id
23, // source_survey_index
std::string("Open Data Commons Public Domain Dedication and Licence (PDDL)"), // license_name
std::string("http://opendatacommons.org/licenses/pddl/1.0/") // license_url
);
// Store the new record in memory and in the BAG.
const auto secondRecordIndex = valueTable.addRecord(record);
uint32_t numRows = 0;
uint32_t numColumns = 0;
std::tie(numRows, numColumns) = dataset->getDescriptor().getDims();
// Set up the georeferenced metadata layer to point to the new records.
// Let's say the first 5 rows of elevation should use the first record
// index, and the next 3 columns use the second record index.
// Start at row 0, go to (including) row 4.
// Use the entire column.
uint32_t rowStart = 0;
uint32_t columnStart = 0;
uint32_t rowEnd = 4;
uint32_t columnEnd = numColumns - 1;
// Create the buffer. The type depends on the indexType used when
// creating the georeferenced metadata layer.
// The buffer contains the first record's index covering the first four
// rows (across all the columns).
size_t numElements = (rowEnd - rowStart + 1) * numColumns;
const std::vector<uint16_t> firstBuffer(numElements, (uint16_t)firstRecordIndex);
compoundLayer.write(rowStart, columnStart, rowEnd, columnEnd,
reinterpret_cast<const uint8_t*>(firstBuffer.data()));
// Start at row 6, go to the last row.
// Start at column 0, go to (including) column 2.
rowStart = 5;
columnStart = 0;
rowEnd = numRows - 1;
columnEnd = 2;
// Create the buffer. The type depends on the indexType used when
// creating the georeferenced metadata layer.
// The buffer contains the second record's index covering the first four
// rows (across all the columns).
numElements = (rowEnd - rowStart + 1) * (columnEnd - columnStart + 1);
const std::vector<uint16_t> secondBuffer(numElements, (uint16_t)secondRecordIndex);
compoundLayer.write(rowStart, columnStart, rowEnd, columnEnd,
reinterpret_cast<const uint8_t*>(secondBuffer.data()));
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
FAIL(e.what());
}
}
void create_unknown_metadata(const std::string& elevationLayerName,
const std::shared_ptr<BAG::Dataset>& dataset) {
// Create georeferenced metadata layer of unknown metadata profile
BAG::RecordDefinition definition(2);
definition[0].name = "dummy_int";
definition[0].type = DT_UINT32;
definition[1].name = "dummy_float";
definition[1].type = DT_FLOAT32;
constexpr uint64_t chunkSize = 100;
constexpr unsigned int compressionLevel = 1;
const BAG::DataType indexType = DT_UINT16;
auto &compoundLayer = dataset->createGeorefMetadataLayer(indexType, UNKNOWN_METADATA_PROFILE,
elevationLayerName, definition, chunkSize,
compressionLevel);
auto &valueTable = compoundLayer.getValueTable();
// Add some records to the georeferenced metadata layer
using BAG::CompoundDataType;
// First record
BAG::Record record{
CompoundDataType{1u}, // dummy_int
CompoundDataType{123.456f}, // dummy_float
};
const auto firstRecordIndex = valueTable.addRecord(record);
// Second record
record = BAG::Record{
CompoundDataType{2u}, // dummy_int
CompoundDataType{456.123f}, // dummy_float
};
const auto secondRecordIndex = valueTable.addRecord(record);
// Write record index values to georeferenced metadata layer raster
uint32_t numRows = 0;
uint32_t numColumns = 0;
std::tie(numRows, numColumns) = dataset->getDescriptor().getDims();
// Set up the georeferenced metadata layer to point to the new records.
// Let's say the first 5 rows of elevation should use the first record
// index, and the next 3 columns use the second record index.
// Start at row 0, go to (including) row 4.
// Use the entire column.
uint32_t rowStart = 0;
uint32_t columnStart = 0;
uint32_t rowEnd = 4;
uint32_t columnEnd = numColumns - 1;
// Create the buffer. The type depends on the indexType used when
// creating the georeferenced metadata layer.
// The buffer contains the first record's index covering the first four
// rows (across all the columns).
size_t numElements = (rowEnd - rowStart + 1) * numColumns;
const std::vector<uint16_t> firstBuffer(numElements, (uint16_t)firstRecordIndex);
compoundLayer.write(rowStart, columnStart, rowEnd, columnEnd,
reinterpret_cast<const uint8_t *>(firstBuffer.data()));
// Start at row 6, go to the last row.
// Start at column 0, go to (including) column 2.
rowStart = 5;
columnStart = 0;
rowEnd = numRows - 1;
columnEnd = 2;
// Create the buffer. The type depends on the indexType used when
// creating the georeferenced metadata layer.
// The buffer contains the second record's index covering the first four
// rows (across all the columns).
numElements = (rowEnd - rowStart + 1) * (columnEnd - columnStart + 1);
const std::vector<uint16_t> secondBuffer(numElements, (uint16_t)secondRecordIndex);
compoundLayer.write(rowStart, columnStart, rowEnd, columnEnd,
reinterpret_cast<const uint8_t *>(secondBuffer.data()));
}
} // namespace TestUtils