-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbag_georefmetadatalayerdescriptor.cpp
More file actions
225 lines (194 loc) · 6.97 KB
/
Copy pathbag_georefmetadatalayerdescriptor.cpp
File metadata and controls
225 lines (194 loc) · 6.97 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
#include "bag_dataset.h"
#include "bag_georefmetadatalayerdescriptor.h"
#include "bag_metadataprofiles.h"
#include "bag_hdfhelper.h"
#include "bag_private.h"
#include "bag_valuetable.h"
#include <iostream>
#include <array>
#include <H5Cpp.h>
namespace BAG {
//! Constructor.
/*!
\param dataset
The BAG Dataset this layer belongs to.
\param name
The name of the simple layer this layer has metadata for.
\param keyType
The type of the key.
Must be DT_UINT8, DT_UINT16, DT_UINT32, or DT_UINT64.
\param definition
The list of fields describing a record/value.
\param chunkSize
The chunk size the HDF5 DataSet will use.
\param compressionLevel
The compression level the HDF5 DataSet will use.
*/
GeorefMetadataLayerDescriptor::GeorefMetadataLayerDescriptor(
Dataset& dataset,
const std::string& name,
GeorefMetadataProfile profile,
DataType keyType,
RecordDefinition definition,
uint32_t rows, uint32_t cols,
uint64_t chunkSize,
int compressionLevel)
: LayerDescriptor(dataset.getNextId(), GEOREF_METADATA_PATH + name, name,
Georef_Metadata, rows, cols, chunkSize, compressionLevel)
, m_pBagDataset(dataset.shared_from_this())
, m_profile(profile)
, m_keyType(keyType)
, m_elementSize(Layer::getElementSize(keyType))
, m_definition(std::move(definition))
{
}
//! Create a georeferenced metadata layer descriptor.
/*!
\param dataset
The BAG Dataset this layer belongs to.
\param name
The name of the simple layer this layer has metadata for.
\param keyType
The type of the key.
Must be DT_UINT8, DT_UINT16, DT_UINT32, or DT_UINT64.
\param definition
The list of fields describing a record/value this layer contains for each node.
\param chunkSize
The chunk size the HDF5 DataSet will use.
\param compressionLevel
The compression level the HDF5 DataSet will use.
\return
The new georeferenced metadata layer descriptor.
*/
std::shared_ptr<GeorefMetadataLayerDescriptor> GeorefMetadataLayerDescriptor::create(
Dataset& dataset,
const std::string& name,
GeorefMetadataProfile profile,
DataType keyType,
RecordDefinition definition,
uint32_t rows, uint32_t cols,
uint64_t chunkSize,
int compressionLevel)
{
return std::shared_ptr<GeorefMetadataLayerDescriptor>(
new GeorefMetadataLayerDescriptor{dataset, name, profile, keyType,
std::move(definition), rows, cols,
chunkSize, compressionLevel});
}
//! Open an existing georeferenced metadata layer descriptor.
/*!
\param dataset
The BAG Dataset this layer belongs to.
\param name
The name of the simple layer this layer has metadata for.
\return
The existing georeferenced metadata layer descriptor.
*/
std::shared_ptr<GeorefMetadataLayerDescriptor> GeorefMetadataLayerDescriptor::open(
Dataset& dataset,
const std::string& name)
{
const auto& h5file = dataset.getH5file();
const std::string internalPath{GEOREF_METADATA_PATH + name + COMPOUND_KEYS};
const auto h5dataSet = ::H5::DataSet{h5file.openDataSet(internalPath)};
// Determine keyType.
const auto fileDataType = h5dataSet.getDataType();
DataType keyType = DT_UNKNOWN_DATA_TYPE;
switch(fileDataType.getSize())
{
case 1:
keyType = DT_UINT8;
break;
case 2:
keyType = DT_UINT16;
break;
case 4:
keyType = DT_UINT32;
break;
case 8:
keyType = DT_UINT64;
break;
default:
throw InvalidKeyType{};
}
// Determine Record Definition.
const auto attribute = h5dataSet.openAttribute(COMPOUND_RECORD_DEFINITION);
const auto fileDataSpace = attribute.getSpace();
const auto numDims = fileDataSpace.getSimpleExtentNdims();
if (numDims != 1)
throw InvalidValueSize{};
hsize_t numFields = 0;
fileDataSpace.getSimpleExtentDims(&numFields, nullptr);
RecordDefinition definition(numFields);
attribute.read(attribute.getDataType(), definition.data());
// Determine chunk size and compression level.
const auto chunkSize = BAG::getChunkSize(h5file, internalPath);
const auto compressionLevel = BAG::getCompressionLevel(h5file, internalPath);
// Read metadata profile as string from HDF5 file attribute and convert to GeorefMetadataProfile enum value.
std::string profileString;
if (h5dataSet.attrExists(METADATA_PROFILE_TYPE)) {
const auto metadataProfileAtt = h5dataSet.openAttribute(METADATA_PROFILE_TYPE);
metadataProfileAtt.read(metadataProfileAtt.getDataType(), profileString);
} else {
std::cerr << "Unable to find georeferenced metadata layer attribute '" << METADATA_PROFILE_TYPE <<
"' for simple layer '" << name << "', setting profile to '" << UNKOWN_METADATA_PROFILE_NAME <<
"'." << std::endl;
profileString = UNKOWN_METADATA_PROFILE_NAME;
}
GeorefMetadataProfile profile;
auto search = BAG::kStringMapGeorefMetadataProfile.find(profileString);
if (search != BAG::kStringMapGeorefMetadataProfile.end()) {
// This is a recognized metadata profile (either a specific known profile or the unknown profile)
profile = search->second;
} else {
std::cerr << "WARNING: Unrecognized georeferenced metadata layer metadata profile '" << profileString <<
"' for simple layer '" << name << "', assuming value was '" << UNKOWN_METADATA_PROFILE_NAME <<
"'." << std::endl;
profile = UNKNOWN_METADATA_PROFILE;
}
std::array<hsize_t, 2> dims;
h5dataSet.getSpace().getSimpleExtentDims(dims.data(), nullptr);
return std::shared_ptr<GeorefMetadataLayerDescriptor>(
new GeorefMetadataLayerDescriptor{dataset, name, profile, keyType, definition,
static_cast<const uint32_t>(dims[0]),
static_cast<const uint32_t>(dims[1]),
chunkSize, compressionLevel});
}
//! \copydoc LayerDescriptor::getDataType
DataType GeorefMetadataLayerDescriptor::getDataTypeProxy() const noexcept
{
return m_keyType;
}
//! Retrieve the BAG Dataset this layer belongs to.
/*!
\return
The BAG Dataset.
*/
std::weak_ptr<Dataset> GeorefMetadataLayerDescriptor::getDataset() const &
{
return m_pBagDataset;
}
//! \copydoc LayerDescriptor::getElementSize
uint8_t GeorefMetadataLayerDescriptor::getElementSizeProxy() const noexcept
{
return m_elementSize;
}
//! Retrieve the definition of a record/value.
/*!
\return
The definition of a record/value.
*/
const RecordDefinition&
GeorefMetadataLayerDescriptor::getDefinition() const & noexcept
{
return m_definition;
}
//! Retrieve the metadata profile type.
/*!
\return
The metadata profile type.
*/
GeorefMetadataProfile GeorefMetadataLayerDescriptor::getProfile() {
return m_profile;
}
} // namespace BAG