/src/bag/api/bag_layer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | |
2 | | #include "bag_layer.h" |
3 | | #include "bag_metadata.h" |
4 | | #include "bag_private.h" |
5 | | #include "bag_trackinglist.h" |
6 | | |
7 | | #include <array> |
8 | | |
9 | | namespace BAG { |
10 | | |
11 | | //! Constructor. |
12 | | /*! |
13 | | \param dataset |
14 | | The BAG Dataset this layer belongs to. |
15 | | \param descriptor |
16 | | The descriptor of this layer. |
17 | | */ |
18 | | Layer::Layer( |
19 | | Dataset& dataset, |
20 | | LayerDescriptor& descriptor) |
21 | 209 | : m_pBagDataset(dataset.shared_from_this()) |
22 | 209 | , m_pLayerDescriptor(descriptor.shared_from_this()) |
23 | 209 | { |
24 | 209 | } |
25 | | |
26 | | //! Retrieve the BAG Dataset this layer belongs to. |
27 | | /*! |
28 | | \return |
29 | | The BAG Dataset this layer belongs to. |
30 | | */ |
31 | | std::weak_ptr<Dataset> Layer::getDataset() & noexcept |
32 | 0 | { |
33 | 0 | return m_pBagDataset; |
34 | 0 | } |
35 | | |
36 | | //! Retrieve the BAG Dataset this layer belongs to. |
37 | | /*! |
38 | | \return |
39 | | The BAG Dataset this layer belongs to. |
40 | | */ |
41 | | std::weak_ptr<const Dataset> Layer::getDataset() const & noexcept |
42 | 0 | { |
43 | 0 | return m_pBagDataset; |
44 | 0 | } |
45 | | |
46 | | //! Determine the type of data stored in the specified layer type. |
47 | | /*! |
48 | | \return |
49 | | The type of data stored in the specified layer type. |
50 | | */ |
51 | | DataType Layer::getDataType(LayerType layerType) noexcept |
52 | 145 | { |
53 | 145 | switch (layerType) |
54 | 145 | { |
55 | 106 | case Elevation: //[[fallthrough]]; |
56 | 130 | case Uncertainty: //[[fallthrough]]; |
57 | 130 | case Hypothesis_Strength: //[[fallthrough]]; |
58 | 130 | case Shoal_Elevation: //[[fallthrough]]; |
59 | 130 | case Std_Dev: //[[fallthrough]]; |
60 | 130 | case Average_Elevation: //[[fallthrough]]; |
61 | 145 | case Nominal_Elevation: |
62 | 145 | return DT_FLOAT32; |
63 | 0 | case Num_Hypotheses: //[[fallthrough]]; |
64 | 0 | case Num_Soundings: |
65 | 0 | return DT_UINT32; |
66 | 0 | case Surface_Correction: |
67 | 0 | case Georef_Metadata: //[[fallthrough]]; |
68 | 0 | return DT_COMPOUND; |
69 | 0 | default: |
70 | 0 | return DT_UNKNOWN_DATA_TYPE; |
71 | 145 | } |
72 | 145 | } |
73 | | |
74 | | //! Retrieve the layer's descriptor. |
75 | | /*! |
76 | | \return |
77 | | The layer's descriptor. |
78 | | Will never be nullptr. |
79 | | */ |
80 | | std::shared_ptr<LayerDescriptor> Layer::getDescriptor() & noexcept |
81 | 243 | { |
82 | 243 | return m_pLayerDescriptor; |
83 | 243 | } |
84 | | |
85 | | //! Retrieve the layer's descriptor. |
86 | | /*! |
87 | | \return |
88 | | The layer's descriptor. |
89 | | Will never be nullptr. |
90 | | */ |
91 | | std::shared_ptr<const LayerDescriptor> Layer::getDescriptor() const & noexcept |
92 | 37 | { |
93 | 37 | return m_pLayerDescriptor; |
94 | 37 | } |
95 | | |
96 | | //! Retrieve the size of the specified data type. |
97 | | /*! |
98 | | \param |
99 | | The type of data. |
100 | | |
101 | | \return |
102 | | The size of the specified data type. |
103 | | */ |
104 | | uint8_t Layer::getElementSize(DataType type) |
105 | 2.87k | { |
106 | 2.87k | switch (type) |
107 | 2.87k | { |
108 | 980 | case DT_FLOAT32: |
109 | 980 | return sizeof(float); |
110 | 181 | case DT_UINT32: |
111 | 181 | return sizeof(uint32_t); |
112 | 0 | case DT_UINT8: |
113 | 0 | return sizeof(uint8_t); |
114 | 46 | case DT_UINT16: |
115 | 46 | return sizeof(uint16_t); |
116 | 0 | case DT_UINT64: |
117 | 0 | return sizeof(uint64_t); |
118 | 655 | case DT_BOOLEAN: |
119 | 655 | return sizeof(bool); |
120 | 1.00k | case DT_STRING: |
121 | 1.00k | return sizeof(char*); |
122 | 1 | case DT_COMPOUND: //[[fallthrough]] |
123 | 1 | case DT_UNKNOWN_DATA_TYPE: //[[fallthrough]] |
124 | 3 | default: |
125 | 3 | throw UnsupportedElementSize{}; |
126 | 2.87k | } |
127 | 2.87k | } |
128 | | |
129 | | //! Retrieve the HDF5 path of the specified layer. |
130 | | /*! |
131 | | \param layerType |
132 | | The type of layer. |
133 | | \param groupType |
134 | | The type of group. |
135 | | NODE or ELEVATION. |
136 | | |
137 | | \return |
138 | | The HDF5 path to the specified layer and group types. |
139 | | */ |
140 | | std::string Layer::getInternalPath( |
141 | | LayerType layerType, |
142 | | GroupType groupType) |
143 | 2.13k | { |
144 | 2.13k | if (groupType == NODE) |
145 | 0 | return NODE_GROUP_PATH; |
146 | | |
147 | 2.13k | if (groupType == ELEVATION) |
148 | 0 | return ELEVATION_SOLUTION_GROUP_PATH; |
149 | | |
150 | 2.13k | if (groupType != UNKNOWN_GROUP_TYPE) |
151 | 0 | throw UnsupportedGroupType{}; |
152 | | |
153 | 2.13k | switch (layerType) |
154 | 2.13k | { |
155 | 322 | case Elevation: |
156 | 322 | return ELEVATION_PATH; |
157 | 240 | case Uncertainty: |
158 | 240 | return UNCERTAINTY_PATH; |
159 | 216 | case Hypothesis_Strength: |
160 | 216 | return HYPOTHESIS_STRENGTH_PATH; |
161 | 216 | case Num_Hypotheses: |
162 | 216 | return NUM_HYPOTHESES_PATH; |
163 | 216 | case Shoal_Elevation: |
164 | 216 | return SHOAL_ELEVATION_PATH; |
165 | 216 | case Std_Dev: |
166 | 216 | return STANDARD_DEV_PATH; |
167 | 216 | case Num_Soundings: |
168 | 216 | return NUM_SOUNDINGS_PATH; |
169 | 216 | case Average_Elevation: |
170 | 216 | return AVERAGE_PATH; |
171 | 231 | case Nominal_Elevation: |
172 | 231 | return NOMINAL_ELEVATION_PATH; |
173 | 50 | case Surface_Correction: |
174 | 50 | return VERT_DATUM_CORR_PATH; |
175 | 0 | case Georef_Metadata: // [[fallthrough]]; |
176 | 0 | case UNKNOWN_LAYER_TYPE: // [[fallthrough]]; |
177 | 0 | default: |
178 | 0 | throw UnsupportedLayerType{}; |
179 | 2.13k | } |
180 | 2.13k | } |
181 | | |
182 | | //! Read a section of data from this layer. |
183 | | /*! |
184 | | Read data from this layer starting at rowStart, columnStart, and continue |
185 | | until rowEnd, columnEnd (inclusive). |
186 | | |
187 | | \param rowStart |
188 | | The starting row. |
189 | | \param columnStart |
190 | | The starting column. |
191 | | \param rowEnd |
192 | | The ending row (inclusive). |
193 | | \param columnEnd |
194 | | The ending column (inclusive). |
195 | | |
196 | | \return |
197 | | The section of data specified by the rows and columns. |
198 | | */ |
199 | | UInt8Array Layer::read( |
200 | | uint32_t rowStart, |
201 | | uint32_t columnStart, |
202 | | uint32_t rowEnd, |
203 | | uint32_t columnEnd) const |
204 | 0 | { |
205 | 0 | if (rowStart > rowEnd || columnStart > columnEnd) |
206 | 0 | throw InvalidReadSize{}; |
207 | | |
208 | 0 | if (m_pBagDataset.expired()) |
209 | 0 | throw DatasetNotFound{}; |
210 | | |
211 | 0 | const auto pDataset = m_pBagDataset.lock(); |
212 | 0 | uint32_t numRows = 0, numColumns = 0; |
213 | 0 | std::tie(numRows, numColumns) = pDataset->getDescriptor().getDims(); |
214 | |
|
215 | 0 | if (columnEnd >= numColumns || rowEnd >= numRows) |
216 | 0 | throw InvalidReadSize{}; |
217 | | |
218 | 0 | return this->readProxy(rowStart, columnStart, rowEnd, columnEnd); |
219 | 0 | } |
220 | | |
221 | | //! Write a section of data to this layer. |
222 | | /*! |
223 | | Write data to this layer starting at rowStart, columnStart, and continue |
224 | | until rowEnd, columnEnd (inclusive). |
225 | | |
226 | | After writing the data, write the attributes. |
227 | | |
228 | | \param rowStart |
229 | | The starting row. |
230 | | \param columnStart |
231 | | The starting column. |
232 | | \param rowEnd |
233 | | The ending row (inclusive). |
234 | | \param columnEnd |
235 | | The ending column (inclusive). |
236 | | \param buffer |
237 | | The data to be written. |
238 | | It must contain at least (rowEnd - rowStart + 1) * (columnEnd - columnStart + 1) elements! |
239 | | */ |
240 | | void Layer::write( |
241 | | uint32_t rowStart, |
242 | | uint32_t columnStart, |
243 | | uint32_t rowEnd, |
244 | | uint32_t columnEnd, |
245 | | const uint8_t* buffer) |
246 | 0 | { |
247 | 0 | if (m_pBagDataset.expired()) |
248 | 0 | throw DatasetNotFound{}; |
249 | | |
250 | 0 | if (rowStart > rowEnd || columnStart > columnEnd) |
251 | 0 | throw InvalidWriteSize{}; |
252 | | |
253 | 0 | if (!buffer) |
254 | 0 | throw InvalidBuffer{}; |
255 | | |
256 | 0 | this->writeProxy(rowStart, columnStart, rowEnd, columnEnd, buffer); |
257 | 0 | this->writeAttributesProxy(); |
258 | 0 | } |
259 | | |
260 | | //! Write the attributes this layer contains to disk. |
261 | | void Layer::writeAttributes() const |
262 | 0 | { |
263 | 0 | if (m_pBagDataset.expired()) |
264 | 0 | throw DatasetNotFound{}; |
265 | | |
266 | 0 | this->writeAttributesProxy(); |
267 | 0 | } |
268 | | |
269 | | } // namespace BAG |
270 | | |