-
Notifications
You must be signed in to change notification settings - Fork 12
/
bag_metadata.cpp
294 lines (248 loc) · 7.15 KB
/
bag_metadata.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
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
#include "bag_dataset.h"
#include "bag_errors.h"
#include "bag_exceptions.h"
#include "bag_layer.h"
#include "bag_metadata.h"
#include "bag_metadata_export.h"
#include "bag_metadata_import.h"
#include "bag_private.h"
#include <fstream>
#include <H5Cpp.h>
namespace BAG
{
//! The HDF5 chunk size of the metadata.
constexpr hsize_t kMetadataChunkSize = 1024;
//! The default constructor.
Metadata::Metadata() noexcept
: m_pMetaStruct(std::make_unique<BagMetadata>())
{
// Can throw, but only if a new fails. std::terminate() is fine then.
bagInitMetadata(*m_pMetaStruct);
}
//! Constructor
/*!
\param pDataset
A shared pointer to the BAG Dataset the metadata belongs to.
*/
Metadata::Metadata(std::shared_ptr<Dataset> pDataset)
: Metadata(*pDataset)
{
}
//! Constructor.
/*!
\param dataset
The BAG Dataset the metadata belongs to.
*/
Metadata::Metadata(Dataset& dataset)
: m_pBagDataset(dataset.shared_from_this())
, m_pMetaStruct(std::make_unique<BagMetadata>())
{
bagInitMetadata(*m_pMetaStruct);
const auto& h5file = dataset.getH5file();
try
{
m_pH5dataSet = std::unique_ptr<::H5::DataSet, DeleteH5dataSet>(
new ::H5::DataSet{h5file.openDataSet(METADATA_PATH)}, DeleteH5dataSet{});
}
catch(...)
{
throw MetadataNotFound{};
}
H5std_string buffer;
const ::H5::StrType stringType{*m_pH5dataSet};
m_pH5dataSet->read(buffer, stringType);
this->loadFromBuffer(buffer);
}
//! Destructor.
Metadata::~Metadata() noexcept
{
if (m_pMetaStruct)
{
try
{
bagFreeMetadata(*m_pMetaStruct);
}
catch(...) // Prevent any exceptions from escaping.
{}
}
}
//! Retrieve the column resolution.
/*!
\return
The column resolution.
*/
double Metadata::columnResolution() const noexcept
{
return m_pMetaStruct->spatialRepresentationInfo->columnResolution;
}
//! Retrieve the number of columns.
/*!
\return
The number of columns.
*/
uint32_t Metadata::columns() const noexcept
{
return m_pMetaStruct->spatialRepresentationInfo->numberOfColumns;
}
//! Create an HDF5 DataSet to store the metadata.
/*!
\param dataset
The BAG Dataset this layer belongs to.
*/
void Metadata::createH5dataSet(
const Dataset& dataset)
{
m_pBagDataset = dataset.shared_from_this();
const auto& h5file = dataset.getH5file();
const auto buffer = exportMetadataToXML(this->getStruct());
const hsize_t xmlLength{buffer.size()};
const hsize_t kUnlimitedSize = H5F_UNLIMITED;
const ::H5::DataSpace h5dataSpace{1, &xmlLength, &kUnlimitedSize};
const ::H5::DSetCreatPropList h5createPropList{};
h5createPropList.setChunk(1, &kMetadataChunkSize);
m_pH5dataSet = std::unique_ptr<::H5::DataSet, DeleteH5dataSet>(
new ::H5::DataSet{h5file.createDataSet(METADATA_PATH,
::H5::PredType::C_S1, h5dataSpace, h5createPropList)},
DeleteH5dataSet{});
m_pH5dataSet->extend(&xmlLength);
}
//! Retrieve the C structure this class wraps.
/*!
\return
The C structure this class wraps.
*/
const BagMetadata& Metadata::getStruct() const & noexcept
{
return *m_pMetaStruct;
}
//! Retrieve the length of the XML in this metadata.
/*!
\return
The length of the XML in this metadata.
*/
size_t Metadata::getXMLlength() const noexcept
{
return m_xmlLength;
}
//! Retrieve a copy of the horizontal reference system as WKT.
/*!
\return
A copy of the horizontal reference system as WKT.
The string is empty if it is not WKT.
*/
std::string Metadata::horizontalReferenceSystemAsWKT() const
{
const auto* type = m_pMetaStruct->horizontalReferenceSystem->type;
if (!type || type != std::string{"WKT"})
return {};
return m_pMetaStruct->horizontalReferenceSystem->definition;
}
//! Retrieve the lower left corner's X value.
/*!
\return
The lower left corner's X value.
*/
double Metadata::llCornerX() const noexcept
{
return m_pMetaStruct->spatialRepresentationInfo->llCornerX;
}
//! Retrieve the lower left corner's Y value.
/*!
\return
The lower left corner's Y value.
*/
double Metadata::llCornerY() const noexcept
{
return m_pMetaStruct->spatialRepresentationInfo->llCornerY;
}
//! Populate the metadata from the XML in the specified file.
/*!
Read the XML from the specified file. An ErrorLoadingMetadata exception is
thrown if an error occurs.
\param fileName
The XML file to read into this class.
*/
void Metadata::loadFromFile(const std::string& fileName)
{
const BagError err = bagImportMetadataFromXmlFile(fileName.c_str(),
*m_pMetaStruct, false);
if (err != BAG_SUCCESS)
throw ErrorLoadingMetadata{err};
std::ifstream ifs{fileName, std::ios_base::in|std::ios_base::binary};
ifs.seekg(0, std::ios_base::end);
m_xmlLength = ifs.tellg();
}
//! Populate the metadata from the XML in the specified buffer.
/*!
Read the XML from the specified buffer. An ErrorLoadingMetadata exception
is thrown if an error occurs.
\param xmlBuffer
The XML buffer..
*/
void Metadata::loadFromBuffer(const std::string& xmlBuffer)
{
const BagError err = bagImportMetadataFromXmlBuffer(xmlBuffer.c_str(),
static_cast<int>(xmlBuffer.size()), *m_pMetaStruct, false);
if (err != BAG_SUCCESS)
throw ErrorLoadingMetadata{err};
m_xmlLength = xmlBuffer.size();
}
//! Retrieve the row resolution.
/*!
\return
The row resolution.
*/
double Metadata::rowResolution() const noexcept
{
return m_pMetaStruct->spatialRepresentationInfo->rowResolution;
}
//! Retrieve the number of rows.
/*!
\return
The number of rows.
*/
uint32_t Metadata::rows() const noexcept
{
return m_pMetaStruct->spatialRepresentationInfo->numberOfRows;
}
//! Retrieve the upper right corner's X value.
/*!
\return
The upper right corner's X value.
*/
double Metadata::urCornerX() const noexcept
{
return m_pMetaStruct->spatialRepresentationInfo->urCornerX;
}
//! Retrieve the upper right corner's Y value.
/*!
\return
The upper right corner's Y value.
*/
double Metadata::urCornerY() const noexcept
{
return m_pMetaStruct->spatialRepresentationInfo->urCornerY;
}
//! Retrieve a copy of the vertical reference system as WKT.
/*!
\return
A copy of the vertical reference system as WKT.
The string is empty if it is not WKT.
*/
std::string Metadata::verticalReferenceSystemAsWKT() const
{
const auto* type = m_pMetaStruct->verticalReferenceSystem->type;
if (!type || type != std::string{"WKT"})
return {};
return m_pMetaStruct->verticalReferenceSystem->definition;
}
//! Write the metadata to the HDF5 file.
void Metadata::write() const
{
const auto buffer = exportMetadataToXML(this->getStruct());
const hsize_t bufferLen = buffer.size();
const hsize_t kMaxSize = H5F_UNLIMITED;
const ::H5::DataSpace h5dataSpace{1, &bufferLen, &kMaxSize};
m_pH5dataSet->write(buffer, ::H5::PredType::C_S1, h5dataSpace);
}
} //namespace BAG