-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbag_create.cpp
268 lines (211 loc) · 8.18 KB
/
bag_create.cpp
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
/************************************************************************
/ File: bagcreate.cpp
/
/ Open Navigation Surface Working Group, 2005
/
/ - Initial implementation
/ Mark Paton, 7/22/2005
/
*************************************************************************/
#include <bag_dataset.h>
#include <bag_metadata.h>
#include <bag_simplelayer.h>
#include <bag_surfacecorrections.h>
#include <bag_surfacecorrectionsdescriptor.h>
#include <bag_types.h>
#include <array>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
namespace {
constexpr uint32_t kGridSize = 100;
constexpr uint32_t kSepSize = 3;
} // namespace
int main(
int argc,
char *argv[])
{
if (argc != 3)
{
std::cerr << "Usage is: bag_create <inputXMLFile> <outputBagFile>\n";
return EXIT_FAILURE;
}
const std::string xmlFileName = argv[1]; // Store the XML fileName
const std::string outFileName = argv[2]; // Store the BAG fileName to write
/* Configure the dynamic ranges for the data layers that we're going to write,
* and set up the separation surface parameters. We generate the elevation and
* uncertainty layers one row at a time, and therefore don't have to make the
* whole thing now.
*/
// Initial construction from the XML metadata example file provided.
printf( "Creating the BAG from XML file metadata, " );
BAG::Metadata metadata;
try
{
metadata.loadFromFile(xmlFileName);
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}
// Create the dataset.
std::shared_ptr<BAG::Dataset> dataset;
try
{
constexpr uint64_t chunkSize = 100;
constexpr int compressionLevel = 1;
dataset = BAG::Dataset::create(outFileName, std::move(metadata),
chunkSize, compressionLevel);
}
catch(const std::exception& e)
{
std::cerr << "Error creating BAG file.\n";
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}
// 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();
}
// 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';
return EXIT_FAILURE;
}
}
// 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';
return EXIT_FAILURE;
}
}
// Add optional nominal elevation dataset
try
{
constexpr uint64_t chunkSize = 100;
constexpr int compressionLevel = 1;
auto& nominalElevationLayer = dataset->createSimpleLayer(
Nominal_Elevation, chunkSize, compressionLevel);
// Set the min/max values (optional).
// NOTE: Layer::write() calls update min/max.
{
const std::array<float, 2> nominalDepthRange{20.0f,
20.0f + ((kGridSize - 1) * (kGridSize - 1) + kGridSize) / 20.0f};
auto pDescriptor = nominalElevationLayer.getDescriptor();
pDescriptor->setMinMax(nominalDepthRange[0], nominalDepthRange[1]);
nominalElevationLayer.writeAttributes();
}
// Write the data.
std::array<float, kGridSize> nominalDepth{};
for (uint32_t row=0; row<kGridSize; ++row)
{
for (uint32_t column=0; column<kGridSize; ++column)
nominalDepth[column] = ((column * row) % kGridSize) + 1.0f +
(column / static_cast<float>(kGridSize));
auto buffer = reinterpret_cast<uint8_t*>(nominalDepth.data());
constexpr uint32_t columnStart = 0;
constexpr uint32_t columnEnd = kGridSize - 1;
nominalElevationLayer.write(row, columnStart, row, columnEnd, buffer);
}
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}
// Add optional sep elevation dataset.
try
{
uint64_t chunkSize = 100; // > 0 chunksize is needed
constexpr int compressionLevel = 0;
constexpr uint8_t kNumCorrectors = 2;
auto& correctionsLayer = dataset->createSurfaceCorrections(
BAG_SURFACE_IRREGULARLY_SPACED, kNumCorrectors, chunkSize,
compressionLevel);
// Set the min/max values (optional).
// NOTE: Layer::write() calls update min/max.
auto pDescriptor =
std::dynamic_pointer_cast<BAG::SurfaceCorrectionsDescriptor>(
correctionsLayer.getDescriptor());
if (!pDescriptor)
{
std::cerr << "Internal error\n";
return EXIT_FAILURE;
}
const std::array<float, 2> sepDepthRange{0.3333f, 103.333f};
// Set the vertical datums.
const std::string verticalDatums{"Test,Unknown"};
pDescriptor->setVerticalDatums(verticalDatums);
// Write the data.
std::array<std::array<BAG::VerticalDatumCorrections, kSepSize>, kSepSize> sepDepth{};
for (uint32_t row=0; row<kSepSize; ++row)
for (uint32_t column=0; column<kSepSize; ++column)
{
sepDepth[row][column].z[0] = -(row + 0.3333f) * (column + 1);
sepDepth[row][column].z[1] = (row + 0.55f) * (column + 1);
sepDepth[row][column].x = (row + 10.3333f) * (column + 1);
sepDepth[row][column].y = (row + 180.3333f) * (column + 1);
}
constexpr uint32_t columnStart = 0;
constexpr uint32_t columnEnd = kSepSize - 1;
for(uint32_t row=0; row<kSepSize; ++row)
{
const auto* buffer = reinterpret_cast<uint8_t*>(sepDepth[row].data());
correctionsLayer.write(row, columnStart, row, columnEnd, buffer);
}
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}
std::cout << "BAG created\n";
return EXIT_SUCCESS;
}