/src/gdal/gcore/gdalmultidim.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Name: gdalmultidim.cpp |
4 | | * Project: GDAL Core |
5 | | * Purpose: GDAL Core C++/Private implementation for multidimensional support |
6 | | * Author: Even Rouault <even.rouault at spatialys.com> |
7 | | * |
8 | | ****************************************************************************** |
9 | | * Copyright (c) 2019, Even Rouault <even.rouault at spatialys.com> |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #include <assert.h> |
15 | | #include <algorithm> |
16 | | #include <limits> |
17 | | #include <list> |
18 | | #include <queue> |
19 | | #include <set> |
20 | | #include <utility> |
21 | | #include <time.h> |
22 | | |
23 | | #include <cmath> |
24 | | #include <ctype.h> // isalnum |
25 | | |
26 | | #include "cpl_error_internal.h" |
27 | | #include "cpl_float.h" |
28 | | #include "gdal_priv.h" |
29 | | #include "gdal_pam.h" |
30 | | #include "gdal_pam_multidim.h" |
31 | | #include "gdal_rat.h" |
32 | | #include "gdal_utils.h" |
33 | | #include "cpl_safemaths.hpp" |
34 | | #include "memmultidim.h" |
35 | | #include "ogrsf_frmts.h" |
36 | | #include "gdalmultidim_priv.h" |
37 | | |
38 | | #if defined(__clang__) || defined(_MSC_VER) |
39 | | #define COMPILER_WARNS_ABOUT_ABSTRACT_VBASE_INIT |
40 | | #endif |
41 | | |
42 | | /************************************************************************/ |
43 | | /* GDALMDArrayUnscaled */ |
44 | | /************************************************************************/ |
45 | | |
46 | | class GDALMDArrayUnscaled final : public GDALPamMDArray |
47 | | { |
48 | | private: |
49 | | std::shared_ptr<GDALMDArray> m_poParent{}; |
50 | | const GDALExtendedDataType m_dt; |
51 | | bool m_bHasNoData; |
52 | | const double m_dfScale; |
53 | | const double m_dfOffset; |
54 | | std::vector<GByte> m_abyRawNoData{}; |
55 | | |
56 | | protected: |
57 | | explicit GDALMDArrayUnscaled(const std::shared_ptr<GDALMDArray> &poParent, |
58 | | double dfScale, double dfOffset, |
59 | | double dfOverriddenDstNodata, GDALDataType eDT) |
60 | 0 | : GDALAbstractMDArray(std::string(), |
61 | 0 | "Unscaled view of " + poParent->GetFullName()), |
62 | 0 | GDALPamMDArray( |
63 | 0 | std::string(), "Unscaled view of " + poParent->GetFullName(), |
64 | 0 | GDALPamMultiDim::GetPAM(poParent), poParent->GetContext()), |
65 | 0 | m_poParent(std::move(poParent)), |
66 | 0 | m_dt(GDALExtendedDataType::Create(eDT)), |
67 | 0 | m_bHasNoData(m_poParent->GetRawNoDataValue() != nullptr), |
68 | 0 | m_dfScale(dfScale), m_dfOffset(dfOffset) |
69 | 0 | { |
70 | 0 | m_abyRawNoData.resize(m_dt.GetSize()); |
71 | 0 | const auto eNonComplexDT = |
72 | 0 | GDALGetNonComplexDataType(m_dt.GetNumericDataType()); |
73 | 0 | GDALCopyWords64( |
74 | 0 | &dfOverriddenDstNodata, GDT_Float64, 0, m_abyRawNoData.data(), |
75 | 0 | eNonComplexDT, GDALGetDataTypeSizeBytes(eNonComplexDT), |
76 | 0 | GDALDataTypeIsComplex(m_dt.GetNumericDataType()) ? 2 : 1); |
77 | 0 | } |
78 | | |
79 | | bool IRead(const GUInt64 *arrayStartIdx, const size_t *count, |
80 | | const GInt64 *arrayStep, const GPtrDiff_t *bufferStride, |
81 | | const GDALExtendedDataType &bufferDataType, |
82 | | void *pDstBuffer) const override; |
83 | | |
84 | | bool IWrite(const GUInt64 *arrayStartIdx, const size_t *count, |
85 | | const GInt64 *arrayStep, const GPtrDiff_t *bufferStride, |
86 | | const GDALExtendedDataType &bufferDataType, |
87 | | const void *pSrcBuffer) override; |
88 | | |
89 | | bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count, |
90 | | CSLConstList papszOptions) const override |
91 | 0 | { |
92 | 0 | return m_poParent->AdviseRead(arrayStartIdx, count, papszOptions); |
93 | 0 | } |
94 | | |
95 | | public: |
96 | | static std::shared_ptr<GDALMDArrayUnscaled> |
97 | | Create(const std::shared_ptr<GDALMDArray> &poParent, double dfScale, |
98 | | double dfOffset, double dfDstNodata, GDALDataType eDT) |
99 | 0 | { |
100 | 0 | auto newAr(std::shared_ptr<GDALMDArrayUnscaled>(new GDALMDArrayUnscaled( |
101 | 0 | poParent, dfScale, dfOffset, dfDstNodata, eDT))); |
102 | 0 | newAr->SetSelf(newAr); |
103 | 0 | return newAr; |
104 | 0 | } |
105 | | |
106 | | bool IsWritable() const override |
107 | 0 | { |
108 | 0 | return m_poParent->IsWritable(); |
109 | 0 | } |
110 | | |
111 | | const std::string &GetFilename() const override |
112 | 0 | { |
113 | 0 | return m_poParent->GetFilename(); |
114 | 0 | } |
115 | | |
116 | | const std::vector<std::shared_ptr<GDALDimension>> & |
117 | | GetDimensions() const override |
118 | 0 | { |
119 | 0 | return m_poParent->GetDimensions(); |
120 | 0 | } |
121 | | |
122 | | const GDALExtendedDataType &GetDataType() const override |
123 | 0 | { |
124 | 0 | return m_dt; |
125 | 0 | } |
126 | | |
127 | | const std::string &GetUnit() const override |
128 | 0 | { |
129 | 0 | return m_poParent->GetUnit(); |
130 | 0 | } |
131 | | |
132 | | std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override |
133 | 0 | { |
134 | 0 | return m_poParent->GetSpatialRef(); |
135 | 0 | } |
136 | | |
137 | | const void *GetRawNoDataValue() const override |
138 | 0 | { |
139 | 0 | return m_bHasNoData ? m_abyRawNoData.data() : nullptr; |
140 | 0 | } |
141 | | |
142 | | bool SetRawNoDataValue(const void *pRawNoData) override |
143 | 0 | { |
144 | 0 | m_bHasNoData = true; |
145 | 0 | memcpy(m_abyRawNoData.data(), pRawNoData, m_dt.GetSize()); |
146 | 0 | return true; |
147 | 0 | } |
148 | | |
149 | | std::vector<GUInt64> GetBlockSize() const override |
150 | 0 | { |
151 | 0 | return m_poParent->GetBlockSize(); |
152 | 0 | } |
153 | | |
154 | | std::shared_ptr<GDALAttribute> |
155 | | GetAttribute(const std::string &osName) const override |
156 | 0 | { |
157 | 0 | return m_poParent->GetAttribute(osName); |
158 | 0 | } |
159 | | |
160 | | std::vector<std::shared_ptr<GDALAttribute>> |
161 | | GetAttributes(CSLConstList papszOptions = nullptr) const override |
162 | 0 | { |
163 | 0 | return m_poParent->GetAttributes(papszOptions); |
164 | 0 | } |
165 | | |
166 | | bool SetUnit(const std::string &osUnit) override |
167 | 0 | { |
168 | 0 | return m_poParent->SetUnit(osUnit); |
169 | 0 | } |
170 | | |
171 | | bool SetSpatialRef(const OGRSpatialReference *poSRS) override |
172 | 0 | { |
173 | 0 | return m_poParent->SetSpatialRef(poSRS); |
174 | 0 | } |
175 | | |
176 | | std::shared_ptr<GDALAttribute> |
177 | | CreateAttribute(const std::string &osName, |
178 | | const std::vector<GUInt64> &anDimensions, |
179 | | const GDALExtendedDataType &oDataType, |
180 | | CSLConstList papszOptions = nullptr) override |
181 | 0 | { |
182 | 0 | return m_poParent->CreateAttribute(osName, anDimensions, oDataType, |
183 | 0 | papszOptions); |
184 | 0 | } |
185 | | }; |
186 | | |
187 | | /************************************************************************/ |
188 | | /* ~GDALIHasAttribute() */ |
189 | | /************************************************************************/ |
190 | | |
191 | 0 | GDALIHasAttribute::~GDALIHasAttribute() = default; |
192 | | |
193 | | /************************************************************************/ |
194 | | /* GetAttribute() */ |
195 | | /************************************************************************/ |
196 | | |
197 | | /** Return an attribute by its name. |
198 | | * |
199 | | * If the attribute does not exist, nullptr should be silently returned. |
200 | | * |
201 | | * @note Driver implementation: this method will fallback to |
202 | | * GetAttributeFromAttributes() is not explicitly implemented |
203 | | * |
204 | | * Drivers known to implement it for groups and arrays: MEM, netCDF. |
205 | | * |
206 | | * This is the same as the C function GDALGroupGetAttribute() or |
207 | | * GDALMDArrayGetAttribute(). |
208 | | * |
209 | | * @param osName Attribute name |
210 | | * @return the attribute, or nullptr if it does not exist or an error occurred. |
211 | | */ |
212 | | std::shared_ptr<GDALAttribute> |
213 | | GDALIHasAttribute::GetAttribute(const std::string &osName) const |
214 | 0 | { |
215 | 0 | return GetAttributeFromAttributes(osName); |
216 | 0 | } |
217 | | |
218 | | /************************************************************************/ |
219 | | /* GetAttributeFromAttributes() */ |
220 | | /************************************************************************/ |
221 | | |
222 | | /** Possible fallback implementation for GetAttribute() using GetAttributes(). |
223 | | */ |
224 | | std::shared_ptr<GDALAttribute> |
225 | | GDALIHasAttribute::GetAttributeFromAttributes(const std::string &osName) const |
226 | 0 | { |
227 | 0 | auto attrs(GetAttributes()); |
228 | 0 | for (const auto &attr : attrs) |
229 | 0 | { |
230 | 0 | if (attr->GetName() == osName) |
231 | 0 | return attr; |
232 | 0 | } |
233 | 0 | return nullptr; |
234 | 0 | } |
235 | | |
236 | | /************************************************************************/ |
237 | | /* GetAttributes() */ |
238 | | /************************************************************************/ |
239 | | |
240 | | /** Return the list of attributes contained in a GDALMDArray or GDALGroup. |
241 | | * |
242 | | * If the attribute does not exist, nullptr should be silently returned. |
243 | | * |
244 | | * @note Driver implementation: optionally implemented. If implemented, |
245 | | * GetAttribute() should also be implemented. |
246 | | * |
247 | | * Drivers known to implement it for groups and arrays: MEM, netCDF. |
248 | | * |
249 | | * This is the same as the C function GDALGroupGetAttributes() or |
250 | | * GDALMDArrayGetAttributes(). |
251 | | |
252 | | * @param papszOptions Driver specific options determining how attributes |
253 | | * should be retrieved. Pass nullptr for default behavior. |
254 | | * |
255 | | * @return the attributes. |
256 | | */ |
257 | | std::vector<std::shared_ptr<GDALAttribute>> |
258 | | GDALIHasAttribute::GetAttributes(CPL_UNUSED CSLConstList papszOptions) const |
259 | 0 | { |
260 | 0 | return {}; |
261 | 0 | } |
262 | | |
263 | | /************************************************************************/ |
264 | | /* CreateAttribute() */ |
265 | | /************************************************************************/ |
266 | | |
267 | | /** Create an attribute within a GDALMDArray or GDALGroup. |
268 | | * |
269 | | * The attribute might not be "physically" created until a value is written |
270 | | * into it. |
271 | | * |
272 | | * Optionally implemented. |
273 | | * |
274 | | * Drivers known to implement it: MEM, netCDF |
275 | | * |
276 | | * This is the same as the C function GDALGroupCreateAttribute() or |
277 | | * GDALMDArrayCreateAttribute() |
278 | | * |
279 | | * @param osName Attribute name. |
280 | | * @param anDimensions List of dimension sizes, ordered from the slowest varying |
281 | | * dimension first to the fastest varying dimension last. |
282 | | * Empty for a scalar attribute (common case) |
283 | | * @param oDataType Attribute data type. |
284 | | * @param papszOptions Driver specific options determining how the attribute. |
285 | | * should be created. |
286 | | * |
287 | | * @return the new attribute, or nullptr if case of error |
288 | | */ |
289 | | std::shared_ptr<GDALAttribute> GDALIHasAttribute::CreateAttribute( |
290 | | CPL_UNUSED const std::string &osName, |
291 | | CPL_UNUSED const std::vector<GUInt64> &anDimensions, |
292 | | CPL_UNUSED const GDALExtendedDataType &oDataType, |
293 | | CPL_UNUSED CSLConstList papszOptions) |
294 | 0 | { |
295 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
296 | 0 | "CreateAttribute() not implemented"); |
297 | 0 | return nullptr; |
298 | 0 | } |
299 | | |
300 | | /************************************************************************/ |
301 | | /* DeleteAttribute() */ |
302 | | /************************************************************************/ |
303 | | |
304 | | /** Delete an attribute from a GDALMDArray or GDALGroup. |
305 | | * |
306 | | * Optionally implemented. |
307 | | * |
308 | | * After this call, if a previously obtained instance of the deleted object |
309 | | * is still alive, no method other than for freeing it should be invoked. |
310 | | * |
311 | | * Drivers known to implement it: MEM, netCDF |
312 | | * |
313 | | * This is the same as the C function GDALGroupDeleteAttribute() or |
314 | | * GDALMDArrayDeleteAttribute() |
315 | | * |
316 | | * @param osName Attribute name. |
317 | | * @param papszOptions Driver specific options determining how the attribute. |
318 | | * should be deleted. |
319 | | * |
320 | | * @return true in case of success |
321 | | * @since GDAL 3.8 |
322 | | */ |
323 | | bool GDALIHasAttribute::DeleteAttribute(CPL_UNUSED const std::string &osName, |
324 | | CPL_UNUSED CSLConstList papszOptions) |
325 | 0 | { |
326 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
327 | 0 | "DeleteAttribute() not implemented"); |
328 | 0 | return false; |
329 | 0 | } |
330 | | |
331 | | /************************************************************************/ |
332 | | /* GDALGroup() */ |
333 | | /************************************************************************/ |
334 | | |
335 | | //! @cond Doxygen_Suppress |
336 | | GDALGroup::GDALGroup(const std::string &osParentName, const std::string &osName, |
337 | | const std::string &osContext) |
338 | 0 | : m_osName(osParentName.empty() ? "/" : osName), |
339 | | m_osFullName( |
340 | 0 | !osParentName.empty() |
341 | 0 | ? ((osParentName == "/" ? "/" : osParentName + "/") + osName) |
342 | 0 | : "/"), |
343 | 0 | m_osContext(osContext) |
344 | 0 | { |
345 | 0 | } |
346 | | |
347 | | //! @endcond |
348 | | |
349 | | /************************************************************************/ |
350 | | /* ~GDALGroup() */ |
351 | | /************************************************************************/ |
352 | | |
353 | 0 | GDALGroup::~GDALGroup() = default; |
354 | | |
355 | | /************************************************************************/ |
356 | | /* GetMDArrayNames() */ |
357 | | /************************************************************************/ |
358 | | |
359 | | /** Return the list of multidimensional array names contained in this group. |
360 | | * |
361 | | * @note Driver implementation: optionally implemented. If implemented, |
362 | | * OpenMDArray() should also be implemented. |
363 | | * |
364 | | * Drivers known to implement it: MEM, netCDF. |
365 | | * |
366 | | * This is the same as the C function GDALGroupGetMDArrayNames(). |
367 | | * |
368 | | * @param papszOptions Driver specific options determining how arrays |
369 | | * should be retrieved. Pass nullptr for default behavior. |
370 | | * |
371 | | * @return the array names. |
372 | | */ |
373 | | std::vector<std::string> |
374 | | GDALGroup::GetMDArrayNames(CPL_UNUSED CSLConstList papszOptions) const |
375 | 0 | { |
376 | 0 | return {}; |
377 | 0 | } |
378 | | |
379 | | /************************************************************************/ |
380 | | /* GetMDArrayFullNamesRecursive() */ |
381 | | /************************************************************************/ |
382 | | |
383 | | /** Return the list of multidimensional array full names contained in this |
384 | | * group and its subgroups. |
385 | | * |
386 | | * This is the same as the C function GDALGroupGetMDArrayFullNamesRecursive(). |
387 | | * |
388 | | * @param papszGroupOptions Driver specific options determining how groups |
389 | | * should be retrieved. Pass nullptr for default behavior. |
390 | | * @param papszArrayOptions Driver specific options determining how arrays |
391 | | * should be retrieved. Pass nullptr for default behavior. |
392 | | * |
393 | | * @return the array full names. |
394 | | * |
395 | | * @since 3.11 |
396 | | */ |
397 | | std::vector<std::string> |
398 | | GDALGroup::GetMDArrayFullNamesRecursive(CSLConstList papszGroupOptions, |
399 | | CSLConstList papszArrayOptions) const |
400 | 0 | { |
401 | 0 | std::vector<std::string> ret; |
402 | 0 | std::list<std::shared_ptr<GDALGroup>> stackGroups; |
403 | 0 | stackGroups.push_back(nullptr); // nullptr means this |
404 | 0 | while (!stackGroups.empty()) |
405 | 0 | { |
406 | 0 | std::shared_ptr<GDALGroup> groupPtr = std::move(stackGroups.front()); |
407 | 0 | stackGroups.erase(stackGroups.begin()); |
408 | 0 | const GDALGroup *poCurGroup = groupPtr ? groupPtr.get() : this; |
409 | 0 | for (const std::string &arrayName : |
410 | 0 | poCurGroup->GetMDArrayNames(papszArrayOptions)) |
411 | 0 | { |
412 | 0 | std::string osFullName = poCurGroup->GetFullName(); |
413 | 0 | if (!osFullName.empty() && osFullName.back() != '/') |
414 | 0 | osFullName += '/'; |
415 | 0 | osFullName += arrayName; |
416 | 0 | ret.push_back(std::move(osFullName)); |
417 | 0 | } |
418 | 0 | auto insertionPoint = stackGroups.begin(); |
419 | 0 | for (const auto &osSubGroup : |
420 | 0 | poCurGroup->GetGroupNames(papszGroupOptions)) |
421 | 0 | { |
422 | 0 | auto poSubGroup = poCurGroup->OpenGroup(osSubGroup); |
423 | 0 | if (poSubGroup) |
424 | 0 | stackGroups.insert(insertionPoint, std::move(poSubGroup)); |
425 | 0 | } |
426 | 0 | } |
427 | |
|
428 | 0 | return ret; |
429 | 0 | } |
430 | | |
431 | | /************************************************************************/ |
432 | | /* OpenMDArray() */ |
433 | | /************************************************************************/ |
434 | | |
435 | | /** Open and return a multidimensional array. |
436 | | * |
437 | | * @note Driver implementation: optionally implemented. If implemented, |
438 | | * GetMDArrayNames() should also be implemented. |
439 | | * |
440 | | * Drivers known to implement it: MEM, netCDF. |
441 | | * |
442 | | * This is the same as the C function GDALGroupOpenMDArray(). |
443 | | * |
444 | | * @param osName Array name. |
445 | | * @param papszOptions Driver specific options determining how the array should |
446 | | * be opened. Pass nullptr for default behavior. |
447 | | * |
448 | | * @return the array, or nullptr. |
449 | | */ |
450 | | std::shared_ptr<GDALMDArray> |
451 | | GDALGroup::OpenMDArray(CPL_UNUSED const std::string &osName, |
452 | | CPL_UNUSED CSLConstList papszOptions) const |
453 | 0 | { |
454 | 0 | return nullptr; |
455 | 0 | } |
456 | | |
457 | | /************************************************************************/ |
458 | | /* GetGroupNames() */ |
459 | | /************************************************************************/ |
460 | | |
461 | | /** Return the list of sub-groups contained in this group. |
462 | | * |
463 | | * @note Driver implementation: optionally implemented. If implemented, |
464 | | * OpenGroup() should also be implemented. |
465 | | * |
466 | | * Drivers known to implement it: MEM, netCDF. |
467 | | * |
468 | | * This is the same as the C function GDALGroupGetGroupNames(). |
469 | | * |
470 | | * @param papszOptions Driver specific options determining how groups |
471 | | * should be retrieved. Pass nullptr for default behavior. |
472 | | * |
473 | | * @return the group names. |
474 | | */ |
475 | | std::vector<std::string> |
476 | | GDALGroup::GetGroupNames(CPL_UNUSED CSLConstList papszOptions) const |
477 | 0 | { |
478 | 0 | return {}; |
479 | 0 | } |
480 | | |
481 | | /************************************************************************/ |
482 | | /* OpenGroup() */ |
483 | | /************************************************************************/ |
484 | | |
485 | | /** Open and return a sub-group. |
486 | | * |
487 | | * @note Driver implementation: optionally implemented. If implemented, |
488 | | * GetGroupNames() should also be implemented. |
489 | | * |
490 | | * Drivers known to implement it: MEM, netCDF. |
491 | | * |
492 | | * This is the same as the C function GDALGroupOpenGroup(). |
493 | | * |
494 | | * @param osName Sub-group name. |
495 | | * @param papszOptions Driver specific options determining how the sub-group |
496 | | * should be opened. Pass nullptr for default behavior. |
497 | | * |
498 | | * @return the group, or nullptr. |
499 | | */ |
500 | | std::shared_ptr<GDALGroup> |
501 | | GDALGroup::OpenGroup(CPL_UNUSED const std::string &osName, |
502 | | CPL_UNUSED CSLConstList papszOptions) const |
503 | 0 | { |
504 | 0 | return nullptr; |
505 | 0 | } |
506 | | |
507 | | /************************************************************************/ |
508 | | /* GetVectorLayerNames() */ |
509 | | /************************************************************************/ |
510 | | |
511 | | /** Return the list of layer names contained in this group. |
512 | | * |
513 | | * @note Driver implementation: optionally implemented. If implemented, |
514 | | * OpenVectorLayer() should also be implemented. |
515 | | * |
516 | | * Drivers known to implement it: OpenFileGDB, FileGDB |
517 | | * |
518 | | * Other drivers will return an empty list. GDALDataset::GetLayerCount() and |
519 | | * GDALDataset::GetLayer() should then be used. |
520 | | * |
521 | | * This is the same as the C function GDALGroupGetVectorLayerNames(). |
522 | | * |
523 | | * @param papszOptions Driver specific options determining how layers |
524 | | * should be retrieved. Pass nullptr for default behavior. |
525 | | * |
526 | | * @return the vector layer names. |
527 | | * @since GDAL 3.4 |
528 | | */ |
529 | | std::vector<std::string> |
530 | | GDALGroup::GetVectorLayerNames(CPL_UNUSED CSLConstList papszOptions) const |
531 | 0 | { |
532 | 0 | return {}; |
533 | 0 | } |
534 | | |
535 | | /************************************************************************/ |
536 | | /* OpenVectorLayer() */ |
537 | | /************************************************************************/ |
538 | | |
539 | | /** Open and return a vector layer. |
540 | | * |
541 | | * Due to the historical ownership of OGRLayer* by GDALDataset*, the |
542 | | * lifetime of the returned OGRLayer* is linked to the one of the owner |
543 | | * dataset (contrary to the general design of this class where objects can be |
544 | | * used independently of the object that returned them) |
545 | | * |
546 | | * @note Driver implementation: optionally implemented. If implemented, |
547 | | * GetVectorLayerNames() should also be implemented. |
548 | | * |
549 | | * Drivers known to implement it: MEM, netCDF. |
550 | | * |
551 | | * This is the same as the C function GDALGroupOpenVectorLayer(). |
552 | | * |
553 | | * @param osName Vector layer name. |
554 | | * @param papszOptions Driver specific options determining how the layer should |
555 | | * be opened. Pass nullptr for default behavior. |
556 | | * |
557 | | * @return the group, or nullptr. |
558 | | */ |
559 | | OGRLayer *GDALGroup::OpenVectorLayer(CPL_UNUSED const std::string &osName, |
560 | | CPL_UNUSED CSLConstList papszOptions) const |
561 | 0 | { |
562 | 0 | return nullptr; |
563 | 0 | } |
564 | | |
565 | | /************************************************************************/ |
566 | | /* GetDimensions() */ |
567 | | /************************************************************************/ |
568 | | |
569 | | /** Return the list of dimensions contained in this group and used by its |
570 | | * arrays. |
571 | | * |
572 | | * This is for dimensions that can potentially be used by several arrays. |
573 | | * Not all drivers might implement this. To retrieve the dimensions used by |
574 | | * a specific array, use GDALMDArray::GetDimensions(). |
575 | | * |
576 | | * Drivers known to implement it: MEM, netCDF |
577 | | * |
578 | | * This is the same as the C function GDALGroupGetDimensions(). |
579 | | * |
580 | | * @param papszOptions Driver specific options determining how groups |
581 | | * should be retrieved. Pass nullptr for default behavior. |
582 | | * |
583 | | * @return the dimensions. |
584 | | */ |
585 | | std::vector<std::shared_ptr<GDALDimension>> |
586 | | GDALGroup::GetDimensions(CPL_UNUSED CSLConstList papszOptions) const |
587 | 0 | { |
588 | 0 | return {}; |
589 | 0 | } |
590 | | |
591 | | /************************************************************************/ |
592 | | /* GetStructuralInfo() */ |
593 | | /************************************************************************/ |
594 | | |
595 | | /** Return structural information on the group. |
596 | | * |
597 | | * This may be the compression, etc.. |
598 | | * |
599 | | * The return value should not be freed and is valid until GDALGroup is |
600 | | * released or this function called again. |
601 | | * |
602 | | * This is the same as the C function GDALGroupGetStructuralInfo(). |
603 | | */ |
604 | | CSLConstList GDALGroup::GetStructuralInfo() const |
605 | 0 | { |
606 | 0 | return nullptr; |
607 | 0 | } |
608 | | |
609 | | /************************************************************************/ |
610 | | /* CreateGroup() */ |
611 | | /************************************************************************/ |
612 | | |
613 | | /** Create a sub-group within a group. |
614 | | * |
615 | | * Optionally implemented by drivers. |
616 | | * |
617 | | * Drivers known to implement it: MEM, netCDF |
618 | | * |
619 | | * This is the same as the C function GDALGroupCreateGroup(). |
620 | | * |
621 | | * @param osName Sub-group name. |
622 | | * @param papszOptions Driver specific options determining how the sub-group |
623 | | * should be created. |
624 | | * |
625 | | * @return the new sub-group, or nullptr in case of error. |
626 | | */ |
627 | | std::shared_ptr<GDALGroup> |
628 | | GDALGroup::CreateGroup(CPL_UNUSED const std::string &osName, |
629 | | CPL_UNUSED CSLConstList papszOptions) |
630 | 0 | { |
631 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "CreateGroup() not implemented"); |
632 | 0 | return nullptr; |
633 | 0 | } |
634 | | |
635 | | /************************************************************************/ |
636 | | /* DeleteGroup() */ |
637 | | /************************************************************************/ |
638 | | |
639 | | /** Delete a sub-group from a group. |
640 | | * |
641 | | * Optionally implemented. |
642 | | * |
643 | | * After this call, if a previously obtained instance of the deleted object |
644 | | * is still alive, no method other than for freeing it should be invoked. |
645 | | * |
646 | | * Drivers known to implement it: MEM, Zarr |
647 | | * |
648 | | * This is the same as the C function GDALGroupDeleteGroup(). |
649 | | * |
650 | | * @param osName Sub-group name. |
651 | | * @param papszOptions Driver specific options determining how the group. |
652 | | * should be deleted. |
653 | | * |
654 | | * @return true in case of success |
655 | | * @since GDAL 3.8 |
656 | | */ |
657 | | bool GDALGroup::DeleteGroup(CPL_UNUSED const std::string &osName, |
658 | | CPL_UNUSED CSLConstList papszOptions) |
659 | 0 | { |
660 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "DeleteGroup() not implemented"); |
661 | 0 | return false; |
662 | 0 | } |
663 | | |
664 | | /************************************************************************/ |
665 | | /* CreateDimension() */ |
666 | | /************************************************************************/ |
667 | | |
668 | | /** Create a dimension within a group. |
669 | | * |
670 | | * @note Driver implementation: drivers supporting CreateDimension() should |
671 | | * implement this method, but do not have necessarily to implement |
672 | | * GDALGroup::GetDimensions(). |
673 | | * |
674 | | * Drivers known to implement it: MEM, netCDF |
675 | | * |
676 | | * This is the same as the C function GDALGroupCreateDimension(). |
677 | | * |
678 | | * @param osName Dimension name. |
679 | | * @param osType Dimension type (might be empty, and ignored by drivers) |
680 | | * @param osDirection Dimension direction (might be empty, and ignored by |
681 | | * drivers) |
682 | | * @param nSize Number of values indexed by this dimension. Should be > 0. |
683 | | * @param papszOptions Driver specific options determining how the dimension |
684 | | * should be created. |
685 | | * |
686 | | * @return the new dimension, or nullptr if case of error |
687 | | */ |
688 | | std::shared_ptr<GDALDimension> GDALGroup::CreateDimension( |
689 | | CPL_UNUSED const std::string &osName, CPL_UNUSED const std::string &osType, |
690 | | CPL_UNUSED const std::string &osDirection, CPL_UNUSED GUInt64 nSize, |
691 | | CPL_UNUSED CSLConstList papszOptions) |
692 | 0 | { |
693 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
694 | 0 | "CreateDimension() not implemented"); |
695 | 0 | return nullptr; |
696 | 0 | } |
697 | | |
698 | | /************************************************************************/ |
699 | | /* CreateMDArray() */ |
700 | | /************************************************************************/ |
701 | | |
702 | | /** Create a multidimensional array within a group. |
703 | | * |
704 | | * It is recommended that the GDALDimension objects passed in aoDimensions |
705 | | * belong to this group, either by retrieving them with GetDimensions() |
706 | | * or creating a new one with CreateDimension(). |
707 | | * |
708 | | * Optionally implemented. |
709 | | * |
710 | | * Drivers known to implement it: MEM, netCDF |
711 | | * |
712 | | * This is the same as the C function GDALGroupCreateMDArray(). |
713 | | * |
714 | | * @note Driver implementation: drivers should take into account the possibility |
715 | | * that GDALDimension object passed in aoDimensions might belong to a different |
716 | | * group / dataset / driver and act accordingly. |
717 | | * |
718 | | * @param osName Array name. |
719 | | * @param aoDimensions List of dimensions, ordered from the slowest varying |
720 | | * dimension first to the fastest varying dimension last. |
721 | | * Might be empty for a scalar array (if supported by |
722 | | * driver) |
723 | | * @param oDataType Array data type. |
724 | | * @param papszOptions Driver specific options determining how the array |
725 | | * should be created. |
726 | | * |
727 | | * @return the new array, or nullptr in case of error |
728 | | */ |
729 | | std::shared_ptr<GDALMDArray> GDALGroup::CreateMDArray( |
730 | | CPL_UNUSED const std::string &osName, |
731 | | CPL_UNUSED const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions, |
732 | | CPL_UNUSED const GDALExtendedDataType &oDataType, |
733 | | CPL_UNUSED CSLConstList papszOptions) |
734 | 0 | { |
735 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "CreateMDArray() not implemented"); |
736 | 0 | return nullptr; |
737 | 0 | } |
738 | | |
739 | | /************************************************************************/ |
740 | | /* DeleteMDArray() */ |
741 | | /************************************************************************/ |
742 | | |
743 | | /** Delete an array from a group. |
744 | | * |
745 | | * Optionally implemented. |
746 | | * |
747 | | * After this call, if a previously obtained instance of the deleted object |
748 | | * is still alive, no method other than for freeing it should be invoked. |
749 | | * |
750 | | * Drivers known to implement it: MEM, Zarr |
751 | | * |
752 | | * This is the same as the C function GDALGroupDeleteMDArray(). |
753 | | * |
754 | | * @param osName Arrayname. |
755 | | * @param papszOptions Driver specific options determining how the array. |
756 | | * should be deleted. |
757 | | * |
758 | | * @return true in case of success |
759 | | * @since GDAL 3.8 |
760 | | */ |
761 | | bool GDALGroup::DeleteMDArray(CPL_UNUSED const std::string &osName, |
762 | | CPL_UNUSED CSLConstList papszOptions) |
763 | 0 | { |
764 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "DeleteMDArray() not implemented"); |
765 | 0 | return false; |
766 | 0 | } |
767 | | |
768 | | /************************************************************************/ |
769 | | /* GetTotalCopyCost() */ |
770 | | /************************************************************************/ |
771 | | |
772 | | /** Return a total "cost" to copy the group. |
773 | | * |
774 | | * Used as a parameter for CopFrom() |
775 | | */ |
776 | | GUInt64 GDALGroup::GetTotalCopyCost() const |
777 | 0 | { |
778 | 0 | GUInt64 nCost = COPY_COST; |
779 | 0 | nCost += GetAttributes().size() * GDALAttribute::COPY_COST; |
780 | |
|
781 | 0 | auto groupNames = GetGroupNames(); |
782 | 0 | for (const auto &name : groupNames) |
783 | 0 | { |
784 | 0 | auto subGroup = OpenGroup(name); |
785 | 0 | if (subGroup) |
786 | 0 | { |
787 | 0 | nCost += subGroup->GetTotalCopyCost(); |
788 | 0 | } |
789 | 0 | } |
790 | |
|
791 | 0 | auto arrayNames = GetMDArrayNames(); |
792 | 0 | for (const auto &name : arrayNames) |
793 | 0 | { |
794 | 0 | auto array = OpenMDArray(name); |
795 | 0 | if (array) |
796 | 0 | { |
797 | 0 | nCost += array->GetTotalCopyCost(); |
798 | 0 | } |
799 | 0 | } |
800 | 0 | return nCost; |
801 | 0 | } |
802 | | |
803 | | /************************************************************************/ |
804 | | /* CopyFrom() */ |
805 | | /************************************************************************/ |
806 | | |
807 | | /** Copy the content of a group into a new (generally empty) group. |
808 | | * |
809 | | * @param poDstRootGroup Destination root group. Must NOT be nullptr. |
810 | | * @param poSrcDS Source dataset. Might be nullptr (but for correct behavior |
811 | | * of some output drivers this is not recommended) |
812 | | * @param poSrcGroup Source group. Must NOT be nullptr. |
813 | | * @param bStrict Whether to enable strict mode. In strict mode, any error will |
814 | | * stop the copy. In relaxed mode, the copy will be attempted to |
815 | | * be pursued. |
816 | | * @param nCurCost Should be provided as a variable initially set to 0. |
817 | | * @param nTotalCost Total cost from GetTotalCopyCost(). |
818 | | * @param pfnProgress Progress callback, or nullptr. |
819 | | * @param pProgressData Progress user data, or nulptr. |
820 | | * @param papszOptions Creation options. Currently, only array creation |
821 | | * options are supported. They must be prefixed with |
822 | | * "ARRAY:" . The scope may be further restricted to arrays of a certain |
823 | | * dimension by adding "IF(DIM={ndims}):" after "ARRAY:". |
824 | | * For example, "ARRAY:IF(DIM=2):BLOCKSIZE=256,256" will |
825 | | * restrict BLOCKSIZE=256,256 to arrays of dimension 2. |
826 | | * Restriction to arrays of a given name is done with adding |
827 | | * "IF(NAME={name}):" after "ARRAY:". {name} can also be |
828 | | * a full qualified name. |
829 | | * A non-driver specific ARRAY option, "AUTOSCALE=YES" can |
830 | | * be used to ask (non indexing) variables of type Float32 or Float64 to be |
831 | | * scaled to UInt16 with scale and offset values being computed from the minimum |
832 | | * and maximum of the source array. The integer data type used can be set with |
833 | | * AUTOSCALE_DATA_TYPE=Byte/UInt16/Int16/UInt32/Int32. |
834 | | * |
835 | | * @return true in case of success (or partial success if bStrict == false). |
836 | | */ |
837 | | bool GDALGroup::CopyFrom(const std::shared_ptr<GDALGroup> &poDstRootGroup, |
838 | | GDALDataset *poSrcDS, |
839 | | const std::shared_ptr<GDALGroup> &poSrcGroup, |
840 | | bool bStrict, GUInt64 &nCurCost, |
841 | | const GUInt64 nTotalCost, GDALProgressFunc pfnProgress, |
842 | | void *pProgressData, CSLConstList papszOptions) |
843 | 0 | { |
844 | 0 | if (pfnProgress == nullptr) |
845 | 0 | pfnProgress = GDALDummyProgress; |
846 | |
|
847 | 0 | #define EXIT_OR_CONTINUE_IF_NULL(x) \ |
848 | 0 | if (!(x)) \ |
849 | 0 | { \ |
850 | 0 | if (bStrict) \ |
851 | 0 | return false; \ |
852 | 0 | continue; \ |
853 | 0 | } \ |
854 | 0 | (void)0 |
855 | |
|
856 | 0 | try |
857 | 0 | { |
858 | 0 | nCurCost += GDALGroup::COPY_COST; |
859 | |
|
860 | 0 | const auto srcDims = poSrcGroup->GetDimensions(); |
861 | 0 | std::map<std::string, std::shared_ptr<GDALDimension>> |
862 | 0 | mapExistingDstDims; |
863 | 0 | std::map<std::string, std::string> mapSrcVariableNameToIndexedDimName; |
864 | 0 | for (const auto &dim : srcDims) |
865 | 0 | { |
866 | 0 | auto dstDim = CreateDimension(dim->GetName(), dim->GetType(), |
867 | 0 | dim->GetDirection(), dim->GetSize()); |
868 | 0 | EXIT_OR_CONTINUE_IF_NULL(dstDim); |
869 | 0 | mapExistingDstDims[dim->GetName()] = std::move(dstDim); |
870 | 0 | auto poIndexingVarSrc(dim->GetIndexingVariable()); |
871 | 0 | if (poIndexingVarSrc) |
872 | 0 | { |
873 | 0 | mapSrcVariableNameToIndexedDimName[poIndexingVarSrc |
874 | 0 | ->GetName()] = |
875 | 0 | dim->GetName(); |
876 | 0 | } |
877 | 0 | } |
878 | | |
879 | 0 | auto attrs = poSrcGroup->GetAttributes(); |
880 | 0 | for (const auto &attr : attrs) |
881 | 0 | { |
882 | 0 | auto dstAttr = |
883 | 0 | CreateAttribute(attr->GetName(), attr->GetDimensionsSize(), |
884 | 0 | attr->GetDataType()); |
885 | 0 | EXIT_OR_CONTINUE_IF_NULL(dstAttr); |
886 | 0 | auto raw(attr->ReadAsRaw()); |
887 | 0 | if (!dstAttr->Write(raw.data(), raw.size()) && bStrict) |
888 | 0 | return false; |
889 | 0 | } |
890 | 0 | if (!attrs.empty()) |
891 | 0 | { |
892 | 0 | nCurCost += attrs.size() * GDALAttribute::COPY_COST; |
893 | 0 | if (!pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData)) |
894 | 0 | return false; |
895 | 0 | } |
896 | | |
897 | 0 | const auto CopyArray = |
898 | 0 | [this, &poSrcDS, &poDstRootGroup, &mapExistingDstDims, |
899 | 0 | &mapSrcVariableNameToIndexedDimName, pfnProgress, pProgressData, |
900 | 0 | papszOptions, bStrict, &nCurCost, |
901 | 0 | nTotalCost](const std::shared_ptr<GDALMDArray> &srcArray) |
902 | 0 | { |
903 | | // Map source dimensions to target dimensions |
904 | 0 | std::vector<std::shared_ptr<GDALDimension>> dstArrayDims; |
905 | 0 | const auto &srcArrayDims(srcArray->GetDimensions()); |
906 | 0 | for (const auto &dim : srcArrayDims) |
907 | 0 | { |
908 | 0 | auto dstDim = poDstRootGroup->OpenDimensionFromFullname( |
909 | 0 | dim->GetFullName()); |
910 | 0 | if (dstDim && dstDim->GetSize() == dim->GetSize()) |
911 | 0 | { |
912 | 0 | dstArrayDims.emplace_back(dstDim); |
913 | 0 | } |
914 | 0 | else |
915 | 0 | { |
916 | 0 | auto oIter = mapExistingDstDims.find(dim->GetName()); |
917 | 0 | if (oIter != mapExistingDstDims.end() && |
918 | 0 | oIter->second->GetSize() == dim->GetSize()) |
919 | 0 | { |
920 | 0 | dstArrayDims.emplace_back(oIter->second); |
921 | 0 | } |
922 | 0 | else |
923 | 0 | { |
924 | 0 | std::string newDimName; |
925 | 0 | if (oIter == mapExistingDstDims.end()) |
926 | 0 | { |
927 | 0 | newDimName = dim->GetName(); |
928 | 0 | } |
929 | 0 | else |
930 | 0 | { |
931 | 0 | std::string newDimNamePrefix(srcArray->GetName() + |
932 | 0 | '_' + dim->GetName()); |
933 | 0 | newDimName = newDimNamePrefix; |
934 | 0 | int nIterCount = 2; |
935 | 0 | while ( |
936 | 0 | cpl::contains(mapExistingDstDims, newDimName)) |
937 | 0 | { |
938 | 0 | newDimName = newDimNamePrefix + |
939 | 0 | CPLSPrintf("_%d", nIterCount); |
940 | 0 | nIterCount++; |
941 | 0 | } |
942 | 0 | } |
943 | 0 | dstDim = CreateDimension(newDimName, dim->GetType(), |
944 | 0 | dim->GetDirection(), |
945 | 0 | dim->GetSize()); |
946 | 0 | if (!dstDim) |
947 | 0 | return false; |
948 | 0 | mapExistingDstDims[newDimName] = dstDim; |
949 | 0 | dstArrayDims.emplace_back(dstDim); |
950 | 0 | } |
951 | 0 | } |
952 | 0 | } |
953 | | |
954 | 0 | CPLStringList aosArrayCO; |
955 | 0 | bool bAutoScale = false; |
956 | 0 | GDALDataType eAutoScaleType = GDT_UInt16; |
957 | 0 | for (const char *pszItem : cpl::Iterate(papszOptions)) |
958 | 0 | { |
959 | 0 | if (STARTS_WITH_CI(pszItem, "ARRAY:")) |
960 | 0 | { |
961 | 0 | const char *pszOption = pszItem + strlen("ARRAY:"); |
962 | 0 | if (STARTS_WITH_CI(pszOption, "IF(DIM=")) |
963 | 0 | { |
964 | 0 | const char *pszNext = strchr(pszOption, ':'); |
965 | 0 | if (pszNext != nullptr) |
966 | 0 | { |
967 | 0 | int nDim = atoi(pszOption + strlen("IF(DIM=")); |
968 | 0 | if (static_cast<size_t>(nDim) == |
969 | 0 | dstArrayDims.size()) |
970 | 0 | { |
971 | 0 | pszOption = pszNext + 1; |
972 | 0 | } |
973 | 0 | else |
974 | 0 | { |
975 | 0 | pszOption = nullptr; |
976 | 0 | } |
977 | 0 | } |
978 | 0 | } |
979 | 0 | else if (STARTS_WITH_CI(pszOption, "IF(NAME=")) |
980 | 0 | { |
981 | 0 | const char *pszName = pszOption + strlen("IF(NAME="); |
982 | 0 | const char *pszNext = strchr(pszName, ':'); |
983 | 0 | if (pszNext != nullptr && pszNext > pszName && |
984 | 0 | pszNext[-1] == ')') |
985 | 0 | { |
986 | 0 | CPLString osName; |
987 | 0 | osName.assign(pszName, pszNext - pszName - 1); |
988 | 0 | if (osName == srcArray->GetName() || |
989 | 0 | osName == srcArray->GetFullName()) |
990 | 0 | { |
991 | 0 | pszOption = pszNext + 1; |
992 | 0 | } |
993 | 0 | else |
994 | 0 | { |
995 | 0 | pszOption = nullptr; |
996 | 0 | } |
997 | 0 | } |
998 | 0 | } |
999 | 0 | if (pszOption) |
1000 | 0 | { |
1001 | 0 | if (STARTS_WITH_CI(pszOption, "AUTOSCALE=")) |
1002 | 0 | { |
1003 | 0 | bAutoScale = |
1004 | 0 | CPLTestBool(pszOption + strlen("AUTOSCALE=")); |
1005 | 0 | } |
1006 | 0 | else if (STARTS_WITH_CI(pszOption, |
1007 | 0 | "AUTOSCALE_DATA_TYPE=")) |
1008 | 0 | { |
1009 | 0 | const char *pszDataType = |
1010 | 0 | pszOption + strlen("AUTOSCALE_DATA_TYPE="); |
1011 | 0 | eAutoScaleType = GDALGetDataTypeByName(pszDataType); |
1012 | 0 | if (GDALDataTypeIsComplex(eAutoScaleType) || |
1013 | 0 | GDALDataTypeIsFloating(eAutoScaleType)) |
1014 | 0 | { |
1015 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
1016 | 0 | "Unsupported value for " |
1017 | 0 | "AUTOSCALE_DATA_TYPE"); |
1018 | 0 | return false; |
1019 | 0 | } |
1020 | 0 | } |
1021 | 0 | else |
1022 | 0 | { |
1023 | 0 | aosArrayCO.AddString(pszOption); |
1024 | 0 | } |
1025 | 0 | } |
1026 | 0 | } |
1027 | 0 | } |
1028 | | |
1029 | 0 | if (aosArrayCO.FetchNameValue("BLOCKSIZE") == nullptr) |
1030 | 0 | { |
1031 | 0 | const auto anBlockSize = srcArray->GetBlockSize(); |
1032 | 0 | std::string osBlockSize; |
1033 | 0 | for (auto v : anBlockSize) |
1034 | 0 | { |
1035 | 0 | if (v == 0) |
1036 | 0 | { |
1037 | 0 | osBlockSize.clear(); |
1038 | 0 | break; |
1039 | 0 | } |
1040 | 0 | if (!osBlockSize.empty()) |
1041 | 0 | osBlockSize += ','; |
1042 | 0 | osBlockSize += std::to_string(v); |
1043 | 0 | } |
1044 | 0 | if (!osBlockSize.empty()) |
1045 | 0 | aosArrayCO.SetNameValue("BLOCKSIZE", osBlockSize.c_str()); |
1046 | 0 | } |
1047 | |
|
1048 | 0 | auto oIterDimName = |
1049 | 0 | mapSrcVariableNameToIndexedDimName.find(srcArray->GetName()); |
1050 | 0 | const auto &srcArrayType = srcArray->GetDataType(); |
1051 | |
|
1052 | 0 | std::shared_ptr<GDALMDArray> dstArray; |
1053 | | |
1054 | | // Only autoscale non-indexing variables |
1055 | 0 | bool bHasOffset = false; |
1056 | 0 | bool bHasScale = false; |
1057 | 0 | if (bAutoScale && srcArrayType.GetClass() == GEDTC_NUMERIC && |
1058 | 0 | (srcArrayType.GetNumericDataType() == GDT_Float16 || |
1059 | 0 | srcArrayType.GetNumericDataType() == GDT_Float32 || |
1060 | 0 | srcArrayType.GetNumericDataType() == GDT_Float64) && |
1061 | 0 | srcArray->GetOffset(&bHasOffset) == 0.0 && !bHasOffset && |
1062 | 0 | srcArray->GetScale(&bHasScale) == 1.0 && !bHasScale && |
1063 | 0 | oIterDimName == mapSrcVariableNameToIndexedDimName.end()) |
1064 | 0 | { |
1065 | 0 | constexpr bool bApproxOK = false; |
1066 | 0 | constexpr bool bForce = true; |
1067 | 0 | double dfMin = 0.0; |
1068 | 0 | double dfMax = 0.0; |
1069 | 0 | if (srcArray->GetStatistics(bApproxOK, bForce, &dfMin, &dfMax, |
1070 | 0 | nullptr, nullptr, nullptr, nullptr, |
1071 | 0 | nullptr) != CE_None) |
1072 | 0 | { |
1073 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1074 | 0 | "Could not retrieve statistics for array %s", |
1075 | 0 | srcArray->GetName().c_str()); |
1076 | 0 | return false; |
1077 | 0 | } |
1078 | 0 | double dfDTMin = 0; |
1079 | 0 | double dfDTMax = 0; |
1080 | 0 | #define setDTMinMax(ctype) \ |
1081 | 0 | do \ |
1082 | 0 | { \ |
1083 | 0 | dfDTMin = static_cast<double>(cpl::NumericLimits<ctype>::lowest()); \ |
1084 | 0 | dfDTMax = static_cast<double>(cpl::NumericLimits<ctype>::max()); \ |
1085 | 0 | } while (0) |
1086 | |
|
1087 | 0 | switch (eAutoScaleType) |
1088 | 0 | { |
1089 | 0 | case GDT_Byte: |
1090 | 0 | setDTMinMax(GByte); |
1091 | 0 | break; |
1092 | 0 | case GDT_Int8: |
1093 | 0 | setDTMinMax(GInt8); |
1094 | 0 | break; |
1095 | 0 | case GDT_UInt16: |
1096 | 0 | setDTMinMax(GUInt16); |
1097 | 0 | break; |
1098 | 0 | case GDT_Int16: |
1099 | 0 | setDTMinMax(GInt16); |
1100 | 0 | break; |
1101 | 0 | case GDT_UInt32: |
1102 | 0 | setDTMinMax(GUInt32); |
1103 | 0 | break; |
1104 | 0 | case GDT_Int32: |
1105 | 0 | setDTMinMax(GInt32); |
1106 | 0 | break; |
1107 | 0 | case GDT_UInt64: |
1108 | 0 | setDTMinMax(std::uint64_t); |
1109 | 0 | break; |
1110 | 0 | case GDT_Int64: |
1111 | 0 | setDTMinMax(std::int64_t); |
1112 | 0 | break; |
1113 | 0 | case GDT_Float16: |
1114 | 0 | case GDT_Float32: |
1115 | 0 | case GDT_Float64: |
1116 | 0 | case GDT_Unknown: |
1117 | 0 | case GDT_CInt16: |
1118 | 0 | case GDT_CInt32: |
1119 | 0 | case GDT_CFloat16: |
1120 | 0 | case GDT_CFloat32: |
1121 | 0 | case GDT_CFloat64: |
1122 | 0 | case GDT_TypeCount: |
1123 | 0 | CPLAssert(false); |
1124 | 0 | } |
1125 | | |
1126 | 0 | dstArray = |
1127 | 0 | CreateMDArray(srcArray->GetName(), dstArrayDims, |
1128 | 0 | GDALExtendedDataType::Create(eAutoScaleType), |
1129 | 0 | aosArrayCO.List()); |
1130 | 0 | if (!dstArray) |
1131 | 0 | return !bStrict; |
1132 | | |
1133 | 0 | if (srcArray->GetRawNoDataValue() != nullptr) |
1134 | 0 | { |
1135 | | // If there's a nodata value in the source array, reserve |
1136 | | // DTMax for that purpose in the target scaled array |
1137 | 0 | if (!dstArray->SetNoDataValue(dfDTMax)) |
1138 | 0 | { |
1139 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1140 | 0 | "Cannot set nodata value"); |
1141 | 0 | return false; |
1142 | 0 | } |
1143 | 0 | dfDTMax--; |
1144 | 0 | } |
1145 | 0 | const double dfScale = |
1146 | 0 | dfMax > dfMin ? (dfMax - dfMin) / (dfDTMax - dfDTMin) : 1.0; |
1147 | 0 | const double dfOffset = dfMin - dfDTMin * dfScale; |
1148 | |
|
1149 | 0 | if (!dstArray->SetOffset(dfOffset) || |
1150 | 0 | !dstArray->SetScale(dfScale)) |
1151 | 0 | { |
1152 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1153 | 0 | "Cannot set scale/offset"); |
1154 | 0 | return false; |
1155 | 0 | } |
1156 | | |
1157 | 0 | auto poUnscaled = dstArray->GetUnscaled(); |
1158 | 0 | if (srcArray->GetRawNoDataValue() != nullptr) |
1159 | 0 | { |
1160 | 0 | poUnscaled->SetNoDataValue( |
1161 | 0 | srcArray->GetNoDataValueAsDouble()); |
1162 | 0 | } |
1163 | | |
1164 | | // Copy source array into unscaled array |
1165 | 0 | if (!poUnscaled->CopyFrom(poSrcDS, srcArray.get(), bStrict, |
1166 | 0 | nCurCost, nTotalCost, pfnProgress, |
1167 | 0 | pProgressData)) |
1168 | 0 | { |
1169 | 0 | return false; |
1170 | 0 | } |
1171 | 0 | } |
1172 | 0 | else |
1173 | 0 | { |
1174 | 0 | dstArray = CreateMDArray(srcArray->GetName(), dstArrayDims, |
1175 | 0 | srcArrayType, aosArrayCO.List()); |
1176 | 0 | if (!dstArray) |
1177 | 0 | return !bStrict; |
1178 | | |
1179 | 0 | if (!dstArray->CopyFrom(poSrcDS, srcArray.get(), bStrict, |
1180 | 0 | nCurCost, nTotalCost, pfnProgress, |
1181 | 0 | pProgressData)) |
1182 | 0 | { |
1183 | 0 | return false; |
1184 | 0 | } |
1185 | 0 | } |
1186 | | |
1187 | | // If this array is the indexing variable of a dimension, link them |
1188 | | // together. |
1189 | 0 | if (oIterDimName != mapSrcVariableNameToIndexedDimName.end()) |
1190 | 0 | { |
1191 | 0 | auto oCorrespondingDimIter = |
1192 | 0 | mapExistingDstDims.find(oIterDimName->second); |
1193 | 0 | if (oCorrespondingDimIter != mapExistingDstDims.end()) |
1194 | 0 | { |
1195 | 0 | CPLErrorStateBackuper oErrorStateBackuper( |
1196 | 0 | CPLQuietErrorHandler); |
1197 | 0 | oCorrespondingDimIter->second->SetIndexingVariable( |
1198 | 0 | std::move(dstArray)); |
1199 | 0 | } |
1200 | 0 | } |
1201 | |
|
1202 | 0 | return true; |
1203 | 0 | }; |
1204 | |
|
1205 | 0 | const auto arrayNames = poSrcGroup->GetMDArrayNames(); |
1206 | | |
1207 | | // Start by copying arrays that are indexing variables of dimensions |
1208 | 0 | for (const auto &name : arrayNames) |
1209 | 0 | { |
1210 | 0 | auto srcArray = poSrcGroup->OpenMDArray(name); |
1211 | 0 | EXIT_OR_CONTINUE_IF_NULL(srcArray); |
1212 | |
|
1213 | 0 | if (cpl::contains(mapSrcVariableNameToIndexedDimName, |
1214 | 0 | srcArray->GetName())) |
1215 | 0 | { |
1216 | 0 | if (!CopyArray(srcArray)) |
1217 | 0 | return false; |
1218 | 0 | } |
1219 | 0 | } |
1220 | | |
1221 | | // Then copy regular arrays |
1222 | 0 | for (const auto &name : arrayNames) |
1223 | 0 | { |
1224 | 0 | auto srcArray = poSrcGroup->OpenMDArray(name); |
1225 | 0 | EXIT_OR_CONTINUE_IF_NULL(srcArray); |
1226 | |
|
1227 | 0 | if (!cpl::contains(mapSrcVariableNameToIndexedDimName, |
1228 | 0 | srcArray->GetName())) |
1229 | 0 | { |
1230 | 0 | if (!CopyArray(srcArray)) |
1231 | 0 | return false; |
1232 | 0 | } |
1233 | 0 | } |
1234 | | |
1235 | 0 | const auto groupNames = poSrcGroup->GetGroupNames(); |
1236 | 0 | for (const auto &name : groupNames) |
1237 | 0 | { |
1238 | 0 | auto srcSubGroup = poSrcGroup->OpenGroup(name); |
1239 | 0 | EXIT_OR_CONTINUE_IF_NULL(srcSubGroup); |
1240 | 0 | auto dstSubGroup = CreateGroup(name); |
1241 | 0 | EXIT_OR_CONTINUE_IF_NULL(dstSubGroup); |
1242 | 0 | if (!dstSubGroup->CopyFrom( |
1243 | 0 | poDstRootGroup, poSrcDS, srcSubGroup, bStrict, nCurCost, |
1244 | 0 | nTotalCost, pfnProgress, pProgressData, papszOptions)) |
1245 | 0 | return false; |
1246 | 0 | } |
1247 | | |
1248 | 0 | if (!pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData)) |
1249 | 0 | return false; |
1250 | | |
1251 | 0 | return true; |
1252 | 0 | } |
1253 | 0 | catch (const std::exception &e) |
1254 | 0 | { |
1255 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what()); |
1256 | 0 | return false; |
1257 | 0 | } |
1258 | 0 | } |
1259 | | |
1260 | | /************************************************************************/ |
1261 | | /* GetInnerMostGroup() */ |
1262 | | /************************************************************************/ |
1263 | | |
1264 | | //! @cond Doxygen_Suppress |
1265 | | const GDALGroup * |
1266 | | GDALGroup::GetInnerMostGroup(const std::string &osPathOrArrayOrDim, |
1267 | | std::shared_ptr<GDALGroup> &curGroupHolder, |
1268 | | std::string &osLastPart) const |
1269 | 0 | { |
1270 | 0 | if (osPathOrArrayOrDim.empty() || osPathOrArrayOrDim[0] != '/') |
1271 | 0 | return nullptr; |
1272 | 0 | const GDALGroup *poCurGroup = this; |
1273 | 0 | CPLStringList aosTokens( |
1274 | 0 | CSLTokenizeString2(osPathOrArrayOrDim.c_str(), "/", 0)); |
1275 | 0 | if (aosTokens.size() == 0) |
1276 | 0 | { |
1277 | 0 | return nullptr; |
1278 | 0 | } |
1279 | | |
1280 | 0 | for (int i = 0; i < aosTokens.size() - 1; i++) |
1281 | 0 | { |
1282 | 0 | curGroupHolder = poCurGroup->OpenGroup(aosTokens[i], nullptr); |
1283 | 0 | if (!curGroupHolder) |
1284 | 0 | { |
1285 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot find group %s", |
1286 | 0 | aosTokens[i]); |
1287 | 0 | return nullptr; |
1288 | 0 | } |
1289 | 0 | poCurGroup = curGroupHolder.get(); |
1290 | 0 | } |
1291 | 0 | osLastPart = aosTokens[aosTokens.size() - 1]; |
1292 | 0 | return poCurGroup; |
1293 | 0 | } |
1294 | | |
1295 | | //! @endcond |
1296 | | |
1297 | | /************************************************************************/ |
1298 | | /* OpenMDArrayFromFullname() */ |
1299 | | /************************************************************************/ |
1300 | | |
1301 | | /** Get an array from its fully qualified name */ |
1302 | | std::shared_ptr<GDALMDArray> |
1303 | | GDALGroup::OpenMDArrayFromFullname(const std::string &osFullName, |
1304 | | CSLConstList papszOptions) const |
1305 | 0 | { |
1306 | 0 | std::string osName; |
1307 | 0 | std::shared_ptr<GDALGroup> curGroupHolder; |
1308 | 0 | auto poGroup(GetInnerMostGroup(osFullName, curGroupHolder, osName)); |
1309 | 0 | if (poGroup == nullptr) |
1310 | 0 | return nullptr; |
1311 | 0 | return poGroup->OpenMDArray(osName, papszOptions); |
1312 | 0 | } |
1313 | | |
1314 | | /************************************************************************/ |
1315 | | /* OpenAttributeFromFullname() */ |
1316 | | /************************************************************************/ |
1317 | | |
1318 | | /** Get an attribute from its fully qualified name */ |
1319 | | std::shared_ptr<GDALAttribute> |
1320 | | GDALGroup::OpenAttributeFromFullname(const std::string &osFullName, |
1321 | | CSLConstList papszOptions) const |
1322 | 0 | { |
1323 | 0 | const auto pos = osFullName.rfind('/'); |
1324 | 0 | if (pos == std::string::npos) |
1325 | 0 | return nullptr; |
1326 | 0 | const std::string attrName = osFullName.substr(pos + 1); |
1327 | 0 | if (pos == 0) |
1328 | 0 | return GetAttribute(attrName); |
1329 | 0 | const std::string container = osFullName.substr(0, pos); |
1330 | 0 | auto poArray = OpenMDArrayFromFullname(container, papszOptions); |
1331 | 0 | if (poArray) |
1332 | 0 | return poArray->GetAttribute(attrName); |
1333 | 0 | auto poGroup = OpenGroupFromFullname(container, papszOptions); |
1334 | 0 | if (poGroup) |
1335 | 0 | return poGroup->GetAttribute(attrName); |
1336 | 0 | return nullptr; |
1337 | 0 | } |
1338 | | |
1339 | | /************************************************************************/ |
1340 | | /* ResolveMDArray() */ |
1341 | | /************************************************************************/ |
1342 | | |
1343 | | /** Locate an array in a group and its subgroups by name. |
1344 | | * |
1345 | | * If osName is a fully qualified name, then OpenMDArrayFromFullname() is first |
1346 | | * used |
1347 | | * Otherwise the search will start from the group identified by osStartingPath, |
1348 | | * and an array whose name is osName will be looked for in this group (if |
1349 | | * osStartingPath is empty or "/", then the current group is used). If there |
1350 | | * is no match, then a recursive descendent search will be made in its |
1351 | | * subgroups. If there is no match in the subgroups, then the parent (if |
1352 | | * existing) of the group pointed by osStartingPath will be used as the new |
1353 | | * starting point for the search. |
1354 | | * |
1355 | | * @param osName name, qualified or not |
1356 | | * @param osStartingPath fully qualified name of the (sub-)group from which |
1357 | | * the search should be started. If this is a non-empty |
1358 | | * string, the group on which this method is called should |
1359 | | * nominally be the root group (otherwise the path will |
1360 | | * be interpreted as from the current group) |
1361 | | * @param papszOptions options to pass to OpenMDArray() |
1362 | | * @since GDAL 3.2 |
1363 | | */ |
1364 | | std::shared_ptr<GDALMDArray> |
1365 | | GDALGroup::ResolveMDArray(const std::string &osName, |
1366 | | const std::string &osStartingPath, |
1367 | | CSLConstList papszOptions) const |
1368 | 0 | { |
1369 | 0 | if (!osName.empty() && osName[0] == '/') |
1370 | 0 | { |
1371 | 0 | auto poArray = OpenMDArrayFromFullname(osName, papszOptions); |
1372 | 0 | if (poArray) |
1373 | 0 | return poArray; |
1374 | 0 | } |
1375 | 0 | std::string osPath(osStartingPath); |
1376 | 0 | std::set<std::string> oSetAlreadyVisited; |
1377 | |
|
1378 | 0 | while (true) |
1379 | 0 | { |
1380 | 0 | std::shared_ptr<GDALGroup> curGroupHolder; |
1381 | 0 | std::shared_ptr<GDALGroup> poGroup; |
1382 | |
|
1383 | 0 | std::queue<std::shared_ptr<GDALGroup>> oQueue; |
1384 | 0 | bool goOn = false; |
1385 | 0 | if (osPath.empty() || osPath == "/") |
1386 | 0 | { |
1387 | 0 | goOn = true; |
1388 | 0 | } |
1389 | 0 | else |
1390 | 0 | { |
1391 | 0 | std::string osLastPart; |
1392 | 0 | const GDALGroup *poGroupPtr = |
1393 | 0 | GetInnerMostGroup(osPath, curGroupHolder, osLastPart); |
1394 | 0 | if (poGroupPtr) |
1395 | 0 | poGroup = poGroupPtr->OpenGroup(osLastPart); |
1396 | 0 | if (poGroup && |
1397 | 0 | !cpl::contains(oSetAlreadyVisited, poGroup->GetFullName())) |
1398 | 0 | { |
1399 | 0 | oQueue.push(poGroup); |
1400 | 0 | goOn = true; |
1401 | 0 | } |
1402 | 0 | } |
1403 | |
|
1404 | 0 | if (goOn) |
1405 | 0 | { |
1406 | 0 | do |
1407 | 0 | { |
1408 | 0 | const GDALGroup *groupPtr; |
1409 | 0 | if (!oQueue.empty()) |
1410 | 0 | { |
1411 | 0 | poGroup = oQueue.front(); |
1412 | 0 | oQueue.pop(); |
1413 | 0 | groupPtr = poGroup.get(); |
1414 | 0 | } |
1415 | 0 | else |
1416 | 0 | { |
1417 | 0 | groupPtr = this; |
1418 | 0 | } |
1419 | |
|
1420 | 0 | auto poArray = groupPtr->OpenMDArray(osName, papszOptions); |
1421 | 0 | if (poArray) |
1422 | 0 | return poArray; |
1423 | | |
1424 | 0 | const auto aosGroupNames = groupPtr->GetGroupNames(); |
1425 | 0 | for (const auto &osGroupName : aosGroupNames) |
1426 | 0 | { |
1427 | 0 | auto poSubGroup = groupPtr->OpenGroup(osGroupName); |
1428 | 0 | if (poSubGroup && !cpl::contains(oSetAlreadyVisited, |
1429 | 0 | poSubGroup->GetFullName())) |
1430 | 0 | { |
1431 | 0 | oQueue.push(poSubGroup); |
1432 | 0 | oSetAlreadyVisited.insert(poSubGroup->GetFullName()); |
1433 | 0 | } |
1434 | 0 | } |
1435 | 0 | } while (!oQueue.empty()); |
1436 | 0 | } |
1437 | | |
1438 | 0 | if (osPath.empty() || osPath == "/") |
1439 | 0 | break; |
1440 | | |
1441 | 0 | const auto nPos = osPath.rfind('/'); |
1442 | 0 | if (nPos == 0) |
1443 | 0 | osPath = "/"; |
1444 | 0 | else |
1445 | 0 | { |
1446 | 0 | if (nPos == std::string::npos) |
1447 | 0 | break; |
1448 | 0 | osPath.resize(nPos); |
1449 | 0 | } |
1450 | 0 | } |
1451 | 0 | return nullptr; |
1452 | 0 | } |
1453 | | |
1454 | | /************************************************************************/ |
1455 | | /* OpenGroupFromFullname() */ |
1456 | | /************************************************************************/ |
1457 | | |
1458 | | /** Get a group from its fully qualified name. |
1459 | | * @since GDAL 3.2 |
1460 | | */ |
1461 | | std::shared_ptr<GDALGroup> |
1462 | | GDALGroup::OpenGroupFromFullname(const std::string &osFullName, |
1463 | | CSLConstList papszOptions) const |
1464 | 0 | { |
1465 | 0 | std::string osName; |
1466 | 0 | std::shared_ptr<GDALGroup> curGroupHolder; |
1467 | 0 | auto poGroup(GetInnerMostGroup(osFullName, curGroupHolder, osName)); |
1468 | 0 | if (poGroup == nullptr) |
1469 | 0 | return nullptr; |
1470 | 0 | return poGroup->OpenGroup(osName, papszOptions); |
1471 | 0 | } |
1472 | | |
1473 | | /************************************************************************/ |
1474 | | /* OpenDimensionFromFullname() */ |
1475 | | /************************************************************************/ |
1476 | | |
1477 | | /** Get a dimension from its fully qualified name */ |
1478 | | std::shared_ptr<GDALDimension> |
1479 | | GDALGroup::OpenDimensionFromFullname(const std::string &osFullName) const |
1480 | 0 | { |
1481 | 0 | std::string osName; |
1482 | 0 | std::shared_ptr<GDALGroup> curGroupHolder; |
1483 | 0 | auto poGroup(GetInnerMostGroup(osFullName, curGroupHolder, osName)); |
1484 | 0 | if (poGroup == nullptr) |
1485 | 0 | return nullptr; |
1486 | 0 | auto dims(poGroup->GetDimensions()); |
1487 | 0 | for (auto &dim : dims) |
1488 | 0 | { |
1489 | 0 | if (dim->GetName() == osName) |
1490 | 0 | return dim; |
1491 | 0 | } |
1492 | 0 | return nullptr; |
1493 | 0 | } |
1494 | | |
1495 | | /************************************************************************/ |
1496 | | /* ClearStatistics() */ |
1497 | | /************************************************************************/ |
1498 | | |
1499 | | /** |
1500 | | * \brief Clear statistics. |
1501 | | * |
1502 | | * @since GDAL 3.4 |
1503 | | */ |
1504 | | void GDALGroup::ClearStatistics() |
1505 | 0 | { |
1506 | 0 | auto groupNames = GetGroupNames(); |
1507 | 0 | for (const auto &name : groupNames) |
1508 | 0 | { |
1509 | 0 | auto subGroup = OpenGroup(name); |
1510 | 0 | if (subGroup) |
1511 | 0 | { |
1512 | 0 | subGroup->ClearStatistics(); |
1513 | 0 | } |
1514 | 0 | } |
1515 | |
|
1516 | 0 | auto arrayNames = GetMDArrayNames(); |
1517 | 0 | for (const auto &name : arrayNames) |
1518 | 0 | { |
1519 | 0 | auto array = OpenMDArray(name); |
1520 | 0 | if (array) |
1521 | 0 | { |
1522 | 0 | array->ClearStatistics(); |
1523 | 0 | } |
1524 | 0 | } |
1525 | 0 | } |
1526 | | |
1527 | | /************************************************************************/ |
1528 | | /* Rename() */ |
1529 | | /************************************************************************/ |
1530 | | |
1531 | | /** Rename the group. |
1532 | | * |
1533 | | * This is not implemented by all drivers. |
1534 | | * |
1535 | | * Drivers known to implement it: MEM, netCDF, ZARR. |
1536 | | * |
1537 | | * This is the same as the C function GDALGroupRename(). |
1538 | | * |
1539 | | * @param osNewName New name. |
1540 | | * |
1541 | | * @return true in case of success |
1542 | | * @since GDAL 3.8 |
1543 | | */ |
1544 | | bool GDALGroup::Rename(CPL_UNUSED const std::string &osNewName) |
1545 | 0 | { |
1546 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Rename() not implemented"); |
1547 | 0 | return false; |
1548 | 0 | } |
1549 | | |
1550 | | /************************************************************************/ |
1551 | | /* BaseRename() */ |
1552 | | /************************************************************************/ |
1553 | | |
1554 | | //! @cond Doxygen_Suppress |
1555 | | void GDALGroup::BaseRename(const std::string &osNewName) |
1556 | 0 | { |
1557 | 0 | m_osFullName.resize(m_osFullName.size() - m_osName.size()); |
1558 | 0 | m_osFullName += osNewName; |
1559 | 0 | m_osName = osNewName; |
1560 | |
|
1561 | 0 | NotifyChildrenOfRenaming(); |
1562 | 0 | } |
1563 | | |
1564 | | //! @endcond |
1565 | | |
1566 | | /************************************************************************/ |
1567 | | /* ParentRenamed() */ |
1568 | | /************************************************************************/ |
1569 | | |
1570 | | //! @cond Doxygen_Suppress |
1571 | | void GDALGroup::ParentRenamed(const std::string &osNewParentFullName) |
1572 | 0 | { |
1573 | 0 | m_osFullName = osNewParentFullName; |
1574 | 0 | m_osFullName += "/"; |
1575 | 0 | m_osFullName += m_osName; |
1576 | |
|
1577 | 0 | NotifyChildrenOfRenaming(); |
1578 | 0 | } |
1579 | | |
1580 | | //! @endcond |
1581 | | |
1582 | | /************************************************************************/ |
1583 | | /* Deleted() */ |
1584 | | /************************************************************************/ |
1585 | | |
1586 | | //! @cond Doxygen_Suppress |
1587 | | void GDALGroup::Deleted() |
1588 | 0 | { |
1589 | 0 | m_bValid = false; |
1590 | |
|
1591 | 0 | NotifyChildrenOfDeletion(); |
1592 | 0 | } |
1593 | | |
1594 | | //! @endcond |
1595 | | |
1596 | | /************************************************************************/ |
1597 | | /* ParentDeleted() */ |
1598 | | /************************************************************************/ |
1599 | | |
1600 | | //! @cond Doxygen_Suppress |
1601 | | void GDALGroup::ParentDeleted() |
1602 | 0 | { |
1603 | 0 | Deleted(); |
1604 | 0 | } |
1605 | | |
1606 | | //! @endcond |
1607 | | |
1608 | | /************************************************************************/ |
1609 | | /* CheckValidAndErrorOutIfNot() */ |
1610 | | /************************************************************************/ |
1611 | | |
1612 | | //! @cond Doxygen_Suppress |
1613 | | bool GDALGroup::CheckValidAndErrorOutIfNot() const |
1614 | 0 | { |
1615 | 0 | if (!m_bValid) |
1616 | 0 | { |
1617 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1618 | 0 | "This object has been deleted. No action on it is possible"); |
1619 | 0 | } |
1620 | 0 | return m_bValid; |
1621 | 0 | } |
1622 | | |
1623 | | //! @endcond |
1624 | | |
1625 | | /************************************************************************/ |
1626 | | /* ~GDALAbstractMDArray() */ |
1627 | | /************************************************************************/ |
1628 | | |
1629 | 0 | GDALAbstractMDArray::~GDALAbstractMDArray() = default; |
1630 | | |
1631 | | /************************************************************************/ |
1632 | | /* GDALAbstractMDArray() */ |
1633 | | /************************************************************************/ |
1634 | | |
1635 | | //! @cond Doxygen_Suppress |
1636 | | GDALAbstractMDArray::GDALAbstractMDArray(const std::string &osParentName, |
1637 | | const std::string &osName) |
1638 | 0 | : m_osName(osName), |
1639 | | m_osFullName( |
1640 | 0 | !osParentName.empty() |
1641 | 0 | ? ((osParentName == "/" ? "/" : osParentName + "/") + osName) |
1642 | 0 | : osName) |
1643 | 0 | { |
1644 | 0 | } |
1645 | | |
1646 | | //! @endcond |
1647 | | |
1648 | | /************************************************************************/ |
1649 | | /* GetDimensions() */ |
1650 | | /************************************************************************/ |
1651 | | |
1652 | | /** \fn GDALAbstractMDArray::GetDimensions() const |
1653 | | * \brief Return the dimensions of an attribute/array. |
1654 | | * |
1655 | | * This is the same as the C functions GDALMDArrayGetDimensions() and |
1656 | | * similar to GDALAttributeGetDimensionsSize(). |
1657 | | */ |
1658 | | |
1659 | | /************************************************************************/ |
1660 | | /* GetDataType() */ |
1661 | | /************************************************************************/ |
1662 | | |
1663 | | /** \fn GDALAbstractMDArray::GetDataType() const |
1664 | | * \brief Return the data type of an attribute/array. |
1665 | | * |
1666 | | * This is the same as the C functions GDALMDArrayGetDataType() and |
1667 | | * GDALAttributeGetDataType() |
1668 | | */ |
1669 | | |
1670 | | /************************************************************************/ |
1671 | | /* GetDimensionCount() */ |
1672 | | /************************************************************************/ |
1673 | | |
1674 | | /** Return the number of dimensions. |
1675 | | * |
1676 | | * Default implementation is GetDimensions().size(), and may be overridden by |
1677 | | * drivers if they have a faster / less expensive implementations. |
1678 | | * |
1679 | | * This is the same as the C function GDALMDArrayGetDimensionCount() or |
1680 | | * GDALAttributeGetDimensionCount(). |
1681 | | * |
1682 | | */ |
1683 | | size_t GDALAbstractMDArray::GetDimensionCount() const |
1684 | 0 | { |
1685 | 0 | return GetDimensions().size(); |
1686 | 0 | } |
1687 | | |
1688 | | /************************************************************************/ |
1689 | | /* Rename() */ |
1690 | | /************************************************************************/ |
1691 | | |
1692 | | /** Rename the attribute/array. |
1693 | | * |
1694 | | * This is not implemented by all drivers. |
1695 | | * |
1696 | | * Drivers known to implement it: MEM, netCDF, Zarr. |
1697 | | * |
1698 | | * This is the same as the C functions GDALMDArrayRename() or |
1699 | | * GDALAttributeRename(). |
1700 | | * |
1701 | | * @param osNewName New name. |
1702 | | * |
1703 | | * @return true in case of success |
1704 | | * @since GDAL 3.8 |
1705 | | */ |
1706 | | bool GDALAbstractMDArray::Rename(CPL_UNUSED const std::string &osNewName) |
1707 | 0 | { |
1708 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Rename() not implemented"); |
1709 | 0 | return false; |
1710 | 0 | } |
1711 | | |
1712 | | /************************************************************************/ |
1713 | | /* CopyValue() */ |
1714 | | /************************************************************************/ |
1715 | | |
1716 | | /** Convert a value from a source type to a destination type. |
1717 | | * |
1718 | | * If dstType is GEDTC_STRING, the written value will be a pointer to a char*, |
1719 | | * that must be freed with CPLFree(). |
1720 | | */ |
1721 | | bool GDALExtendedDataType::CopyValue(const void *pSrc, |
1722 | | const GDALExtendedDataType &srcType, |
1723 | | void *pDst, |
1724 | | const GDALExtendedDataType &dstType) |
1725 | 0 | { |
1726 | 0 | if (srcType.GetClass() == GEDTC_NUMERIC && |
1727 | 0 | dstType.GetClass() == GEDTC_NUMERIC) |
1728 | 0 | { |
1729 | 0 | GDALCopyWords64(pSrc, srcType.GetNumericDataType(), 0, pDst, |
1730 | 0 | dstType.GetNumericDataType(), 0, 1); |
1731 | 0 | return true; |
1732 | 0 | } |
1733 | 0 | if (srcType.GetClass() == GEDTC_STRING && |
1734 | 0 | dstType.GetClass() == GEDTC_STRING) |
1735 | 0 | { |
1736 | 0 | const char *srcStrPtr; |
1737 | 0 | memcpy(&srcStrPtr, pSrc, sizeof(const char *)); |
1738 | 0 | char *pszDup = srcStrPtr ? CPLStrdup(srcStrPtr) : nullptr; |
1739 | 0 | *reinterpret_cast<void **>(pDst) = pszDup; |
1740 | 0 | return true; |
1741 | 0 | } |
1742 | 0 | if (srcType.GetClass() == GEDTC_NUMERIC && |
1743 | 0 | dstType.GetClass() == GEDTC_STRING) |
1744 | 0 | { |
1745 | 0 | const char *str = nullptr; |
1746 | 0 | switch (srcType.GetNumericDataType()) |
1747 | 0 | { |
1748 | 0 | case GDT_Unknown: |
1749 | 0 | break; |
1750 | 0 | case GDT_Byte: |
1751 | 0 | str = CPLSPrintf("%d", *static_cast<const GByte *>(pSrc)); |
1752 | 0 | break; |
1753 | 0 | case GDT_Int8: |
1754 | 0 | str = CPLSPrintf("%d", *static_cast<const GInt8 *>(pSrc)); |
1755 | 0 | break; |
1756 | 0 | case GDT_UInt16: |
1757 | 0 | str = CPLSPrintf("%d", *static_cast<const GUInt16 *>(pSrc)); |
1758 | 0 | break; |
1759 | 0 | case GDT_Int16: |
1760 | 0 | str = CPLSPrintf("%d", *static_cast<const GInt16 *>(pSrc)); |
1761 | 0 | break; |
1762 | 0 | case GDT_UInt32: |
1763 | 0 | str = CPLSPrintf("%u", *static_cast<const GUInt32 *>(pSrc)); |
1764 | 0 | break; |
1765 | 0 | case GDT_Int32: |
1766 | 0 | str = CPLSPrintf("%d", *static_cast<const GInt32 *>(pSrc)); |
1767 | 0 | break; |
1768 | 0 | case GDT_UInt64: |
1769 | 0 | str = |
1770 | 0 | CPLSPrintf(CPL_FRMT_GUIB, |
1771 | 0 | static_cast<GUIntBig>( |
1772 | 0 | *static_cast<const std::uint64_t *>(pSrc))); |
1773 | 0 | break; |
1774 | 0 | case GDT_Int64: |
1775 | 0 | str = CPLSPrintf(CPL_FRMT_GIB, |
1776 | 0 | static_cast<GIntBig>( |
1777 | 0 | *static_cast<const std::int64_t *>(pSrc))); |
1778 | 0 | break; |
1779 | 0 | case GDT_Float16: |
1780 | 0 | str = CPLSPrintf("%.5g", |
1781 | 0 | double(*static_cast<const GFloat16 *>(pSrc))); |
1782 | 0 | break; |
1783 | 0 | case GDT_Float32: |
1784 | 0 | str = CPLSPrintf( |
1785 | 0 | "%.9g", |
1786 | 0 | static_cast<double>(*static_cast<const float *>(pSrc))); |
1787 | 0 | break; |
1788 | 0 | case GDT_Float64: |
1789 | 0 | str = CPLSPrintf("%.17g", *static_cast<const double *>(pSrc)); |
1790 | 0 | break; |
1791 | 0 | case GDT_CInt16: |
1792 | 0 | { |
1793 | 0 | const GInt16 *src = static_cast<const GInt16 *>(pSrc); |
1794 | 0 | str = CPLSPrintf("%d+%dj", src[0], src[1]); |
1795 | 0 | break; |
1796 | 0 | } |
1797 | 0 | case GDT_CInt32: |
1798 | 0 | { |
1799 | 0 | const GInt32 *src = static_cast<const GInt32 *>(pSrc); |
1800 | 0 | str = CPLSPrintf("%d+%dj", src[0], src[1]); |
1801 | 0 | break; |
1802 | 0 | } |
1803 | 0 | case GDT_CFloat16: |
1804 | 0 | { |
1805 | 0 | const GFloat16 *src = static_cast<const GFloat16 *>(pSrc); |
1806 | 0 | str = CPLSPrintf("%.5g+%.5gj", double(src[0]), double(src[1])); |
1807 | 0 | break; |
1808 | 0 | } |
1809 | 0 | case GDT_CFloat32: |
1810 | 0 | { |
1811 | 0 | const float *src = static_cast<const float *>(pSrc); |
1812 | 0 | str = CPLSPrintf("%.9g+%.9gj", double(src[0]), double(src[1])); |
1813 | 0 | break; |
1814 | 0 | } |
1815 | 0 | case GDT_CFloat64: |
1816 | 0 | { |
1817 | 0 | const double *src = static_cast<const double *>(pSrc); |
1818 | 0 | str = CPLSPrintf("%.17g+%.17gj", src[0], src[1]); |
1819 | 0 | break; |
1820 | 0 | } |
1821 | 0 | case GDT_TypeCount: |
1822 | 0 | CPLAssert(false); |
1823 | 0 | break; |
1824 | 0 | } |
1825 | 0 | char *pszDup = str ? CPLStrdup(str) : nullptr; |
1826 | 0 | *reinterpret_cast<void **>(pDst) = pszDup; |
1827 | 0 | return true; |
1828 | 0 | } |
1829 | 0 | if (srcType.GetClass() == GEDTC_STRING && |
1830 | 0 | dstType.GetClass() == GEDTC_NUMERIC) |
1831 | 0 | { |
1832 | 0 | const char *srcStrPtr; |
1833 | 0 | memcpy(&srcStrPtr, pSrc, sizeof(const char *)); |
1834 | 0 | if (dstType.GetNumericDataType() == GDT_Int64) |
1835 | 0 | { |
1836 | 0 | *(static_cast<int64_t *>(pDst)) = |
1837 | 0 | srcStrPtr == nullptr ? 0 |
1838 | 0 | : static_cast<int64_t>(atoll(srcStrPtr)); |
1839 | 0 | } |
1840 | 0 | else if (dstType.GetNumericDataType() == GDT_UInt64) |
1841 | 0 | { |
1842 | 0 | *(static_cast<uint64_t *>(pDst)) = |
1843 | 0 | srcStrPtr == nullptr |
1844 | 0 | ? 0 |
1845 | 0 | : static_cast<uint64_t>(strtoull(srcStrPtr, nullptr, 10)); |
1846 | 0 | } |
1847 | 0 | else |
1848 | 0 | { |
1849 | 0 | const double dfVal = srcStrPtr == nullptr ? 0 : CPLAtof(srcStrPtr); |
1850 | 0 | GDALCopyWords64(&dfVal, GDT_Float64, 0, pDst, |
1851 | 0 | dstType.GetNumericDataType(), 0, 1); |
1852 | 0 | } |
1853 | 0 | return true; |
1854 | 0 | } |
1855 | 0 | if (srcType.GetClass() == GEDTC_COMPOUND && |
1856 | 0 | dstType.GetClass() == GEDTC_COMPOUND) |
1857 | 0 | { |
1858 | 0 | const auto &srcComponents = srcType.GetComponents(); |
1859 | 0 | const auto &dstComponents = dstType.GetComponents(); |
1860 | 0 | const GByte *pabySrc = static_cast<const GByte *>(pSrc); |
1861 | 0 | GByte *pabyDst = static_cast<GByte *>(pDst); |
1862 | |
|
1863 | 0 | std::map<std::string, const std::unique_ptr<GDALEDTComponent> *> |
1864 | 0 | srcComponentMap; |
1865 | 0 | for (const auto &srcComp : srcComponents) |
1866 | 0 | { |
1867 | 0 | srcComponentMap[srcComp->GetName()] = &srcComp; |
1868 | 0 | } |
1869 | 0 | for (const auto &dstComp : dstComponents) |
1870 | 0 | { |
1871 | 0 | auto oIter = srcComponentMap.find(dstComp->GetName()); |
1872 | 0 | if (oIter == srcComponentMap.end()) |
1873 | 0 | return false; |
1874 | 0 | const auto &srcComp = *(oIter->second); |
1875 | 0 | if (!GDALExtendedDataType::CopyValue( |
1876 | 0 | pabySrc + srcComp->GetOffset(), srcComp->GetType(), |
1877 | 0 | pabyDst + dstComp->GetOffset(), dstComp->GetType())) |
1878 | 0 | { |
1879 | 0 | return false; |
1880 | 0 | } |
1881 | 0 | } |
1882 | 0 | return true; |
1883 | 0 | } |
1884 | | |
1885 | 0 | return false; |
1886 | 0 | } |
1887 | | |
1888 | | /************************************************************************/ |
1889 | | /* CopyValues() */ |
1890 | | /************************************************************************/ |
1891 | | |
1892 | | /** Convert severals value from a source type to a destination type. |
1893 | | * |
1894 | | * If dstType is GEDTC_STRING, the written value will be a pointer to a char*, |
1895 | | * that must be freed with CPLFree(). |
1896 | | */ |
1897 | | bool GDALExtendedDataType::CopyValues(const void *pSrc, |
1898 | | const GDALExtendedDataType &srcType, |
1899 | | GPtrDiff_t nSrcStrideInElts, void *pDst, |
1900 | | const GDALExtendedDataType &dstType, |
1901 | | GPtrDiff_t nDstStrideInElts, |
1902 | | size_t nValues) |
1903 | 0 | { |
1904 | 0 | const auto nSrcStrideInBytes = |
1905 | 0 | nSrcStrideInElts * static_cast<GPtrDiff_t>(srcType.GetSize()); |
1906 | 0 | const auto nDstStrideInBytes = |
1907 | 0 | nDstStrideInElts * static_cast<GPtrDiff_t>(dstType.GetSize()); |
1908 | 0 | if (srcType.GetClass() == GEDTC_NUMERIC && |
1909 | 0 | dstType.GetClass() == GEDTC_NUMERIC && |
1910 | 0 | nSrcStrideInBytes >= std::numeric_limits<int>::min() && |
1911 | 0 | nSrcStrideInBytes <= std::numeric_limits<int>::max() && |
1912 | 0 | nDstStrideInBytes >= std::numeric_limits<int>::min() && |
1913 | 0 | nDstStrideInBytes <= std::numeric_limits<int>::max()) |
1914 | 0 | { |
1915 | 0 | GDALCopyWords64(pSrc, srcType.GetNumericDataType(), |
1916 | 0 | static_cast<int>(nSrcStrideInBytes), pDst, |
1917 | 0 | dstType.GetNumericDataType(), |
1918 | 0 | static_cast<int>(nDstStrideInBytes), nValues); |
1919 | 0 | } |
1920 | 0 | else |
1921 | 0 | { |
1922 | 0 | const GByte *pabySrc = static_cast<const GByte *>(pSrc); |
1923 | 0 | GByte *pabyDst = static_cast<GByte *>(pDst); |
1924 | 0 | for (size_t i = 0; i < nValues; ++i) |
1925 | 0 | { |
1926 | 0 | if (!CopyValue(pabySrc, srcType, pabyDst, dstType)) |
1927 | 0 | return false; |
1928 | 0 | pabySrc += nSrcStrideInBytes; |
1929 | 0 | pabyDst += nDstStrideInBytes; |
1930 | 0 | } |
1931 | 0 | } |
1932 | 0 | return true; |
1933 | 0 | } |
1934 | | |
1935 | | /************************************************************************/ |
1936 | | /* CheckReadWriteParams() */ |
1937 | | /************************************************************************/ |
1938 | | //! @cond Doxygen_Suppress |
1939 | | bool GDALAbstractMDArray::CheckReadWriteParams( |
1940 | | const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *&arrayStep, |
1941 | | const GPtrDiff_t *&bufferStride, const GDALExtendedDataType &bufferDataType, |
1942 | | const void *buffer, const void *buffer_alloc_start, |
1943 | | size_t buffer_alloc_size, std::vector<GInt64> &tmp_arrayStep, |
1944 | | std::vector<GPtrDiff_t> &tmp_bufferStride) const |
1945 | 0 | { |
1946 | 0 | const auto lamda_error = []() |
1947 | 0 | { |
1948 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1949 | 0 | "Not all elements pointed by buffer will fit in " |
1950 | 0 | "[buffer_alloc_start, " |
1951 | 0 | "buffer_alloc_start + buffer_alloc_size]"); |
1952 | 0 | }; |
1953 | |
|
1954 | 0 | const auto &dims = GetDimensions(); |
1955 | 0 | if (dims.empty()) |
1956 | 0 | { |
1957 | 0 | if (buffer_alloc_start) |
1958 | 0 | { |
1959 | 0 | const size_t elementSize = bufferDataType.GetSize(); |
1960 | 0 | const GByte *paby_buffer = static_cast<const GByte *>(buffer); |
1961 | 0 | const GByte *paby_buffer_alloc_start = |
1962 | 0 | static_cast<const GByte *>(buffer_alloc_start); |
1963 | 0 | const GByte *paby_buffer_alloc_end = |
1964 | 0 | paby_buffer_alloc_start + buffer_alloc_size; |
1965 | |
|
1966 | 0 | if (paby_buffer < paby_buffer_alloc_start || |
1967 | 0 | paby_buffer + elementSize > paby_buffer_alloc_end) |
1968 | 0 | { |
1969 | 0 | lamda_error(); |
1970 | 0 | return false; |
1971 | 0 | } |
1972 | 0 | } |
1973 | 0 | return true; |
1974 | 0 | } |
1975 | | |
1976 | 0 | if (arrayStep == nullptr) |
1977 | 0 | { |
1978 | 0 | tmp_arrayStep.resize(dims.size(), 1); |
1979 | 0 | arrayStep = tmp_arrayStep.data(); |
1980 | 0 | } |
1981 | 0 | for (size_t i = 0; i < dims.size(); i++) |
1982 | 0 | { |
1983 | 0 | assert(count); |
1984 | 0 | if (count[i] == 0) |
1985 | 0 | { |
1986 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "count[%u] = 0 is invalid", |
1987 | 0 | static_cast<unsigned>(i)); |
1988 | 0 | return false; |
1989 | 0 | } |
1990 | 0 | } |
1991 | 0 | bool bufferStride_all_positive = true; |
1992 | 0 | if (bufferStride == nullptr) |
1993 | 0 | { |
1994 | 0 | GPtrDiff_t stride = 1; |
1995 | 0 | assert(dims.empty() || count != nullptr); |
1996 | | // To compute strides we must proceed from the fastest varying dimension |
1997 | | // (the last one), and then reverse the result |
1998 | 0 | for (size_t i = dims.size(); i != 0;) |
1999 | 0 | { |
2000 | 0 | --i; |
2001 | 0 | tmp_bufferStride.push_back(stride); |
2002 | 0 | GUInt64 newStride = 0; |
2003 | 0 | bool bOK; |
2004 | 0 | try |
2005 | 0 | { |
2006 | 0 | newStride = (CPLSM(static_cast<uint64_t>(stride)) * |
2007 | 0 | CPLSM(static_cast<uint64_t>(count[i]))) |
2008 | 0 | .v(); |
2009 | 0 | bOK = static_cast<size_t>(newStride) == newStride && |
2010 | 0 | newStride < std::numeric_limits<size_t>::max() / 2; |
2011 | 0 | } |
2012 | 0 | catch (...) |
2013 | 0 | { |
2014 | 0 | bOK = false; |
2015 | 0 | } |
2016 | 0 | if (!bOK) |
2017 | 0 | { |
2018 | 0 | CPLError(CE_Failure, CPLE_OutOfMemory, "Too big count values"); |
2019 | 0 | return false; |
2020 | 0 | } |
2021 | 0 | stride = static_cast<GPtrDiff_t>(newStride); |
2022 | 0 | } |
2023 | 0 | std::reverse(tmp_bufferStride.begin(), tmp_bufferStride.end()); |
2024 | 0 | bufferStride = tmp_bufferStride.data(); |
2025 | 0 | } |
2026 | 0 | else |
2027 | 0 | { |
2028 | 0 | for (size_t i = 0; i < dims.size(); i++) |
2029 | 0 | { |
2030 | 0 | if (bufferStride[i] < 0) |
2031 | 0 | { |
2032 | 0 | bufferStride_all_positive = false; |
2033 | 0 | break; |
2034 | 0 | } |
2035 | 0 | } |
2036 | 0 | } |
2037 | 0 | for (size_t i = 0; i < dims.size(); i++) |
2038 | 0 | { |
2039 | 0 | assert(arrayStartIdx); |
2040 | 0 | assert(count); |
2041 | 0 | if (arrayStartIdx[i] >= dims[i]->GetSize()) |
2042 | 0 | { |
2043 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2044 | 0 | "arrayStartIdx[%u] = " CPL_FRMT_GUIB " >= " CPL_FRMT_GUIB, |
2045 | 0 | static_cast<unsigned>(i), |
2046 | 0 | static_cast<GUInt64>(arrayStartIdx[i]), |
2047 | 0 | static_cast<GUInt64>(dims[i]->GetSize())); |
2048 | 0 | return false; |
2049 | 0 | } |
2050 | 0 | bool bOverflow; |
2051 | 0 | if (arrayStep[i] >= 0) |
2052 | 0 | { |
2053 | 0 | try |
2054 | 0 | { |
2055 | 0 | bOverflow = (CPLSM(static_cast<uint64_t>(arrayStartIdx[i])) + |
2056 | 0 | CPLSM(static_cast<uint64_t>(count[i] - 1)) * |
2057 | 0 | CPLSM(static_cast<uint64_t>(arrayStep[i]))) |
2058 | 0 | .v() >= dims[i]->GetSize(); |
2059 | 0 | } |
2060 | 0 | catch (...) |
2061 | 0 | { |
2062 | 0 | bOverflow = true; |
2063 | 0 | } |
2064 | 0 | if (bOverflow) |
2065 | 0 | { |
2066 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2067 | 0 | "arrayStartIdx[%u] + (count[%u]-1) * arrayStep[%u] " |
2068 | 0 | ">= " CPL_FRMT_GUIB, |
2069 | 0 | static_cast<unsigned>(i), static_cast<unsigned>(i), |
2070 | 0 | static_cast<unsigned>(i), |
2071 | 0 | static_cast<GUInt64>(dims[i]->GetSize())); |
2072 | 0 | return false; |
2073 | 0 | } |
2074 | 0 | } |
2075 | 0 | else |
2076 | 0 | { |
2077 | 0 | try |
2078 | 0 | { |
2079 | 0 | bOverflow = |
2080 | 0 | arrayStartIdx[i] < |
2081 | 0 | (CPLSM(static_cast<uint64_t>(count[i] - 1)) * |
2082 | 0 | CPLSM(arrayStep[i] == std::numeric_limits<GInt64>::min() |
2083 | 0 | ? (static_cast<uint64_t>(1) << 63) |
2084 | 0 | : static_cast<uint64_t>(-arrayStep[i]))) |
2085 | 0 | .v(); |
2086 | 0 | } |
2087 | 0 | catch (...) |
2088 | 0 | { |
2089 | 0 | bOverflow = true; |
2090 | 0 | } |
2091 | 0 | if (bOverflow) |
2092 | 0 | { |
2093 | 0 | CPLError( |
2094 | 0 | CE_Failure, CPLE_AppDefined, |
2095 | 0 | "arrayStartIdx[%u] + (count[%u]-1) * arrayStep[%u] < 0", |
2096 | 0 | static_cast<unsigned>(i), static_cast<unsigned>(i), |
2097 | 0 | static_cast<unsigned>(i)); |
2098 | 0 | return false; |
2099 | 0 | } |
2100 | 0 | } |
2101 | 0 | } |
2102 | | |
2103 | 0 | if (buffer_alloc_start) |
2104 | 0 | { |
2105 | 0 | const size_t elementSize = bufferDataType.GetSize(); |
2106 | 0 | const GByte *paby_buffer = static_cast<const GByte *>(buffer); |
2107 | 0 | const GByte *paby_buffer_alloc_start = |
2108 | 0 | static_cast<const GByte *>(buffer_alloc_start); |
2109 | 0 | const GByte *paby_buffer_alloc_end = |
2110 | 0 | paby_buffer_alloc_start + buffer_alloc_size; |
2111 | 0 | if (bufferStride_all_positive) |
2112 | 0 | { |
2113 | 0 | if (paby_buffer < paby_buffer_alloc_start) |
2114 | 0 | { |
2115 | 0 | lamda_error(); |
2116 | 0 | return false; |
2117 | 0 | } |
2118 | 0 | GUInt64 nOffset = elementSize; |
2119 | 0 | for (size_t i = 0; i < dims.size(); i++) |
2120 | 0 | { |
2121 | 0 | try |
2122 | 0 | { |
2123 | 0 | nOffset = (CPLSM(static_cast<uint64_t>(nOffset)) + |
2124 | 0 | CPLSM(static_cast<uint64_t>(bufferStride[i])) * |
2125 | 0 | CPLSM(static_cast<uint64_t>(count[i] - 1)) * |
2126 | 0 | CPLSM(static_cast<uint64_t>(elementSize))) |
2127 | 0 | .v(); |
2128 | 0 | } |
2129 | 0 | catch (...) |
2130 | 0 | { |
2131 | 0 | lamda_error(); |
2132 | 0 | return false; |
2133 | 0 | } |
2134 | 0 | } |
2135 | | #if SIZEOF_VOIDP == 4 |
2136 | | if (static_cast<size_t>(nOffset) != nOffset) |
2137 | | { |
2138 | | lamda_error(); |
2139 | | return false; |
2140 | | } |
2141 | | #endif |
2142 | 0 | if (paby_buffer + nOffset > paby_buffer_alloc_end) |
2143 | 0 | { |
2144 | 0 | lamda_error(); |
2145 | 0 | return false; |
2146 | 0 | } |
2147 | 0 | } |
2148 | 0 | else if (dims.size() < 31) |
2149 | 0 | { |
2150 | | // Check all corners of the hypercube |
2151 | 0 | const unsigned nLoops = 1U << static_cast<unsigned>(dims.size()); |
2152 | 0 | for (unsigned iCornerCode = 0; iCornerCode < nLoops; iCornerCode++) |
2153 | 0 | { |
2154 | 0 | const GByte *paby = paby_buffer; |
2155 | 0 | for (unsigned i = 0; i < static_cast<unsigned>(dims.size()); |
2156 | 0 | i++) |
2157 | 0 | { |
2158 | 0 | if (iCornerCode & (1U << i)) |
2159 | 0 | { |
2160 | | // We should check for integer overflows |
2161 | 0 | paby += bufferStride[i] * (count[i] - 1) * elementSize; |
2162 | 0 | } |
2163 | 0 | } |
2164 | 0 | if (paby < paby_buffer_alloc_start || |
2165 | 0 | paby + elementSize > paby_buffer_alloc_end) |
2166 | 0 | { |
2167 | 0 | lamda_error(); |
2168 | 0 | return false; |
2169 | 0 | } |
2170 | 0 | } |
2171 | 0 | } |
2172 | 0 | } |
2173 | | |
2174 | 0 | return true; |
2175 | 0 | } |
2176 | | |
2177 | | //! @endcond |
2178 | | |
2179 | | /************************************************************************/ |
2180 | | /* Read() */ |
2181 | | /************************************************************************/ |
2182 | | |
2183 | | /** Read part or totality of a multidimensional array or attribute. |
2184 | | * |
2185 | | * This will extract the content of a hyper-rectangle from the array into |
2186 | | * a user supplied buffer. |
2187 | | * |
2188 | | * If bufferDataType is of type string, the values written in pDstBuffer |
2189 | | * will be char* pointers and the strings should be freed with CPLFree(). |
2190 | | * |
2191 | | * This is the same as the C function GDALMDArrayRead(). |
2192 | | * |
2193 | | * @param arrayStartIdx Values representing the starting index to read |
2194 | | * in each dimension (in [0, aoDims[i].GetSize()-1] range). |
2195 | | * Array of GetDimensionCount() values. Must not be |
2196 | | * nullptr, unless for a zero-dimensional array. |
2197 | | * |
2198 | | * @param count Values representing the number of values to extract in |
2199 | | * each dimension. |
2200 | | * Array of GetDimensionCount() values. Must not be |
2201 | | * nullptr, unless for a zero-dimensional array. |
2202 | | * |
2203 | | * @param arrayStep Spacing between values to extract in each dimension. |
2204 | | * The spacing is in number of array elements, not bytes. |
2205 | | * If provided, must contain GetDimensionCount() values. |
2206 | | * If set to nullptr, [1, 1, ... 1] will be used as a |
2207 | | * default to indicate consecutive elements. |
2208 | | * |
2209 | | * @param bufferStride Spacing between values to store in pDstBuffer. |
2210 | | * The spacing is in number of array elements, not bytes. |
2211 | | * If provided, must contain GetDimensionCount() values. |
2212 | | * Negative values are possible (for example to reorder |
2213 | | * from bottom-to-top to top-to-bottom). |
2214 | | * If set to nullptr, will be set so that pDstBuffer is |
2215 | | * written in a compact way, with elements of the last / |
2216 | | * fastest varying dimension being consecutive. |
2217 | | * |
2218 | | * @param bufferDataType Data type of values in pDstBuffer. |
2219 | | * |
2220 | | * @param pDstBuffer User buffer to store the values read. Should be big |
2221 | | * enough to store the number of values indicated by |
2222 | | * count[] and with the spacing of bufferStride[]. |
2223 | | * |
2224 | | * @param pDstBufferAllocStart Optional pointer that can be used to validate the |
2225 | | * validity of pDstBuffer. pDstBufferAllocStart |
2226 | | * should be the pointer returned by the malloc() or equivalent call used to |
2227 | | * allocate the buffer. It will generally be equal to pDstBuffer (when |
2228 | | * bufferStride[] values are all positive), but not necessarily. If specified, |
2229 | | * nDstBufferAllocSize should be also set to the appropriate value. If no |
2230 | | * validation is needed, nullptr can be passed. |
2231 | | * |
2232 | | * @param nDstBufferAllocSize Optional buffer size, that can be used to |
2233 | | * validate the validity of pDstBuffer. This is the size of the buffer starting |
2234 | | * at pDstBufferAllocStart. If specified, pDstBufferAllocStart should be also |
2235 | | * set to the appropriate value. |
2236 | | * If no validation is needed, 0 can be passed. |
2237 | | * |
2238 | | * @return true in case of success. |
2239 | | */ |
2240 | | bool GDALAbstractMDArray::Read( |
2241 | | const GUInt64 *arrayStartIdx, const size_t *count, |
2242 | | const GInt64 *arrayStep, // step in elements |
2243 | | const GPtrDiff_t *bufferStride, // stride in elements |
2244 | | const GDALExtendedDataType &bufferDataType, void *pDstBuffer, |
2245 | | const void *pDstBufferAllocStart, size_t nDstBufferAllocSize) const |
2246 | 0 | { |
2247 | 0 | if (!GetDataType().CanConvertTo(bufferDataType)) |
2248 | 0 | { |
2249 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2250 | 0 | "Array data type is not convertible to buffer data type"); |
2251 | 0 | return false; |
2252 | 0 | } |
2253 | | |
2254 | 0 | std::vector<GInt64> tmp_arrayStep; |
2255 | 0 | std::vector<GPtrDiff_t> tmp_bufferStride; |
2256 | 0 | if (!CheckReadWriteParams(arrayStartIdx, count, arrayStep, bufferStride, |
2257 | 0 | bufferDataType, pDstBuffer, pDstBufferAllocStart, |
2258 | 0 | nDstBufferAllocSize, tmp_arrayStep, |
2259 | 0 | tmp_bufferStride)) |
2260 | 0 | { |
2261 | 0 | return false; |
2262 | 0 | } |
2263 | | |
2264 | 0 | return IRead(arrayStartIdx, count, arrayStep, bufferStride, bufferDataType, |
2265 | 0 | pDstBuffer); |
2266 | 0 | } |
2267 | | |
2268 | | /************************************************************************/ |
2269 | | /* IWrite() */ |
2270 | | /************************************************************************/ |
2271 | | |
2272 | | //! @cond Doxygen_Suppress |
2273 | | bool GDALAbstractMDArray::IWrite(const GUInt64 *, const size_t *, |
2274 | | const GInt64 *, const GPtrDiff_t *, |
2275 | | const GDALExtendedDataType &, const void *) |
2276 | 0 | { |
2277 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "IWrite() not implemented"); |
2278 | 0 | return false; |
2279 | 0 | } |
2280 | | |
2281 | | //! @endcond |
2282 | | |
2283 | | /************************************************************************/ |
2284 | | /* Write() */ |
2285 | | /************************************************************************/ |
2286 | | |
2287 | | /** Write part or totality of a multidimensional array or attribute. |
2288 | | * |
2289 | | * This will set the content of a hyper-rectangle into the array from |
2290 | | * a user supplied buffer. |
2291 | | * |
2292 | | * If bufferDataType is of type string, the values read from pSrcBuffer |
2293 | | * will be char* pointers. |
2294 | | * |
2295 | | * This is the same as the C function GDALMDArrayWrite(). |
2296 | | * |
2297 | | * @param arrayStartIdx Values representing the starting index to write |
2298 | | * in each dimension (in [0, aoDims[i].GetSize()-1] range). |
2299 | | * Array of GetDimensionCount() values. Must not be |
2300 | | * nullptr, unless for a zero-dimensional array. |
2301 | | * |
2302 | | * @param count Values representing the number of values to write in |
2303 | | * each dimension. |
2304 | | * Array of GetDimensionCount() values. Must not be |
2305 | | * nullptr, unless for a zero-dimensional array. |
2306 | | * |
2307 | | * @param arrayStep Spacing between values to write in each dimension. |
2308 | | * The spacing is in number of array elements, not bytes. |
2309 | | * If provided, must contain GetDimensionCount() values. |
2310 | | * If set to nullptr, [1, 1, ... 1] will be used as a |
2311 | | * default to indicate consecutive elements. |
2312 | | * |
2313 | | * @param bufferStride Spacing between values to read from pSrcBuffer. |
2314 | | * The spacing is in number of array elements, not bytes. |
2315 | | * If provided, must contain GetDimensionCount() values. |
2316 | | * Negative values are possible (for example to reorder |
2317 | | * from bottom-to-top to top-to-bottom). |
2318 | | * If set to nullptr, will be set so that pSrcBuffer is |
2319 | | * written in a compact way, with elements of the last / |
2320 | | * fastest varying dimension being consecutive. |
2321 | | * |
2322 | | * @param bufferDataType Data type of values in pSrcBuffer. |
2323 | | * |
2324 | | * @param pSrcBuffer User buffer to read the values from. Should be big |
2325 | | * enough to store the number of values indicated by |
2326 | | * count[] and with the spacing of bufferStride[]. |
2327 | | * |
2328 | | * @param pSrcBufferAllocStart Optional pointer that can be used to validate the |
2329 | | * validity of pSrcBuffer. pSrcBufferAllocStart |
2330 | | * should be the pointer returned by the malloc() or equivalent call used to |
2331 | | * allocate the buffer. It will generally be equal to pSrcBuffer (when |
2332 | | * bufferStride[] values are all positive), but not necessarily. If specified, |
2333 | | * nSrcBufferAllocSize should be also set to the appropriate value. If no |
2334 | | * validation is needed, nullptr can be passed. |
2335 | | * |
2336 | | * @param nSrcBufferAllocSize Optional buffer size, that can be used to |
2337 | | * validate the validity of pSrcBuffer. This is the size of the buffer starting |
2338 | | * at pSrcBufferAllocStart. If specified, pDstBufferAllocStart should be also |
2339 | | * set to the appropriate value. |
2340 | | * If no validation is needed, 0 can be passed. |
2341 | | * |
2342 | | * @return true in case of success. |
2343 | | */ |
2344 | | bool GDALAbstractMDArray::Write(const GUInt64 *arrayStartIdx, |
2345 | | const size_t *count, const GInt64 *arrayStep, |
2346 | | const GPtrDiff_t *bufferStride, |
2347 | | const GDALExtendedDataType &bufferDataType, |
2348 | | const void *pSrcBuffer, |
2349 | | const void *pSrcBufferAllocStart, |
2350 | | size_t nSrcBufferAllocSize) |
2351 | 0 | { |
2352 | 0 | if (!bufferDataType.CanConvertTo(GetDataType())) |
2353 | 0 | { |
2354 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2355 | 0 | "Buffer data type is not convertible to array data type"); |
2356 | 0 | return false; |
2357 | 0 | } |
2358 | | |
2359 | 0 | std::vector<GInt64> tmp_arrayStep; |
2360 | 0 | std::vector<GPtrDiff_t> tmp_bufferStride; |
2361 | 0 | if (!CheckReadWriteParams(arrayStartIdx, count, arrayStep, bufferStride, |
2362 | 0 | bufferDataType, pSrcBuffer, pSrcBufferAllocStart, |
2363 | 0 | nSrcBufferAllocSize, tmp_arrayStep, |
2364 | 0 | tmp_bufferStride)) |
2365 | 0 | { |
2366 | 0 | return false; |
2367 | 0 | } |
2368 | | |
2369 | 0 | return IWrite(arrayStartIdx, count, arrayStep, bufferStride, bufferDataType, |
2370 | 0 | pSrcBuffer); |
2371 | 0 | } |
2372 | | |
2373 | | /************************************************************************/ |
2374 | | /* GetTotalElementsCount() */ |
2375 | | /************************************************************************/ |
2376 | | |
2377 | | /** Return the total number of values in the array. |
2378 | | * |
2379 | | * This is the same as the C functions GDALMDArrayGetTotalElementsCount() |
2380 | | * and GDALAttributeGetTotalElementsCount(). |
2381 | | * |
2382 | | */ |
2383 | | GUInt64 GDALAbstractMDArray::GetTotalElementsCount() const |
2384 | 0 | { |
2385 | 0 | const auto &dims = GetDimensions(); |
2386 | 0 | if (dims.empty()) |
2387 | 0 | return 1; |
2388 | 0 | GUInt64 nElts = 1; |
2389 | 0 | for (const auto &dim : dims) |
2390 | 0 | { |
2391 | 0 | try |
2392 | 0 | { |
2393 | 0 | nElts = (CPLSM(static_cast<uint64_t>(nElts)) * |
2394 | 0 | CPLSM(static_cast<uint64_t>(dim->GetSize()))) |
2395 | 0 | .v(); |
2396 | 0 | } |
2397 | 0 | catch (...) |
2398 | 0 | { |
2399 | 0 | return 0; |
2400 | 0 | } |
2401 | 0 | } |
2402 | 0 | return nElts; |
2403 | 0 | } |
2404 | | |
2405 | | /************************************************************************/ |
2406 | | /* GetBlockSize() */ |
2407 | | /************************************************************************/ |
2408 | | |
2409 | | /** Return the "natural" block size of the array along all dimensions. |
2410 | | * |
2411 | | * Some drivers might organize the array in tiles/blocks and reading/writing |
2412 | | * aligned on those tile/block boundaries will be more efficient. |
2413 | | * |
2414 | | * The returned number of elements in the vector is the same as |
2415 | | * GetDimensionCount(). A value of 0 should be interpreted as no hint regarding |
2416 | | * the natural block size along the considered dimension. |
2417 | | * "Flat" arrays will typically return a vector of values set to 0. |
2418 | | * |
2419 | | * The default implementation will return a vector of values set to 0. |
2420 | | * |
2421 | | * This method is used by GetProcessingChunkSize(). |
2422 | | * |
2423 | | * Pedantic note: the returned type is GUInt64, so in the highly unlikeley |
2424 | | * theoretical case of a 32-bit platform, this might exceed its size_t |
2425 | | * allocation capabilities. |
2426 | | * |
2427 | | * This is the same as the C function GDALMDArrayGetBlockSize(). |
2428 | | * |
2429 | | * @return the block size, in number of elements along each dimension. |
2430 | | */ |
2431 | | std::vector<GUInt64> GDALAbstractMDArray::GetBlockSize() const |
2432 | 0 | { |
2433 | 0 | return std::vector<GUInt64>(GetDimensionCount()); |
2434 | 0 | } |
2435 | | |
2436 | | /************************************************************************/ |
2437 | | /* GetProcessingChunkSize() */ |
2438 | | /************************************************************************/ |
2439 | | |
2440 | | /** \brief Return an optimal chunk size for read/write operations, given the |
2441 | | * natural block size and memory constraints specified. |
2442 | | * |
2443 | | * This method will use GetBlockSize() to define a chunk whose dimensions are |
2444 | | * multiple of those returned by GetBlockSize() (unless the block define by |
2445 | | * GetBlockSize() is larger than nMaxChunkMemory, in which case it will be |
2446 | | * returned by this method). |
2447 | | * |
2448 | | * This is the same as the C function GDALMDArrayGetProcessingChunkSize(). |
2449 | | * |
2450 | | * @param nMaxChunkMemory Maximum amount of memory, in bytes, to use for the |
2451 | | * chunk. |
2452 | | * |
2453 | | * @return the chunk size, in number of elements along each dimension. |
2454 | | */ |
2455 | | std::vector<size_t> |
2456 | | GDALAbstractMDArray::GetProcessingChunkSize(size_t nMaxChunkMemory) const |
2457 | 0 | { |
2458 | 0 | const auto &dims = GetDimensions(); |
2459 | 0 | const auto &nDTSize = GetDataType().GetSize(); |
2460 | 0 | std::vector<size_t> anChunkSize; |
2461 | 0 | auto blockSize = GetBlockSize(); |
2462 | 0 | CPLAssert(blockSize.size() == dims.size()); |
2463 | 0 | size_t nChunkSize = nDTSize; |
2464 | 0 | bool bOverflow = false; |
2465 | 0 | constexpr auto kSIZE_T_MAX = std::numeric_limits<size_t>::max(); |
2466 | | // Initialize anChunkSize[i] with blockSize[i] by properly clamping in |
2467 | | // [1, min(sizet_max, dim_size[i])] |
2468 | | // Also make sure that the product of all anChunkSize[i]) fits on size_t |
2469 | 0 | for (size_t i = 0; i < dims.size(); i++) |
2470 | 0 | { |
2471 | 0 | const auto sizeDimI = |
2472 | 0 | std::max(static_cast<size_t>(1), |
2473 | 0 | static_cast<size_t>( |
2474 | 0 | std::min(static_cast<GUInt64>(kSIZE_T_MAX), |
2475 | 0 | std::min(blockSize[i], dims[i]->GetSize())))); |
2476 | 0 | anChunkSize.push_back(sizeDimI); |
2477 | 0 | if (nChunkSize > kSIZE_T_MAX / sizeDimI) |
2478 | 0 | { |
2479 | 0 | bOverflow = true; |
2480 | 0 | } |
2481 | 0 | else |
2482 | 0 | { |
2483 | 0 | nChunkSize *= sizeDimI; |
2484 | 0 | } |
2485 | 0 | } |
2486 | 0 | if (nChunkSize == 0) |
2487 | 0 | return anChunkSize; |
2488 | | |
2489 | | // If the product of all anChunkSize[i] does not fit on size_t, then |
2490 | | // set lowest anChunkSize[i] to 1. |
2491 | 0 | if (bOverflow) |
2492 | 0 | { |
2493 | 0 | nChunkSize = nDTSize; |
2494 | 0 | bOverflow = false; |
2495 | 0 | for (size_t i = dims.size(); i > 0;) |
2496 | 0 | { |
2497 | 0 | --i; |
2498 | 0 | if (bOverflow || nChunkSize > kSIZE_T_MAX / anChunkSize[i]) |
2499 | 0 | { |
2500 | 0 | bOverflow = true; |
2501 | 0 | anChunkSize[i] = 1; |
2502 | 0 | } |
2503 | 0 | else |
2504 | 0 | { |
2505 | 0 | nChunkSize *= anChunkSize[i]; |
2506 | 0 | } |
2507 | 0 | } |
2508 | 0 | } |
2509 | |
|
2510 | 0 | nChunkSize = nDTSize; |
2511 | 0 | std::vector<size_t> anAccBlockSizeFromStart; |
2512 | 0 | for (size_t i = 0; i < dims.size(); i++) |
2513 | 0 | { |
2514 | 0 | nChunkSize *= anChunkSize[i]; |
2515 | 0 | anAccBlockSizeFromStart.push_back(nChunkSize); |
2516 | 0 | } |
2517 | 0 | if (nChunkSize <= nMaxChunkMemory / 2) |
2518 | 0 | { |
2519 | 0 | size_t nVoxelsFromEnd = 1; |
2520 | 0 | for (size_t i = dims.size(); i > 0;) |
2521 | 0 | { |
2522 | 0 | --i; |
2523 | 0 | const auto nCurBlockSize = |
2524 | 0 | anAccBlockSizeFromStart[i] * nVoxelsFromEnd; |
2525 | 0 | const auto nMul = nMaxChunkMemory / nCurBlockSize; |
2526 | 0 | if (nMul >= 2) |
2527 | 0 | { |
2528 | 0 | const auto nSizeThisDim(dims[i]->GetSize()); |
2529 | 0 | const auto nBlocksThisDim = |
2530 | 0 | DIV_ROUND_UP(nSizeThisDim, anChunkSize[i]); |
2531 | 0 | anChunkSize[i] = static_cast<size_t>(std::min( |
2532 | 0 | anChunkSize[i] * |
2533 | 0 | std::min(static_cast<GUInt64>(nMul), nBlocksThisDim), |
2534 | 0 | nSizeThisDim)); |
2535 | 0 | } |
2536 | 0 | nVoxelsFromEnd *= anChunkSize[i]; |
2537 | 0 | } |
2538 | 0 | } |
2539 | 0 | return anChunkSize; |
2540 | 0 | } |
2541 | | |
2542 | | /************************************************************************/ |
2543 | | /* BaseRename() */ |
2544 | | /************************************************************************/ |
2545 | | |
2546 | | //! @cond Doxygen_Suppress |
2547 | | void GDALAbstractMDArray::BaseRename(const std::string &osNewName) |
2548 | 0 | { |
2549 | 0 | m_osFullName.resize(m_osFullName.size() - m_osName.size()); |
2550 | 0 | m_osFullName += osNewName; |
2551 | 0 | m_osName = osNewName; |
2552 | |
|
2553 | 0 | NotifyChildrenOfRenaming(); |
2554 | 0 | } |
2555 | | |
2556 | | //! @endcond |
2557 | | |
2558 | | //! @cond Doxygen_Suppress |
2559 | | /************************************************************************/ |
2560 | | /* ParentRenamed() */ |
2561 | | /************************************************************************/ |
2562 | | |
2563 | | void GDALAbstractMDArray::ParentRenamed(const std::string &osNewParentFullName) |
2564 | 0 | { |
2565 | 0 | m_osFullName = osNewParentFullName; |
2566 | 0 | m_osFullName += "/"; |
2567 | 0 | m_osFullName += m_osName; |
2568 | |
|
2569 | 0 | NotifyChildrenOfRenaming(); |
2570 | 0 | } |
2571 | | |
2572 | | //! @endcond |
2573 | | |
2574 | | /************************************************************************/ |
2575 | | /* Deleted() */ |
2576 | | /************************************************************************/ |
2577 | | |
2578 | | //! @cond Doxygen_Suppress |
2579 | | void GDALAbstractMDArray::Deleted() |
2580 | 0 | { |
2581 | 0 | m_bValid = false; |
2582 | |
|
2583 | 0 | NotifyChildrenOfDeletion(); |
2584 | 0 | } |
2585 | | |
2586 | | //! @endcond |
2587 | | |
2588 | | /************************************************************************/ |
2589 | | /* ParentDeleted() */ |
2590 | | /************************************************************************/ |
2591 | | |
2592 | | //! @cond Doxygen_Suppress |
2593 | | void GDALAbstractMDArray::ParentDeleted() |
2594 | 0 | { |
2595 | 0 | Deleted(); |
2596 | 0 | } |
2597 | | |
2598 | | //! @endcond |
2599 | | |
2600 | | /************************************************************************/ |
2601 | | /* CheckValidAndErrorOutIfNot() */ |
2602 | | /************************************************************************/ |
2603 | | |
2604 | | //! @cond Doxygen_Suppress |
2605 | | bool GDALAbstractMDArray::CheckValidAndErrorOutIfNot() const |
2606 | 0 | { |
2607 | 0 | if (!m_bValid) |
2608 | 0 | { |
2609 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2610 | 0 | "This object has been deleted. No action on it is possible"); |
2611 | 0 | } |
2612 | 0 | return m_bValid; |
2613 | 0 | } |
2614 | | |
2615 | | //! @endcond |
2616 | | |
2617 | | /************************************************************************/ |
2618 | | /* SetUnit() */ |
2619 | | /************************************************************************/ |
2620 | | |
2621 | | /** Set the variable unit. |
2622 | | * |
2623 | | * Values should conform as much as possible with those allowed by |
2624 | | * the NetCDF CF conventions: |
2625 | | * http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.html#units |
2626 | | * but others might be returned. |
2627 | | * |
2628 | | * Few examples are "meter", "degrees", "second", ... |
2629 | | * Empty value means unknown. |
2630 | | * |
2631 | | * This is the same as the C function GDALMDArraySetUnit() |
2632 | | * |
2633 | | * @note Driver implementation: optionally implemented. |
2634 | | * |
2635 | | * @param osUnit unit name. |
2636 | | * @return true in case of success. |
2637 | | */ |
2638 | | bool GDALMDArray::SetUnit(CPL_UNUSED const std::string &osUnit) |
2639 | 0 | { |
2640 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "SetUnit() not implemented"); |
2641 | 0 | return false; |
2642 | 0 | } |
2643 | | |
2644 | | /************************************************************************/ |
2645 | | /* GetUnit() */ |
2646 | | /************************************************************************/ |
2647 | | |
2648 | | /** Return the array unit. |
2649 | | * |
2650 | | * Values should conform as much as possible with those allowed by |
2651 | | * the NetCDF CF conventions: |
2652 | | * http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.html#units |
2653 | | * but others might be returned. |
2654 | | * |
2655 | | * Few examples are "meter", "degrees", "second", ... |
2656 | | * Empty value means unknown. |
2657 | | * |
2658 | | * This is the same as the C function GDALMDArrayGetUnit() |
2659 | | */ |
2660 | | const std::string &GDALMDArray::GetUnit() const |
2661 | 0 | { |
2662 | 0 | static const std::string emptyString; |
2663 | 0 | return emptyString; |
2664 | 0 | } |
2665 | | |
2666 | | /************************************************************************/ |
2667 | | /* SetSpatialRef() */ |
2668 | | /************************************************************************/ |
2669 | | |
2670 | | /** Assign a spatial reference system object to the array. |
2671 | | * |
2672 | | * This is the same as the C function GDALMDArraySetSpatialRef(). |
2673 | | */ |
2674 | | bool GDALMDArray::SetSpatialRef(CPL_UNUSED const OGRSpatialReference *poSRS) |
2675 | 0 | { |
2676 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "SetSpatialRef() not implemented"); |
2677 | 0 | return false; |
2678 | 0 | } |
2679 | | |
2680 | | /************************************************************************/ |
2681 | | /* GetSpatialRef() */ |
2682 | | /************************************************************************/ |
2683 | | |
2684 | | /** Return the spatial reference system object associated with the array. |
2685 | | * |
2686 | | * This is the same as the C function GDALMDArrayGetSpatialRef(). |
2687 | | */ |
2688 | | std::shared_ptr<OGRSpatialReference> GDALMDArray::GetSpatialRef() const |
2689 | 0 | { |
2690 | 0 | return nullptr; |
2691 | 0 | } |
2692 | | |
2693 | | /************************************************************************/ |
2694 | | /* GetRawNoDataValue() */ |
2695 | | /************************************************************************/ |
2696 | | |
2697 | | /** Return the nodata value as a "raw" value. |
2698 | | * |
2699 | | * The value returned might be nullptr in case of no nodata value. When |
2700 | | * a nodata value is registered, a non-nullptr will be returned whose size in |
2701 | | * bytes is GetDataType().GetSize(). |
2702 | | * |
2703 | | * The returned value should not be modified or freed. It is valid until |
2704 | | * the array is destroyed, or the next call to GetRawNoDataValue() or |
2705 | | * SetRawNoDataValue(), or any similar methods. |
2706 | | * |
2707 | | * @note Driver implementation: this method shall be implemented if nodata |
2708 | | * is supported. |
2709 | | * |
2710 | | * This is the same as the C function GDALMDArrayGetRawNoDataValue(). |
2711 | | * |
2712 | | * @return nullptr or a pointer to GetDataType().GetSize() bytes. |
2713 | | */ |
2714 | | const void *GDALMDArray::GetRawNoDataValue() const |
2715 | 0 | { |
2716 | 0 | return nullptr; |
2717 | 0 | } |
2718 | | |
2719 | | /************************************************************************/ |
2720 | | /* GetNoDataValueAsDouble() */ |
2721 | | /************************************************************************/ |
2722 | | |
2723 | | /** Return the nodata value as a double. |
2724 | | * |
2725 | | * This is the same as the C function GDALMDArrayGetNoDataValueAsDouble(). |
2726 | | * |
2727 | | * @param pbHasNoData Pointer to a output boolean that will be set to true if |
2728 | | * a nodata value exists and can be converted to double. Might be nullptr. |
2729 | | * |
2730 | | * @return the nodata value as a double. A 0.0 value might also indicate the |
2731 | | * absence of a nodata value or an error in the conversion (*pbHasNoData will be |
2732 | | * set to false then). |
2733 | | */ |
2734 | | double GDALMDArray::GetNoDataValueAsDouble(bool *pbHasNoData) const |
2735 | 0 | { |
2736 | 0 | const void *pNoData = GetRawNoDataValue(); |
2737 | 0 | double dfNoData = 0.0; |
2738 | 0 | const auto &eDT = GetDataType(); |
2739 | 0 | const bool ok = pNoData != nullptr && eDT.GetClass() == GEDTC_NUMERIC; |
2740 | 0 | if (ok) |
2741 | 0 | { |
2742 | 0 | GDALCopyWords64(pNoData, eDT.GetNumericDataType(), 0, &dfNoData, |
2743 | 0 | GDT_Float64, 0, 1); |
2744 | 0 | } |
2745 | 0 | if (pbHasNoData) |
2746 | 0 | *pbHasNoData = ok; |
2747 | 0 | return dfNoData; |
2748 | 0 | } |
2749 | | |
2750 | | /************************************************************************/ |
2751 | | /* GetNoDataValueAsInt64() */ |
2752 | | /************************************************************************/ |
2753 | | |
2754 | | /** Return the nodata value as a Int64. |
2755 | | * |
2756 | | * @param pbHasNoData Pointer to a output boolean that will be set to true if |
2757 | | * a nodata value exists and can be converted to Int64. Might be nullptr. |
2758 | | * |
2759 | | * This is the same as the C function GDALMDArrayGetNoDataValueAsInt64(). |
2760 | | * |
2761 | | * @return the nodata value as a Int64 |
2762 | | * |
2763 | | * @since GDAL 3.5 |
2764 | | */ |
2765 | | int64_t GDALMDArray::GetNoDataValueAsInt64(bool *pbHasNoData) const |
2766 | 0 | { |
2767 | 0 | const void *pNoData = GetRawNoDataValue(); |
2768 | 0 | int64_t nNoData = GDAL_PAM_DEFAULT_NODATA_VALUE_INT64; |
2769 | 0 | const auto &eDT = GetDataType(); |
2770 | 0 | const bool ok = pNoData != nullptr && eDT.GetClass() == GEDTC_NUMERIC; |
2771 | 0 | if (ok) |
2772 | 0 | { |
2773 | 0 | GDALCopyWords64(pNoData, eDT.GetNumericDataType(), 0, &nNoData, |
2774 | 0 | GDT_Int64, 0, 1); |
2775 | 0 | } |
2776 | 0 | if (pbHasNoData) |
2777 | 0 | *pbHasNoData = ok; |
2778 | 0 | return nNoData; |
2779 | 0 | } |
2780 | | |
2781 | | /************************************************************************/ |
2782 | | /* GetNoDataValueAsUInt64() */ |
2783 | | /************************************************************************/ |
2784 | | |
2785 | | /** Return the nodata value as a UInt64. |
2786 | | * |
2787 | | * This is the same as the C function GDALMDArrayGetNoDataValueAsUInt64(). |
2788 | | |
2789 | | * @param pbHasNoData Pointer to a output boolean that will be set to true if |
2790 | | * a nodata value exists and can be converted to UInt64. Might be nullptr. |
2791 | | * |
2792 | | * @return the nodata value as a UInt64 |
2793 | | * |
2794 | | * @since GDAL 3.5 |
2795 | | */ |
2796 | | uint64_t GDALMDArray::GetNoDataValueAsUInt64(bool *pbHasNoData) const |
2797 | 0 | { |
2798 | 0 | const void *pNoData = GetRawNoDataValue(); |
2799 | 0 | uint64_t nNoData = GDAL_PAM_DEFAULT_NODATA_VALUE_UINT64; |
2800 | 0 | const auto &eDT = GetDataType(); |
2801 | 0 | const bool ok = pNoData != nullptr && eDT.GetClass() == GEDTC_NUMERIC; |
2802 | 0 | if (ok) |
2803 | 0 | { |
2804 | 0 | GDALCopyWords64(pNoData, eDT.GetNumericDataType(), 0, &nNoData, |
2805 | 0 | GDT_UInt64, 0, 1); |
2806 | 0 | } |
2807 | 0 | if (pbHasNoData) |
2808 | 0 | *pbHasNoData = ok; |
2809 | 0 | return nNoData; |
2810 | 0 | } |
2811 | | |
2812 | | /************************************************************************/ |
2813 | | /* SetRawNoDataValue() */ |
2814 | | /************************************************************************/ |
2815 | | |
2816 | | /** Set the nodata value as a "raw" value. |
2817 | | * |
2818 | | * The value passed might be nullptr in case of no nodata value. When |
2819 | | * a nodata value is registered, a non-nullptr whose size in |
2820 | | * bytes is GetDataType().GetSize() must be passed. |
2821 | | * |
2822 | | * This is the same as the C function GDALMDArraySetRawNoDataValue(). |
2823 | | * |
2824 | | * @note Driver implementation: this method shall be implemented if setting |
2825 | | nodata |
2826 | | * is supported. |
2827 | | |
2828 | | * @return true in case of success. |
2829 | | */ |
2830 | | bool GDALMDArray::SetRawNoDataValue(CPL_UNUSED const void *pRawNoData) |
2831 | 0 | { |
2832 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
2833 | 0 | "SetRawNoDataValue() not implemented"); |
2834 | 0 | return false; |
2835 | 0 | } |
2836 | | |
2837 | | /************************************************************************/ |
2838 | | /* SetNoDataValue() */ |
2839 | | /************************************************************************/ |
2840 | | |
2841 | | /** Set the nodata value as a double. |
2842 | | * |
2843 | | * If the natural data type of the attribute/array is not double, type |
2844 | | * conversion will occur to the type returned by GetDataType(). |
2845 | | * |
2846 | | * This is the same as the C function GDALMDArraySetNoDataValueAsDouble(). |
2847 | | * |
2848 | | * @return true in case of success. |
2849 | | */ |
2850 | | bool GDALMDArray::SetNoDataValue(double dfNoData) |
2851 | 0 | { |
2852 | 0 | void *pRawNoData = CPLMalloc(GetDataType().GetSize()); |
2853 | 0 | bool bRet = false; |
2854 | 0 | if (GDALExtendedDataType::CopyValue( |
2855 | 0 | &dfNoData, GDALExtendedDataType::Create(GDT_Float64), pRawNoData, |
2856 | 0 | GetDataType())) |
2857 | 0 | { |
2858 | 0 | bRet = SetRawNoDataValue(pRawNoData); |
2859 | 0 | } |
2860 | 0 | CPLFree(pRawNoData); |
2861 | 0 | return bRet; |
2862 | 0 | } |
2863 | | |
2864 | | /************************************************************************/ |
2865 | | /* SetNoDataValue() */ |
2866 | | /************************************************************************/ |
2867 | | |
2868 | | /** Set the nodata value as a Int64. |
2869 | | * |
2870 | | * If the natural data type of the attribute/array is not Int64, type conversion |
2871 | | * will occur to the type returned by GetDataType(). |
2872 | | * |
2873 | | * This is the same as the C function GDALMDArraySetNoDataValueAsInt64(). |
2874 | | * |
2875 | | * @return true in case of success. |
2876 | | * |
2877 | | * @since GDAL 3.5 |
2878 | | */ |
2879 | | bool GDALMDArray::SetNoDataValue(int64_t nNoData) |
2880 | 0 | { |
2881 | 0 | void *pRawNoData = CPLMalloc(GetDataType().GetSize()); |
2882 | 0 | bool bRet = false; |
2883 | 0 | if (GDALExtendedDataType::CopyValue(&nNoData, |
2884 | 0 | GDALExtendedDataType::Create(GDT_Int64), |
2885 | 0 | pRawNoData, GetDataType())) |
2886 | 0 | { |
2887 | 0 | bRet = SetRawNoDataValue(pRawNoData); |
2888 | 0 | } |
2889 | 0 | CPLFree(pRawNoData); |
2890 | 0 | return bRet; |
2891 | 0 | } |
2892 | | |
2893 | | /************************************************************************/ |
2894 | | /* SetNoDataValue() */ |
2895 | | /************************************************************************/ |
2896 | | |
2897 | | /** Set the nodata value as a Int64. |
2898 | | * |
2899 | | * If the natural data type of the attribute/array is not Int64, type conversion |
2900 | | * will occur to the type returned by GetDataType(). |
2901 | | * |
2902 | | * This is the same as the C function GDALMDArraySetNoDataValueAsUInt64(). |
2903 | | * |
2904 | | * @return true in case of success. |
2905 | | * |
2906 | | * @since GDAL 3.5 |
2907 | | */ |
2908 | | bool GDALMDArray::SetNoDataValue(uint64_t nNoData) |
2909 | 0 | { |
2910 | 0 | void *pRawNoData = CPLMalloc(GetDataType().GetSize()); |
2911 | 0 | bool bRet = false; |
2912 | 0 | if (GDALExtendedDataType::CopyValue( |
2913 | 0 | &nNoData, GDALExtendedDataType::Create(GDT_UInt64), pRawNoData, |
2914 | 0 | GetDataType())) |
2915 | 0 | { |
2916 | 0 | bRet = SetRawNoDataValue(pRawNoData); |
2917 | 0 | } |
2918 | 0 | CPLFree(pRawNoData); |
2919 | 0 | return bRet; |
2920 | 0 | } |
2921 | | |
2922 | | /************************************************************************/ |
2923 | | /* Resize() */ |
2924 | | /************************************************************************/ |
2925 | | |
2926 | | /** Resize an array to new dimensions. |
2927 | | * |
2928 | | * Not all drivers may allow this operation, and with restrictions (e.g. |
2929 | | * for netCDF, this is limited to growing of "unlimited" dimensions) |
2930 | | * |
2931 | | * Resizing a dimension used in other arrays will cause those other arrays |
2932 | | * to be resized. |
2933 | | * |
2934 | | * This is the same as the C function GDALMDArrayResize(). |
2935 | | * |
2936 | | * @param anNewDimSizes Array of GetDimensionCount() values containing the |
2937 | | * new size of each indexing dimension. |
2938 | | * @param papszOptions Options. (Driver specific) |
2939 | | * @return true in case of success. |
2940 | | * @since GDAL 3.7 |
2941 | | */ |
2942 | | bool GDALMDArray::Resize(CPL_UNUSED const std::vector<GUInt64> &anNewDimSizes, |
2943 | | CPL_UNUSED CSLConstList papszOptions) |
2944 | 0 | { |
2945 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
2946 | 0 | "Resize() is not supported for this array"); |
2947 | 0 | return false; |
2948 | 0 | } |
2949 | | |
2950 | | /************************************************************************/ |
2951 | | /* SetScale() */ |
2952 | | /************************************************************************/ |
2953 | | |
2954 | | /** Set the scale value to apply to raw values. |
2955 | | * |
2956 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
2957 | | * |
2958 | | * This is the same as the C function GDALMDArraySetScale() / |
2959 | | * GDALMDArraySetScaleEx(). |
2960 | | * |
2961 | | * @note Driver implementation: this method shall be implemented if setting |
2962 | | * scale is supported. |
2963 | | * |
2964 | | * @param dfScale scale |
2965 | | * @param eStorageType Data type to which create the potential attribute that |
2966 | | * will store the scale. Added in GDAL 3.3 If let to its GDT_Unknown value, the |
2967 | | * implementation will decide automatically the data type. Note that changing |
2968 | | * the data type after initial setting might not be supported. |
2969 | | * @return true in case of success. |
2970 | | */ |
2971 | | bool GDALMDArray::SetScale(CPL_UNUSED double dfScale, |
2972 | | CPL_UNUSED GDALDataType eStorageType) |
2973 | 0 | { |
2974 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "SetScale() not implemented"); |
2975 | 0 | return false; |
2976 | 0 | } |
2977 | | |
2978 | | /************************************************************************/ |
2979 | | /* SetOffset) */ |
2980 | | /************************************************************************/ |
2981 | | |
2982 | | /** Set the offset value to apply to raw values. |
2983 | | * |
2984 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
2985 | | * |
2986 | | * This is the same as the C function GDALMDArraySetOffset() / |
2987 | | * GDALMDArraySetOffsetEx(). |
2988 | | * |
2989 | | * @note Driver implementation: this method shall be implemented if setting |
2990 | | * offset is supported. |
2991 | | * |
2992 | | * @param dfOffset Offset |
2993 | | * @param eStorageType Data type to which create the potential attribute that |
2994 | | * will store the offset. Added in GDAL 3.3 If let to its GDT_Unknown value, the |
2995 | | * implementation will decide automatically the data type. Note that changing |
2996 | | * the data type after initial setting might not be supported. |
2997 | | * @return true in case of success. |
2998 | | */ |
2999 | | bool GDALMDArray::SetOffset(CPL_UNUSED double dfOffset, |
3000 | | CPL_UNUSED GDALDataType eStorageType) |
3001 | 0 | { |
3002 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "SetOffset() not implemented"); |
3003 | 0 | return false; |
3004 | 0 | } |
3005 | | |
3006 | | /************************************************************************/ |
3007 | | /* GetScale() */ |
3008 | | /************************************************************************/ |
3009 | | |
3010 | | /** Get the scale value to apply to raw values. |
3011 | | * |
3012 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
3013 | | * |
3014 | | * This is the same as the C function GDALMDArrayGetScale(). |
3015 | | * |
3016 | | * @note Driver implementation: this method shall be implemented if getting |
3017 | | * scale is supported. |
3018 | | * |
3019 | | * @param pbHasScale Pointer to a output boolean that will be set to true if |
3020 | | * a scale value exists. Might be nullptr. |
3021 | | * @param peStorageType Pointer to a output GDALDataType that will be set to |
3022 | | * the storage type of the scale value, when known/relevant. Otherwise will be |
3023 | | * set to GDT_Unknown. Might be nullptr. Since GDAL 3.3 |
3024 | | * |
3025 | | * @return the scale value. A 1.0 value might also indicate the |
3026 | | * absence of a scale value. |
3027 | | */ |
3028 | | double GDALMDArray::GetScale(CPL_UNUSED bool *pbHasScale, |
3029 | | CPL_UNUSED GDALDataType *peStorageType) const |
3030 | 0 | { |
3031 | 0 | if (pbHasScale) |
3032 | 0 | *pbHasScale = false; |
3033 | 0 | return 1.0; |
3034 | 0 | } |
3035 | | |
3036 | | /************************************************************************/ |
3037 | | /* GetOffset() */ |
3038 | | /************************************************************************/ |
3039 | | |
3040 | | /** Get the offset value to apply to raw values. |
3041 | | * |
3042 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
3043 | | * |
3044 | | * This is the same as the C function GDALMDArrayGetOffset(). |
3045 | | * |
3046 | | * @note Driver implementation: this method shall be implemented if getting |
3047 | | * offset is supported. |
3048 | | * |
3049 | | * @param pbHasOffset Pointer to a output boolean that will be set to true if |
3050 | | * a offset value exists. Might be nullptr. |
3051 | | * @param peStorageType Pointer to a output GDALDataType that will be set to |
3052 | | * the storage type of the offset value, when known/relevant. Otherwise will be |
3053 | | * set to GDT_Unknown. Might be nullptr. Since GDAL 3.3 |
3054 | | * |
3055 | | * @return the offset value. A 0.0 value might also indicate the |
3056 | | * absence of a offset value. |
3057 | | */ |
3058 | | double GDALMDArray::GetOffset(CPL_UNUSED bool *pbHasOffset, |
3059 | | CPL_UNUSED GDALDataType *peStorageType) const |
3060 | 0 | { |
3061 | 0 | if (pbHasOffset) |
3062 | 0 | *pbHasOffset = false; |
3063 | 0 | return 0.0; |
3064 | 0 | } |
3065 | | |
3066 | | /************************************************************************/ |
3067 | | /* ProcessPerChunk() */ |
3068 | | /************************************************************************/ |
3069 | | |
3070 | | namespace |
3071 | | { |
3072 | | enum class Caller |
3073 | | { |
3074 | | CALLER_END_OF_LOOP, |
3075 | | CALLER_IN_LOOP, |
3076 | | }; |
3077 | | } |
3078 | | |
3079 | | /** \brief Call a user-provided function to operate on an array chunk by chunk. |
3080 | | * |
3081 | | * This method is to be used when doing operations on an array, or a subset of |
3082 | | * it, in a chunk by chunk way. |
3083 | | * |
3084 | | * @param arrayStartIdx Values representing the starting index to use |
3085 | | * in each dimension (in [0, aoDims[i].GetSize()-1] range). |
3086 | | * Array of GetDimensionCount() values. Must not be |
3087 | | * nullptr, unless for a zero-dimensional array. |
3088 | | * |
3089 | | * @param count Values representing the number of values to use in |
3090 | | * each dimension. |
3091 | | * Array of GetDimensionCount() values. Must not be |
3092 | | * nullptr, unless for a zero-dimensional array. |
3093 | | * |
3094 | | * @param chunkSize Values representing the chunk size in each dimension. |
3095 | | * Might typically the output of GetProcessingChunkSize(). |
3096 | | * Array of GetDimensionCount() values. Must not be |
3097 | | * nullptr, unless for a zero-dimensional array. |
3098 | | * |
3099 | | * @param pfnFunc User-provided function of type FuncProcessPerChunkType. |
3100 | | * Must NOT be nullptr. |
3101 | | * |
3102 | | * @param pUserData Pointer to pass as the value of the pUserData argument |
3103 | | * of FuncProcessPerChunkType. Might be nullptr (depends on pfnFunc. |
3104 | | * |
3105 | | * @return true in case of success. |
3106 | | */ |
3107 | | bool GDALAbstractMDArray::ProcessPerChunk(const GUInt64 *arrayStartIdx, |
3108 | | const GUInt64 *count, |
3109 | | const size_t *chunkSize, |
3110 | | FuncProcessPerChunkType pfnFunc, |
3111 | | void *pUserData) |
3112 | 0 | { |
3113 | 0 | const auto &dims = GetDimensions(); |
3114 | 0 | if (dims.empty()) |
3115 | 0 | { |
3116 | 0 | return pfnFunc(this, nullptr, nullptr, 1, 1, pUserData); |
3117 | 0 | } |
3118 | | |
3119 | | // Sanity check |
3120 | 0 | size_t nTotalChunkSize = 1; |
3121 | 0 | for (size_t i = 0; i < dims.size(); i++) |
3122 | 0 | { |
3123 | 0 | const auto nSizeThisDim(dims[i]->GetSize()); |
3124 | 0 | if (count[i] == 0 || count[i] > nSizeThisDim || |
3125 | 0 | arrayStartIdx[i] > nSizeThisDim - count[i]) |
3126 | 0 | { |
3127 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3128 | 0 | "Inconsistent arrayStartIdx[] / count[] values " |
3129 | 0 | "regarding array size"); |
3130 | 0 | return false; |
3131 | 0 | } |
3132 | 0 | if (chunkSize[i] == 0 || chunkSize[i] > nSizeThisDim || |
3133 | 0 | chunkSize[i] > std::numeric_limits<size_t>::max() / nTotalChunkSize) |
3134 | 0 | { |
3135 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3136 | 0 | "Inconsistent chunkSize[] values"); |
3137 | 0 | return false; |
3138 | 0 | } |
3139 | 0 | nTotalChunkSize *= chunkSize[i]; |
3140 | 0 | } |
3141 | | |
3142 | 0 | size_t dimIdx = 0; |
3143 | 0 | std::vector<GUInt64> chunkArrayStartIdx(dims.size()); |
3144 | 0 | std::vector<size_t> chunkCount(dims.size()); |
3145 | |
|
3146 | 0 | struct Stack |
3147 | 0 | { |
3148 | 0 | GUInt64 nBlockCounter = 0; |
3149 | 0 | GUInt64 nBlocksMinusOne = 0; |
3150 | 0 | size_t first_count = 0; // only used if nBlocks > 1 |
3151 | 0 | Caller return_point = Caller::CALLER_END_OF_LOOP; |
3152 | 0 | }; |
3153 | |
|
3154 | 0 | std::vector<Stack> stack(dims.size()); |
3155 | 0 | GUInt64 iCurChunk = 0; |
3156 | 0 | GUInt64 nChunkCount = 1; |
3157 | 0 | for (size_t i = 0; i < dims.size(); i++) |
3158 | 0 | { |
3159 | 0 | const auto nStartBlock = arrayStartIdx[i] / chunkSize[i]; |
3160 | 0 | const auto nEndBlock = (arrayStartIdx[i] + count[i] - 1) / chunkSize[i]; |
3161 | 0 | stack[i].nBlocksMinusOne = nEndBlock - nStartBlock; |
3162 | 0 | nChunkCount *= 1 + stack[i].nBlocksMinusOne; |
3163 | 0 | if (stack[i].nBlocksMinusOne == 0) |
3164 | 0 | { |
3165 | 0 | chunkArrayStartIdx[i] = arrayStartIdx[i]; |
3166 | 0 | chunkCount[i] = static_cast<size_t>(count[i]); |
3167 | 0 | } |
3168 | 0 | else |
3169 | 0 | { |
3170 | 0 | stack[i].first_count = static_cast<size_t>( |
3171 | 0 | (nStartBlock + 1) * chunkSize[i] - arrayStartIdx[i]); |
3172 | 0 | } |
3173 | 0 | } |
3174 | |
|
3175 | 0 | lbl_next_depth: |
3176 | 0 | if (dimIdx == dims.size()) |
3177 | 0 | { |
3178 | 0 | ++iCurChunk; |
3179 | 0 | if (!pfnFunc(this, chunkArrayStartIdx.data(), chunkCount.data(), |
3180 | 0 | iCurChunk, nChunkCount, pUserData)) |
3181 | 0 | { |
3182 | 0 | return false; |
3183 | 0 | } |
3184 | 0 | } |
3185 | 0 | else |
3186 | 0 | { |
3187 | 0 | if (stack[dimIdx].nBlocksMinusOne != 0) |
3188 | 0 | { |
3189 | 0 | stack[dimIdx].nBlockCounter = stack[dimIdx].nBlocksMinusOne; |
3190 | 0 | chunkArrayStartIdx[dimIdx] = arrayStartIdx[dimIdx]; |
3191 | 0 | chunkCount[dimIdx] = stack[dimIdx].first_count; |
3192 | 0 | stack[dimIdx].return_point = Caller::CALLER_IN_LOOP; |
3193 | 0 | while (true) |
3194 | 0 | { |
3195 | 0 | dimIdx++; |
3196 | 0 | goto lbl_next_depth; |
3197 | 0 | lbl_return_to_caller_in_loop: |
3198 | 0 | --stack[dimIdx].nBlockCounter; |
3199 | 0 | if (stack[dimIdx].nBlockCounter == 0) |
3200 | 0 | break; |
3201 | 0 | chunkArrayStartIdx[dimIdx] += chunkCount[dimIdx]; |
3202 | 0 | chunkCount[dimIdx] = chunkSize[dimIdx]; |
3203 | 0 | } |
3204 | | |
3205 | 0 | chunkArrayStartIdx[dimIdx] += chunkCount[dimIdx]; |
3206 | 0 | chunkCount[dimIdx] = |
3207 | 0 | static_cast<size_t>(arrayStartIdx[dimIdx] + count[dimIdx] - |
3208 | 0 | chunkArrayStartIdx[dimIdx]); |
3209 | 0 | stack[dimIdx].return_point = Caller::CALLER_END_OF_LOOP; |
3210 | 0 | } |
3211 | 0 | dimIdx++; |
3212 | 0 | goto lbl_next_depth; |
3213 | 0 | lbl_return_to_caller_end_of_loop: |
3214 | 0 | if (dimIdx == 0) |
3215 | 0 | goto end; |
3216 | 0 | } |
3217 | | |
3218 | 0 | assert(dimIdx > 0); |
3219 | 0 | dimIdx--; |
3220 | | // cppcheck-suppress negativeContainerIndex |
3221 | 0 | switch (stack[dimIdx].return_point) |
3222 | 0 | { |
3223 | 0 | case Caller::CALLER_END_OF_LOOP: |
3224 | 0 | goto lbl_return_to_caller_end_of_loop; |
3225 | 0 | case Caller::CALLER_IN_LOOP: |
3226 | 0 | goto lbl_return_to_caller_in_loop; |
3227 | 0 | } |
3228 | 0 | end: |
3229 | 0 | return true; |
3230 | 0 | } |
3231 | | |
3232 | | /************************************************************************/ |
3233 | | /* GDALAttribute() */ |
3234 | | /************************************************************************/ |
3235 | | |
3236 | | //! @cond Doxygen_Suppress |
3237 | | GDALAttribute::GDALAttribute(CPL_UNUSED const std::string &osParentName, |
3238 | | CPL_UNUSED const std::string &osName) |
3239 | | #if !defined(COMPILER_WARNS_ABOUT_ABSTRACT_VBASE_INIT) |
3240 | | : GDALAbstractMDArray(osParentName, osName) |
3241 | | #endif |
3242 | 0 | { |
3243 | 0 | } |
3244 | | |
3245 | 0 | GDALAttribute::~GDALAttribute() = default; |
3246 | | |
3247 | | //! @endcond |
3248 | | |
3249 | | /************************************************************************/ |
3250 | | /* GetDimensionSize() */ |
3251 | | /************************************************************************/ |
3252 | | |
3253 | | /** Return the size of the dimensions of the attribute. |
3254 | | * |
3255 | | * This will be an empty array for a scalar (single value) attribute. |
3256 | | * |
3257 | | * This is the same as the C function GDALAttributeGetDimensionsSize(). |
3258 | | */ |
3259 | | std::vector<GUInt64> GDALAttribute::GetDimensionsSize() const |
3260 | 0 | { |
3261 | 0 | const auto &dims = GetDimensions(); |
3262 | 0 | std::vector<GUInt64> ret; |
3263 | 0 | ret.reserve(dims.size()); |
3264 | 0 | for (const auto &dim : dims) |
3265 | 0 | ret.push_back(dim->GetSize()); |
3266 | 0 | return ret; |
3267 | 0 | } |
3268 | | |
3269 | | /************************************************************************/ |
3270 | | /* GDALRawResult() */ |
3271 | | /************************************************************************/ |
3272 | | |
3273 | | //! @cond Doxygen_Suppress |
3274 | | GDALRawResult::GDALRawResult(GByte *raw, const GDALExtendedDataType &dt, |
3275 | | size_t nEltCount) |
3276 | 0 | : m_dt(dt), m_nEltCount(nEltCount), m_nSize(nEltCount * dt.GetSize()), |
3277 | 0 | m_raw(raw) |
3278 | 0 | { |
3279 | 0 | } |
3280 | | |
3281 | | //! @endcond |
3282 | | |
3283 | | /************************************************************************/ |
3284 | | /* GDALRawResult() */ |
3285 | | /************************************************************************/ |
3286 | | |
3287 | | /** Move constructor. */ |
3288 | | GDALRawResult::GDALRawResult(GDALRawResult &&other) |
3289 | 0 | : m_dt(std::move(other.m_dt)), m_nEltCount(other.m_nEltCount), |
3290 | 0 | m_nSize(other.m_nSize), m_raw(other.m_raw) |
3291 | 0 | { |
3292 | 0 | other.m_nEltCount = 0; |
3293 | 0 | other.m_nSize = 0; |
3294 | 0 | other.m_raw = nullptr; |
3295 | 0 | } |
3296 | | |
3297 | | /************************************************************************/ |
3298 | | /* FreeMe() */ |
3299 | | /************************************************************************/ |
3300 | | |
3301 | | void GDALRawResult::FreeMe() |
3302 | 0 | { |
3303 | 0 | if (m_raw && m_dt.NeedsFreeDynamicMemory()) |
3304 | 0 | { |
3305 | 0 | GByte *pabyPtr = m_raw; |
3306 | 0 | const auto nDTSize(m_dt.GetSize()); |
3307 | 0 | for (size_t i = 0; i < m_nEltCount; ++i) |
3308 | 0 | { |
3309 | 0 | m_dt.FreeDynamicMemory(pabyPtr); |
3310 | 0 | pabyPtr += nDTSize; |
3311 | 0 | } |
3312 | 0 | } |
3313 | 0 | VSIFree(m_raw); |
3314 | 0 | } |
3315 | | |
3316 | | /************************************************************************/ |
3317 | | /* operator=() */ |
3318 | | /************************************************************************/ |
3319 | | |
3320 | | /** Move assignment. */ |
3321 | | GDALRawResult &GDALRawResult::operator=(GDALRawResult &&other) |
3322 | 0 | { |
3323 | 0 | FreeMe(); |
3324 | 0 | m_dt = std::move(other.m_dt); |
3325 | 0 | m_nEltCount = other.m_nEltCount; |
3326 | 0 | m_nSize = other.m_nSize; |
3327 | 0 | m_raw = other.m_raw; |
3328 | 0 | other.m_nEltCount = 0; |
3329 | 0 | other.m_nSize = 0; |
3330 | 0 | other.m_raw = nullptr; |
3331 | 0 | return *this; |
3332 | 0 | } |
3333 | | |
3334 | | /************************************************************************/ |
3335 | | /* ~GDALRawResult() */ |
3336 | | /************************************************************************/ |
3337 | | |
3338 | | /** Destructor. */ |
3339 | | GDALRawResult::~GDALRawResult() |
3340 | 0 | { |
3341 | 0 | FreeMe(); |
3342 | 0 | } |
3343 | | |
3344 | | /************************************************************************/ |
3345 | | /* StealData() */ |
3346 | | /************************************************************************/ |
3347 | | |
3348 | | //! @cond Doxygen_Suppress |
3349 | | /** Return buffer to caller which becomes owner of it. |
3350 | | * Only to be used by GDALAttributeReadAsRaw(). |
3351 | | */ |
3352 | | GByte *GDALRawResult::StealData() |
3353 | 0 | { |
3354 | 0 | GByte *ret = m_raw; |
3355 | 0 | m_raw = nullptr; |
3356 | 0 | m_nEltCount = 0; |
3357 | 0 | m_nSize = 0; |
3358 | 0 | return ret; |
3359 | 0 | } |
3360 | | |
3361 | | //! @endcond |
3362 | | |
3363 | | /************************************************************************/ |
3364 | | /* ReadAsRaw() */ |
3365 | | /************************************************************************/ |
3366 | | |
3367 | | /** Return the raw value of an attribute. |
3368 | | * |
3369 | | * |
3370 | | * This is the same as the C function GDALAttributeReadAsRaw(). |
3371 | | */ |
3372 | | GDALRawResult GDALAttribute::ReadAsRaw() const |
3373 | 0 | { |
3374 | 0 | const auto nEltCount(GetTotalElementsCount()); |
3375 | 0 | const auto &dt(GetDataType()); |
3376 | 0 | const auto nDTSize(dt.GetSize()); |
3377 | 0 | GByte *res = static_cast<GByte *>( |
3378 | 0 | VSI_MALLOC2_VERBOSE(static_cast<size_t>(nEltCount), nDTSize)); |
3379 | 0 | if (!res) |
3380 | 0 | return GDALRawResult(nullptr, dt, 0); |
3381 | 0 | const auto &dims = GetDimensions(); |
3382 | 0 | const auto nDims = GetDimensionCount(); |
3383 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3384 | 0 | std::vector<size_t> count(1 + nDims); |
3385 | 0 | for (size_t i = 0; i < nDims; i++) |
3386 | 0 | { |
3387 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3388 | 0 | } |
3389 | 0 | if (!Read(startIdx.data(), count.data(), nullptr, nullptr, dt, &res[0], |
3390 | 0 | &res[0], static_cast<size_t>(nEltCount * nDTSize))) |
3391 | 0 | { |
3392 | 0 | VSIFree(res); |
3393 | 0 | return GDALRawResult(nullptr, dt, 0); |
3394 | 0 | } |
3395 | 0 | return GDALRawResult(res, dt, static_cast<size_t>(nEltCount)); |
3396 | 0 | } |
3397 | | |
3398 | | /************************************************************************/ |
3399 | | /* ReadAsString() */ |
3400 | | /************************************************************************/ |
3401 | | |
3402 | | /** Return the value of an attribute as a string. |
3403 | | * |
3404 | | * The returned string should not be freed, and its lifetime does not |
3405 | | * excess a next call to ReadAsString() on the same object, or the deletion |
3406 | | * of the object itself. |
3407 | | * |
3408 | | * This function will only return the first element if there are several. |
3409 | | * |
3410 | | * This is the same as the C function GDALAttributeReadAsString() |
3411 | | * |
3412 | | * @return a string, or nullptr. |
3413 | | */ |
3414 | | const char *GDALAttribute::ReadAsString() const |
3415 | 0 | { |
3416 | 0 | const auto nDims = GetDimensionCount(); |
3417 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3418 | 0 | std::vector<size_t> count(1 + nDims, 1); |
3419 | 0 | char *szRet = nullptr; |
3420 | 0 | if (!Read(startIdx.data(), count.data(), nullptr, nullptr, |
3421 | 0 | GDALExtendedDataType::CreateString(), &szRet, &szRet, |
3422 | 0 | sizeof(szRet)) || |
3423 | 0 | szRet == nullptr) |
3424 | 0 | { |
3425 | 0 | return nullptr; |
3426 | 0 | } |
3427 | 0 | m_osCachedVal = szRet; |
3428 | 0 | CPLFree(szRet); |
3429 | 0 | return m_osCachedVal.c_str(); |
3430 | 0 | } |
3431 | | |
3432 | | /************************************************************************/ |
3433 | | /* ReadAsInt() */ |
3434 | | /************************************************************************/ |
3435 | | |
3436 | | /** Return the value of an attribute as a integer. |
3437 | | * |
3438 | | * This function will only return the first element if there are several. |
3439 | | * |
3440 | | * It can fail if its value can not be converted to integer. |
3441 | | * |
3442 | | * This is the same as the C function GDALAttributeReadAsInt() |
3443 | | * |
3444 | | * @return a integer, or INT_MIN in case of error. |
3445 | | */ |
3446 | | int GDALAttribute::ReadAsInt() const |
3447 | 0 | { |
3448 | 0 | const auto nDims = GetDimensionCount(); |
3449 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3450 | 0 | std::vector<size_t> count(1 + nDims, 1); |
3451 | 0 | int nRet = INT_MIN; |
3452 | 0 | Read(startIdx.data(), count.data(), nullptr, nullptr, |
3453 | 0 | GDALExtendedDataType::Create(GDT_Int32), &nRet, &nRet, sizeof(nRet)); |
3454 | 0 | return nRet; |
3455 | 0 | } |
3456 | | |
3457 | | /************************************************************************/ |
3458 | | /* ReadAsInt64() */ |
3459 | | /************************************************************************/ |
3460 | | |
3461 | | /** Return the value of an attribute as an int64_t. |
3462 | | * |
3463 | | * This function will only return the first element if there are several. |
3464 | | * |
3465 | | * It can fail if its value can not be converted to long. |
3466 | | * |
3467 | | * This is the same as the C function GDALAttributeReadAsInt64() |
3468 | | * |
3469 | | * @return an int64_t, or INT64_MIN in case of error. |
3470 | | */ |
3471 | | int64_t GDALAttribute::ReadAsInt64() const |
3472 | 0 | { |
3473 | 0 | const auto nDims = GetDimensionCount(); |
3474 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3475 | 0 | std::vector<size_t> count(1 + nDims, 1); |
3476 | 0 | int64_t nRet = INT64_MIN; |
3477 | 0 | Read(startIdx.data(), count.data(), nullptr, nullptr, |
3478 | 0 | GDALExtendedDataType::Create(GDT_Int64), &nRet, &nRet, sizeof(nRet)); |
3479 | 0 | return nRet; |
3480 | 0 | } |
3481 | | |
3482 | | /************************************************************************/ |
3483 | | /* ReadAsDouble() */ |
3484 | | /************************************************************************/ |
3485 | | |
3486 | | /** Return the value of an attribute as a double. |
3487 | | * |
3488 | | * This function will only return the first element if there are several. |
3489 | | * |
3490 | | * It can fail if its value can not be converted to double. |
3491 | | * |
3492 | | * This is the same as the C function GDALAttributeReadAsInt() |
3493 | | * |
3494 | | * @return a double value. |
3495 | | */ |
3496 | | double GDALAttribute::ReadAsDouble() const |
3497 | 0 | { |
3498 | 0 | const auto nDims = GetDimensionCount(); |
3499 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3500 | 0 | std::vector<size_t> count(1 + nDims, 1); |
3501 | 0 | double dfRet = 0; |
3502 | 0 | Read(startIdx.data(), count.data(), nullptr, nullptr, |
3503 | 0 | GDALExtendedDataType::Create(GDT_Float64), &dfRet, &dfRet, |
3504 | 0 | sizeof(dfRet)); |
3505 | 0 | return dfRet; |
3506 | 0 | } |
3507 | | |
3508 | | /************************************************************************/ |
3509 | | /* ReadAsStringArray() */ |
3510 | | /************************************************************************/ |
3511 | | |
3512 | | /** Return the value of an attribute as an array of strings. |
3513 | | * |
3514 | | * This is the same as the C function GDALAttributeReadAsStringArray() |
3515 | | */ |
3516 | | CPLStringList GDALAttribute::ReadAsStringArray() const |
3517 | 0 | { |
3518 | 0 | const auto nElts = GetTotalElementsCount(); |
3519 | 0 | if (nElts > static_cast<unsigned>(std::numeric_limits<int>::max() - 1)) |
3520 | 0 | return CPLStringList(); |
3521 | 0 | char **papszList = static_cast<char **>( |
3522 | 0 | VSI_CALLOC_VERBOSE(static_cast<int>(nElts) + 1, sizeof(char *))); |
3523 | 0 | const auto &dims = GetDimensions(); |
3524 | 0 | const auto nDims = GetDimensionCount(); |
3525 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3526 | 0 | std::vector<size_t> count(1 + nDims); |
3527 | 0 | for (size_t i = 0; i < nDims; i++) |
3528 | 0 | { |
3529 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3530 | 0 | } |
3531 | 0 | Read(startIdx.data(), count.data(), nullptr, nullptr, |
3532 | 0 | GDALExtendedDataType::CreateString(), papszList, papszList, |
3533 | 0 | sizeof(char *) * static_cast<int>(nElts)); |
3534 | 0 | for (int i = 0; i < static_cast<int>(nElts); i++) |
3535 | 0 | { |
3536 | 0 | if (papszList[i] == nullptr) |
3537 | 0 | papszList[i] = CPLStrdup(""); |
3538 | 0 | } |
3539 | 0 | return CPLStringList(papszList); |
3540 | 0 | } |
3541 | | |
3542 | | /************************************************************************/ |
3543 | | /* ReadAsIntArray() */ |
3544 | | /************************************************************************/ |
3545 | | |
3546 | | /** Return the value of an attribute as an array of integers. |
3547 | | * |
3548 | | * This is the same as the C function GDALAttributeReadAsIntArray(). |
3549 | | */ |
3550 | | std::vector<int> GDALAttribute::ReadAsIntArray() const |
3551 | 0 | { |
3552 | 0 | const auto nElts = GetTotalElementsCount(); |
3553 | | #if SIZEOF_VOIDP == 4 |
3554 | | if (nElts > static_cast<size_t>(nElts)) |
3555 | | return {}; |
3556 | | #endif |
3557 | 0 | std::vector<int> res(static_cast<size_t>(nElts)); |
3558 | 0 | const auto &dims = GetDimensions(); |
3559 | 0 | const auto nDims = GetDimensionCount(); |
3560 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3561 | 0 | std::vector<size_t> count(1 + nDims); |
3562 | 0 | for (size_t i = 0; i < nDims; i++) |
3563 | 0 | { |
3564 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3565 | 0 | } |
3566 | 0 | Read(startIdx.data(), count.data(), nullptr, nullptr, |
3567 | 0 | GDALExtendedDataType::Create(GDT_Int32), &res[0], res.data(), |
3568 | 0 | res.size() * sizeof(res[0])); |
3569 | 0 | return res; |
3570 | 0 | } |
3571 | | |
3572 | | /************************************************************************/ |
3573 | | /* ReadAsInt64Array() */ |
3574 | | /************************************************************************/ |
3575 | | |
3576 | | /** Return the value of an attribute as an array of int64_t. |
3577 | | * |
3578 | | * This is the same as the C function GDALAttributeReadAsInt64Array(). |
3579 | | */ |
3580 | | std::vector<int64_t> GDALAttribute::ReadAsInt64Array() const |
3581 | 0 | { |
3582 | 0 | const auto nElts = GetTotalElementsCount(); |
3583 | | #if SIZEOF_VOIDP == 4 |
3584 | | if (nElts > static_cast<size_t>(nElts)) |
3585 | | return {}; |
3586 | | #endif |
3587 | 0 | std::vector<int64_t> res(static_cast<size_t>(nElts)); |
3588 | 0 | const auto &dims = GetDimensions(); |
3589 | 0 | const auto nDims = GetDimensionCount(); |
3590 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3591 | 0 | std::vector<size_t> count(1 + nDims); |
3592 | 0 | for (size_t i = 0; i < nDims; i++) |
3593 | 0 | { |
3594 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3595 | 0 | } |
3596 | 0 | Read(startIdx.data(), count.data(), nullptr, nullptr, |
3597 | 0 | GDALExtendedDataType::Create(GDT_Int64), &res[0], res.data(), |
3598 | 0 | res.size() * sizeof(res[0])); |
3599 | 0 | return res; |
3600 | 0 | } |
3601 | | |
3602 | | /************************************************************************/ |
3603 | | /* ReadAsDoubleArray() */ |
3604 | | /************************************************************************/ |
3605 | | |
3606 | | /** Return the value of an attribute as an array of double. |
3607 | | * |
3608 | | * This is the same as the C function GDALAttributeReadAsDoubleArray(). |
3609 | | */ |
3610 | | std::vector<double> GDALAttribute::ReadAsDoubleArray() const |
3611 | 0 | { |
3612 | 0 | const auto nElts = GetTotalElementsCount(); |
3613 | | #if SIZEOF_VOIDP == 4 |
3614 | | if (nElts > static_cast<size_t>(nElts)) |
3615 | | return {}; |
3616 | | #endif |
3617 | 0 | std::vector<double> res(static_cast<size_t>(nElts)); |
3618 | 0 | const auto &dims = GetDimensions(); |
3619 | 0 | const auto nDims = GetDimensionCount(); |
3620 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3621 | 0 | std::vector<size_t> count(1 + nDims); |
3622 | 0 | for (size_t i = 0; i < nDims; i++) |
3623 | 0 | { |
3624 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3625 | 0 | } |
3626 | 0 | Read(startIdx.data(), count.data(), nullptr, nullptr, |
3627 | 0 | GDALExtendedDataType::Create(GDT_Float64), &res[0], res.data(), |
3628 | 0 | res.size() * sizeof(res[0])); |
3629 | 0 | return res; |
3630 | 0 | } |
3631 | | |
3632 | | /************************************************************************/ |
3633 | | /* Write() */ |
3634 | | /************************************************************************/ |
3635 | | |
3636 | | /** Write an attribute from raw values expressed in GetDataType() |
3637 | | * |
3638 | | * The values should be provided in the type of GetDataType() and there should |
3639 | | * be exactly GetTotalElementsCount() of them. |
3640 | | * If GetDataType() is a string, each value should be a char* pointer. |
3641 | | * |
3642 | | * This is the same as the C function GDALAttributeWriteRaw(). |
3643 | | * |
3644 | | * @param pabyValue Buffer of nLen bytes. |
3645 | | * @param nLen Size of pabyValue in bytes. Should be equal to |
3646 | | * GetTotalElementsCount() * GetDataType().GetSize() |
3647 | | * @return true in case of success. |
3648 | | */ |
3649 | | bool GDALAttribute::Write(const void *pabyValue, size_t nLen) |
3650 | 0 | { |
3651 | 0 | if (nLen != GetTotalElementsCount() * GetDataType().GetSize()) |
3652 | 0 | { |
3653 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3654 | 0 | "Length is not of expected value"); |
3655 | 0 | return false; |
3656 | 0 | } |
3657 | 0 | const auto &dims = GetDimensions(); |
3658 | 0 | const auto nDims = GetDimensionCount(); |
3659 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3660 | 0 | std::vector<size_t> count(1 + nDims); |
3661 | 0 | for (size_t i = 0; i < nDims; i++) |
3662 | 0 | { |
3663 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3664 | 0 | } |
3665 | 0 | return Write(startIdx.data(), count.data(), nullptr, nullptr, GetDataType(), |
3666 | 0 | pabyValue, pabyValue, nLen); |
3667 | 0 | } |
3668 | | |
3669 | | /************************************************************************/ |
3670 | | /* Write() */ |
3671 | | /************************************************************************/ |
3672 | | |
3673 | | /** Write an attribute from a string value. |
3674 | | * |
3675 | | * Type conversion will be performed if needed. If the attribute contains |
3676 | | * multiple values, only the first one will be updated. |
3677 | | * |
3678 | | * This is the same as the C function GDALAttributeWriteString(). |
3679 | | * |
3680 | | * @param pszValue Pointer to a string. |
3681 | | * @return true in case of success. |
3682 | | */ |
3683 | | bool GDALAttribute::Write(const char *pszValue) |
3684 | 0 | { |
3685 | 0 | const auto nDims = GetDimensionCount(); |
3686 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3687 | 0 | std::vector<size_t> count(1 + nDims, 1); |
3688 | 0 | return Write(startIdx.data(), count.data(), nullptr, nullptr, |
3689 | 0 | GDALExtendedDataType::CreateString(), &pszValue, &pszValue, |
3690 | 0 | sizeof(pszValue)); |
3691 | 0 | } |
3692 | | |
3693 | | /************************************************************************/ |
3694 | | /* WriteInt() */ |
3695 | | /************************************************************************/ |
3696 | | |
3697 | | /** Write an attribute from a integer value. |
3698 | | * |
3699 | | * Type conversion will be performed if needed. If the attribute contains |
3700 | | * multiple values, only the first one will be updated. |
3701 | | * |
3702 | | * This is the same as the C function GDALAttributeWriteInt(). |
3703 | | * |
3704 | | * @param nVal Value. |
3705 | | * @return true in case of success. |
3706 | | */ |
3707 | | bool GDALAttribute::WriteInt(int nVal) |
3708 | 0 | { |
3709 | 0 | const auto nDims = GetDimensionCount(); |
3710 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3711 | 0 | std::vector<size_t> count(1 + nDims, 1); |
3712 | 0 | return Write(startIdx.data(), count.data(), nullptr, nullptr, |
3713 | 0 | GDALExtendedDataType::Create(GDT_Int32), &nVal, &nVal, |
3714 | 0 | sizeof(nVal)); |
3715 | 0 | } |
3716 | | |
3717 | | /************************************************************************/ |
3718 | | /* WriteInt64() */ |
3719 | | /************************************************************************/ |
3720 | | |
3721 | | /** Write an attribute from an int64_t value. |
3722 | | * |
3723 | | * Type conversion will be performed if needed. If the attribute contains |
3724 | | * multiple values, only the first one will be updated. |
3725 | | * |
3726 | | * This is the same as the C function GDALAttributeWriteInt(). |
3727 | | * |
3728 | | * @param nVal Value. |
3729 | | * @return true in case of success. |
3730 | | */ |
3731 | | bool GDALAttribute::WriteInt64(int64_t nVal) |
3732 | 0 | { |
3733 | 0 | const auto nDims = GetDimensionCount(); |
3734 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3735 | 0 | std::vector<size_t> count(1 + nDims, 1); |
3736 | 0 | return Write(startIdx.data(), count.data(), nullptr, nullptr, |
3737 | 0 | GDALExtendedDataType::Create(GDT_Int64), &nVal, &nVal, |
3738 | 0 | sizeof(nVal)); |
3739 | 0 | } |
3740 | | |
3741 | | /************************************************************************/ |
3742 | | /* Write() */ |
3743 | | /************************************************************************/ |
3744 | | |
3745 | | /** Write an attribute from a double value. |
3746 | | * |
3747 | | * Type conversion will be performed if needed. If the attribute contains |
3748 | | * multiple values, only the first one will be updated. |
3749 | | * |
3750 | | * This is the same as the C function GDALAttributeWriteDouble(). |
3751 | | * |
3752 | | * @param dfVal Value. |
3753 | | * @return true in case of success. |
3754 | | */ |
3755 | | bool GDALAttribute::Write(double dfVal) |
3756 | 0 | { |
3757 | 0 | const auto nDims = GetDimensionCount(); |
3758 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3759 | 0 | std::vector<size_t> count(1 + nDims, 1); |
3760 | 0 | return Write(startIdx.data(), count.data(), nullptr, nullptr, |
3761 | 0 | GDALExtendedDataType::Create(GDT_Float64), &dfVal, &dfVal, |
3762 | 0 | sizeof(dfVal)); |
3763 | 0 | } |
3764 | | |
3765 | | /************************************************************************/ |
3766 | | /* Write() */ |
3767 | | /************************************************************************/ |
3768 | | |
3769 | | /** Write an attribute from an array of strings. |
3770 | | * |
3771 | | * Type conversion will be performed if needed. |
3772 | | * |
3773 | | * Exactly GetTotalElementsCount() strings must be provided |
3774 | | * |
3775 | | * This is the same as the C function GDALAttributeWriteStringArray(). |
3776 | | * |
3777 | | * @param vals Array of strings. |
3778 | | * @return true in case of success. |
3779 | | */ |
3780 | | bool GDALAttribute::Write(CSLConstList vals) |
3781 | 0 | { |
3782 | 0 | if (static_cast<size_t>(CSLCount(vals)) != GetTotalElementsCount()) |
3783 | 0 | { |
3784 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid number of input values"); |
3785 | 0 | return false; |
3786 | 0 | } |
3787 | 0 | const auto nDims = GetDimensionCount(); |
3788 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3789 | 0 | std::vector<size_t> count(1 + nDims); |
3790 | 0 | const auto &dims = GetDimensions(); |
3791 | 0 | for (size_t i = 0; i < nDims; i++) |
3792 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3793 | 0 | return Write(startIdx.data(), count.data(), nullptr, nullptr, |
3794 | 0 | GDALExtendedDataType::CreateString(), vals, vals, |
3795 | 0 | static_cast<size_t>(GetTotalElementsCount()) * sizeof(char *)); |
3796 | 0 | } |
3797 | | |
3798 | | /************************************************************************/ |
3799 | | /* Write() */ |
3800 | | /************************************************************************/ |
3801 | | |
3802 | | /** Write an attribute from an array of int. |
3803 | | * |
3804 | | * Type conversion will be performed if needed. |
3805 | | * |
3806 | | * Exactly GetTotalElementsCount() strings must be provided |
3807 | | * |
3808 | | * This is the same as the C function GDALAttributeWriteIntArray() |
3809 | | * |
3810 | | * @param vals Array of int. |
3811 | | * @param nVals Should be equal to GetTotalElementsCount(). |
3812 | | * @return true in case of success. |
3813 | | */ |
3814 | | bool GDALAttribute::Write(const int *vals, size_t nVals) |
3815 | 0 | { |
3816 | 0 | if (nVals != GetTotalElementsCount()) |
3817 | 0 | { |
3818 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid number of input values"); |
3819 | 0 | return false; |
3820 | 0 | } |
3821 | 0 | const auto nDims = GetDimensionCount(); |
3822 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3823 | 0 | std::vector<size_t> count(1 + nDims); |
3824 | 0 | const auto &dims = GetDimensions(); |
3825 | 0 | for (size_t i = 0; i < nDims; i++) |
3826 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3827 | 0 | return Write(startIdx.data(), count.data(), nullptr, nullptr, |
3828 | 0 | GDALExtendedDataType::Create(GDT_Int32), vals, vals, |
3829 | 0 | static_cast<size_t>(GetTotalElementsCount()) * sizeof(GInt32)); |
3830 | 0 | } |
3831 | | |
3832 | | /************************************************************************/ |
3833 | | /* Write() */ |
3834 | | /************************************************************************/ |
3835 | | |
3836 | | /** Write an attribute from an array of int64_t. |
3837 | | * |
3838 | | * Type conversion will be performed if needed. |
3839 | | * |
3840 | | * Exactly GetTotalElementsCount() strings must be provided |
3841 | | * |
3842 | | * This is the same as the C function GDALAttributeWriteLongArray() |
3843 | | * |
3844 | | * @param vals Array of int64_t. |
3845 | | * @param nVals Should be equal to GetTotalElementsCount(). |
3846 | | * @return true in case of success. |
3847 | | */ |
3848 | | bool GDALAttribute::Write(const int64_t *vals, size_t nVals) |
3849 | 0 | { |
3850 | 0 | if (nVals != GetTotalElementsCount()) |
3851 | 0 | { |
3852 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid number of input values"); |
3853 | 0 | return false; |
3854 | 0 | } |
3855 | 0 | const auto nDims = GetDimensionCount(); |
3856 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3857 | 0 | std::vector<size_t> count(1 + nDims); |
3858 | 0 | const auto &dims = GetDimensions(); |
3859 | 0 | for (size_t i = 0; i < nDims; i++) |
3860 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3861 | 0 | return Write(startIdx.data(), count.data(), nullptr, nullptr, |
3862 | 0 | GDALExtendedDataType::Create(GDT_Int64), vals, vals, |
3863 | 0 | static_cast<size_t>(GetTotalElementsCount()) * |
3864 | 0 | sizeof(int64_t)); |
3865 | 0 | } |
3866 | | |
3867 | | /************************************************************************/ |
3868 | | /* Write() */ |
3869 | | /************************************************************************/ |
3870 | | |
3871 | | /** Write an attribute from an array of double. |
3872 | | * |
3873 | | * Type conversion will be performed if needed. |
3874 | | * |
3875 | | * Exactly GetTotalElementsCount() strings must be provided |
3876 | | * |
3877 | | * This is the same as the C function GDALAttributeWriteDoubleArray() |
3878 | | * |
3879 | | * @param vals Array of double. |
3880 | | * @param nVals Should be equal to GetTotalElementsCount(). |
3881 | | * @return true in case of success. |
3882 | | */ |
3883 | | bool GDALAttribute::Write(const double *vals, size_t nVals) |
3884 | 0 | { |
3885 | 0 | if (nVals != GetTotalElementsCount()) |
3886 | 0 | { |
3887 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid number of input values"); |
3888 | 0 | return false; |
3889 | 0 | } |
3890 | 0 | const auto nDims = GetDimensionCount(); |
3891 | 0 | std::vector<GUInt64> startIdx(1 + nDims, 0); |
3892 | 0 | std::vector<size_t> count(1 + nDims); |
3893 | 0 | const auto &dims = GetDimensions(); |
3894 | 0 | for (size_t i = 0; i < nDims; i++) |
3895 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
3896 | 0 | return Write(startIdx.data(), count.data(), nullptr, nullptr, |
3897 | 0 | GDALExtendedDataType::Create(GDT_Float64), vals, vals, |
3898 | 0 | static_cast<size_t>(GetTotalElementsCount()) * sizeof(double)); |
3899 | 0 | } |
3900 | | |
3901 | | /************************************************************************/ |
3902 | | /* GDALMDArray() */ |
3903 | | /************************************************************************/ |
3904 | | |
3905 | | //! @cond Doxygen_Suppress |
3906 | | GDALMDArray::GDALMDArray(CPL_UNUSED const std::string &osParentName, |
3907 | | CPL_UNUSED const std::string &osName, |
3908 | | const std::string &osContext) |
3909 | | : |
3910 | | #if !defined(COMPILER_WARNS_ABOUT_ABSTRACT_VBASE_INIT) |
3911 | | GDALAbstractMDArray(osParentName, osName), |
3912 | | #endif |
3913 | 0 | m_osContext(osContext) |
3914 | 0 | { |
3915 | 0 | } |
3916 | | |
3917 | | //! @endcond |
3918 | | |
3919 | | /************************************************************************/ |
3920 | | /* GetTotalCopyCost() */ |
3921 | | /************************************************************************/ |
3922 | | |
3923 | | /** Return a total "cost" to copy the array. |
3924 | | * |
3925 | | * Used as a parameter for CopyFrom() |
3926 | | */ |
3927 | | GUInt64 GDALMDArray::GetTotalCopyCost() const |
3928 | 0 | { |
3929 | 0 | return COPY_COST + GetAttributes().size() * GDALAttribute::COPY_COST + |
3930 | 0 | GetTotalElementsCount() * GetDataType().GetSize(); |
3931 | 0 | } |
3932 | | |
3933 | | /************************************************************************/ |
3934 | | /* CopyFromAllExceptValues() */ |
3935 | | /************************************************************************/ |
3936 | | |
3937 | | //! @cond Doxygen_Suppress |
3938 | | |
3939 | | bool GDALMDArray::CopyFromAllExceptValues(const GDALMDArray *poSrcArray, |
3940 | | bool bStrict, GUInt64 &nCurCost, |
3941 | | const GUInt64 nTotalCost, |
3942 | | GDALProgressFunc pfnProgress, |
3943 | | void *pProgressData) |
3944 | 0 | { |
3945 | | // Nodata setting must be one of the first things done for TileDB |
3946 | 0 | const void *pNoData = poSrcArray->GetRawNoDataValue(); |
3947 | 0 | if (pNoData && poSrcArray->GetDataType() == GetDataType()) |
3948 | 0 | { |
3949 | 0 | SetRawNoDataValue(pNoData); |
3950 | 0 | } |
3951 | |
|
3952 | 0 | const bool bThisIsUnscaledArray = |
3953 | 0 | dynamic_cast<GDALMDArrayUnscaled *>(this) != nullptr; |
3954 | 0 | auto attrs = poSrcArray->GetAttributes(); |
3955 | 0 | for (const auto &attr : attrs) |
3956 | 0 | { |
3957 | 0 | const auto &osAttrName = attr->GetName(); |
3958 | 0 | if (bThisIsUnscaledArray) |
3959 | 0 | { |
3960 | 0 | if (osAttrName == "missing_value" || osAttrName == "_FillValue" || |
3961 | 0 | osAttrName == "valid_min" || osAttrName == "valid_max" || |
3962 | 0 | osAttrName == "valid_range") |
3963 | 0 | { |
3964 | 0 | continue; |
3965 | 0 | } |
3966 | 0 | } |
3967 | | |
3968 | 0 | auto dstAttr = CreateAttribute(osAttrName, attr->GetDimensionsSize(), |
3969 | 0 | attr->GetDataType()); |
3970 | 0 | if (!dstAttr) |
3971 | 0 | { |
3972 | 0 | if (bStrict) |
3973 | 0 | return false; |
3974 | 0 | continue; |
3975 | 0 | } |
3976 | 0 | auto raw = attr->ReadAsRaw(); |
3977 | 0 | if (!dstAttr->Write(raw.data(), raw.size()) && bStrict) |
3978 | 0 | return false; |
3979 | 0 | } |
3980 | 0 | if (!attrs.empty()) |
3981 | 0 | { |
3982 | 0 | nCurCost += attrs.size() * GDALAttribute::COPY_COST; |
3983 | 0 | if (pfnProgress && |
3984 | 0 | !pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData)) |
3985 | 0 | return false; |
3986 | 0 | } |
3987 | | |
3988 | 0 | auto srcSRS = poSrcArray->GetSpatialRef(); |
3989 | 0 | if (srcSRS) |
3990 | 0 | { |
3991 | 0 | SetSpatialRef(srcSRS.get()); |
3992 | 0 | } |
3993 | |
|
3994 | 0 | const std::string &osUnit(poSrcArray->GetUnit()); |
3995 | 0 | if (!osUnit.empty()) |
3996 | 0 | { |
3997 | 0 | SetUnit(osUnit); |
3998 | 0 | } |
3999 | |
|
4000 | 0 | bool bGotValue = false; |
4001 | 0 | GDALDataType eOffsetStorageType = GDT_Unknown; |
4002 | 0 | const double dfOffset = |
4003 | 0 | poSrcArray->GetOffset(&bGotValue, &eOffsetStorageType); |
4004 | 0 | if (bGotValue) |
4005 | 0 | { |
4006 | 0 | SetOffset(dfOffset, eOffsetStorageType); |
4007 | 0 | } |
4008 | |
|
4009 | 0 | bGotValue = false; |
4010 | 0 | GDALDataType eScaleStorageType = GDT_Unknown; |
4011 | 0 | const double dfScale = poSrcArray->GetScale(&bGotValue, &eScaleStorageType); |
4012 | 0 | if (bGotValue) |
4013 | 0 | { |
4014 | 0 | SetScale(dfScale, eScaleStorageType); |
4015 | 0 | } |
4016 | |
|
4017 | 0 | return true; |
4018 | 0 | } |
4019 | | |
4020 | | //! @endcond |
4021 | | |
4022 | | /************************************************************************/ |
4023 | | /* CopyFrom() */ |
4024 | | /************************************************************************/ |
4025 | | |
4026 | | /** Copy the content of an array into a new (generally empty) array. |
4027 | | * |
4028 | | * @param poSrcDS Source dataset. Might be nullptr (but for correct behavior |
4029 | | * of some output drivers this is not recommended) |
4030 | | * @param poSrcArray Source array. Should NOT be nullptr. |
4031 | | * @param bStrict Whether to enable strict mode. In strict mode, any error will |
4032 | | * stop the copy. In relaxed mode, the copy will be attempted to |
4033 | | * be pursued. |
4034 | | * @param nCurCost Should be provided as a variable initially set to 0. |
4035 | | * @param nTotalCost Total cost from GetTotalCopyCost(). |
4036 | | * @param pfnProgress Progress callback, or nullptr. |
4037 | | * @param pProgressData Progress user data, or nulptr. |
4038 | | * |
4039 | | * @return true in case of success (or partial success if bStrict == false). |
4040 | | */ |
4041 | | bool GDALMDArray::CopyFrom(CPL_UNUSED GDALDataset *poSrcDS, |
4042 | | const GDALMDArray *poSrcArray, bool bStrict, |
4043 | | GUInt64 &nCurCost, const GUInt64 nTotalCost, |
4044 | | GDALProgressFunc pfnProgress, void *pProgressData) |
4045 | 0 | { |
4046 | 0 | if (pfnProgress == nullptr) |
4047 | 0 | pfnProgress = GDALDummyProgress; |
4048 | |
|
4049 | 0 | nCurCost += GDALMDArray::COPY_COST; |
4050 | |
|
4051 | 0 | if (!CopyFromAllExceptValues(poSrcArray, bStrict, nCurCost, nTotalCost, |
4052 | 0 | pfnProgress, pProgressData)) |
4053 | 0 | { |
4054 | 0 | return false; |
4055 | 0 | } |
4056 | | |
4057 | 0 | const auto &dims = poSrcArray->GetDimensions(); |
4058 | 0 | const auto nDTSize = poSrcArray->GetDataType().GetSize(); |
4059 | 0 | if (dims.empty()) |
4060 | 0 | { |
4061 | 0 | std::vector<GByte> abyTmp(nDTSize); |
4062 | 0 | if (!(poSrcArray->Read(nullptr, nullptr, nullptr, nullptr, |
4063 | 0 | GetDataType(), &abyTmp[0]) && |
4064 | 0 | Write(nullptr, nullptr, nullptr, nullptr, GetDataType(), |
4065 | 0 | &abyTmp[0])) && |
4066 | 0 | bStrict) |
4067 | 0 | { |
4068 | 0 | return false; |
4069 | 0 | } |
4070 | 0 | nCurCost += GetTotalElementsCount() * GetDataType().GetSize(); |
4071 | 0 | if (!pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData)) |
4072 | 0 | return false; |
4073 | 0 | } |
4074 | 0 | else |
4075 | 0 | { |
4076 | 0 | std::vector<GUInt64> arrayStartIdx(dims.size()); |
4077 | 0 | std::vector<GUInt64> count(dims.size()); |
4078 | 0 | for (size_t i = 0; i < dims.size(); i++) |
4079 | 0 | { |
4080 | 0 | count[i] = static_cast<size_t>(dims[i]->GetSize()); |
4081 | 0 | } |
4082 | |
|
4083 | 0 | struct CopyFunc |
4084 | 0 | { |
4085 | 0 | GDALMDArray *poDstArray = nullptr; |
4086 | 0 | std::vector<GByte> abyTmp{}; |
4087 | 0 | GDALProgressFunc pfnProgress = nullptr; |
4088 | 0 | void *pProgressData = nullptr; |
4089 | 0 | GUInt64 nCurCost = 0; |
4090 | 0 | GUInt64 nTotalCost = 0; |
4091 | 0 | GUInt64 nTotalBytesThisArray = 0; |
4092 | 0 | bool bStop = false; |
4093 | |
|
4094 | 0 | static bool f(GDALAbstractMDArray *l_poSrcArray, |
4095 | 0 | const GUInt64 *chunkArrayStartIdx, |
4096 | 0 | const size_t *chunkCount, GUInt64 iCurChunk, |
4097 | 0 | GUInt64 nChunkCount, void *pUserData) |
4098 | 0 | { |
4099 | 0 | const auto &dt(l_poSrcArray->GetDataType()); |
4100 | 0 | auto data = static_cast<CopyFunc *>(pUserData); |
4101 | 0 | auto poDstArray = data->poDstArray; |
4102 | 0 | if (!l_poSrcArray->Read(chunkArrayStartIdx, chunkCount, nullptr, |
4103 | 0 | nullptr, dt, &data->abyTmp[0])) |
4104 | 0 | { |
4105 | 0 | return false; |
4106 | 0 | } |
4107 | 0 | bool bRet = |
4108 | 0 | poDstArray->Write(chunkArrayStartIdx, chunkCount, nullptr, |
4109 | 0 | nullptr, dt, &data->abyTmp[0]); |
4110 | 0 | if (dt.NeedsFreeDynamicMemory()) |
4111 | 0 | { |
4112 | 0 | const auto l_nDTSize = dt.GetSize(); |
4113 | 0 | GByte *ptr = &data->abyTmp[0]; |
4114 | 0 | const size_t l_nDims(l_poSrcArray->GetDimensionCount()); |
4115 | 0 | size_t nEltCount = 1; |
4116 | 0 | for (size_t i = 0; i < l_nDims; ++i) |
4117 | 0 | { |
4118 | 0 | nEltCount *= chunkCount[i]; |
4119 | 0 | } |
4120 | 0 | for (size_t i = 0; i < nEltCount; i++) |
4121 | 0 | { |
4122 | 0 | dt.FreeDynamicMemory(ptr); |
4123 | 0 | ptr += l_nDTSize; |
4124 | 0 | } |
4125 | 0 | } |
4126 | 0 | if (!bRet) |
4127 | 0 | { |
4128 | 0 | return false; |
4129 | 0 | } |
4130 | | |
4131 | 0 | double dfCurCost = |
4132 | 0 | double(data->nCurCost) + double(iCurChunk) / nChunkCount * |
4133 | 0 | data->nTotalBytesThisArray; |
4134 | 0 | if (!data->pfnProgress(dfCurCost / data->nTotalCost, "", |
4135 | 0 | data->pProgressData)) |
4136 | 0 | { |
4137 | 0 | data->bStop = true; |
4138 | 0 | return false; |
4139 | 0 | } |
4140 | | |
4141 | 0 | return true; |
4142 | 0 | } |
4143 | 0 | }; |
4144 | |
|
4145 | 0 | CopyFunc copyFunc; |
4146 | 0 | copyFunc.poDstArray = this; |
4147 | 0 | copyFunc.nCurCost = nCurCost; |
4148 | 0 | copyFunc.nTotalCost = nTotalCost; |
4149 | 0 | copyFunc.nTotalBytesThisArray = GetTotalElementsCount() * nDTSize; |
4150 | 0 | copyFunc.pfnProgress = pfnProgress; |
4151 | 0 | copyFunc.pProgressData = pProgressData; |
4152 | 0 | const char *pszSwathSize = |
4153 | 0 | CPLGetConfigOption("GDAL_SWATH_SIZE", nullptr); |
4154 | 0 | const size_t nMaxChunkSize = |
4155 | 0 | pszSwathSize |
4156 | 0 | ? static_cast<size_t>( |
4157 | 0 | std::min(GIntBig(std::numeric_limits<size_t>::max() / 2), |
4158 | 0 | CPLAtoGIntBig(pszSwathSize))) |
4159 | 0 | : static_cast<size_t>( |
4160 | 0 | std::min(GIntBig(std::numeric_limits<size_t>::max() / 2), |
4161 | 0 | GDALGetCacheMax64() / 4)); |
4162 | 0 | const auto anChunkSizes(GetProcessingChunkSize(nMaxChunkSize)); |
4163 | 0 | size_t nRealChunkSize = nDTSize; |
4164 | 0 | for (const auto &nChunkSize : anChunkSizes) |
4165 | 0 | { |
4166 | 0 | nRealChunkSize *= nChunkSize; |
4167 | 0 | } |
4168 | 0 | try |
4169 | 0 | { |
4170 | 0 | copyFunc.abyTmp.resize(nRealChunkSize); |
4171 | 0 | } |
4172 | 0 | catch (const std::exception &) |
4173 | 0 | { |
4174 | 0 | CPLError(CE_Failure, CPLE_OutOfMemory, |
4175 | 0 | "Cannot allocate temporary buffer"); |
4176 | 0 | nCurCost += copyFunc.nTotalBytesThisArray; |
4177 | 0 | return false; |
4178 | 0 | } |
4179 | 0 | if (copyFunc.nTotalBytesThisArray != 0 && |
4180 | 0 | !const_cast<GDALMDArray *>(poSrcArray) |
4181 | 0 | ->ProcessPerChunk(arrayStartIdx.data(), count.data(), |
4182 | 0 | anChunkSizes.data(), CopyFunc::f, |
4183 | 0 | ©Func) && |
4184 | 0 | (bStrict || copyFunc.bStop)) |
4185 | 0 | { |
4186 | 0 | nCurCost += copyFunc.nTotalBytesThisArray; |
4187 | 0 | return false; |
4188 | 0 | } |
4189 | 0 | nCurCost += copyFunc.nTotalBytesThisArray; |
4190 | 0 | } |
4191 | | |
4192 | 0 | return true; |
4193 | 0 | } |
4194 | | |
4195 | | /************************************************************************/ |
4196 | | /* GetStructuralInfo() */ |
4197 | | /************************************************************************/ |
4198 | | |
4199 | | /** Return structural information on the array. |
4200 | | * |
4201 | | * This may be the compression, etc.. |
4202 | | * |
4203 | | * The return value should not be freed and is valid until GDALMDArray is |
4204 | | * released or this function called again. |
4205 | | * |
4206 | | * This is the same as the C function GDALMDArrayGetStructuralInfo(). |
4207 | | */ |
4208 | | CSLConstList GDALMDArray::GetStructuralInfo() const |
4209 | 0 | { |
4210 | 0 | return nullptr; |
4211 | 0 | } |
4212 | | |
4213 | | /************************************************************************/ |
4214 | | /* AdviseRead() */ |
4215 | | /************************************************************************/ |
4216 | | |
4217 | | /** Advise driver of upcoming read requests. |
4218 | | * |
4219 | | * Some GDAL drivers operate more efficiently if they know in advance what |
4220 | | * set of upcoming read requests will be made. The AdviseRead() method allows |
4221 | | * an application to notify the driver of the region of interest. |
4222 | | * |
4223 | | * Many drivers just ignore the AdviseRead() call, but it can dramatically |
4224 | | * accelerate access via some drivers. One such case is when reading through |
4225 | | * a DAP dataset with the netCDF driver (a in-memory cache array is then created |
4226 | | * with the region of interest defined by AdviseRead()) |
4227 | | * |
4228 | | * This is the same as the C function GDALMDArrayAdviseRead(). |
4229 | | * |
4230 | | * @param arrayStartIdx Values representing the starting index to read |
4231 | | * in each dimension (in [0, aoDims[i].GetSize()-1] range). |
4232 | | * Array of GetDimensionCount() values. |
4233 | | * Can be nullptr as a synonymous for [0 for i in |
4234 | | * range(GetDimensionCount() ] |
4235 | | * |
4236 | | * @param count Values representing the number of values to extract in |
4237 | | * each dimension. |
4238 | | * Array of GetDimensionCount() values. |
4239 | | * Can be nullptr as a synonymous for |
4240 | | * [ aoDims[i].GetSize() - arrayStartIdx[i] for i in |
4241 | | * range(GetDimensionCount() ] |
4242 | | * |
4243 | | * @param papszOptions Driver specific options, or nullptr. Consult driver |
4244 | | * documentation. |
4245 | | * |
4246 | | * @return true in case of success (ignoring the advice is a success) |
4247 | | * |
4248 | | * @since GDAL 3.2 |
4249 | | */ |
4250 | | bool GDALMDArray::AdviseRead(const GUInt64 *arrayStartIdx, const size_t *count, |
4251 | | CSLConstList papszOptions) const |
4252 | 0 | { |
4253 | 0 | const auto nDimCount = GetDimensionCount(); |
4254 | 0 | if (nDimCount == 0) |
4255 | 0 | return true; |
4256 | | |
4257 | 0 | std::vector<GUInt64> tmp_arrayStartIdx; |
4258 | 0 | if (arrayStartIdx == nullptr) |
4259 | 0 | { |
4260 | 0 | tmp_arrayStartIdx.resize(nDimCount); |
4261 | 0 | arrayStartIdx = tmp_arrayStartIdx.data(); |
4262 | 0 | } |
4263 | |
|
4264 | 0 | std::vector<size_t> tmp_count; |
4265 | 0 | if (count == nullptr) |
4266 | 0 | { |
4267 | 0 | tmp_count.resize(nDimCount); |
4268 | 0 | const auto &dims = GetDimensions(); |
4269 | 0 | for (size_t i = 0; i < nDimCount; i++) |
4270 | 0 | { |
4271 | 0 | const GUInt64 nSize = dims[i]->GetSize() - arrayStartIdx[i]; |
4272 | | #if SIZEOF_VOIDP < 8 |
4273 | | if (nSize != static_cast<size_t>(nSize)) |
4274 | | { |
4275 | | CPLError(CE_Failure, CPLE_AppDefined, "Integer overflow"); |
4276 | | return false; |
4277 | | } |
4278 | | #endif |
4279 | 0 | tmp_count[i] = static_cast<size_t>(nSize); |
4280 | 0 | } |
4281 | 0 | count = tmp_count.data(); |
4282 | 0 | } |
4283 | |
|
4284 | 0 | std::vector<GInt64> tmp_arrayStep; |
4285 | 0 | std::vector<GPtrDiff_t> tmp_bufferStride; |
4286 | 0 | const GInt64 *arrayStep = nullptr; |
4287 | 0 | const GPtrDiff_t *bufferStride = nullptr; |
4288 | 0 | if (!CheckReadWriteParams(arrayStartIdx, count, arrayStep, bufferStride, |
4289 | 0 | GDALExtendedDataType::Create(GDT_Unknown), |
4290 | 0 | nullptr, nullptr, 0, tmp_arrayStep, |
4291 | 0 | tmp_bufferStride)) |
4292 | 0 | { |
4293 | 0 | return false; |
4294 | 0 | } |
4295 | | |
4296 | 0 | return IAdviseRead(arrayStartIdx, count, papszOptions); |
4297 | 0 | } |
4298 | | |
4299 | | /************************************************************************/ |
4300 | | /* IAdviseRead() */ |
4301 | | /************************************************************************/ |
4302 | | |
4303 | | //! @cond Doxygen_Suppress |
4304 | | bool GDALMDArray::IAdviseRead(const GUInt64 *, const size_t *, |
4305 | | CSLConstList /* papszOptions*/) const |
4306 | 0 | { |
4307 | 0 | return true; |
4308 | 0 | } |
4309 | | |
4310 | | //! @endcond |
4311 | | |
4312 | | /************************************************************************/ |
4313 | | /* MassageName() */ |
4314 | | /************************************************************************/ |
4315 | | |
4316 | | //! @cond Doxygen_Suppress |
4317 | | /*static*/ std::string GDALMDArray::MassageName(const std::string &inputName) |
4318 | 0 | { |
4319 | 0 | std::string ret; |
4320 | 0 | for (const char ch : inputName) |
4321 | 0 | { |
4322 | 0 | if (!isalnum(static_cast<unsigned char>(ch))) |
4323 | 0 | ret += '_'; |
4324 | 0 | else |
4325 | 0 | ret += ch; |
4326 | 0 | } |
4327 | 0 | return ret; |
4328 | 0 | } |
4329 | | |
4330 | | //! @endcond |
4331 | | |
4332 | | /************************************************************************/ |
4333 | | /* GetCacheRootGroup() */ |
4334 | | /************************************************************************/ |
4335 | | |
4336 | | //! @cond Doxygen_Suppress |
4337 | | std::shared_ptr<GDALGroup> |
4338 | | GDALMDArray::GetCacheRootGroup(bool bCanCreate, |
4339 | | std::string &osCacheFilenameOut) const |
4340 | 0 | { |
4341 | 0 | const auto &osFilename = GetFilename(); |
4342 | 0 | if (osFilename.empty()) |
4343 | 0 | { |
4344 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
4345 | 0 | "Cannot cache an array with an empty filename"); |
4346 | 0 | return nullptr; |
4347 | 0 | } |
4348 | | |
4349 | 0 | osCacheFilenameOut = osFilename + ".gmac"; |
4350 | 0 | if (STARTS_WITH(osFilename.c_str(), "/vsicurl/http")) |
4351 | 0 | { |
4352 | 0 | const auto nPosQuestionMark = osFilename.find('?'); |
4353 | 0 | if (nPosQuestionMark != std::string::npos) |
4354 | 0 | { |
4355 | 0 | osCacheFilenameOut = |
4356 | 0 | osFilename.substr(0, nPosQuestionMark) |
4357 | 0 | .append(".gmac") |
4358 | 0 | .append(osFilename.substr(nPosQuestionMark)); |
4359 | 0 | } |
4360 | 0 | } |
4361 | 0 | const char *pszProxy = PamGetProxy(osCacheFilenameOut.c_str()); |
4362 | 0 | if (pszProxy != nullptr) |
4363 | 0 | osCacheFilenameOut = pszProxy; |
4364 | |
|
4365 | 0 | std::unique_ptr<GDALDataset> poDS; |
4366 | 0 | VSIStatBufL sStat; |
4367 | 0 | if (VSIStatL(osCacheFilenameOut.c_str(), &sStat) == 0) |
4368 | 0 | { |
4369 | 0 | poDS.reset(GDALDataset::Open(osCacheFilenameOut.c_str(), |
4370 | 0 | GDAL_OF_MULTIDIM_RASTER | GDAL_OF_UPDATE, |
4371 | 0 | nullptr, nullptr, nullptr)); |
4372 | 0 | } |
4373 | 0 | if (poDS) |
4374 | 0 | { |
4375 | 0 | CPLDebug("GDAL", "Opening cache %s", osCacheFilenameOut.c_str()); |
4376 | 0 | return poDS->GetRootGroup(); |
4377 | 0 | } |
4378 | | |
4379 | 0 | if (bCanCreate) |
4380 | 0 | { |
4381 | 0 | const char *pszDrvName = "netCDF"; |
4382 | 0 | GDALDriver *poDrv = GetGDALDriverManager()->GetDriverByName(pszDrvName); |
4383 | 0 | if (poDrv == nullptr) |
4384 | 0 | { |
4385 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot get driver %s", |
4386 | 0 | pszDrvName); |
4387 | 0 | return nullptr; |
4388 | 0 | } |
4389 | 0 | { |
4390 | 0 | CPLErrorHandlerPusher oHandlerPusher(CPLQuietErrorHandler); |
4391 | 0 | CPLErrorStateBackuper oErrorStateBackuper; |
4392 | 0 | poDS.reset(poDrv->CreateMultiDimensional(osCacheFilenameOut.c_str(), |
4393 | 0 | nullptr, nullptr)); |
4394 | 0 | } |
4395 | 0 | if (!poDS) |
4396 | 0 | { |
4397 | 0 | pszProxy = PamAllocateProxy(osCacheFilenameOut.c_str()); |
4398 | 0 | if (pszProxy) |
4399 | 0 | { |
4400 | 0 | osCacheFilenameOut = pszProxy; |
4401 | 0 | poDS.reset(poDrv->CreateMultiDimensional( |
4402 | 0 | osCacheFilenameOut.c_str(), nullptr, nullptr)); |
4403 | 0 | } |
4404 | 0 | } |
4405 | 0 | if (poDS) |
4406 | 0 | { |
4407 | 0 | CPLDebug("GDAL", "Creating cache %s", osCacheFilenameOut.c_str()); |
4408 | 0 | return poDS->GetRootGroup(); |
4409 | 0 | } |
4410 | 0 | else |
4411 | 0 | { |
4412 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
4413 | 0 | "Cannot create %s. Set the GDAL_PAM_PROXY_DIR " |
4414 | 0 | "configuration option to write the cache in " |
4415 | 0 | "another directory", |
4416 | 0 | osCacheFilenameOut.c_str()); |
4417 | 0 | } |
4418 | 0 | } |
4419 | | |
4420 | 0 | return nullptr; |
4421 | 0 | } |
4422 | | |
4423 | | //! @endcond |
4424 | | |
4425 | | /************************************************************************/ |
4426 | | /* Cache() */ |
4427 | | /************************************************************************/ |
4428 | | |
4429 | | /** Cache the content of the array into an auxiliary filename. |
4430 | | * |
4431 | | * The main purpose of this method is to be able to cache views that are |
4432 | | * expensive to compute, such as transposed arrays. |
4433 | | * |
4434 | | * The array will be stored in a file whose name is the one of |
4435 | | * GetFilename(), with an extra .gmac extension (stands for GDAL |
4436 | | * Multidimensional Array Cache). The cache is a netCDF dataset. |
4437 | | * |
4438 | | * If the .gmac file cannot be written next to the dataset, the |
4439 | | * GDAL_PAM_PROXY_DIR will be used, if set, to write the cache file into that |
4440 | | * directory. |
4441 | | * |
4442 | | * The GDALMDArray::Read() method will automatically use the cache when it |
4443 | | * exists. There is no timestamp checks between the source array and the cached |
4444 | | * array. If the source arrays changes, the cache must be manually deleted. |
4445 | | * |
4446 | | * This is the same as the C function GDALMDArrayCache() |
4447 | | * |
4448 | | * @note Driver implementation: optionally implemented. |
4449 | | * |
4450 | | * @param papszOptions List of options, null terminated, or NULL. Currently |
4451 | | * the only option supported is BLOCKSIZE=bs0,bs1,...,bsN |
4452 | | * to specify the block size of the cached array. |
4453 | | * @return true in case of success. |
4454 | | */ |
4455 | | bool GDALMDArray::Cache(CSLConstList papszOptions) const |
4456 | 0 | { |
4457 | 0 | std::string osCacheFilename; |
4458 | 0 | auto poRG = GetCacheRootGroup(true, osCacheFilename); |
4459 | 0 | if (!poRG) |
4460 | 0 | return false; |
4461 | | |
4462 | 0 | const std::string osCachedArrayName(MassageName(GetFullName())); |
4463 | 0 | if (poRG->OpenMDArray(osCachedArrayName)) |
4464 | 0 | { |
4465 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
4466 | 0 | "An array with same name %s already exists in %s", |
4467 | 0 | osCachedArrayName.c_str(), osCacheFilename.c_str()); |
4468 | 0 | return false; |
4469 | 0 | } |
4470 | | |
4471 | 0 | CPLStringList aosOptions; |
4472 | 0 | aosOptions.SetNameValue("COMPRESS", "DEFLATE"); |
4473 | 0 | const auto &aoDims = GetDimensions(); |
4474 | 0 | std::vector<std::shared_ptr<GDALDimension>> aoNewDims; |
4475 | 0 | if (!aoDims.empty()) |
4476 | 0 | { |
4477 | 0 | std::string osBlockSize( |
4478 | 0 | CSLFetchNameValueDef(papszOptions, "BLOCKSIZE", "")); |
4479 | 0 | if (osBlockSize.empty()) |
4480 | 0 | { |
4481 | 0 | const auto anBlockSize = GetBlockSize(); |
4482 | 0 | int idxDim = 0; |
4483 | 0 | for (auto nBlockSize : anBlockSize) |
4484 | 0 | { |
4485 | 0 | if (idxDim > 0) |
4486 | 0 | osBlockSize += ','; |
4487 | 0 | if (nBlockSize == 0) |
4488 | 0 | nBlockSize = 256; |
4489 | 0 | nBlockSize = std::min(nBlockSize, aoDims[idxDim]->GetSize()); |
4490 | 0 | osBlockSize += |
4491 | 0 | std::to_string(static_cast<uint64_t>(nBlockSize)); |
4492 | 0 | idxDim++; |
4493 | 0 | } |
4494 | 0 | } |
4495 | 0 | aosOptions.SetNameValue("BLOCKSIZE", osBlockSize.c_str()); |
4496 | |
|
4497 | 0 | int idxDim = 0; |
4498 | 0 | for (const auto &poDim : aoDims) |
4499 | 0 | { |
4500 | 0 | auto poNewDim = poRG->CreateDimension( |
4501 | 0 | osCachedArrayName + '_' + std::to_string(idxDim), |
4502 | 0 | poDim->GetType(), poDim->GetDirection(), poDim->GetSize()); |
4503 | 0 | if (!poNewDim) |
4504 | 0 | return false; |
4505 | 0 | aoNewDims.emplace_back(poNewDim); |
4506 | 0 | idxDim++; |
4507 | 0 | } |
4508 | 0 | } |
4509 | | |
4510 | 0 | auto poCachedArray = poRG->CreateMDArray(osCachedArrayName, aoNewDims, |
4511 | 0 | GetDataType(), aosOptions.List()); |
4512 | 0 | if (!poCachedArray) |
4513 | 0 | { |
4514 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Cannot create %s in %s", |
4515 | 0 | osCachedArrayName.c_str(), osCacheFilename.c_str()); |
4516 | 0 | return false; |
4517 | 0 | } |
4518 | | |
4519 | 0 | GUInt64 nCost = 0; |
4520 | 0 | return poCachedArray->CopyFrom(nullptr, this, |
4521 | 0 | false, // strict |
4522 | 0 | nCost, GetTotalCopyCost(), nullptr, nullptr); |
4523 | 0 | } |
4524 | | |
4525 | | /************************************************************************/ |
4526 | | /* Read() */ |
4527 | | /************************************************************************/ |
4528 | | |
4529 | | bool GDALMDArray::Read(const GUInt64 *arrayStartIdx, const size_t *count, |
4530 | | const GInt64 *arrayStep, // step in elements |
4531 | | const GPtrDiff_t *bufferStride, // stride in elements |
4532 | | const GDALExtendedDataType &bufferDataType, |
4533 | | void *pDstBuffer, const void *pDstBufferAllocStart, |
4534 | | size_t nDstBufferAllocSize) const |
4535 | 0 | { |
4536 | 0 | if (!m_bHasTriedCachedArray) |
4537 | 0 | { |
4538 | 0 | m_bHasTriedCachedArray = true; |
4539 | 0 | if (IsCacheable()) |
4540 | 0 | { |
4541 | 0 | const auto &osFilename = GetFilename(); |
4542 | 0 | if (!osFilename.empty() && |
4543 | 0 | !EQUAL(CPLGetExtensionSafe(osFilename.c_str()).c_str(), "gmac")) |
4544 | 0 | { |
4545 | 0 | std::string osCacheFilename; |
4546 | 0 | auto poRG = GetCacheRootGroup(false, osCacheFilename); |
4547 | 0 | if (poRG) |
4548 | 0 | { |
4549 | 0 | const std::string osCachedArrayName( |
4550 | 0 | MassageName(GetFullName())); |
4551 | 0 | m_poCachedArray = poRG->OpenMDArray(osCachedArrayName); |
4552 | 0 | if (m_poCachedArray) |
4553 | 0 | { |
4554 | 0 | const auto &dims = GetDimensions(); |
4555 | 0 | const auto &cachedDims = |
4556 | 0 | m_poCachedArray->GetDimensions(); |
4557 | 0 | const size_t nDims = dims.size(); |
4558 | 0 | bool ok = |
4559 | 0 | m_poCachedArray->GetDataType() == GetDataType() && |
4560 | 0 | cachedDims.size() == nDims; |
4561 | 0 | for (size_t i = 0; ok && i < nDims; ++i) |
4562 | 0 | { |
4563 | 0 | ok = dims[i]->GetSize() == cachedDims[i]->GetSize(); |
4564 | 0 | } |
4565 | 0 | if (ok) |
4566 | 0 | { |
4567 | 0 | CPLDebug("GDAL", "Cached array for %s found in %s", |
4568 | 0 | osCachedArrayName.c_str(), |
4569 | 0 | osCacheFilename.c_str()); |
4570 | 0 | } |
4571 | 0 | else |
4572 | 0 | { |
4573 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
4574 | 0 | "Cached array %s in %s has incompatible " |
4575 | 0 | "characteristics with current array.", |
4576 | 0 | osCachedArrayName.c_str(), |
4577 | 0 | osCacheFilename.c_str()); |
4578 | 0 | m_poCachedArray.reset(); |
4579 | 0 | } |
4580 | 0 | } |
4581 | 0 | } |
4582 | 0 | } |
4583 | 0 | } |
4584 | 0 | } |
4585 | |
|
4586 | 0 | const auto array = m_poCachedArray ? m_poCachedArray.get() : this; |
4587 | 0 | if (!array->GetDataType().CanConvertTo(bufferDataType)) |
4588 | 0 | { |
4589 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
4590 | 0 | "Array data type is not convertible to buffer data type"); |
4591 | 0 | return false; |
4592 | 0 | } |
4593 | | |
4594 | 0 | std::vector<GInt64> tmp_arrayStep; |
4595 | 0 | std::vector<GPtrDiff_t> tmp_bufferStride; |
4596 | 0 | if (!array->CheckReadWriteParams(arrayStartIdx, count, arrayStep, |
4597 | 0 | bufferStride, bufferDataType, pDstBuffer, |
4598 | 0 | pDstBufferAllocStart, nDstBufferAllocSize, |
4599 | 0 | tmp_arrayStep, tmp_bufferStride)) |
4600 | 0 | { |
4601 | 0 | return false; |
4602 | 0 | } |
4603 | | |
4604 | 0 | return array->IRead(arrayStartIdx, count, arrayStep, bufferStride, |
4605 | 0 | bufferDataType, pDstBuffer); |
4606 | 0 | } |
4607 | | |
4608 | | /************************************************************************/ |
4609 | | /* GetRootGroup() */ |
4610 | | /************************************************************************/ |
4611 | | |
4612 | | /** Return the root group to which this arrays belongs too. |
4613 | | * |
4614 | | * Note that arrays may be free standing and some drivers may not implement |
4615 | | * this method, hence nullptr may be returned. |
4616 | | * |
4617 | | * It is used internally by the GetResampled() method to detect if GLT |
4618 | | * orthorectification is available. |
4619 | | * |
4620 | | * @return the root group, or nullptr. |
4621 | | * @since GDAL 3.8 |
4622 | | */ |
4623 | | std::shared_ptr<GDALGroup> GDALMDArray::GetRootGroup() const |
4624 | 0 | { |
4625 | 0 | return nullptr; |
4626 | 0 | } |
4627 | | |
4628 | | //! @cond Doxygen_Suppress |
4629 | | |
4630 | | /************************************************************************/ |
4631 | | /* IsTransposedRequest() */ |
4632 | | /************************************************************************/ |
4633 | | |
4634 | | bool GDALMDArray::IsTransposedRequest( |
4635 | | const size_t *count, |
4636 | | const GPtrDiff_t *bufferStride) const // stride in elements |
4637 | 0 | { |
4638 | | /* |
4639 | | For example: |
4640 | | count = [2,3,4] |
4641 | | strides = [12, 4, 1] (2-1)*12+(3-1)*4+(4-1)*1=23 ==> row major |
4642 | | stride [12, 1, 3] (2-1)*12+(3-1)*1+(4-1)*3=23 ==> |
4643 | | (axis[0],axis[2],axis[1]) transposition [1, 8, 2] (2-1)*1+ |
4644 | | (3-1)*8+(4-1)*2=23 ==> (axis[2],axis[1],axis[0]) transposition |
4645 | | */ |
4646 | 0 | const size_t nDims(GetDimensionCount()); |
4647 | 0 | size_t nCurStrideForRowMajorStrides = 1; |
4648 | 0 | bool bRowMajorStrides = true; |
4649 | 0 | size_t nElts = 1; |
4650 | 0 | size_t nLastIdx = 0; |
4651 | 0 | for (size_t i = nDims; i > 0;) |
4652 | 0 | { |
4653 | 0 | --i; |
4654 | 0 | if (bufferStride[i] < 0) |
4655 | 0 | return false; |
4656 | 0 | if (static_cast<size_t>(bufferStride[i]) != |
4657 | 0 | nCurStrideForRowMajorStrides) |
4658 | 0 | { |
4659 | 0 | bRowMajorStrides = false; |
4660 | 0 | } |
4661 | | // Integer overflows have already been checked in CheckReadWriteParams() |
4662 | 0 | nCurStrideForRowMajorStrides *= count[i]; |
4663 | 0 | nElts *= count[i]; |
4664 | 0 | nLastIdx += static_cast<size_t>(bufferStride[i]) * (count[i] - 1); |
4665 | 0 | } |
4666 | 0 | if (bRowMajorStrides) |
4667 | 0 | return false; |
4668 | 0 | return nLastIdx == nElts - 1; |
4669 | 0 | } |
4670 | | |
4671 | | /************************************************************************/ |
4672 | | /* CopyToFinalBufferSameDataType() */ |
4673 | | /************************************************************************/ |
4674 | | |
4675 | | template <size_t N> |
4676 | | void CopyToFinalBufferSameDataType(const void *pSrcBuffer, void *pDstBuffer, |
4677 | | size_t nDims, const size_t *count, |
4678 | | const GPtrDiff_t *bufferStride) |
4679 | 0 | { |
4680 | 0 | std::vector<size_t> anStackCount(nDims); |
4681 | 0 | std::vector<GByte *> pabyDstBufferStack(nDims + 1); |
4682 | 0 | const GByte *pabySrcBuffer = static_cast<const GByte *>(pSrcBuffer); |
4683 | 0 | #if defined(__GNUC__) |
4684 | 0 | #pragma GCC diagnostic push |
4685 | 0 | #pragma GCC diagnostic ignored "-Wnull-dereference" |
4686 | 0 | #endif |
4687 | 0 | pabyDstBufferStack[0] = static_cast<GByte *>(pDstBuffer); |
4688 | 0 | #if defined(__GNUC__) |
4689 | 0 | #pragma GCC diagnostic pop |
4690 | 0 | #endif |
4691 | 0 | size_t iDim = 0; |
4692 | |
|
4693 | 0 | lbl_next_depth: |
4694 | 0 | if (iDim == nDims - 1) |
4695 | 0 | { |
4696 | 0 | size_t n = count[iDim]; |
4697 | 0 | GByte *pabyDstBuffer = pabyDstBufferStack[iDim]; |
4698 | 0 | const auto bufferStrideLastDim = bufferStride[iDim] * N; |
4699 | 0 | while (n > 0) |
4700 | 0 | { |
4701 | 0 | --n; |
4702 | 0 | memcpy(pabyDstBuffer, pabySrcBuffer, N); |
4703 | 0 | pabyDstBuffer += bufferStrideLastDim; |
4704 | 0 | pabySrcBuffer += N; |
4705 | 0 | } |
4706 | 0 | } |
4707 | 0 | else |
4708 | 0 | { |
4709 | 0 | anStackCount[iDim] = count[iDim]; |
4710 | 0 | while (true) |
4711 | 0 | { |
4712 | 0 | ++iDim; |
4713 | 0 | pabyDstBufferStack[iDim] = pabyDstBufferStack[iDim - 1]; |
4714 | 0 | goto lbl_next_depth; |
4715 | 0 | lbl_return_to_caller_in_loop: |
4716 | 0 | --iDim; |
4717 | 0 | --anStackCount[iDim]; |
4718 | 0 | if (anStackCount[iDim] == 0) |
4719 | 0 | break; |
4720 | 0 | pabyDstBufferStack[iDim] += bufferStride[iDim] * N; |
4721 | 0 | } |
4722 | 0 | } |
4723 | 0 | if (iDim > 0) |
4724 | 0 | goto lbl_return_to_caller_in_loop; |
4725 | 0 | } Unexecuted instantiation: void CopyToFinalBufferSameDataType<1ul>(void const*, void*, unsigned long, unsigned long const*, long long const*) Unexecuted instantiation: void CopyToFinalBufferSameDataType<2ul>(void const*, void*, unsigned long, unsigned long const*, long long const*) Unexecuted instantiation: void CopyToFinalBufferSameDataType<4ul>(void const*, void*, unsigned long, unsigned long const*, long long const*) Unexecuted instantiation: void CopyToFinalBufferSameDataType<8ul>(void const*, void*, unsigned long, unsigned long const*, long long const*) |
4726 | | |
4727 | | /************************************************************************/ |
4728 | | /* CopyToFinalBuffer() */ |
4729 | | /************************************************************************/ |
4730 | | |
4731 | | static void CopyToFinalBuffer(const void *pSrcBuffer, |
4732 | | const GDALExtendedDataType &eSrcDataType, |
4733 | | void *pDstBuffer, |
4734 | | const GDALExtendedDataType &eDstDataType, |
4735 | | size_t nDims, const size_t *count, |
4736 | | const GPtrDiff_t *bufferStride) |
4737 | 0 | { |
4738 | 0 | const size_t nSrcDataTypeSize(eSrcDataType.GetSize()); |
4739 | | // Use specialized implementation for well-known data types when no |
4740 | | // type conversion is needed |
4741 | 0 | if (eSrcDataType == eDstDataType) |
4742 | 0 | { |
4743 | 0 | if (nSrcDataTypeSize == 1) |
4744 | 0 | { |
4745 | 0 | CopyToFinalBufferSameDataType<1>(pSrcBuffer, pDstBuffer, nDims, |
4746 | 0 | count, bufferStride); |
4747 | 0 | return; |
4748 | 0 | } |
4749 | 0 | else if (nSrcDataTypeSize == 2) |
4750 | 0 | { |
4751 | 0 | CopyToFinalBufferSameDataType<2>(pSrcBuffer, pDstBuffer, nDims, |
4752 | 0 | count, bufferStride); |
4753 | 0 | return; |
4754 | 0 | } |
4755 | 0 | else if (nSrcDataTypeSize == 4) |
4756 | 0 | { |
4757 | 0 | CopyToFinalBufferSameDataType<4>(pSrcBuffer, pDstBuffer, nDims, |
4758 | 0 | count, bufferStride); |
4759 | 0 | return; |
4760 | 0 | } |
4761 | 0 | else if (nSrcDataTypeSize == 8) |
4762 | 0 | { |
4763 | 0 | CopyToFinalBufferSameDataType<8>(pSrcBuffer, pDstBuffer, nDims, |
4764 | 0 | count, bufferStride); |
4765 | 0 | return; |
4766 | 0 | } |
4767 | 0 | } |
4768 | | |
4769 | 0 | const size_t nDstDataTypeSize(eDstDataType.GetSize()); |
4770 | 0 | std::vector<size_t> anStackCount(nDims); |
4771 | 0 | std::vector<GByte *> pabyDstBufferStack(nDims + 1); |
4772 | 0 | const GByte *pabySrcBuffer = static_cast<const GByte *>(pSrcBuffer); |
4773 | 0 | pabyDstBufferStack[0] = static_cast<GByte *>(pDstBuffer); |
4774 | 0 | size_t iDim = 0; |
4775 | |
|
4776 | 0 | lbl_next_depth: |
4777 | 0 | if (iDim == nDims - 1) |
4778 | 0 | { |
4779 | 0 | GDALExtendedDataType::CopyValues(pabySrcBuffer, eSrcDataType, 1, |
4780 | 0 | pabyDstBufferStack[iDim], eDstDataType, |
4781 | 0 | bufferStride[iDim], count[iDim]); |
4782 | 0 | pabySrcBuffer += count[iDim] * nSrcDataTypeSize; |
4783 | 0 | } |
4784 | 0 | else |
4785 | 0 | { |
4786 | 0 | anStackCount[iDim] = count[iDim]; |
4787 | 0 | while (true) |
4788 | 0 | { |
4789 | 0 | ++iDim; |
4790 | 0 | pabyDstBufferStack[iDim] = pabyDstBufferStack[iDim - 1]; |
4791 | 0 | goto lbl_next_depth; |
4792 | 0 | lbl_return_to_caller_in_loop: |
4793 | 0 | --iDim; |
4794 | 0 | --anStackCount[iDim]; |
4795 | 0 | if (anStackCount[iDim] == 0) |
4796 | 0 | break; |
4797 | 0 | pabyDstBufferStack[iDim] += bufferStride[iDim] * nDstDataTypeSize; |
4798 | 0 | } |
4799 | 0 | } |
4800 | 0 | if (iDim > 0) |
4801 | 0 | goto lbl_return_to_caller_in_loop; |
4802 | 0 | } |
4803 | | |
4804 | | /************************************************************************/ |
4805 | | /* TransposeLast2Dims() */ |
4806 | | /************************************************************************/ |
4807 | | |
4808 | | static bool TransposeLast2Dims(void *pDstBuffer, |
4809 | | const GDALExtendedDataType &eDT, |
4810 | | const size_t nDims, const size_t *count, |
4811 | | const size_t nEltsNonLast2Dims) |
4812 | 0 | { |
4813 | 0 | const size_t nEltsLast2Dims = count[nDims - 2] * count[nDims - 1]; |
4814 | 0 | const auto nDTSize = eDT.GetSize(); |
4815 | 0 | void *pTempBufferForLast2DimsTranspose = |
4816 | 0 | VSI_MALLOC2_VERBOSE(nEltsLast2Dims, nDTSize); |
4817 | 0 | if (pTempBufferForLast2DimsTranspose == nullptr) |
4818 | 0 | return false; |
4819 | | |
4820 | 0 | GByte *pabyDstBuffer = static_cast<GByte *>(pDstBuffer); |
4821 | 0 | for (size_t i = 0; i < nEltsNonLast2Dims; ++i) |
4822 | 0 | { |
4823 | 0 | GDALTranspose2D(pabyDstBuffer, eDT.GetNumericDataType(), |
4824 | 0 | pTempBufferForLast2DimsTranspose, |
4825 | 0 | eDT.GetNumericDataType(), count[nDims - 1], |
4826 | 0 | count[nDims - 2]); |
4827 | 0 | memcpy(pabyDstBuffer, pTempBufferForLast2DimsTranspose, |
4828 | 0 | nDTSize * nEltsLast2Dims); |
4829 | 0 | pabyDstBuffer += nDTSize * nEltsLast2Dims; |
4830 | 0 | } |
4831 | |
|
4832 | 0 | VSIFree(pTempBufferForLast2DimsTranspose); |
4833 | |
|
4834 | 0 | return true; |
4835 | 0 | } |
4836 | | |
4837 | | /************************************************************************/ |
4838 | | /* ReadForTransposedRequest() */ |
4839 | | /************************************************************************/ |
4840 | | |
4841 | | // Using the netCDF/HDF5 APIs to read a slice with strides that express a |
4842 | | // transposed view yield to extremely poor/unusable performance. This fixes |
4843 | | // this by using temporary memory to read in a contiguous buffer in a |
4844 | | // row-major order, and then do the transposition to the final buffer. |
4845 | | |
4846 | | bool GDALMDArray::ReadForTransposedRequest( |
4847 | | const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep, |
4848 | | const GPtrDiff_t *bufferStride, const GDALExtendedDataType &bufferDataType, |
4849 | | void *pDstBuffer) const |
4850 | 0 | { |
4851 | 0 | const size_t nDims(GetDimensionCount()); |
4852 | 0 | if (nDims == 0) |
4853 | 0 | { |
4854 | 0 | CPLAssert(false); |
4855 | 0 | return false; // shouldn't happen |
4856 | 0 | } |
4857 | 0 | size_t nElts = 1; |
4858 | 0 | for (size_t i = 0; i < nDims; ++i) |
4859 | 0 | nElts *= count[i]; |
4860 | |
|
4861 | 0 | std::vector<GPtrDiff_t> tmpBufferStrides(nDims); |
4862 | 0 | tmpBufferStrides.back() = 1; |
4863 | 0 | for (size_t i = nDims - 1; i > 0;) |
4864 | 0 | { |
4865 | 0 | --i; |
4866 | 0 | tmpBufferStrides[i] = tmpBufferStrides[i + 1] * count[i + 1]; |
4867 | 0 | } |
4868 | |
|
4869 | 0 | const auto &eDT = GetDataType(); |
4870 | 0 | const auto nDTSize = eDT.GetSize(); |
4871 | 0 | if (bufferDataType == eDT && nDims >= 2 && bufferStride[nDims - 2] == 1 && |
4872 | 0 | static_cast<size_t>(bufferStride[nDims - 1]) == count[nDims - 2] && |
4873 | 0 | (nDTSize == 1 || nDTSize == 2 || nDTSize == 4 || nDTSize == 8)) |
4874 | 0 | { |
4875 | | // Optimization of the optimization if only the last 2 dims are |
4876 | | // transposed that saves on temporary buffer allocation |
4877 | 0 | const size_t nEltsLast2Dims = count[nDims - 2] * count[nDims - 1]; |
4878 | 0 | size_t nCurStrideForRowMajorStrides = nEltsLast2Dims; |
4879 | 0 | bool bRowMajorStridesForNonLast2Dims = true; |
4880 | 0 | size_t nEltsNonLast2Dims = 1; |
4881 | 0 | for (size_t i = nDims - 2; i > 0;) |
4882 | 0 | { |
4883 | 0 | --i; |
4884 | 0 | if (static_cast<size_t>(bufferStride[i]) != |
4885 | 0 | nCurStrideForRowMajorStrides) |
4886 | 0 | { |
4887 | 0 | bRowMajorStridesForNonLast2Dims = false; |
4888 | 0 | } |
4889 | | // Integer overflows have already been checked in |
4890 | | // CheckReadWriteParams() |
4891 | 0 | nCurStrideForRowMajorStrides *= count[i]; |
4892 | 0 | nEltsNonLast2Dims *= count[i]; |
4893 | 0 | } |
4894 | 0 | if (bRowMajorStridesForNonLast2Dims) |
4895 | 0 | { |
4896 | | // We read in the final buffer! |
4897 | 0 | if (!IRead(arrayStartIdx, count, arrayStep, tmpBufferStrides.data(), |
4898 | 0 | eDT, pDstBuffer)) |
4899 | 0 | { |
4900 | 0 | return false; |
4901 | 0 | } |
4902 | | |
4903 | 0 | return TransposeLast2Dims(pDstBuffer, eDT, nDims, count, |
4904 | 0 | nEltsNonLast2Dims); |
4905 | 0 | } |
4906 | 0 | } |
4907 | | |
4908 | 0 | void *pTempBuffer = VSI_MALLOC2_VERBOSE(nElts, eDT.GetSize()); |
4909 | 0 | if (pTempBuffer == nullptr) |
4910 | 0 | return false; |
4911 | | |
4912 | 0 | if (!IRead(arrayStartIdx, count, arrayStep, tmpBufferStrides.data(), eDT, |
4913 | 0 | pTempBuffer)) |
4914 | 0 | { |
4915 | 0 | VSIFree(pTempBuffer); |
4916 | 0 | return false; |
4917 | 0 | } |
4918 | 0 | CopyToFinalBuffer(pTempBuffer, eDT, pDstBuffer, bufferDataType, nDims, |
4919 | 0 | count, bufferStride); |
4920 | |
|
4921 | 0 | if (eDT.NeedsFreeDynamicMemory()) |
4922 | 0 | { |
4923 | 0 | GByte *pabyPtr = static_cast<GByte *>(pTempBuffer); |
4924 | 0 | for (size_t i = 0; i < nElts; ++i) |
4925 | 0 | { |
4926 | 0 | eDT.FreeDynamicMemory(pabyPtr); |
4927 | 0 | pabyPtr += nDTSize; |
4928 | 0 | } |
4929 | 0 | } |
4930 | |
|
4931 | 0 | VSIFree(pTempBuffer); |
4932 | 0 | return true; |
4933 | 0 | } |
4934 | | |
4935 | | /************************************************************************/ |
4936 | | /* IsStepOneContiguousRowMajorOrderedSameDataType() */ |
4937 | | /************************************************************************/ |
4938 | | |
4939 | | // Returns true if at all following conditions are met: |
4940 | | // arrayStep[] == 1, bufferDataType == GetDataType() and bufferStride[] |
4941 | | // defines a row-major ordered contiguous buffer. |
4942 | | bool GDALMDArray::IsStepOneContiguousRowMajorOrderedSameDataType( |
4943 | | const size_t *count, const GInt64 *arrayStep, |
4944 | | const GPtrDiff_t *bufferStride, |
4945 | | const GDALExtendedDataType &bufferDataType) const |
4946 | 0 | { |
4947 | 0 | if (bufferDataType != GetDataType()) |
4948 | 0 | return false; |
4949 | 0 | size_t nExpectedStride = 1; |
4950 | 0 | for (size_t i = GetDimensionCount(); i > 0;) |
4951 | 0 | { |
4952 | 0 | --i; |
4953 | 0 | if (arrayStep[i] != 1 || bufferStride[i] < 0 || |
4954 | 0 | static_cast<size_t>(bufferStride[i]) != nExpectedStride) |
4955 | 0 | { |
4956 | 0 | return false; |
4957 | 0 | } |
4958 | 0 | nExpectedStride *= count[i]; |
4959 | 0 | } |
4960 | 0 | return true; |
4961 | 0 | } |
4962 | | |
4963 | | /************************************************************************/ |
4964 | | /* ReadUsingContiguousIRead() */ |
4965 | | /************************************************************************/ |
4966 | | |
4967 | | // Used for example by the TileDB driver when requesting it with |
4968 | | // arrayStep[] != 1, bufferDataType != GetDataType() or bufferStride[] |
4969 | | // not defining a row-major ordered contiguous buffer. |
4970 | | // Should only be called when at least one of the above conditions are true, |
4971 | | // which can be tested with IsStepOneContiguousRowMajorOrderedSameDataType() |
4972 | | // returning none. |
4973 | | // This method will call IRead() again with arrayStep[] == 1, |
4974 | | // bufferDataType == GetDataType() and bufferStride[] defining a row-major |
4975 | | // ordered contiguous buffer, on a temporary buffer. And it will rearrange the |
4976 | | // content of that temporary buffer onto pDstBuffer. |
4977 | | bool GDALMDArray::ReadUsingContiguousIRead( |
4978 | | const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep, |
4979 | | const GPtrDiff_t *bufferStride, const GDALExtendedDataType &bufferDataType, |
4980 | | void *pDstBuffer) const |
4981 | 0 | { |
4982 | 0 | const size_t nDims(GetDimensionCount()); |
4983 | 0 | std::vector<GUInt64> anTmpStartIdx(nDims); |
4984 | 0 | std::vector<size_t> anTmpCount(nDims); |
4985 | 0 | const auto &oType = GetDataType(); |
4986 | 0 | size_t nMemArraySize = oType.GetSize(); |
4987 | 0 | std::vector<GPtrDiff_t> anTmpStride(nDims); |
4988 | 0 | GPtrDiff_t nStride = 1; |
4989 | 0 | for (size_t i = nDims; i > 0;) |
4990 | 0 | { |
4991 | 0 | --i; |
4992 | 0 | if (arrayStep[i] > 0) |
4993 | 0 | anTmpStartIdx[i] = arrayStartIdx[i]; |
4994 | 0 | else |
4995 | 0 | anTmpStartIdx[i] = |
4996 | 0 | arrayStartIdx[i] - (count[i] - 1) * (-arrayStep[i]); |
4997 | 0 | const uint64_t nCount = |
4998 | 0 | (count[i] - 1) * static_cast<uint64_t>(std::abs(arrayStep[i])) + 1; |
4999 | 0 | if (nCount > std::numeric_limits<size_t>::max() / nMemArraySize) |
5000 | 0 | { |
5001 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
5002 | 0 | "Read() failed due to too large memory requirement"); |
5003 | 0 | return false; |
5004 | 0 | } |
5005 | 0 | anTmpCount[i] = static_cast<size_t>(nCount); |
5006 | 0 | nMemArraySize *= anTmpCount[i]; |
5007 | 0 | anTmpStride[i] = nStride; |
5008 | 0 | nStride *= anTmpCount[i]; |
5009 | 0 | } |
5010 | 0 | std::unique_ptr<void, decltype(&VSIFree)> pTmpBuffer( |
5011 | 0 | VSI_MALLOC_VERBOSE(nMemArraySize), VSIFree); |
5012 | 0 | if (!pTmpBuffer) |
5013 | 0 | return false; |
5014 | 0 | if (!IRead(anTmpStartIdx.data(), anTmpCount.data(), |
5015 | 0 | std::vector<GInt64>(nDims, 1).data(), // steps |
5016 | 0 | anTmpStride.data(), oType, pTmpBuffer.get())) |
5017 | 0 | { |
5018 | 0 | return false; |
5019 | 0 | } |
5020 | 0 | std::vector<std::shared_ptr<GDALDimension>> apoTmpDims(nDims); |
5021 | 0 | for (size_t i = 0; i < nDims; ++i) |
5022 | 0 | { |
5023 | 0 | if (arrayStep[i] > 0) |
5024 | 0 | anTmpStartIdx[i] = 0; |
5025 | 0 | else |
5026 | 0 | anTmpStartIdx[i] = anTmpCount[i] - 1; |
5027 | 0 | apoTmpDims[i] = std::make_shared<GDALDimension>( |
5028 | 0 | std::string(), std::string(), std::string(), std::string(), |
5029 | 0 | anTmpCount[i]); |
5030 | 0 | } |
5031 | 0 | auto poMEMArray = |
5032 | 0 | MEMMDArray::Create(std::string(), std::string(), apoTmpDims, oType); |
5033 | 0 | return poMEMArray->Init(static_cast<GByte *>(pTmpBuffer.get())) && |
5034 | 0 | poMEMArray->Read(anTmpStartIdx.data(), count, arrayStep, |
5035 | 0 | bufferStride, bufferDataType, pDstBuffer); |
5036 | 0 | } |
5037 | | |
5038 | | //! @endcond |
5039 | | |
5040 | | /************************************************************************/ |
5041 | | /* GDALSlicedMDArray */ |
5042 | | /************************************************************************/ |
5043 | | |
5044 | | class GDALSlicedMDArray final : public GDALPamMDArray |
5045 | | { |
5046 | | private: |
5047 | | std::shared_ptr<GDALMDArray> m_poParent{}; |
5048 | | std::vector<std::shared_ptr<GDALDimension>> m_dims{}; |
5049 | | std::vector<size_t> m_mapDimIdxToParentDimIdx{}; // of size m_dims.size() |
5050 | | std::vector<std::shared_ptr<GDALMDArray>> m_apoNewIndexingVariables{}; |
5051 | | std::vector<Range> |
5052 | | m_parentRanges{}; // of size m_poParent->GetDimensionCount() |
5053 | | |
5054 | | mutable std::vector<GUInt64> m_parentStart; |
5055 | | mutable std::vector<size_t> m_parentCount; |
5056 | | mutable std::vector<GInt64> m_parentStep; |
5057 | | mutable std::vector<GPtrDiff_t> m_parentStride; |
5058 | | |
5059 | | void PrepareParentArrays(const GUInt64 *arrayStartIdx, const size_t *count, |
5060 | | const GInt64 *arrayStep, |
5061 | | const GPtrDiff_t *bufferStride) const; |
5062 | | |
5063 | | protected: |
5064 | | explicit GDALSlicedMDArray( |
5065 | | const std::shared_ptr<GDALMDArray> &poParent, |
5066 | | const std::string &viewExpr, |
5067 | | std::vector<std::shared_ptr<GDALDimension>> &&dims, |
5068 | | std::vector<size_t> &&mapDimIdxToParentDimIdx, |
5069 | | std::vector<std::shared_ptr<GDALMDArray>> &&apoNewIndexingVariables, |
5070 | | std::vector<Range> &&parentRanges) |
5071 | 0 | : GDALAbstractMDArray(std::string(), "Sliced view of " + |
5072 | 0 | poParent->GetFullName() + |
5073 | 0 | " (" + viewExpr + ")"), |
5074 | 0 | GDALPamMDArray(std::string(), |
5075 | 0 | "Sliced view of " + poParent->GetFullName() + " (" + |
5076 | 0 | viewExpr + ")", |
5077 | 0 | GDALPamMultiDim::GetPAM(poParent), |
5078 | 0 | poParent->GetContext()), |
5079 | 0 | m_poParent(std::move(poParent)), m_dims(std::move(dims)), |
5080 | 0 | m_mapDimIdxToParentDimIdx(std::move(mapDimIdxToParentDimIdx)), |
5081 | 0 | m_apoNewIndexingVariables(std::move(apoNewIndexingVariables)), |
5082 | 0 | m_parentRanges(std::move(parentRanges)), |
5083 | 0 | m_parentStart(m_poParent->GetDimensionCount()), |
5084 | 0 | m_parentCount(m_poParent->GetDimensionCount(), 1), |
5085 | 0 | m_parentStep(m_poParent->GetDimensionCount()), |
5086 | 0 | m_parentStride(m_poParent->GetDimensionCount()) |
5087 | 0 | { |
5088 | 0 | } |
5089 | | |
5090 | | bool IRead(const GUInt64 *arrayStartIdx, const size_t *count, |
5091 | | const GInt64 *arrayStep, const GPtrDiff_t *bufferStride, |
5092 | | const GDALExtendedDataType &bufferDataType, |
5093 | | void *pDstBuffer) const override; |
5094 | | |
5095 | | bool IWrite(const GUInt64 *arrayStartIdx, const size_t *count, |
5096 | | const GInt64 *arrayStep, const GPtrDiff_t *bufferStride, |
5097 | | const GDALExtendedDataType &bufferDataType, |
5098 | | const void *pSrcBuffer) override; |
5099 | | |
5100 | | bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count, |
5101 | | CSLConstList papszOptions) const override; |
5102 | | |
5103 | | public: |
5104 | | static std::shared_ptr<GDALSlicedMDArray> |
5105 | | Create(const std::shared_ptr<GDALMDArray> &poParent, |
5106 | | const std::string &viewExpr, |
5107 | | std::vector<std::shared_ptr<GDALDimension>> &&dims, |
5108 | | std::vector<size_t> &&mapDimIdxToParentDimIdx, |
5109 | | std::vector<std::shared_ptr<GDALMDArray>> &&apoNewIndexingVariables, |
5110 | | std::vector<Range> &&parentRanges) |
5111 | 0 | { |
5112 | 0 | CPLAssert(dims.size() == mapDimIdxToParentDimIdx.size()); |
5113 | 0 | CPLAssert(parentRanges.size() == poParent->GetDimensionCount()); |
5114 | | |
5115 | 0 | auto newAr(std::shared_ptr<GDALSlicedMDArray>(new GDALSlicedMDArray( |
5116 | 0 | poParent, viewExpr, std::move(dims), |
5117 | 0 | std::move(mapDimIdxToParentDimIdx), |
5118 | 0 | std::move(apoNewIndexingVariables), std::move(parentRanges)))); |
5119 | 0 | newAr->SetSelf(newAr); |
5120 | 0 | return newAr; |
5121 | 0 | } |
5122 | | |
5123 | | bool IsWritable() const override |
5124 | 0 | { |
5125 | 0 | return m_poParent->IsWritable(); |
5126 | 0 | } |
5127 | | |
5128 | | const std::string &GetFilename() const override |
5129 | 0 | { |
5130 | 0 | return m_poParent->GetFilename(); |
5131 | 0 | } |
5132 | | |
5133 | | const std::vector<std::shared_ptr<GDALDimension>> & |
5134 | | GetDimensions() const override |
5135 | 0 | { |
5136 | 0 | return m_dims; |
5137 | 0 | } |
5138 | | |
5139 | | const GDALExtendedDataType &GetDataType() const override |
5140 | 0 | { |
5141 | 0 | return m_poParent->GetDataType(); |
5142 | 0 | } |
5143 | | |
5144 | | const std::string &GetUnit() const override |
5145 | 0 | { |
5146 | 0 | return m_poParent->GetUnit(); |
5147 | 0 | } |
5148 | | |
5149 | | // bool SetUnit(const std::string& osUnit) override { return |
5150 | | // m_poParent->SetUnit(osUnit); } |
5151 | | |
5152 | | std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override |
5153 | 0 | { |
5154 | 0 | auto poSrcSRS = m_poParent->GetSpatialRef(); |
5155 | 0 | if (!poSrcSRS) |
5156 | 0 | return nullptr; |
5157 | 0 | auto srcMapping = poSrcSRS->GetDataAxisToSRSAxisMapping(); |
5158 | 0 | std::vector<int> dstMapping; |
5159 | 0 | for (int srcAxis : srcMapping) |
5160 | 0 | { |
5161 | 0 | bool bFound = false; |
5162 | 0 | for (size_t i = 0; i < m_mapDimIdxToParentDimIdx.size(); i++) |
5163 | 0 | { |
5164 | 0 | if (static_cast<int>(m_mapDimIdxToParentDimIdx[i]) == |
5165 | 0 | srcAxis - 1) |
5166 | 0 | { |
5167 | 0 | dstMapping.push_back(static_cast<int>(i) + 1); |
5168 | 0 | bFound = true; |
5169 | 0 | break; |
5170 | 0 | } |
5171 | 0 | } |
5172 | 0 | if (!bFound) |
5173 | 0 | { |
5174 | 0 | dstMapping.push_back(0); |
5175 | 0 | } |
5176 | 0 | } |
5177 | 0 | auto poClone(std::shared_ptr<OGRSpatialReference>(poSrcSRS->Clone())); |
5178 | 0 | poClone->SetDataAxisToSRSAxisMapping(dstMapping); |
5179 | 0 | return poClone; |
5180 | 0 | } |
5181 | | |
5182 | | const void *GetRawNoDataValue() const override |
5183 | 0 | { |
5184 | 0 | return m_poParent->GetRawNoDataValue(); |
5185 | 0 | } |
5186 | | |
5187 | | // bool SetRawNoDataValue(const void* pRawNoData) override { return |
5188 | | // m_poParent->SetRawNoDataValue(pRawNoData); } |
5189 | | |
5190 | | double GetOffset(bool *pbHasOffset, |
5191 | | GDALDataType *peStorageType) const override |
5192 | 0 | { |
5193 | 0 | return m_poParent->GetOffset(pbHasOffset, peStorageType); |
5194 | 0 | } |
5195 | | |
5196 | | double GetScale(bool *pbHasScale, |
5197 | | GDALDataType *peStorageType) const override |
5198 | 0 | { |
5199 | 0 | return m_poParent->GetScale(pbHasScale, peStorageType); |
5200 | 0 | } |
5201 | | |
5202 | | // bool SetOffset(double dfOffset) override { return |
5203 | | // m_poParent->SetOffset(dfOffset); } |
5204 | | |
5205 | | // bool SetScale(double dfScale) override { return |
5206 | | // m_poParent->SetScale(dfScale); } |
5207 | | |
5208 | | std::vector<GUInt64> GetBlockSize() const override |
5209 | 0 | { |
5210 | 0 | std::vector<GUInt64> ret(GetDimensionCount()); |
5211 | 0 | const auto parentBlockSize(m_poParent->GetBlockSize()); |
5212 | 0 | for (size_t i = 0; i < m_mapDimIdxToParentDimIdx.size(); ++i) |
5213 | 0 | { |
5214 | 0 | const auto iOldAxis = m_mapDimIdxToParentDimIdx[i]; |
5215 | 0 | if (iOldAxis != static_cast<size_t>(-1)) |
5216 | 0 | { |
5217 | 0 | ret[i] = parentBlockSize[iOldAxis]; |
5218 | 0 | } |
5219 | 0 | } |
5220 | 0 | return ret; |
5221 | 0 | } |
5222 | | |
5223 | | std::shared_ptr<GDALAttribute> |
5224 | | GetAttribute(const std::string &osName) const override |
5225 | 0 | { |
5226 | 0 | return m_poParent->GetAttribute(osName); |
5227 | 0 | } |
5228 | | |
5229 | | std::vector<std::shared_ptr<GDALAttribute>> |
5230 | | GetAttributes(CSLConstList papszOptions = nullptr) const override |
5231 | 0 | { |
5232 | 0 | return m_poParent->GetAttributes(papszOptions); |
5233 | 0 | } |
5234 | | }; |
5235 | | |
5236 | | /************************************************************************/ |
5237 | | /* PrepareParentArrays() */ |
5238 | | /************************************************************************/ |
5239 | | |
5240 | | void GDALSlicedMDArray::PrepareParentArrays( |
5241 | | const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep, |
5242 | | const GPtrDiff_t *bufferStride) const |
5243 | 0 | { |
5244 | 0 | const size_t nParentDimCount = m_parentRanges.size(); |
5245 | 0 | for (size_t i = 0; i < nParentDimCount; i++) |
5246 | 0 | { |
5247 | | // For dimensions in parent that have no existence in sliced array |
5248 | 0 | m_parentStart[i] = m_parentRanges[i].m_nStartIdx; |
5249 | 0 | } |
5250 | |
|
5251 | 0 | for (size_t i = 0; i < m_dims.size(); i++) |
5252 | 0 | { |
5253 | 0 | const auto iParent = m_mapDimIdxToParentDimIdx[i]; |
5254 | 0 | if (iParent != static_cast<size_t>(-1)) |
5255 | 0 | { |
5256 | 0 | m_parentStart[iParent] = |
5257 | 0 | m_parentRanges[iParent].m_nIncr >= 0 |
5258 | 0 | ? m_parentRanges[iParent].m_nStartIdx + |
5259 | 0 | arrayStartIdx[i] * m_parentRanges[iParent].m_nIncr |
5260 | 0 | : m_parentRanges[iParent].m_nStartIdx - |
5261 | 0 | arrayStartIdx[i] * |
5262 | 0 | static_cast<GUInt64>( |
5263 | 0 | -m_parentRanges[iParent].m_nIncr); |
5264 | 0 | m_parentCount[iParent] = count[i]; |
5265 | 0 | if (arrayStep) |
5266 | 0 | { |
5267 | 0 | m_parentStep[iParent] = |
5268 | 0 | count[i] == 1 ? 1 : |
5269 | | // other checks should have ensured this does |
5270 | | // not overflow |
5271 | 0 | arrayStep[i] * m_parentRanges[iParent].m_nIncr; |
5272 | 0 | } |
5273 | 0 | if (bufferStride) |
5274 | 0 | { |
5275 | 0 | m_parentStride[iParent] = bufferStride[i]; |
5276 | 0 | } |
5277 | 0 | } |
5278 | 0 | } |
5279 | 0 | } |
5280 | | |
5281 | | /************************************************************************/ |
5282 | | /* IRead() */ |
5283 | | /************************************************************************/ |
5284 | | |
5285 | | bool GDALSlicedMDArray::IRead(const GUInt64 *arrayStartIdx, const size_t *count, |
5286 | | const GInt64 *arrayStep, |
5287 | | const GPtrDiff_t *bufferStride, |
5288 | | const GDALExtendedDataType &bufferDataType, |
5289 | | void *pDstBuffer) const |
5290 | 0 | { |
5291 | 0 | PrepareParentArrays(arrayStartIdx, count, arrayStep, bufferStride); |
5292 | 0 | return m_poParent->Read(m_parentStart.data(), m_parentCount.data(), |
5293 | 0 | m_parentStep.data(), m_parentStride.data(), |
5294 | 0 | bufferDataType, pDstBuffer); |
5295 | 0 | } |
5296 | | |
5297 | | /************************************************************************/ |
5298 | | /* IWrite() */ |
5299 | | /************************************************************************/ |
5300 | | |
5301 | | bool GDALSlicedMDArray::IWrite(const GUInt64 *arrayStartIdx, |
5302 | | const size_t *count, const GInt64 *arrayStep, |
5303 | | const GPtrDiff_t *bufferStride, |
5304 | | const GDALExtendedDataType &bufferDataType, |
5305 | | const void *pSrcBuffer) |
5306 | 0 | { |
5307 | 0 | PrepareParentArrays(arrayStartIdx, count, arrayStep, bufferStride); |
5308 | 0 | return m_poParent->Write(m_parentStart.data(), m_parentCount.data(), |
5309 | 0 | m_parentStep.data(), m_parentStride.data(), |
5310 | 0 | bufferDataType, pSrcBuffer); |
5311 | 0 | } |
5312 | | |
5313 | | /************************************************************************/ |
5314 | | /* IAdviseRead() */ |
5315 | | /************************************************************************/ |
5316 | | |
5317 | | bool GDALSlicedMDArray::IAdviseRead(const GUInt64 *arrayStartIdx, |
5318 | | const size_t *count, |
5319 | | CSLConstList papszOptions) const |
5320 | 0 | { |
5321 | 0 | PrepareParentArrays(arrayStartIdx, count, nullptr, nullptr); |
5322 | 0 | return m_poParent->AdviseRead(m_parentStart.data(), m_parentCount.data(), |
5323 | 0 | papszOptions); |
5324 | 0 | } |
5325 | | |
5326 | | /************************************************************************/ |
5327 | | /* CreateSlicedArray() */ |
5328 | | /************************************************************************/ |
5329 | | |
5330 | | static std::shared_ptr<GDALMDArray> |
5331 | | CreateSlicedArray(const std::shared_ptr<GDALMDArray> &self, |
5332 | | const std::string &viewExpr, const std::string &activeSlice, |
5333 | | bool bRenameDimensions, |
5334 | | std::vector<GDALMDArray::ViewSpec> &viewSpecs) |
5335 | 0 | { |
5336 | 0 | const auto &srcDims(self->GetDimensions()); |
5337 | 0 | if (srcDims.empty()) |
5338 | 0 | { |
5339 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot slice a 0-d array"); |
5340 | 0 | return nullptr; |
5341 | 0 | } |
5342 | | |
5343 | 0 | CPLStringList aosTokens(CSLTokenizeString2(activeSlice.c_str(), ",", 0)); |
5344 | 0 | const auto nTokens = static_cast<size_t>(aosTokens.size()); |
5345 | |
|
5346 | 0 | std::vector<std::shared_ptr<GDALDimension>> newDims; |
5347 | 0 | std::vector<size_t> mapDimIdxToParentDimIdx; |
5348 | 0 | std::vector<GDALSlicedMDArray::Range> parentRanges; |
5349 | 0 | newDims.reserve(nTokens); |
5350 | 0 | mapDimIdxToParentDimIdx.reserve(nTokens); |
5351 | 0 | parentRanges.reserve(nTokens); |
5352 | |
|
5353 | 0 | bool bGotEllipsis = false; |
5354 | 0 | size_t nCurSrcDim = 0; |
5355 | 0 | std::vector<std::shared_ptr<GDALMDArray>> apoNewIndexingVariables; |
5356 | 0 | for (size_t i = 0; i < nTokens; i++) |
5357 | 0 | { |
5358 | 0 | const char *pszIdxSpec = aosTokens[i]; |
5359 | 0 | if (EQUAL(pszIdxSpec, "...")) |
5360 | 0 | { |
5361 | 0 | if (bGotEllipsis) |
5362 | 0 | { |
5363 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
5364 | 0 | "Only one single ellipsis is supported"); |
5365 | 0 | return nullptr; |
5366 | 0 | } |
5367 | 0 | bGotEllipsis = true; |
5368 | 0 | const auto nSubstitutionCount = srcDims.size() - (nTokens - 1); |
5369 | 0 | for (size_t j = 0; j < nSubstitutionCount; j++, nCurSrcDim++) |
5370 | 0 | { |
5371 | 0 | parentRanges.emplace_back(0, 1); |
5372 | 0 | newDims.push_back(srcDims[nCurSrcDim]); |
5373 | 0 | mapDimIdxToParentDimIdx.push_back(nCurSrcDim); |
5374 | 0 | } |
5375 | 0 | continue; |
5376 | 0 | } |
5377 | 0 | else if (EQUAL(pszIdxSpec, "newaxis") || |
5378 | 0 | EQUAL(pszIdxSpec, "np.newaxis")) |
5379 | 0 | { |
5380 | 0 | newDims.push_back(std::make_shared<GDALDimension>( |
5381 | 0 | std::string(), "newaxis", std::string(), std::string(), 1)); |
5382 | 0 | mapDimIdxToParentDimIdx.push_back(static_cast<size_t>(-1)); |
5383 | 0 | continue; |
5384 | 0 | } |
5385 | 0 | else if (CPLGetValueType(pszIdxSpec) == CPL_VALUE_INTEGER) |
5386 | 0 | { |
5387 | 0 | if (nCurSrcDim >= srcDims.size()) |
5388 | 0 | { |
5389 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Too many values in %s", |
5390 | 0 | activeSlice.c_str()); |
5391 | 0 | return nullptr; |
5392 | 0 | } |
5393 | | |
5394 | 0 | auto nVal = CPLAtoGIntBig(pszIdxSpec); |
5395 | 0 | GUInt64 nDimSize = srcDims[nCurSrcDim]->GetSize(); |
5396 | 0 | if ((nVal >= 0 && static_cast<GUInt64>(nVal) >= nDimSize) || |
5397 | 0 | (nVal < 0 && nDimSize < static_cast<GUInt64>(-nVal))) |
5398 | 0 | { |
5399 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
5400 | 0 | "Index " CPL_FRMT_GIB " is out of bounds", nVal); |
5401 | 0 | return nullptr; |
5402 | 0 | } |
5403 | 0 | if (nVal < 0) |
5404 | 0 | nVal += nDimSize; |
5405 | 0 | parentRanges.emplace_back(nVal, 0); |
5406 | 0 | } |
5407 | 0 | else |
5408 | 0 | { |
5409 | 0 | if (nCurSrcDim >= srcDims.size()) |
5410 | 0 | { |
5411 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Too many values in %s", |
5412 | 0 | activeSlice.c_str()); |
5413 | 0 | return nullptr; |
5414 | 0 | } |
5415 | | |
5416 | 0 | CPLStringList aosRangeTokens( |
5417 | 0 | CSLTokenizeString2(pszIdxSpec, ":", CSLT_ALLOWEMPTYTOKENS)); |
5418 | 0 | int nRangeTokens = aosRangeTokens.size(); |
5419 | 0 | if (nRangeTokens > 3) |
5420 | 0 | { |
5421 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Too many : in %s", |
5422 | 0 | pszIdxSpec); |
5423 | 0 | return nullptr; |
5424 | 0 | } |
5425 | 0 | if (nRangeTokens <= 1) |
5426 | 0 | { |
5427 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid value %s", |
5428 | 0 | pszIdxSpec); |
5429 | 0 | return nullptr; |
5430 | 0 | } |
5431 | 0 | const char *pszStart = aosRangeTokens[0]; |
5432 | 0 | const char *pszEnd = aosRangeTokens[1]; |
5433 | 0 | const char *pszInc = (nRangeTokens == 3) ? aosRangeTokens[2] : ""; |
5434 | 0 | GDALSlicedMDArray::Range range; |
5435 | 0 | const GUInt64 nDimSize(srcDims[nCurSrcDim]->GetSize()); |
5436 | 0 | range.m_nIncr = EQUAL(pszInc, "") ? 1 : CPLAtoGIntBig(pszInc); |
5437 | 0 | if (range.m_nIncr == 0 || |
5438 | 0 | range.m_nIncr == std::numeric_limits<GInt64>::min()) |
5439 | 0 | { |
5440 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid increment"); |
5441 | 0 | return nullptr; |
5442 | 0 | } |
5443 | 0 | auto startIdx(CPLAtoGIntBig(pszStart)); |
5444 | 0 | if (startIdx < 0) |
5445 | 0 | { |
5446 | 0 | if (nDimSize < static_cast<GUInt64>(-startIdx)) |
5447 | 0 | startIdx = 0; |
5448 | 0 | else |
5449 | 0 | startIdx = nDimSize + startIdx; |
5450 | 0 | } |
5451 | 0 | const bool bPosIncr = range.m_nIncr > 0; |
5452 | 0 | range.m_nStartIdx = startIdx; |
5453 | 0 | range.m_nStartIdx = EQUAL(pszStart, "") |
5454 | 0 | ? (bPosIncr ? 0 : nDimSize - 1) |
5455 | 0 | : range.m_nStartIdx; |
5456 | 0 | if (range.m_nStartIdx >= nDimSize - 1) |
5457 | 0 | range.m_nStartIdx = nDimSize - 1; |
5458 | 0 | auto endIdx(CPLAtoGIntBig(pszEnd)); |
5459 | 0 | if (endIdx < 0) |
5460 | 0 | { |
5461 | 0 | const auto positiveEndIdx = static_cast<GUInt64>(-endIdx); |
5462 | 0 | if (nDimSize < positiveEndIdx) |
5463 | 0 | endIdx = 0; |
5464 | 0 | else |
5465 | 0 | endIdx = nDimSize - positiveEndIdx; |
5466 | 0 | } |
5467 | 0 | GUInt64 nEndIdx = endIdx; |
5468 | 0 | nEndIdx = EQUAL(pszEnd, "") ? (!bPosIncr ? 0 : nDimSize) : nEndIdx; |
5469 | 0 | if (pszStart[0] || pszEnd[0]) |
5470 | 0 | { |
5471 | 0 | if ((bPosIncr && range.m_nStartIdx >= nEndIdx) || |
5472 | 0 | (!bPosIncr && range.m_nStartIdx <= nEndIdx)) |
5473 | 0 | { |
5474 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
5475 | 0 | "Output dimension of size 0 is not allowed"); |
5476 | 0 | return nullptr; |
5477 | 0 | } |
5478 | 0 | } |
5479 | 0 | int inc = (EQUAL(pszEnd, "") && !bPosIncr) ? 1 : 0; |
5480 | 0 | const auto nAbsIncr = std::abs(range.m_nIncr); |
5481 | 0 | const GUInt64 newSize = |
5482 | 0 | (pszStart[0] == 0 && pszEnd[0] == 0 && |
5483 | 0 | range.m_nStartIdx == nEndIdx) |
5484 | 0 | ? 1 |
5485 | 0 | : bPosIncr |
5486 | 0 | ? DIV_ROUND_UP(nEndIdx - range.m_nStartIdx, nAbsIncr) |
5487 | 0 | : DIV_ROUND_UP(inc + range.m_nStartIdx - nEndIdx, nAbsIncr); |
5488 | 0 | const auto &poSrcDim = srcDims[nCurSrcDim]; |
5489 | 0 | if (range.m_nStartIdx == 0 && range.m_nIncr == 1 && |
5490 | 0 | newSize == poSrcDim->GetSize()) |
5491 | 0 | { |
5492 | 0 | newDims.push_back(poSrcDim); |
5493 | 0 | } |
5494 | 0 | else |
5495 | 0 | { |
5496 | 0 | std::string osNewDimName(poSrcDim->GetName()); |
5497 | 0 | if (bRenameDimensions) |
5498 | 0 | { |
5499 | 0 | osNewDimName = |
5500 | 0 | "subset_" + poSrcDim->GetName() + |
5501 | 0 | CPLSPrintf("_" CPL_FRMT_GUIB "_" CPL_FRMT_GIB |
5502 | 0 | "_" CPL_FRMT_GUIB, |
5503 | 0 | static_cast<GUIntBig>(range.m_nStartIdx), |
5504 | 0 | static_cast<GIntBig>(range.m_nIncr), |
5505 | 0 | static_cast<GUIntBig>(newSize)); |
5506 | 0 | } |
5507 | 0 | auto poNewDim = std::make_shared<GDALDimensionWeakIndexingVar>( |
5508 | 0 | std::string(), osNewDimName, poSrcDim->GetType(), |
5509 | 0 | range.m_nIncr > 0 ? poSrcDim->GetDirection() |
5510 | 0 | : std::string(), |
5511 | 0 | newSize); |
5512 | 0 | auto poSrcIndexingVar = poSrcDim->GetIndexingVariable(); |
5513 | 0 | if (poSrcIndexingVar && |
5514 | 0 | poSrcIndexingVar->GetDimensionCount() == 1 && |
5515 | 0 | poSrcIndexingVar->GetDimensions()[0] == poSrcDim) |
5516 | 0 | { |
5517 | 0 | std::vector<std::shared_ptr<GDALDimension>> |
5518 | 0 | indexingVarNewDims{poNewDim}; |
5519 | 0 | std::vector<size_t> indexingVarMapDimIdxToParentDimIdx{0}; |
5520 | 0 | std::vector<std::shared_ptr<GDALMDArray>> |
5521 | 0 | indexingVarNewIndexingVar; |
5522 | 0 | std::vector<GDALSlicedMDArray::Range> |
5523 | 0 | indexingVarParentRanges{range}; |
5524 | 0 | auto poNewIndexingVar = GDALSlicedMDArray::Create( |
5525 | 0 | poSrcIndexingVar, pszIdxSpec, |
5526 | 0 | std::move(indexingVarNewDims), |
5527 | 0 | std::move(indexingVarMapDimIdxToParentDimIdx), |
5528 | 0 | std::move(indexingVarNewIndexingVar), |
5529 | 0 | std::move(indexingVarParentRanges)); |
5530 | 0 | poNewDim->SetIndexingVariable(poNewIndexingVar); |
5531 | 0 | apoNewIndexingVariables.push_back( |
5532 | 0 | std::move(poNewIndexingVar)); |
5533 | 0 | } |
5534 | 0 | newDims.push_back(std::move(poNewDim)); |
5535 | 0 | } |
5536 | 0 | mapDimIdxToParentDimIdx.push_back(nCurSrcDim); |
5537 | 0 | parentRanges.emplace_back(range); |
5538 | 0 | } |
5539 | | |
5540 | 0 | nCurSrcDim++; |
5541 | 0 | } |
5542 | 0 | for (; nCurSrcDim < srcDims.size(); nCurSrcDim++) |
5543 | 0 | { |
5544 | 0 | parentRanges.emplace_back(0, 1); |
5545 | 0 | newDims.push_back(srcDims[nCurSrcDim]); |
5546 | 0 | mapDimIdxToParentDimIdx.push_back(nCurSrcDim); |
5547 | 0 | } |
5548 | |
|
5549 | 0 | GDALMDArray::ViewSpec viewSpec; |
5550 | 0 | viewSpec.m_mapDimIdxToParentDimIdx = mapDimIdxToParentDimIdx; |
5551 | 0 | viewSpec.m_parentRanges = parentRanges; |
5552 | 0 | viewSpecs.emplace_back(std::move(viewSpec)); |
5553 | |
|
5554 | 0 | return GDALSlicedMDArray::Create( |
5555 | 0 | self, viewExpr, std::move(newDims), std::move(mapDimIdxToParentDimIdx), |
5556 | 0 | std::move(apoNewIndexingVariables), std::move(parentRanges)); |
5557 | 0 | } |
5558 | | |
5559 | | /************************************************************************/ |
5560 | | /* GDALExtractFieldMDArray */ |
5561 | | /************************************************************************/ |
5562 | | |
5563 | | class GDALExtractFieldMDArray final : public GDALPamMDArray |
5564 | | { |
5565 | | private: |
5566 | | std::shared_ptr<GDALMDArray> m_poParent{}; |
5567 | | GDALExtendedDataType m_dt; |
5568 | | std::string m_srcCompName; |
5569 | | mutable std::vector<GByte> m_pabyNoData{}; |
5570 | | |
5571 | | protected: |
5572 | | GDALExtractFieldMDArray(const std::shared_ptr<GDALMDArray> &poParent, |
5573 | | const std::string &fieldName, |
5574 | | const std::unique_ptr<GDALEDTComponent> &srcComp) |
5575 | 0 | : GDALAbstractMDArray(std::string(), "Extract field " + fieldName + |
5576 | 0 | " of " + |
5577 | 0 | poParent->GetFullName()), |
5578 | 0 | GDALPamMDArray( |
5579 | 0 | std::string(), |
5580 | 0 | "Extract field " + fieldName + " of " + poParent->GetFullName(), |
5581 | 0 | GDALPamMultiDim::GetPAM(poParent), poParent->GetContext()), |
5582 | 0 | m_poParent(poParent), m_dt(srcComp->GetType()), |
5583 | 0 | m_srcCompName(srcComp->GetName()) |
5584 | 0 | { |
5585 | 0 | m_pabyNoData.resize(m_dt.GetSize()); |
5586 | 0 | } |
5587 | | |
5588 | | bool IRead(const GUInt64 *arrayStartIdx, const size_t *count, |
5589 | | const GInt64 *arrayStep, const GPtrDiff_t *bufferStride, |
5590 | | const GDALExtendedDataType &bufferDataType, |
5591 | | void *pDstBuffer) const override; |
5592 | | |
5593 | | bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count, |
5594 | | CSLConstList papszOptions) const override |
5595 | 0 | { |
5596 | 0 | return m_poParent->AdviseRead(arrayStartIdx, count, papszOptions); |
5597 | 0 | } |
5598 | | |
5599 | | public: |
5600 | | static std::shared_ptr<GDALExtractFieldMDArray> |
5601 | | Create(const std::shared_ptr<GDALMDArray> &poParent, |
5602 | | const std::string &fieldName, |
5603 | | const std::unique_ptr<GDALEDTComponent> &srcComp) |
5604 | 0 | { |
5605 | 0 | auto newAr(std::shared_ptr<GDALExtractFieldMDArray>( |
5606 | 0 | new GDALExtractFieldMDArray(poParent, fieldName, srcComp))); |
5607 | 0 | newAr->SetSelf(newAr); |
5608 | 0 | return newAr; |
5609 | 0 | } |
5610 | | |
5611 | | ~GDALExtractFieldMDArray() override |
5612 | 0 | { |
5613 | 0 | m_dt.FreeDynamicMemory(&m_pabyNoData[0]); |
5614 | 0 | } |
5615 | | |
5616 | | bool IsWritable() const override |
5617 | 0 | { |
5618 | 0 | return m_poParent->IsWritable(); |
5619 | 0 | } |
5620 | | |
5621 | | const std::string &GetFilename() const override |
5622 | 0 | { |
5623 | 0 | return m_poParent->GetFilename(); |
5624 | 0 | } |
5625 | | |
5626 | | const std::vector<std::shared_ptr<GDALDimension>> & |
5627 | | GetDimensions() const override |
5628 | 0 | { |
5629 | 0 | return m_poParent->GetDimensions(); |
5630 | 0 | } |
5631 | | |
5632 | | const GDALExtendedDataType &GetDataType() const override |
5633 | 0 | { |
5634 | 0 | return m_dt; |
5635 | 0 | } |
5636 | | |
5637 | | const std::string &GetUnit() const override |
5638 | 0 | { |
5639 | 0 | return m_poParent->GetUnit(); |
5640 | 0 | } |
5641 | | |
5642 | | std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override |
5643 | 0 | { |
5644 | 0 | return m_poParent->GetSpatialRef(); |
5645 | 0 | } |
5646 | | |
5647 | | const void *GetRawNoDataValue() const override |
5648 | 0 | { |
5649 | 0 | const void *parentNoData = m_poParent->GetRawNoDataValue(); |
5650 | 0 | if (parentNoData == nullptr) |
5651 | 0 | return nullptr; |
5652 | | |
5653 | 0 | m_dt.FreeDynamicMemory(&m_pabyNoData[0]); |
5654 | 0 | memset(&m_pabyNoData[0], 0, m_dt.GetSize()); |
5655 | |
|
5656 | 0 | std::vector<std::unique_ptr<GDALEDTComponent>> comps; |
5657 | 0 | comps.emplace_back(std::unique_ptr<GDALEDTComponent>( |
5658 | 0 | new GDALEDTComponent(m_srcCompName, 0, m_dt))); |
5659 | 0 | auto tmpDT(GDALExtendedDataType::Create(std::string(), m_dt.GetSize(), |
5660 | 0 | std::move(comps))); |
5661 | |
|
5662 | 0 | GDALExtendedDataType::CopyValue(parentNoData, m_poParent->GetDataType(), |
5663 | 0 | &m_pabyNoData[0], tmpDT); |
5664 | |
|
5665 | 0 | return &m_pabyNoData[0]; |
5666 | 0 | } |
5667 | | |
5668 | | double GetOffset(bool *pbHasOffset, |
5669 | | GDALDataType *peStorageType) const override |
5670 | 0 | { |
5671 | 0 | return m_poParent->GetOffset(pbHasOffset, peStorageType); |
5672 | 0 | } |
5673 | | |
5674 | | double GetScale(bool *pbHasScale, |
5675 | | GDALDataType *peStorageType) const override |
5676 | 0 | { |
5677 | 0 | return m_poParent->GetScale(pbHasScale, peStorageType); |
5678 | 0 | } |
5679 | | |
5680 | | std::vector<GUInt64> GetBlockSize() const override |
5681 | 0 | { |
5682 | 0 | return m_poParent->GetBlockSize(); |
5683 | 0 | } |
5684 | | }; |
5685 | | |
5686 | | /************************************************************************/ |
5687 | | /* IRead() */ |
5688 | | /************************************************************************/ |
5689 | | |
5690 | | bool GDALExtractFieldMDArray::IRead(const GUInt64 *arrayStartIdx, |
5691 | | const size_t *count, |
5692 | | const GInt64 *arrayStep, |
5693 | | const GPtrDiff_t *bufferStride, |
5694 | | const GDALExtendedDataType &bufferDataType, |
5695 | | void *pDstBuffer) const |
5696 | 0 | { |
5697 | 0 | std::vector<std::unique_ptr<GDALEDTComponent>> comps; |
5698 | 0 | comps.emplace_back(std::unique_ptr<GDALEDTComponent>( |
5699 | 0 | new GDALEDTComponent(m_srcCompName, 0, bufferDataType))); |
5700 | 0 | auto tmpDT(GDALExtendedDataType::Create( |
5701 | 0 | std::string(), bufferDataType.GetSize(), std::move(comps))); |
5702 | |
|
5703 | 0 | return m_poParent->Read(arrayStartIdx, count, arrayStep, bufferStride, |
5704 | 0 | tmpDT, pDstBuffer); |
5705 | 0 | } |
5706 | | |
5707 | | /************************************************************************/ |
5708 | | /* CreateFieldNameExtractArray() */ |
5709 | | /************************************************************************/ |
5710 | | |
5711 | | static std::shared_ptr<GDALMDArray> |
5712 | | CreateFieldNameExtractArray(const std::shared_ptr<GDALMDArray> &self, |
5713 | | const std::string &fieldName) |
5714 | 0 | { |
5715 | 0 | CPLAssert(self->GetDataType().GetClass() == GEDTC_COMPOUND); |
5716 | 0 | const std::unique_ptr<GDALEDTComponent> *srcComp = nullptr; |
5717 | 0 | for (const auto &comp : self->GetDataType().GetComponents()) |
5718 | 0 | { |
5719 | 0 | if (comp->GetName() == fieldName) |
5720 | 0 | { |
5721 | 0 | srcComp = ∁ |
5722 | 0 | break; |
5723 | 0 | } |
5724 | 0 | } |
5725 | 0 | if (srcComp == nullptr) |
5726 | 0 | { |
5727 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot find field %s", |
5728 | 0 | fieldName.c_str()); |
5729 | 0 | return nullptr; |
5730 | 0 | } |
5731 | 0 | return GDALExtractFieldMDArray::Create(self, fieldName, *srcComp); |
5732 | 0 | } |
5733 | | |
5734 | | /************************************************************************/ |
5735 | | /* GetView() */ |
5736 | | /************************************************************************/ |
5737 | | |
5738 | | // clang-format off |
5739 | | /** Return a view of the array using slicing or field access. |
5740 | | * |
5741 | | * The slice expression uses the same syntax as NumPy basic slicing and |
5742 | | * indexing. See |
5743 | | * https://www.numpy.org/devdocs/reference/arrays.indexing.html#basic-slicing-and-indexing |
5744 | | * Or it can use field access by name. See |
5745 | | * https://www.numpy.org/devdocs/reference/arrays.indexing.html#field-access |
5746 | | * |
5747 | | * Multiple [] bracket elements can be concatenated, with a slice expression |
5748 | | * or field name inside each. |
5749 | | * |
5750 | | * For basic slicing and indexing, inside each [] bracket element, a list of |
5751 | | * indexes that apply to successive source dimensions, can be specified, using |
5752 | | * integer indexing (e.g. 1), range indexing (start:stop:step), ellipsis (...) |
5753 | | * or newaxis, using a comma separator. |
5754 | | * |
5755 | | * Examples with a 2-dimensional array whose content is [[0,1,2,3],[4,5,6,7]]. |
5756 | | * <ul> |
5757 | | * <li>GetView("[1][2]"): returns a 0-dimensional/scalar array with the value |
5758 | | * at index 1 in the first dimension, and index 2 in the second dimension |
5759 | | * from the source array. That is 5</li> |
5760 | | * <li>GetView("[1]")->GetView("[2]"): same as above. Above is actually |
5761 | | * implemented internally doing this intermediate slicing approach.</li> |
5762 | | * <li>GetView("[1,2]"): same as above, but a bit more performant.</li> |
5763 | | * <li>GetView("[1]"): returns a 1-dimensional array, sliced at index 1 in the |
5764 | | * first dimension. That is [4,5,6,7].</li> |
5765 | | * <li>GetView("[:,2]"): returns a 1-dimensional array, sliced at index 2 in the |
5766 | | * second dimension. That is [2,6].</li> |
5767 | | * <li>GetView("[:,2:3:]"): returns a 2-dimensional array, sliced at index 2 in |
5768 | | * the second dimension. That is [[2],[6]].</li> |
5769 | | * <li>GetView("[::,2]"): Same as |
5770 | | * above.</li> <li>GetView("[...,2]"): same as above, in that case, since the |
5771 | | * ellipsis only expands to one dimension here.</li> |
5772 | | * <li>GetView("[:,::2]"): |
5773 | | * returns a 2-dimensional array, with even-indexed elements of the second |
5774 | | * dimension. That is [[0,2],[4,6]].</li> |
5775 | | * <li>GetView("[:,1::2]"): returns a |
5776 | | * 2-dimensional array, with odd-indexed elements of the second dimension. That |
5777 | | * is [[1,3],[5,7]].</li> |
5778 | | * <li>GetView("[:,1:3:]"): returns a 2-dimensional |
5779 | | * array, with elements of the second dimension with index in the range [1,3[. |
5780 | | * That is [[1,2],[5,6]].</li> |
5781 | | * <li>GetView("[::-1,:]"): returns a 2-dimensional |
5782 | | * array, with the values in first dimension reversed. That is |
5783 | | * [[4,5,6,7],[0,1,2,3]].</li> |
5784 | | * <li>GetView("[newaxis,...]"): returns a |
5785 | | * 3-dimensional array, with an additional dimension of size 1 put at the |
5786 | | * beginning. That is [[[0,1,2,3],[4,5,6,7]]].</li> |
5787 | | * </ul> |
5788 | | * |
5789 | | * One difference with NumPy behavior is that ranges that would result in |
5790 | | * zero elements are not allowed (dimensions of size 0 not being allowed in the |
5791 | | * GDAL multidimensional model). |
5792 | | * |
5793 | | * For field access, the syntax to use is ["field_name"] or ['field_name']. |
5794 | | * Multiple field specification is not supported currently. |
5795 | | * |
5796 | | * Both type of access can be combined, e.g. GetView("[1]['field_name']") |
5797 | | * |
5798 | | * \note When using the GDAL Python bindings, natural Python syntax can be |
5799 | | * used. That is ar[0,::,1]["foo"] will be internally translated to |
5800 | | * ar.GetView("[0,::,1]['foo']") |
5801 | | * \note When using the C++ API and integer indexing only, you may use the |
5802 | | * at(idx0, idx1, ...) method. |
5803 | | * |
5804 | | * The returned array holds a reference to the original one, and thus is |
5805 | | * a view of it (not a copy). If the content of the original array changes, |
5806 | | * the content of the view array too. When using basic slicing and indexing, |
5807 | | * the view can be written if the underlying array is writable. |
5808 | | * |
5809 | | * This is the same as the C function GDALMDArrayGetView() |
5810 | | * |
5811 | | * @param viewExpr Expression expressing basic slicing and indexing, or field |
5812 | | * access. |
5813 | | * @return a new array, that holds a reference to the original one, and thus is |
5814 | | * a view of it (not a copy), or nullptr in case of error. |
5815 | | */ |
5816 | | // clang-format on |
5817 | | |
5818 | | std::shared_ptr<GDALMDArray> |
5819 | | GDALMDArray::GetView(const std::string &viewExpr) const |
5820 | 0 | { |
5821 | 0 | std::vector<ViewSpec> viewSpecs; |
5822 | 0 | return GetView(viewExpr, true, viewSpecs); |
5823 | 0 | } |
5824 | | |
5825 | | //! @cond Doxygen_Suppress |
5826 | | std::shared_ptr<GDALMDArray> |
5827 | | GDALMDArray::GetView(const std::string &viewExpr, bool bRenameDimensions, |
5828 | | std::vector<ViewSpec> &viewSpecs) const |
5829 | 0 | { |
5830 | 0 | auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock()); |
5831 | 0 | if (!self) |
5832 | 0 | { |
5833 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
5834 | 0 | "Driver implementation issue: m_pSelf not set !"); |
5835 | 0 | return nullptr; |
5836 | 0 | } |
5837 | 0 | std::string curExpr(viewExpr); |
5838 | 0 | while (true) |
5839 | 0 | { |
5840 | 0 | if (curExpr.empty() || curExpr[0] != '[') |
5841 | 0 | { |
5842 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
5843 | 0 | "Slice string should start with ['"); |
5844 | 0 | return nullptr; |
5845 | 0 | } |
5846 | | |
5847 | 0 | std::string fieldName; |
5848 | 0 | size_t endExpr; |
5849 | 0 | if (curExpr.size() > 2 && (curExpr[1] == '"' || curExpr[1] == '\'')) |
5850 | 0 | { |
5851 | 0 | if (self->GetDataType().GetClass() != GEDTC_COMPOUND) |
5852 | 0 | { |
5853 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
5854 | 0 | "Field access not allowed on non-compound data type"); |
5855 | 0 | return nullptr; |
5856 | 0 | } |
5857 | 0 | size_t idx = 2; |
5858 | 0 | for (; idx < curExpr.size(); idx++) |
5859 | 0 | { |
5860 | 0 | const char ch = curExpr[idx]; |
5861 | 0 | if (ch == curExpr[1]) |
5862 | 0 | break; |
5863 | 0 | if (ch == '\\' && idx + 1 < curExpr.size()) |
5864 | 0 | { |
5865 | 0 | fieldName += curExpr[idx + 1]; |
5866 | 0 | idx++; |
5867 | 0 | } |
5868 | 0 | else |
5869 | 0 | { |
5870 | 0 | fieldName += ch; |
5871 | 0 | } |
5872 | 0 | } |
5873 | 0 | if (idx + 1 >= curExpr.size() || curExpr[idx + 1] != ']') |
5874 | 0 | { |
5875 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
5876 | 0 | "Invalid field access specification"); |
5877 | 0 | return nullptr; |
5878 | 0 | } |
5879 | 0 | endExpr = idx + 1; |
5880 | 0 | } |
5881 | 0 | else |
5882 | 0 | { |
5883 | 0 | endExpr = curExpr.find(']'); |
5884 | 0 | } |
5885 | 0 | if (endExpr == std::string::npos) |
5886 | 0 | { |
5887 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Missing ]'"); |
5888 | 0 | return nullptr; |
5889 | 0 | } |
5890 | 0 | if (endExpr == 1) |
5891 | 0 | { |
5892 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "[] not allowed"); |
5893 | 0 | return nullptr; |
5894 | 0 | } |
5895 | 0 | std::string activeSlice(curExpr.substr(1, endExpr - 1)); |
5896 | |
|
5897 | 0 | if (!fieldName.empty()) |
5898 | 0 | { |
5899 | 0 | ViewSpec viewSpec; |
5900 | 0 | viewSpec.m_osFieldName = fieldName; |
5901 | 0 | viewSpecs.emplace_back(std::move(viewSpec)); |
5902 | 0 | } |
5903 | |
|
5904 | 0 | auto newArray = !fieldName.empty() |
5905 | 0 | ? CreateFieldNameExtractArray(self, fieldName) |
5906 | 0 | : CreateSlicedArray(self, viewExpr, activeSlice, |
5907 | 0 | bRenameDimensions, viewSpecs); |
5908 | |
|
5909 | 0 | if (endExpr == curExpr.size() - 1) |
5910 | 0 | { |
5911 | 0 | return newArray; |
5912 | 0 | } |
5913 | 0 | self = std::move(newArray); |
5914 | 0 | curExpr = curExpr.substr(endExpr + 1); |
5915 | 0 | } |
5916 | 0 | } |
5917 | | |
5918 | | //! @endcond |
5919 | | |
5920 | | std::shared_ptr<GDALMDArray> |
5921 | | GDALMDArray::GetView(const std::vector<GUInt64> &indices) const |
5922 | 0 | { |
5923 | 0 | std::string osExpr("["); |
5924 | 0 | bool bFirst = true; |
5925 | 0 | for (const auto &idx : indices) |
5926 | 0 | { |
5927 | 0 | if (!bFirst) |
5928 | 0 | osExpr += ','; |
5929 | 0 | bFirst = false; |
5930 | 0 | osExpr += CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(idx)); |
5931 | 0 | } |
5932 | 0 | return GetView(osExpr + ']'); |
5933 | 0 | } |
5934 | | |
5935 | | /************************************************************************/ |
5936 | | /* operator[] */ |
5937 | | /************************************************************************/ |
5938 | | |
5939 | | /** Return a view of the array using field access |
5940 | | * |
5941 | | * Equivalent of GetView("['fieldName']") |
5942 | | * |
5943 | | * \note When operating on a shared_ptr, use (*array)["fieldName"] syntax. |
5944 | | */ |
5945 | | std::shared_ptr<GDALMDArray> |
5946 | | GDALMDArray::operator[](const std::string &fieldName) const |
5947 | 0 | { |
5948 | 0 | return GetView(CPLSPrintf("['%s']", CPLString(fieldName) |
5949 | 0 | .replaceAll('\\', "\\\\") |
5950 | 0 | .replaceAll('\'', "\\\'") |
5951 | 0 | .c_str())); |
5952 | 0 | } |
5953 | | |
5954 | | /************************************************************************/ |
5955 | | /* GDALMDArrayTransposed */ |
5956 | | /************************************************************************/ |
5957 | | |
5958 | | class GDALMDArrayTransposed final : public GDALPamMDArray |
5959 | | { |
5960 | | private: |
5961 | | std::shared_ptr<GDALMDArray> m_poParent{}; |
5962 | | std::vector<int> m_anMapNewAxisToOldAxis{}; |
5963 | | std::vector<std::shared_ptr<GDALDimension>> m_dims{}; |
5964 | | |
5965 | | mutable std::vector<GUInt64> m_parentStart; |
5966 | | mutable std::vector<size_t> m_parentCount; |
5967 | | mutable std::vector<GInt64> m_parentStep; |
5968 | | mutable std::vector<GPtrDiff_t> m_parentStride; |
5969 | | |
5970 | | void PrepareParentArrays(const GUInt64 *arrayStartIdx, const size_t *count, |
5971 | | const GInt64 *arrayStep, |
5972 | | const GPtrDiff_t *bufferStride) const; |
5973 | | |
5974 | | static std::string |
5975 | | MappingToStr(const std::vector<int> &anMapNewAxisToOldAxis) |
5976 | 0 | { |
5977 | 0 | std::string ret; |
5978 | 0 | ret += '['; |
5979 | 0 | for (size_t i = 0; i < anMapNewAxisToOldAxis.size(); ++i) |
5980 | 0 | { |
5981 | 0 | if (i > 0) |
5982 | 0 | ret += ','; |
5983 | 0 | ret += CPLSPrintf("%d", anMapNewAxisToOldAxis[i]); |
5984 | 0 | } |
5985 | 0 | ret += ']'; |
5986 | 0 | return ret; |
5987 | 0 | } |
5988 | | |
5989 | | protected: |
5990 | | GDALMDArrayTransposed(const std::shared_ptr<GDALMDArray> &poParent, |
5991 | | const std::vector<int> &anMapNewAxisToOldAxis, |
5992 | | std::vector<std::shared_ptr<GDALDimension>> &&dims) |
5993 | 0 | : GDALAbstractMDArray(std::string(), |
5994 | 0 | "Transposed view of " + poParent->GetFullName() + |
5995 | 0 | " along " + |
5996 | 0 | MappingToStr(anMapNewAxisToOldAxis)), |
5997 | 0 | GDALPamMDArray(std::string(), |
5998 | 0 | "Transposed view of " + poParent->GetFullName() + |
5999 | 0 | " along " + MappingToStr(anMapNewAxisToOldAxis), |
6000 | 0 | GDALPamMultiDim::GetPAM(poParent), |
6001 | 0 | poParent->GetContext()), |
6002 | 0 | m_poParent(std::move(poParent)), |
6003 | 0 | m_anMapNewAxisToOldAxis(anMapNewAxisToOldAxis), |
6004 | 0 | m_dims(std::move(dims)), |
6005 | 0 | m_parentStart(m_poParent->GetDimensionCount()), |
6006 | 0 | m_parentCount(m_poParent->GetDimensionCount()), |
6007 | 0 | m_parentStep(m_poParent->GetDimensionCount()), |
6008 | 0 | m_parentStride(m_poParent->GetDimensionCount()) |
6009 | 0 | { |
6010 | 0 | } |
6011 | | |
6012 | | bool IRead(const GUInt64 *arrayStartIdx, const size_t *count, |
6013 | | const GInt64 *arrayStep, const GPtrDiff_t *bufferStride, |
6014 | | const GDALExtendedDataType &bufferDataType, |
6015 | | void *pDstBuffer) const override; |
6016 | | |
6017 | | bool IWrite(const GUInt64 *arrayStartIdx, const size_t *count, |
6018 | | const GInt64 *arrayStep, const GPtrDiff_t *bufferStride, |
6019 | | const GDALExtendedDataType &bufferDataType, |
6020 | | const void *pSrcBuffer) override; |
6021 | | |
6022 | | bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count, |
6023 | | CSLConstList papszOptions) const override; |
6024 | | |
6025 | | public: |
6026 | | static std::shared_ptr<GDALMDArrayTransposed> |
6027 | | Create(const std::shared_ptr<GDALMDArray> &poParent, |
6028 | | const std::vector<int> &anMapNewAxisToOldAxis) |
6029 | 0 | { |
6030 | 0 | const auto &parentDims(poParent->GetDimensions()); |
6031 | 0 | std::vector<std::shared_ptr<GDALDimension>> dims; |
6032 | 0 | for (const auto iOldAxis : anMapNewAxisToOldAxis) |
6033 | 0 | { |
6034 | 0 | if (iOldAxis < 0) |
6035 | 0 | { |
6036 | 0 | dims.push_back(std::make_shared<GDALDimension>( |
6037 | 0 | std::string(), "newaxis", std::string(), std::string(), 1)); |
6038 | 0 | } |
6039 | 0 | else |
6040 | 0 | { |
6041 | 0 | dims.emplace_back(parentDims[iOldAxis]); |
6042 | 0 | } |
6043 | 0 | } |
6044 | |
|
6045 | 0 | auto newAr( |
6046 | 0 | std::shared_ptr<GDALMDArrayTransposed>(new GDALMDArrayTransposed( |
6047 | 0 | poParent, anMapNewAxisToOldAxis, std::move(dims)))); |
6048 | 0 | newAr->SetSelf(newAr); |
6049 | 0 | return newAr; |
6050 | 0 | } |
6051 | | |
6052 | | bool IsWritable() const override |
6053 | 0 | { |
6054 | 0 | return m_poParent->IsWritable(); |
6055 | 0 | } |
6056 | | |
6057 | | const std::string &GetFilename() const override |
6058 | 0 | { |
6059 | 0 | return m_poParent->GetFilename(); |
6060 | 0 | } |
6061 | | |
6062 | | const std::vector<std::shared_ptr<GDALDimension>> & |
6063 | | GetDimensions() const override |
6064 | 0 | { |
6065 | 0 | return m_dims; |
6066 | 0 | } |
6067 | | |
6068 | | const GDALExtendedDataType &GetDataType() const override |
6069 | 0 | { |
6070 | 0 | return m_poParent->GetDataType(); |
6071 | 0 | } |
6072 | | |
6073 | | const std::string &GetUnit() const override |
6074 | 0 | { |
6075 | 0 | return m_poParent->GetUnit(); |
6076 | 0 | } |
6077 | | |
6078 | | std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override |
6079 | 0 | { |
6080 | 0 | auto poSrcSRS = m_poParent->GetSpatialRef(); |
6081 | 0 | if (!poSrcSRS) |
6082 | 0 | return nullptr; |
6083 | 0 | auto srcMapping = poSrcSRS->GetDataAxisToSRSAxisMapping(); |
6084 | 0 | std::vector<int> dstMapping; |
6085 | 0 | for (int srcAxis : srcMapping) |
6086 | 0 | { |
6087 | 0 | bool bFound = false; |
6088 | 0 | for (size_t i = 0; i < m_anMapNewAxisToOldAxis.size(); i++) |
6089 | 0 | { |
6090 | 0 | if (m_anMapNewAxisToOldAxis[i] == srcAxis - 1) |
6091 | 0 | { |
6092 | 0 | dstMapping.push_back(static_cast<int>(i) + 1); |
6093 | 0 | bFound = true; |
6094 | 0 | break; |
6095 | 0 | } |
6096 | 0 | } |
6097 | 0 | if (!bFound) |
6098 | 0 | { |
6099 | 0 | dstMapping.push_back(0); |
6100 | 0 | } |
6101 | 0 | } |
6102 | 0 | auto poClone(std::shared_ptr<OGRSpatialReference>(poSrcSRS->Clone())); |
6103 | 0 | poClone->SetDataAxisToSRSAxisMapping(dstMapping); |
6104 | 0 | return poClone; |
6105 | 0 | } |
6106 | | |
6107 | | const void *GetRawNoDataValue() const override |
6108 | 0 | { |
6109 | 0 | return m_poParent->GetRawNoDataValue(); |
6110 | 0 | } |
6111 | | |
6112 | | // bool SetRawNoDataValue(const void* pRawNoData) override { return |
6113 | | // m_poParent->SetRawNoDataValue(pRawNoData); } |
6114 | | |
6115 | | double GetOffset(bool *pbHasOffset, |
6116 | | GDALDataType *peStorageType) const override |
6117 | 0 | { |
6118 | 0 | return m_poParent->GetOffset(pbHasOffset, peStorageType); |
6119 | 0 | } |
6120 | | |
6121 | | double GetScale(bool *pbHasScale, |
6122 | | GDALDataType *peStorageType) const override |
6123 | 0 | { |
6124 | 0 | return m_poParent->GetScale(pbHasScale, peStorageType); |
6125 | 0 | } |
6126 | | |
6127 | | // bool SetOffset(double dfOffset) override { return |
6128 | | // m_poParent->SetOffset(dfOffset); } |
6129 | | |
6130 | | // bool SetScale(double dfScale) override { return |
6131 | | // m_poParent->SetScale(dfScale); } |
6132 | | |
6133 | | std::vector<GUInt64> GetBlockSize() const override |
6134 | 0 | { |
6135 | 0 | std::vector<GUInt64> ret(GetDimensionCount()); |
6136 | 0 | const auto parentBlockSize(m_poParent->GetBlockSize()); |
6137 | 0 | for (size_t i = 0; i < m_anMapNewAxisToOldAxis.size(); ++i) |
6138 | 0 | { |
6139 | 0 | const auto iOldAxis = m_anMapNewAxisToOldAxis[i]; |
6140 | 0 | if (iOldAxis >= 0) |
6141 | 0 | { |
6142 | 0 | ret[i] = parentBlockSize[iOldAxis]; |
6143 | 0 | } |
6144 | 0 | } |
6145 | 0 | return ret; |
6146 | 0 | } |
6147 | | |
6148 | | std::shared_ptr<GDALAttribute> |
6149 | | GetAttribute(const std::string &osName) const override |
6150 | 0 | { |
6151 | 0 | return m_poParent->GetAttribute(osName); |
6152 | 0 | } |
6153 | | |
6154 | | std::vector<std::shared_ptr<GDALAttribute>> |
6155 | | GetAttributes(CSLConstList papszOptions = nullptr) const override |
6156 | 0 | { |
6157 | 0 | return m_poParent->GetAttributes(papszOptions); |
6158 | 0 | } |
6159 | | }; |
6160 | | |
6161 | | /************************************************************************/ |
6162 | | /* PrepareParentArrays() */ |
6163 | | /************************************************************************/ |
6164 | | |
6165 | | void GDALMDArrayTransposed::PrepareParentArrays( |
6166 | | const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep, |
6167 | | const GPtrDiff_t *bufferStride) const |
6168 | 0 | { |
6169 | 0 | for (size_t i = 0; i < m_anMapNewAxisToOldAxis.size(); ++i) |
6170 | 0 | { |
6171 | 0 | const auto iOldAxis = m_anMapNewAxisToOldAxis[i]; |
6172 | 0 | if (iOldAxis >= 0) |
6173 | 0 | { |
6174 | 0 | m_parentStart[iOldAxis] = arrayStartIdx[i]; |
6175 | 0 | m_parentCount[iOldAxis] = count[i]; |
6176 | 0 | if (arrayStep) // only null when called from IAdviseRead() |
6177 | 0 | { |
6178 | 0 | m_parentStep[iOldAxis] = arrayStep[i]; |
6179 | 0 | } |
6180 | 0 | if (bufferStride) // only null when called from IAdviseRead() |
6181 | 0 | { |
6182 | 0 | m_parentStride[iOldAxis] = bufferStride[i]; |
6183 | 0 | } |
6184 | 0 | } |
6185 | 0 | } |
6186 | 0 | } |
6187 | | |
6188 | | /************************************************************************/ |
6189 | | /* IRead() */ |
6190 | | /************************************************************************/ |
6191 | | |
6192 | | bool GDALMDArrayTransposed::IRead(const GUInt64 *arrayStartIdx, |
6193 | | const size_t *count, const GInt64 *arrayStep, |
6194 | | const GPtrDiff_t *bufferStride, |
6195 | | const GDALExtendedDataType &bufferDataType, |
6196 | | void *pDstBuffer) const |
6197 | 0 | { |
6198 | 0 | PrepareParentArrays(arrayStartIdx, count, arrayStep, bufferStride); |
6199 | 0 | return m_poParent->Read(m_parentStart.data(), m_parentCount.data(), |
6200 | 0 | m_parentStep.data(), m_parentStride.data(), |
6201 | 0 | bufferDataType, pDstBuffer); |
6202 | 0 | } |
6203 | | |
6204 | | /************************************************************************/ |
6205 | | /* IWrite() */ |
6206 | | /************************************************************************/ |
6207 | | |
6208 | | bool GDALMDArrayTransposed::IWrite(const GUInt64 *arrayStartIdx, |
6209 | | const size_t *count, const GInt64 *arrayStep, |
6210 | | const GPtrDiff_t *bufferStride, |
6211 | | const GDALExtendedDataType &bufferDataType, |
6212 | | const void *pSrcBuffer) |
6213 | 0 | { |
6214 | 0 | PrepareParentArrays(arrayStartIdx, count, arrayStep, bufferStride); |
6215 | 0 | return m_poParent->Write(m_parentStart.data(), m_parentCount.data(), |
6216 | 0 | m_parentStep.data(), m_parentStride.data(), |
6217 | 0 | bufferDataType, pSrcBuffer); |
6218 | 0 | } |
6219 | | |
6220 | | /************************************************************************/ |
6221 | | /* IAdviseRead() */ |
6222 | | /************************************************************************/ |
6223 | | |
6224 | | bool GDALMDArrayTransposed::IAdviseRead(const GUInt64 *arrayStartIdx, |
6225 | | const size_t *count, |
6226 | | CSLConstList papszOptions) const |
6227 | 0 | { |
6228 | 0 | PrepareParentArrays(arrayStartIdx, count, nullptr, nullptr); |
6229 | 0 | return m_poParent->AdviseRead(m_parentStart.data(), m_parentCount.data(), |
6230 | 0 | papszOptions); |
6231 | 0 | } |
6232 | | |
6233 | | /************************************************************************/ |
6234 | | /* Transpose() */ |
6235 | | /************************************************************************/ |
6236 | | |
6237 | | /** Return a view of the array whose axis have been reordered. |
6238 | | * |
6239 | | * The anMapNewAxisToOldAxis parameter should contain all the values between 0 |
6240 | | * and GetDimensionCount() - 1, and each only once. |
6241 | | * -1 can be used as a special index value to ask for the insertion of a new |
6242 | | * axis of size 1. |
6243 | | * The new array will have anMapNewAxisToOldAxis.size() axis, and if i is the |
6244 | | * index of one of its dimension, it corresponds to the axis of index |
6245 | | * anMapNewAxisToOldAxis[i] from the current array. |
6246 | | * |
6247 | | * This is similar to the numpy.transpose() method |
6248 | | * |
6249 | | * The returned array holds a reference to the original one, and thus is |
6250 | | * a view of it (not a copy). If the content of the original array changes, |
6251 | | * the content of the view array too. The view can be written if the underlying |
6252 | | * array is writable. |
6253 | | * |
6254 | | * Note that I/O performance in such a transposed view might be poor. |
6255 | | * |
6256 | | * This is the same as the C function GDALMDArrayTranspose(). |
6257 | | * |
6258 | | * @return a new array, that holds a reference to the original one, and thus is |
6259 | | * a view of it (not a copy), or nullptr in case of error. |
6260 | | */ |
6261 | | std::shared_ptr<GDALMDArray> |
6262 | | GDALMDArray::Transpose(const std::vector<int> &anMapNewAxisToOldAxis) const |
6263 | 0 | { |
6264 | 0 | auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock()); |
6265 | 0 | if (!self) |
6266 | 0 | { |
6267 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6268 | 0 | "Driver implementation issue: m_pSelf not set !"); |
6269 | 0 | return nullptr; |
6270 | 0 | } |
6271 | 0 | const int nDims = static_cast<int>(GetDimensionCount()); |
6272 | 0 | std::vector<bool> alreadyUsedOldAxis(nDims, false); |
6273 | 0 | int nCountOldAxis = 0; |
6274 | 0 | for (const auto iOldAxis : anMapNewAxisToOldAxis) |
6275 | 0 | { |
6276 | 0 | if (iOldAxis < -1 || iOldAxis >= nDims) |
6277 | 0 | { |
6278 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid axis number"); |
6279 | 0 | return nullptr; |
6280 | 0 | } |
6281 | 0 | if (iOldAxis >= 0) |
6282 | 0 | { |
6283 | 0 | if (alreadyUsedOldAxis[iOldAxis]) |
6284 | 0 | { |
6285 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Axis %d is repeated", |
6286 | 0 | iOldAxis); |
6287 | 0 | return nullptr; |
6288 | 0 | } |
6289 | 0 | alreadyUsedOldAxis[iOldAxis] = true; |
6290 | 0 | nCountOldAxis++; |
6291 | 0 | } |
6292 | 0 | } |
6293 | 0 | if (nCountOldAxis != nDims) |
6294 | 0 | { |
6295 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6296 | 0 | "One or several original axis missing"); |
6297 | 0 | return nullptr; |
6298 | 0 | } |
6299 | 0 | return GDALMDArrayTransposed::Create(self, anMapNewAxisToOldAxis); |
6300 | 0 | } |
6301 | | |
6302 | | /************************************************************************/ |
6303 | | /* IRead() */ |
6304 | | /************************************************************************/ |
6305 | | |
6306 | | bool GDALMDArrayUnscaled::IRead(const GUInt64 *arrayStartIdx, |
6307 | | const size_t *count, const GInt64 *arrayStep, |
6308 | | const GPtrDiff_t *bufferStride, |
6309 | | const GDALExtendedDataType &bufferDataType, |
6310 | | void *pDstBuffer) const |
6311 | 0 | { |
6312 | 0 | const double dfScale = m_dfScale; |
6313 | 0 | const double dfOffset = m_dfOffset; |
6314 | 0 | const bool bDTIsComplex = GDALDataTypeIsComplex(m_dt.GetNumericDataType()); |
6315 | 0 | const auto dtDouble = |
6316 | 0 | GDALExtendedDataType::Create(bDTIsComplex ? GDT_CFloat64 : GDT_Float64); |
6317 | 0 | const size_t nDTSize = dtDouble.GetSize(); |
6318 | 0 | const bool bTempBufferNeeded = (dtDouble != bufferDataType); |
6319 | |
|
6320 | 0 | double adfSrcNoData[2] = {0, 0}; |
6321 | 0 | if (m_bHasNoData) |
6322 | 0 | { |
6323 | 0 | GDALExtendedDataType::CopyValue(m_poParent->GetRawNoDataValue(), |
6324 | 0 | m_poParent->GetDataType(), |
6325 | 0 | &adfSrcNoData[0], dtDouble); |
6326 | 0 | } |
6327 | |
|
6328 | 0 | const auto nDims = GetDimensions().size(); |
6329 | 0 | if (nDims == 0) |
6330 | 0 | { |
6331 | 0 | double adfVal[2]; |
6332 | 0 | if (!m_poParent->Read(arrayStartIdx, count, arrayStep, bufferStride, |
6333 | 0 | dtDouble, &adfVal[0])) |
6334 | 0 | { |
6335 | 0 | return false; |
6336 | 0 | } |
6337 | 0 | if (!m_bHasNoData || adfVal[0] != adfSrcNoData[0]) |
6338 | 0 | { |
6339 | 0 | adfVal[0] = adfVal[0] * dfScale + dfOffset; |
6340 | 0 | if (bDTIsComplex) |
6341 | 0 | { |
6342 | 0 | adfVal[1] = adfVal[1] * dfScale + dfOffset; |
6343 | 0 | } |
6344 | 0 | GDALExtendedDataType::CopyValue(&adfVal[0], dtDouble, pDstBuffer, |
6345 | 0 | bufferDataType); |
6346 | 0 | } |
6347 | 0 | else |
6348 | 0 | { |
6349 | 0 | GDALExtendedDataType::CopyValue(m_abyRawNoData.data(), m_dt, |
6350 | 0 | pDstBuffer, bufferDataType); |
6351 | 0 | } |
6352 | 0 | return true; |
6353 | 0 | } |
6354 | | |
6355 | 0 | std::vector<GPtrDiff_t> actualBufferStrideVector; |
6356 | 0 | const GPtrDiff_t *actualBufferStridePtr = bufferStride; |
6357 | 0 | void *pTempBuffer = pDstBuffer; |
6358 | 0 | if (bTempBufferNeeded) |
6359 | 0 | { |
6360 | 0 | size_t nElts = 1; |
6361 | 0 | actualBufferStrideVector.resize(nDims); |
6362 | 0 | for (size_t i = 0; i < nDims; i++) |
6363 | 0 | nElts *= count[i]; |
6364 | 0 | actualBufferStrideVector.back() = 1; |
6365 | 0 | for (size_t i = nDims - 1; i > 0;) |
6366 | 0 | { |
6367 | 0 | --i; |
6368 | 0 | actualBufferStrideVector[i] = |
6369 | 0 | actualBufferStrideVector[i + 1] * count[i + 1]; |
6370 | 0 | } |
6371 | 0 | actualBufferStridePtr = actualBufferStrideVector.data(); |
6372 | 0 | pTempBuffer = VSI_MALLOC2_VERBOSE(nDTSize, nElts); |
6373 | 0 | if (!pTempBuffer) |
6374 | 0 | return false; |
6375 | 0 | } |
6376 | 0 | if (!m_poParent->Read(arrayStartIdx, count, arrayStep, |
6377 | 0 | actualBufferStridePtr, dtDouble, pTempBuffer)) |
6378 | 0 | { |
6379 | 0 | if (bTempBufferNeeded) |
6380 | 0 | VSIFree(pTempBuffer); |
6381 | 0 | return false; |
6382 | 0 | } |
6383 | | |
6384 | 0 | struct Stack |
6385 | 0 | { |
6386 | 0 | size_t nIters = 0; |
6387 | 0 | double *src_ptr = nullptr; |
6388 | 0 | GByte *dst_ptr = nullptr; |
6389 | 0 | GPtrDiff_t src_inc_offset = 0; |
6390 | 0 | GPtrDiff_t dst_inc_offset = 0; |
6391 | 0 | }; |
6392 | |
|
6393 | 0 | std::vector<Stack> stack(nDims); |
6394 | 0 | const size_t nBufferDTSize = bufferDataType.GetSize(); |
6395 | 0 | for (size_t i = 0; i < nDims; i++) |
6396 | 0 | { |
6397 | 0 | stack[i].src_inc_offset = |
6398 | 0 | actualBufferStridePtr[i] * (bDTIsComplex ? 2 : 1); |
6399 | 0 | stack[i].dst_inc_offset = |
6400 | 0 | static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize); |
6401 | 0 | } |
6402 | 0 | stack[0].src_ptr = static_cast<double *>(pTempBuffer); |
6403 | 0 | stack[0].dst_ptr = static_cast<GByte *>(pDstBuffer); |
6404 | |
|
6405 | 0 | size_t dimIdx = 0; |
6406 | 0 | const size_t nDimsMinus1 = nDims - 1; |
6407 | 0 | GByte abyDstNoData[16]; |
6408 | 0 | CPLAssert(nBufferDTSize <= sizeof(abyDstNoData)); |
6409 | 0 | GDALExtendedDataType::CopyValue(m_abyRawNoData.data(), m_dt, abyDstNoData, |
6410 | 0 | bufferDataType); |
6411 | |
|
6412 | 0 | lbl_next_depth: |
6413 | 0 | if (dimIdx == nDimsMinus1) |
6414 | 0 | { |
6415 | 0 | auto nIters = count[dimIdx]; |
6416 | 0 | double *padfVal = stack[dimIdx].src_ptr; |
6417 | 0 | GByte *dst_ptr = stack[dimIdx].dst_ptr; |
6418 | 0 | while (true) |
6419 | 0 | { |
6420 | 0 | if (!m_bHasNoData || padfVal[0] != adfSrcNoData[0]) |
6421 | 0 | { |
6422 | 0 | padfVal[0] = padfVal[0] * dfScale + dfOffset; |
6423 | 0 | if (bDTIsComplex) |
6424 | 0 | { |
6425 | 0 | padfVal[1] = padfVal[1] * dfScale + dfOffset; |
6426 | 0 | } |
6427 | 0 | if (bTempBufferNeeded) |
6428 | 0 | { |
6429 | 0 | GDALExtendedDataType::CopyValue(&padfVal[0], dtDouble, |
6430 | 0 | dst_ptr, bufferDataType); |
6431 | 0 | } |
6432 | 0 | } |
6433 | 0 | else |
6434 | 0 | { |
6435 | 0 | memcpy(dst_ptr, abyDstNoData, nBufferDTSize); |
6436 | 0 | } |
6437 | |
|
6438 | 0 | if ((--nIters) == 0) |
6439 | 0 | break; |
6440 | 0 | padfVal += stack[dimIdx].src_inc_offset; |
6441 | 0 | dst_ptr += stack[dimIdx].dst_inc_offset; |
6442 | 0 | } |
6443 | 0 | } |
6444 | 0 | else |
6445 | 0 | { |
6446 | 0 | stack[dimIdx].nIters = count[dimIdx]; |
6447 | 0 | while (true) |
6448 | 0 | { |
6449 | 0 | dimIdx++; |
6450 | 0 | stack[dimIdx].src_ptr = stack[dimIdx - 1].src_ptr; |
6451 | 0 | stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr; |
6452 | 0 | goto lbl_next_depth; |
6453 | 0 | lbl_return_to_caller: |
6454 | 0 | dimIdx--; |
6455 | 0 | if ((--stack[dimIdx].nIters) == 0) |
6456 | 0 | break; |
6457 | 0 | stack[dimIdx].src_ptr += stack[dimIdx].src_inc_offset; |
6458 | 0 | stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset; |
6459 | 0 | } |
6460 | 0 | } |
6461 | 0 | if (dimIdx > 0) |
6462 | 0 | goto lbl_return_to_caller; |
6463 | | |
6464 | 0 | if (bTempBufferNeeded) |
6465 | 0 | VSIFree(pTempBuffer); |
6466 | 0 | return true; |
6467 | 0 | } |
6468 | | |
6469 | | /************************************************************************/ |
6470 | | /* IWrite() */ |
6471 | | /************************************************************************/ |
6472 | | |
6473 | | bool GDALMDArrayUnscaled::IWrite(const GUInt64 *arrayStartIdx, |
6474 | | const size_t *count, const GInt64 *arrayStep, |
6475 | | const GPtrDiff_t *bufferStride, |
6476 | | const GDALExtendedDataType &bufferDataType, |
6477 | | const void *pSrcBuffer) |
6478 | 0 | { |
6479 | 0 | const double dfScale = m_dfScale; |
6480 | 0 | const double dfOffset = m_dfOffset; |
6481 | 0 | const bool bDTIsComplex = GDALDataTypeIsComplex(m_dt.GetNumericDataType()); |
6482 | 0 | const auto dtDouble = |
6483 | 0 | GDALExtendedDataType::Create(bDTIsComplex ? GDT_CFloat64 : GDT_Float64); |
6484 | 0 | const size_t nDTSize = dtDouble.GetSize(); |
6485 | 0 | const bool bIsBufferDataTypeNativeDataType = (dtDouble == bufferDataType); |
6486 | 0 | const bool bSelfAndParentHaveNoData = |
6487 | 0 | m_bHasNoData && m_poParent->GetRawNoDataValue() != nullptr; |
6488 | 0 | double dfNoData = 0; |
6489 | 0 | if (m_bHasNoData) |
6490 | 0 | { |
6491 | 0 | GDALCopyWords64(m_abyRawNoData.data(), m_dt.GetNumericDataType(), 0, |
6492 | 0 | &dfNoData, GDT_Float64, 0, 1); |
6493 | 0 | } |
6494 | |
|
6495 | 0 | double adfSrcNoData[2] = {0, 0}; |
6496 | 0 | if (bSelfAndParentHaveNoData) |
6497 | 0 | { |
6498 | 0 | GDALExtendedDataType::CopyValue(m_poParent->GetRawNoDataValue(), |
6499 | 0 | m_poParent->GetDataType(), |
6500 | 0 | &adfSrcNoData[0], dtDouble); |
6501 | 0 | } |
6502 | |
|
6503 | 0 | const auto nDims = GetDimensions().size(); |
6504 | 0 | if (nDims == 0) |
6505 | 0 | { |
6506 | 0 | double adfVal[2]; |
6507 | 0 | GDALExtendedDataType::CopyValue(pSrcBuffer, bufferDataType, &adfVal[0], |
6508 | 0 | dtDouble); |
6509 | 0 | if (bSelfAndParentHaveNoData && |
6510 | 0 | (std::isnan(adfVal[0]) || adfVal[0] == dfNoData)) |
6511 | 0 | { |
6512 | 0 | return m_poParent->Write(arrayStartIdx, count, arrayStep, |
6513 | 0 | bufferStride, m_poParent->GetDataType(), |
6514 | 0 | m_poParent->GetRawNoDataValue()); |
6515 | 0 | } |
6516 | 0 | else |
6517 | 0 | { |
6518 | 0 | adfVal[0] = (adfVal[0] - dfOffset) / dfScale; |
6519 | 0 | if (bDTIsComplex) |
6520 | 0 | { |
6521 | 0 | adfVal[1] = (adfVal[1] - dfOffset) / dfScale; |
6522 | 0 | } |
6523 | 0 | return m_poParent->Write(arrayStartIdx, count, arrayStep, |
6524 | 0 | bufferStride, dtDouble, &adfVal[0]); |
6525 | 0 | } |
6526 | 0 | } |
6527 | | |
6528 | 0 | std::vector<GPtrDiff_t> tmpBufferStrideVector; |
6529 | 0 | size_t nElts = 1; |
6530 | 0 | tmpBufferStrideVector.resize(nDims); |
6531 | 0 | for (size_t i = 0; i < nDims; i++) |
6532 | 0 | nElts *= count[i]; |
6533 | 0 | tmpBufferStrideVector.back() = 1; |
6534 | 0 | for (size_t i = nDims - 1; i > 0;) |
6535 | 0 | { |
6536 | 0 | --i; |
6537 | 0 | tmpBufferStrideVector[i] = tmpBufferStrideVector[i + 1] * count[i + 1]; |
6538 | 0 | } |
6539 | 0 | const GPtrDiff_t *tmpBufferStridePtr = tmpBufferStrideVector.data(); |
6540 | 0 | void *pTempBuffer = VSI_MALLOC2_VERBOSE(nDTSize, nElts); |
6541 | 0 | if (!pTempBuffer) |
6542 | 0 | return false; |
6543 | | |
6544 | 0 | struct Stack |
6545 | 0 | { |
6546 | 0 | size_t nIters = 0; |
6547 | 0 | double *dst_ptr = nullptr; |
6548 | 0 | const GByte *src_ptr = nullptr; |
6549 | 0 | GPtrDiff_t src_inc_offset = 0; |
6550 | 0 | GPtrDiff_t dst_inc_offset = 0; |
6551 | 0 | }; |
6552 | |
|
6553 | 0 | std::vector<Stack> stack(nDims); |
6554 | 0 | const size_t nBufferDTSize = bufferDataType.GetSize(); |
6555 | 0 | for (size_t i = 0; i < nDims; i++) |
6556 | 0 | { |
6557 | 0 | stack[i].dst_inc_offset = |
6558 | 0 | tmpBufferStridePtr[i] * (bDTIsComplex ? 2 : 1); |
6559 | 0 | stack[i].src_inc_offset = |
6560 | 0 | static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize); |
6561 | 0 | } |
6562 | 0 | stack[0].dst_ptr = static_cast<double *>(pTempBuffer); |
6563 | 0 | stack[0].src_ptr = static_cast<const GByte *>(pSrcBuffer); |
6564 | |
|
6565 | 0 | size_t dimIdx = 0; |
6566 | 0 | const size_t nDimsMinus1 = nDims - 1; |
6567 | |
|
6568 | 0 | lbl_next_depth: |
6569 | 0 | if (dimIdx == nDimsMinus1) |
6570 | 0 | { |
6571 | 0 | auto nIters = count[dimIdx]; |
6572 | 0 | double *dst_ptr = stack[dimIdx].dst_ptr; |
6573 | 0 | const GByte *src_ptr = stack[dimIdx].src_ptr; |
6574 | 0 | while (true) |
6575 | 0 | { |
6576 | 0 | double adfVal[2]; |
6577 | 0 | const double *padfSrcVal; |
6578 | 0 | if (bIsBufferDataTypeNativeDataType) |
6579 | 0 | { |
6580 | 0 | padfSrcVal = reinterpret_cast<const double *>(src_ptr); |
6581 | 0 | } |
6582 | 0 | else |
6583 | 0 | { |
6584 | 0 | GDALExtendedDataType::CopyValue(src_ptr, bufferDataType, |
6585 | 0 | &adfVal[0], dtDouble); |
6586 | 0 | padfSrcVal = adfVal; |
6587 | 0 | } |
6588 | |
|
6589 | 0 | if (bSelfAndParentHaveNoData && |
6590 | 0 | (std::isnan(padfSrcVal[0]) || padfSrcVal[0] == dfNoData)) |
6591 | 0 | { |
6592 | 0 | dst_ptr[0] = adfSrcNoData[0]; |
6593 | 0 | if (bDTIsComplex) |
6594 | 0 | { |
6595 | 0 | dst_ptr[1] = adfSrcNoData[1]; |
6596 | 0 | } |
6597 | 0 | } |
6598 | 0 | else |
6599 | 0 | { |
6600 | 0 | dst_ptr[0] = (padfSrcVal[0] - dfOffset) / dfScale; |
6601 | 0 | if (bDTIsComplex) |
6602 | 0 | { |
6603 | 0 | dst_ptr[1] = (padfSrcVal[1] - dfOffset) / dfScale; |
6604 | 0 | } |
6605 | 0 | } |
6606 | |
|
6607 | 0 | if ((--nIters) == 0) |
6608 | 0 | break; |
6609 | 0 | dst_ptr += stack[dimIdx].dst_inc_offset; |
6610 | 0 | src_ptr += stack[dimIdx].src_inc_offset; |
6611 | 0 | } |
6612 | 0 | } |
6613 | 0 | else |
6614 | 0 | { |
6615 | 0 | stack[dimIdx].nIters = count[dimIdx]; |
6616 | 0 | while (true) |
6617 | 0 | { |
6618 | 0 | dimIdx++; |
6619 | 0 | stack[dimIdx].src_ptr = stack[dimIdx - 1].src_ptr; |
6620 | 0 | stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr; |
6621 | 0 | goto lbl_next_depth; |
6622 | 0 | lbl_return_to_caller: |
6623 | 0 | dimIdx--; |
6624 | 0 | if ((--stack[dimIdx].nIters) == 0) |
6625 | 0 | break; |
6626 | 0 | stack[dimIdx].src_ptr += stack[dimIdx].src_inc_offset; |
6627 | 0 | stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset; |
6628 | 0 | } |
6629 | 0 | } |
6630 | 0 | if (dimIdx > 0) |
6631 | 0 | goto lbl_return_to_caller; |
6632 | | |
6633 | | // If the parent array is not double/complex-double, then convert the |
6634 | | // values to it, before calling Write(), as some implementations can be |
6635 | | // very slow when doing the type conversion. |
6636 | 0 | const auto &eParentDT = m_poParent->GetDataType(); |
6637 | 0 | const size_t nParentDTSize = eParentDT.GetSize(); |
6638 | 0 | if (nParentDTSize <= nDTSize / 2) |
6639 | 0 | { |
6640 | | // Copy in-place by making sure that source and target do not overlap |
6641 | 0 | const auto eNumericDT = dtDouble.GetNumericDataType(); |
6642 | 0 | const auto eParentNumericDT = eParentDT.GetNumericDataType(); |
6643 | | |
6644 | | // Copy first element |
6645 | 0 | { |
6646 | 0 | std::vector<GByte> abyTemp(nParentDTSize); |
6647 | 0 | GDALCopyWords64(static_cast<GByte *>(pTempBuffer), eNumericDT, |
6648 | 0 | static_cast<int>(nDTSize), &abyTemp[0], |
6649 | 0 | eParentNumericDT, static_cast<int>(nParentDTSize), |
6650 | 0 | 1); |
6651 | 0 | memcpy(pTempBuffer, abyTemp.data(), abyTemp.size()); |
6652 | 0 | } |
6653 | | // Remaining elements |
6654 | 0 | for (size_t i = 1; i < nElts; ++i) |
6655 | 0 | { |
6656 | 0 | GDALCopyWords64( |
6657 | 0 | static_cast<GByte *>(pTempBuffer) + i * nDTSize, eNumericDT, 0, |
6658 | 0 | static_cast<GByte *>(pTempBuffer) + i * nParentDTSize, |
6659 | 0 | eParentNumericDT, 0, 1); |
6660 | 0 | } |
6661 | 0 | } |
6662 | |
|
6663 | 0 | const bool ret = |
6664 | 0 | m_poParent->Write(arrayStartIdx, count, arrayStep, tmpBufferStridePtr, |
6665 | 0 | eParentDT, pTempBuffer); |
6666 | |
|
6667 | 0 | VSIFree(pTempBuffer); |
6668 | 0 | return ret; |
6669 | 0 | } |
6670 | | |
6671 | | /************************************************************************/ |
6672 | | /* GetUnscaled() */ |
6673 | | /************************************************************************/ |
6674 | | |
6675 | | /** Return an array that is the unscaled version of the current one. |
6676 | | * |
6677 | | * That is each value of the unscaled array will be |
6678 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
6679 | | * |
6680 | | * Starting with GDAL 3.3, the Write() method is implemented and will convert |
6681 | | * from unscaled values to raw values. |
6682 | | * |
6683 | | * This is the same as the C function GDALMDArrayGetUnscaled(). |
6684 | | * |
6685 | | * @param dfOverriddenScale Custom scale value instead of GetScale() |
6686 | | * @param dfOverriddenOffset Custom offset value instead of GetOffset() |
6687 | | * @param dfOverriddenDstNodata Custom target nodata value instead of NaN |
6688 | | * @return a new array, that holds a reference to the original one, and thus is |
6689 | | * a view of it (not a copy), or nullptr in case of error. |
6690 | | */ |
6691 | | std::shared_ptr<GDALMDArray> |
6692 | | GDALMDArray::GetUnscaled(double dfOverriddenScale, double dfOverriddenOffset, |
6693 | | double dfOverriddenDstNodata) const |
6694 | 0 | { |
6695 | 0 | auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock()); |
6696 | 0 | if (!self) |
6697 | 0 | { |
6698 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6699 | 0 | "Driver implementation issue: m_pSelf not set !"); |
6700 | 0 | return nullptr; |
6701 | 0 | } |
6702 | 0 | if (GetDataType().GetClass() != GEDTC_NUMERIC) |
6703 | 0 | { |
6704 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6705 | 0 | "GetUnscaled() only supports numeric data type"); |
6706 | 0 | return nullptr; |
6707 | 0 | } |
6708 | 0 | const double dfScale = |
6709 | 0 | std::isnan(dfOverriddenScale) ? GetScale() : dfOverriddenScale; |
6710 | 0 | const double dfOffset = |
6711 | 0 | std::isnan(dfOverriddenOffset) ? GetOffset() : dfOverriddenOffset; |
6712 | 0 | if (dfScale == 1.0 && dfOffset == 0.0) |
6713 | 0 | return self; |
6714 | | |
6715 | 0 | GDALDataType eDT = GDALDataTypeIsComplex(GetDataType().GetNumericDataType()) |
6716 | 0 | ? GDT_CFloat64 |
6717 | 0 | : GDT_Float64; |
6718 | 0 | if (dfOverriddenScale == -1 && dfOverriddenOffset == 0) |
6719 | 0 | { |
6720 | 0 | if (GetDataType().GetNumericDataType() == GDT_Float16) |
6721 | 0 | eDT = GDT_Float16; |
6722 | 0 | if (GetDataType().GetNumericDataType() == GDT_Float32) |
6723 | 0 | eDT = GDT_Float32; |
6724 | 0 | } |
6725 | |
|
6726 | 0 | return GDALMDArrayUnscaled::Create(self, dfScale, dfOffset, |
6727 | 0 | dfOverriddenDstNodata, eDT); |
6728 | 0 | } |
6729 | | |
6730 | | /************************************************************************/ |
6731 | | /* GDALMDArrayMask */ |
6732 | | /************************************************************************/ |
6733 | | |
6734 | | class GDALMDArrayMask final : public GDALPamMDArray |
6735 | | { |
6736 | | private: |
6737 | | std::shared_ptr<GDALMDArray> m_poParent{}; |
6738 | | GDALExtendedDataType m_dt{GDALExtendedDataType::Create(GDT_Byte)}; |
6739 | | double m_dfMissingValue = 0.0; |
6740 | | bool m_bHasMissingValue = false; |
6741 | | double m_dfFillValue = 0.0; |
6742 | | bool m_bHasFillValue = false; |
6743 | | double m_dfValidMin = 0.0; |
6744 | | bool m_bHasValidMin = false; |
6745 | | double m_dfValidMax = 0.0; |
6746 | | bool m_bHasValidMax = false; |
6747 | | std::vector<uint32_t> m_anValidFlagMasks{}; |
6748 | | std::vector<uint32_t> m_anValidFlagValues{}; |
6749 | | |
6750 | | bool Init(CSLConstList papszOptions); |
6751 | | |
6752 | | template <typename Type> |
6753 | | void |
6754 | | ReadInternal(const size_t *count, const GPtrDiff_t *bufferStride, |
6755 | | const GDALExtendedDataType &bufferDataType, void *pDstBuffer, |
6756 | | const void *pTempBuffer, |
6757 | | const GDALExtendedDataType &oTmpBufferDT, |
6758 | | const std::vector<GPtrDiff_t> &tmpBufferStrideVector) const; |
6759 | | |
6760 | | protected: |
6761 | | explicit GDALMDArrayMask(const std::shared_ptr<GDALMDArray> &poParent) |
6762 | 0 | : GDALAbstractMDArray(std::string(), |
6763 | 0 | "Mask of " + poParent->GetFullName()), |
6764 | 0 | GDALPamMDArray(std::string(), "Mask of " + poParent->GetFullName(), |
6765 | 0 | GDALPamMultiDim::GetPAM(poParent), |
6766 | 0 | poParent->GetContext()), |
6767 | 0 | m_poParent(std::move(poParent)) |
6768 | 0 | { |
6769 | 0 | } |
6770 | | |
6771 | | bool IRead(const GUInt64 *arrayStartIdx, const size_t *count, |
6772 | | const GInt64 *arrayStep, const GPtrDiff_t *bufferStride, |
6773 | | const GDALExtendedDataType &bufferDataType, |
6774 | | void *pDstBuffer) const override; |
6775 | | |
6776 | | bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count, |
6777 | | CSLConstList papszOptions) const override |
6778 | 0 | { |
6779 | 0 | return m_poParent->AdviseRead(arrayStartIdx, count, papszOptions); |
6780 | 0 | } |
6781 | | |
6782 | | public: |
6783 | | static std::shared_ptr<GDALMDArrayMask> |
6784 | | Create(const std::shared_ptr<GDALMDArray> &poParent, |
6785 | | CSLConstList papszOptions); |
6786 | | |
6787 | | bool IsWritable() const override |
6788 | 0 | { |
6789 | 0 | return false; |
6790 | 0 | } |
6791 | | |
6792 | | const std::string &GetFilename() const override |
6793 | 0 | { |
6794 | 0 | return m_poParent->GetFilename(); |
6795 | 0 | } |
6796 | | |
6797 | | const std::vector<std::shared_ptr<GDALDimension>> & |
6798 | | GetDimensions() const override |
6799 | 0 | { |
6800 | 0 | return m_poParent->GetDimensions(); |
6801 | 0 | } |
6802 | | |
6803 | | const GDALExtendedDataType &GetDataType() const override |
6804 | 0 | { |
6805 | 0 | return m_dt; |
6806 | 0 | } |
6807 | | |
6808 | | std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override |
6809 | 0 | { |
6810 | 0 | return m_poParent->GetSpatialRef(); |
6811 | 0 | } |
6812 | | |
6813 | | std::vector<GUInt64> GetBlockSize() const override |
6814 | 0 | { |
6815 | 0 | return m_poParent->GetBlockSize(); |
6816 | 0 | } |
6817 | | }; |
6818 | | |
6819 | | /************************************************************************/ |
6820 | | /* GDALMDArrayMask::Create() */ |
6821 | | /************************************************************************/ |
6822 | | |
6823 | | /* static */ std::shared_ptr<GDALMDArrayMask> |
6824 | | GDALMDArrayMask::Create(const std::shared_ptr<GDALMDArray> &poParent, |
6825 | | CSLConstList papszOptions) |
6826 | 0 | { |
6827 | 0 | auto newAr(std::shared_ptr<GDALMDArrayMask>(new GDALMDArrayMask(poParent))); |
6828 | 0 | newAr->SetSelf(newAr); |
6829 | 0 | if (!newAr->Init(papszOptions)) |
6830 | 0 | return nullptr; |
6831 | 0 | return newAr; |
6832 | 0 | } |
6833 | | |
6834 | | /************************************************************************/ |
6835 | | /* GDALMDArrayMask::Init() */ |
6836 | | /************************************************************************/ |
6837 | | |
6838 | | bool GDALMDArrayMask::Init(CSLConstList papszOptions) |
6839 | 0 | { |
6840 | 0 | const auto GetSingleValNumericAttr = |
6841 | 0 | [this](const char *pszAttrName, bool &bHasVal, double &dfVal) |
6842 | 0 | { |
6843 | 0 | auto poAttr = m_poParent->GetAttribute(pszAttrName); |
6844 | 0 | if (poAttr && poAttr->GetDataType().GetClass() == GEDTC_NUMERIC) |
6845 | 0 | { |
6846 | 0 | const auto anDimSizes = poAttr->GetDimensionsSize(); |
6847 | 0 | if (anDimSizes.empty() || |
6848 | 0 | (anDimSizes.size() == 1 && anDimSizes[0] == 1)) |
6849 | 0 | { |
6850 | 0 | bHasVal = true; |
6851 | 0 | dfVal = poAttr->ReadAsDouble(); |
6852 | 0 | } |
6853 | 0 | } |
6854 | 0 | }; |
6855 | |
|
6856 | 0 | GetSingleValNumericAttr("missing_value", m_bHasMissingValue, |
6857 | 0 | m_dfMissingValue); |
6858 | 0 | GetSingleValNumericAttr("_FillValue", m_bHasFillValue, m_dfFillValue); |
6859 | 0 | GetSingleValNumericAttr("valid_min", m_bHasValidMin, m_dfValidMin); |
6860 | 0 | GetSingleValNumericAttr("valid_max", m_bHasValidMax, m_dfValidMax); |
6861 | |
|
6862 | 0 | { |
6863 | 0 | auto poValidRange = m_poParent->GetAttribute("valid_range"); |
6864 | 0 | if (poValidRange && poValidRange->GetDimensionsSize().size() == 1 && |
6865 | 0 | poValidRange->GetDimensionsSize()[0] == 2 && |
6866 | 0 | poValidRange->GetDataType().GetClass() == GEDTC_NUMERIC) |
6867 | 0 | { |
6868 | 0 | m_bHasValidMin = true; |
6869 | 0 | m_bHasValidMax = true; |
6870 | 0 | auto vals = poValidRange->ReadAsDoubleArray(); |
6871 | 0 | CPLAssert(vals.size() == 2); |
6872 | 0 | m_dfValidMin = vals[0]; |
6873 | 0 | m_dfValidMax = vals[1]; |
6874 | 0 | } |
6875 | 0 | } |
6876 | | |
6877 | | // Take into account |
6878 | | // https://cfconventions.org/cf-conventions/cf-conventions.html#flags |
6879 | | // Cf GDALMDArray::GetMask() for semantics of UNMASK_FLAGS |
6880 | 0 | const char *pszUnmaskFlags = |
6881 | 0 | CSLFetchNameValue(papszOptions, "UNMASK_FLAGS"); |
6882 | 0 | if (pszUnmaskFlags) |
6883 | 0 | { |
6884 | 0 | const auto IsScalarStringAttr = |
6885 | 0 | [](const std::shared_ptr<GDALAttribute> &poAttr) |
6886 | 0 | { |
6887 | 0 | return poAttr->GetDataType().GetClass() == GEDTC_STRING && |
6888 | 0 | (poAttr->GetDimensionsSize().empty() || |
6889 | 0 | (poAttr->GetDimensionsSize().size() == 1 && |
6890 | 0 | poAttr->GetDimensionsSize()[0] == 1)); |
6891 | 0 | }; |
6892 | |
|
6893 | 0 | auto poFlagMeanings = m_poParent->GetAttribute("flag_meanings"); |
6894 | 0 | if (!(poFlagMeanings && IsScalarStringAttr(poFlagMeanings))) |
6895 | 0 | { |
6896 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6897 | 0 | "UNMASK_FLAGS option specified but array has no " |
6898 | 0 | "flag_meanings attribute"); |
6899 | 0 | return false; |
6900 | 0 | } |
6901 | 0 | const char *pszFlagMeanings = poFlagMeanings->ReadAsString(); |
6902 | 0 | if (!pszFlagMeanings) |
6903 | 0 | { |
6904 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6905 | 0 | "Cannot read flag_meanings attribute"); |
6906 | 0 | return false; |
6907 | 0 | } |
6908 | | |
6909 | 0 | const auto IsSingleDimNumericAttr = |
6910 | 0 | [](const std::shared_ptr<GDALAttribute> &poAttr) |
6911 | 0 | { |
6912 | 0 | return poAttr->GetDataType().GetClass() == GEDTC_NUMERIC && |
6913 | 0 | poAttr->GetDimensionsSize().size() == 1; |
6914 | 0 | }; |
6915 | |
|
6916 | 0 | auto poFlagValues = m_poParent->GetAttribute("flag_values"); |
6917 | 0 | const bool bHasFlagValues = |
6918 | 0 | poFlagValues && IsSingleDimNumericAttr(poFlagValues); |
6919 | |
|
6920 | 0 | auto poFlagMasks = m_poParent->GetAttribute("flag_masks"); |
6921 | 0 | const bool bHasFlagMasks = |
6922 | 0 | poFlagMasks && IsSingleDimNumericAttr(poFlagMasks); |
6923 | |
|
6924 | 0 | if (!bHasFlagValues && !bHasFlagMasks) |
6925 | 0 | { |
6926 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6927 | 0 | "Cannot find flag_values and/or flag_masks attribute"); |
6928 | 0 | return false; |
6929 | 0 | } |
6930 | | |
6931 | 0 | const CPLStringList aosUnmaskFlags( |
6932 | 0 | CSLTokenizeString2(pszUnmaskFlags, ",", 0)); |
6933 | 0 | const CPLStringList aosFlagMeanings( |
6934 | 0 | CSLTokenizeString2(pszFlagMeanings, " ", 0)); |
6935 | |
|
6936 | 0 | if (bHasFlagValues) |
6937 | 0 | { |
6938 | 0 | const auto eType = poFlagValues->GetDataType().GetNumericDataType(); |
6939 | | // We could support Int64 or UInt64, but more work... |
6940 | 0 | if (eType != GDT_Byte && eType != GDT_Int8 && eType != GDT_UInt16 && |
6941 | 0 | eType != GDT_Int16 && eType != GDT_UInt32 && eType != GDT_Int32) |
6942 | 0 | { |
6943 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
6944 | 0 | "Unsupported data type for flag_values attribute: %s", |
6945 | 0 | GDALGetDataTypeName(eType)); |
6946 | 0 | return false; |
6947 | 0 | } |
6948 | 0 | } |
6949 | | |
6950 | 0 | if (bHasFlagMasks) |
6951 | 0 | { |
6952 | 0 | const auto eType = poFlagMasks->GetDataType().GetNumericDataType(); |
6953 | | // We could support Int64 or UInt64, but more work... |
6954 | 0 | if (eType != GDT_Byte && eType != GDT_Int8 && eType != GDT_UInt16 && |
6955 | 0 | eType != GDT_Int16 && eType != GDT_UInt32 && eType != GDT_Int32) |
6956 | 0 | { |
6957 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
6958 | 0 | "Unsupported data type for flag_masks attribute: %s", |
6959 | 0 | GDALGetDataTypeName(eType)); |
6960 | 0 | return false; |
6961 | 0 | } |
6962 | 0 | } |
6963 | | |
6964 | 0 | const std::vector<double> adfValues( |
6965 | 0 | bHasFlagValues ? poFlagValues->ReadAsDoubleArray() |
6966 | 0 | : std::vector<double>()); |
6967 | 0 | const std::vector<double> adfMasks( |
6968 | 0 | bHasFlagMasks ? poFlagMasks->ReadAsDoubleArray() |
6969 | 0 | : std::vector<double>()); |
6970 | |
|
6971 | 0 | if (bHasFlagValues && |
6972 | 0 | adfValues.size() != static_cast<size_t>(aosFlagMeanings.size())) |
6973 | 0 | { |
6974 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6975 | 0 | "Number of values in flag_values attribute is different " |
6976 | 0 | "from the one in flag_meanings"); |
6977 | 0 | return false; |
6978 | 0 | } |
6979 | | |
6980 | 0 | if (bHasFlagMasks && |
6981 | 0 | adfMasks.size() != static_cast<size_t>(aosFlagMeanings.size())) |
6982 | 0 | { |
6983 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
6984 | 0 | "Number of values in flag_masks attribute is different " |
6985 | 0 | "from the one in flag_meanings"); |
6986 | 0 | return false; |
6987 | 0 | } |
6988 | | |
6989 | 0 | for (int i = 0; i < aosUnmaskFlags.size(); ++i) |
6990 | 0 | { |
6991 | 0 | const int nIdxFlag = aosFlagMeanings.FindString(aosUnmaskFlags[i]); |
6992 | 0 | if (nIdxFlag < 0) |
6993 | 0 | { |
6994 | 0 | CPLError( |
6995 | 0 | CE_Failure, CPLE_AppDefined, |
6996 | 0 | "Cannot fing flag %s in flag_meanings = '%s' attribute", |
6997 | 0 | aosUnmaskFlags[i], pszFlagMeanings); |
6998 | 0 | return false; |
6999 | 0 | } |
7000 | | |
7001 | 0 | if (bHasFlagValues && adfValues[nIdxFlag] < 0) |
7002 | 0 | { |
7003 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
7004 | 0 | "Invalid value in flag_values[%d] = %f", nIdxFlag, |
7005 | 0 | adfValues[nIdxFlag]); |
7006 | 0 | return false; |
7007 | 0 | } |
7008 | | |
7009 | 0 | if (bHasFlagMasks && adfMasks[nIdxFlag] < 0) |
7010 | 0 | { |
7011 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
7012 | 0 | "Invalid value in flag_masks[%d] = %f", nIdxFlag, |
7013 | 0 | adfMasks[nIdxFlag]); |
7014 | 0 | return false; |
7015 | 0 | } |
7016 | | |
7017 | 0 | if (bHasFlagValues) |
7018 | 0 | { |
7019 | 0 | m_anValidFlagValues.push_back( |
7020 | 0 | static_cast<uint32_t>(adfValues[nIdxFlag])); |
7021 | 0 | } |
7022 | |
|
7023 | 0 | if (bHasFlagMasks) |
7024 | 0 | { |
7025 | 0 | m_anValidFlagMasks.push_back( |
7026 | 0 | static_cast<uint32_t>(adfMasks[nIdxFlag])); |
7027 | 0 | } |
7028 | 0 | } |
7029 | 0 | } |
7030 | | |
7031 | 0 | return true; |
7032 | 0 | } |
7033 | | |
7034 | | /************************************************************************/ |
7035 | | /* IRead() */ |
7036 | | /************************************************************************/ |
7037 | | |
7038 | | bool GDALMDArrayMask::IRead(const GUInt64 *arrayStartIdx, const size_t *count, |
7039 | | const GInt64 *arrayStep, |
7040 | | const GPtrDiff_t *bufferStride, |
7041 | | const GDALExtendedDataType &bufferDataType, |
7042 | | void *pDstBuffer) const |
7043 | 0 | { |
7044 | 0 | if (bufferDataType.GetClass() != GEDTC_NUMERIC) |
7045 | 0 | { |
7046 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
7047 | 0 | "%s: only reading to a numeric data type is supported", |
7048 | 0 | __func__); |
7049 | 0 | return false; |
7050 | 0 | } |
7051 | 0 | size_t nElts = 1; |
7052 | 0 | const size_t nDims = GetDimensionCount(); |
7053 | 0 | std::vector<GPtrDiff_t> tmpBufferStrideVector(nDims); |
7054 | 0 | for (size_t i = 0; i < nDims; i++) |
7055 | 0 | nElts *= count[i]; |
7056 | 0 | if (nDims > 0) |
7057 | 0 | { |
7058 | 0 | tmpBufferStrideVector.back() = 1; |
7059 | 0 | for (size_t i = nDims - 1; i > 0;) |
7060 | 0 | { |
7061 | 0 | --i; |
7062 | 0 | tmpBufferStrideVector[i] = |
7063 | 0 | tmpBufferStrideVector[i + 1] * count[i + 1]; |
7064 | 0 | } |
7065 | 0 | } |
7066 | | |
7067 | | /* Optimized case: if we are an integer data type and that there is no */ |
7068 | | /* attribute that can be used to set mask = 0, then fill the mask buffer */ |
7069 | | /* directly */ |
7070 | 0 | if (!m_bHasMissingValue && !m_bHasFillValue && !m_bHasValidMin && |
7071 | 0 | !m_bHasValidMax && m_anValidFlagValues.empty() && |
7072 | 0 | m_anValidFlagMasks.empty() && |
7073 | 0 | m_poParent->GetRawNoDataValue() == nullptr && |
7074 | 0 | GDALDataTypeIsInteger(m_poParent->GetDataType().GetNumericDataType())) |
7075 | 0 | { |
7076 | 0 | const bool bBufferDataTypeIsByte = bufferDataType == m_dt; |
7077 | 0 | if (bBufferDataTypeIsByte) // Byte case |
7078 | 0 | { |
7079 | 0 | bool bContiguous = true; |
7080 | 0 | for (size_t i = 0; i < nDims; i++) |
7081 | 0 | { |
7082 | 0 | if (bufferStride[i] != tmpBufferStrideVector[i]) |
7083 | 0 | { |
7084 | 0 | bContiguous = false; |
7085 | 0 | break; |
7086 | 0 | } |
7087 | 0 | } |
7088 | 0 | if (bContiguous) |
7089 | 0 | { |
7090 | | // CPLDebug("GDAL", "GetMask(): contiguous case"); |
7091 | 0 | memset(pDstBuffer, 1, nElts); |
7092 | 0 | return true; |
7093 | 0 | } |
7094 | 0 | } |
7095 | | |
7096 | 0 | struct Stack |
7097 | 0 | { |
7098 | 0 | size_t nIters = 0; |
7099 | 0 | GByte *dst_ptr = nullptr; |
7100 | 0 | GPtrDiff_t dst_inc_offset = 0; |
7101 | 0 | }; |
7102 | |
|
7103 | 0 | std::vector<Stack> stack(std::max(static_cast<size_t>(1), nDims)); |
7104 | 0 | const size_t nBufferDTSize = bufferDataType.GetSize(); |
7105 | 0 | for (size_t i = 0; i < nDims; i++) |
7106 | 0 | { |
7107 | 0 | stack[i].dst_inc_offset = |
7108 | 0 | static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize); |
7109 | 0 | } |
7110 | 0 | stack[0].dst_ptr = static_cast<GByte *>(pDstBuffer); |
7111 | |
|
7112 | 0 | size_t dimIdx = 0; |
7113 | 0 | const size_t nDimsMinus1 = nDims > 0 ? nDims - 1 : 0; |
7114 | 0 | GByte abyOne[16]; // 16 is sizeof GDT_CFloat64 |
7115 | 0 | CPLAssert(nBufferDTSize <= 16); |
7116 | 0 | const GByte flag = 1; |
7117 | 0 | GDALCopyWords64(&flag, GDT_Byte, 0, abyOne, |
7118 | 0 | bufferDataType.GetNumericDataType(), 0, 1); |
7119 | |
|
7120 | 0 | lbl_next_depth: |
7121 | 0 | if (dimIdx == nDimsMinus1) |
7122 | 0 | { |
7123 | 0 | auto nIters = nDims > 0 ? count[dimIdx] : 1; |
7124 | 0 | GByte *dst_ptr = stack[dimIdx].dst_ptr; |
7125 | |
|
7126 | 0 | while (true) |
7127 | 0 | { |
7128 | | // cppcheck-suppress knownConditionTrueFalse |
7129 | 0 | if (bBufferDataTypeIsByte) |
7130 | 0 | { |
7131 | 0 | *dst_ptr = flag; |
7132 | 0 | } |
7133 | 0 | else |
7134 | 0 | { |
7135 | 0 | memcpy(dst_ptr, abyOne, nBufferDTSize); |
7136 | 0 | } |
7137 | |
|
7138 | 0 | if ((--nIters) == 0) |
7139 | 0 | break; |
7140 | 0 | dst_ptr += stack[dimIdx].dst_inc_offset; |
7141 | 0 | } |
7142 | 0 | } |
7143 | 0 | else |
7144 | 0 | { |
7145 | 0 | stack[dimIdx].nIters = count[dimIdx]; |
7146 | 0 | while (true) |
7147 | 0 | { |
7148 | 0 | dimIdx++; |
7149 | 0 | stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr; |
7150 | 0 | goto lbl_next_depth; |
7151 | 0 | lbl_return_to_caller: |
7152 | 0 | dimIdx--; |
7153 | 0 | if ((--stack[dimIdx].nIters) == 0) |
7154 | 0 | break; |
7155 | 0 | stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset; |
7156 | 0 | } |
7157 | 0 | } |
7158 | 0 | if (dimIdx > 0) |
7159 | 0 | goto lbl_return_to_caller; |
7160 | | |
7161 | 0 | return true; |
7162 | 0 | } |
7163 | | |
7164 | 0 | const auto oTmpBufferDT = |
7165 | 0 | GDALDataTypeIsComplex(m_poParent->GetDataType().GetNumericDataType()) |
7166 | 0 | ? GDALExtendedDataType::Create(GDT_Float64) |
7167 | 0 | : m_poParent->GetDataType(); |
7168 | 0 | const size_t nTmpBufferDTSize = oTmpBufferDT.GetSize(); |
7169 | 0 | void *pTempBuffer = VSI_MALLOC2_VERBOSE(nTmpBufferDTSize, nElts); |
7170 | 0 | if (!pTempBuffer) |
7171 | 0 | return false; |
7172 | 0 | if (!m_poParent->Read(arrayStartIdx, count, arrayStep, |
7173 | 0 | tmpBufferStrideVector.data(), oTmpBufferDT, |
7174 | 0 | pTempBuffer)) |
7175 | 0 | { |
7176 | 0 | VSIFree(pTempBuffer); |
7177 | 0 | return false; |
7178 | 0 | } |
7179 | | |
7180 | 0 | switch (oTmpBufferDT.GetNumericDataType()) |
7181 | 0 | { |
7182 | 0 | case GDT_Byte: |
7183 | 0 | ReadInternal<GByte>(count, bufferStride, bufferDataType, pDstBuffer, |
7184 | 0 | pTempBuffer, oTmpBufferDT, |
7185 | 0 | tmpBufferStrideVector); |
7186 | 0 | break; |
7187 | | |
7188 | 0 | case GDT_Int8: |
7189 | 0 | ReadInternal<GInt8>(count, bufferStride, bufferDataType, pDstBuffer, |
7190 | 0 | pTempBuffer, oTmpBufferDT, |
7191 | 0 | tmpBufferStrideVector); |
7192 | 0 | break; |
7193 | | |
7194 | 0 | case GDT_UInt16: |
7195 | 0 | ReadInternal<GUInt16>(count, bufferStride, bufferDataType, |
7196 | 0 | pDstBuffer, pTempBuffer, oTmpBufferDT, |
7197 | 0 | tmpBufferStrideVector); |
7198 | 0 | break; |
7199 | | |
7200 | 0 | case GDT_Int16: |
7201 | 0 | ReadInternal<GInt16>(count, bufferStride, bufferDataType, |
7202 | 0 | pDstBuffer, pTempBuffer, oTmpBufferDT, |
7203 | 0 | tmpBufferStrideVector); |
7204 | 0 | break; |
7205 | | |
7206 | 0 | case GDT_UInt32: |
7207 | 0 | ReadInternal<GUInt32>(count, bufferStride, bufferDataType, |
7208 | 0 | pDstBuffer, pTempBuffer, oTmpBufferDT, |
7209 | 0 | tmpBufferStrideVector); |
7210 | 0 | break; |
7211 | | |
7212 | 0 | case GDT_Int32: |
7213 | 0 | ReadInternal<GInt32>(count, bufferStride, bufferDataType, |
7214 | 0 | pDstBuffer, pTempBuffer, oTmpBufferDT, |
7215 | 0 | tmpBufferStrideVector); |
7216 | 0 | break; |
7217 | | |
7218 | 0 | case GDT_UInt64: |
7219 | 0 | ReadInternal<std::uint64_t>(count, bufferStride, bufferDataType, |
7220 | 0 | pDstBuffer, pTempBuffer, oTmpBufferDT, |
7221 | 0 | tmpBufferStrideVector); |
7222 | 0 | break; |
7223 | | |
7224 | 0 | case GDT_Int64: |
7225 | 0 | ReadInternal<std::int64_t>(count, bufferStride, bufferDataType, |
7226 | 0 | pDstBuffer, pTempBuffer, oTmpBufferDT, |
7227 | 0 | tmpBufferStrideVector); |
7228 | 0 | break; |
7229 | | |
7230 | 0 | case GDT_Float16: |
7231 | 0 | ReadInternal<GFloat16>(count, bufferStride, bufferDataType, |
7232 | 0 | pDstBuffer, pTempBuffer, oTmpBufferDT, |
7233 | 0 | tmpBufferStrideVector); |
7234 | 0 | break; |
7235 | | |
7236 | 0 | case GDT_Float32: |
7237 | 0 | ReadInternal<float>(count, bufferStride, bufferDataType, pDstBuffer, |
7238 | 0 | pTempBuffer, oTmpBufferDT, |
7239 | 0 | tmpBufferStrideVector); |
7240 | 0 | break; |
7241 | | |
7242 | 0 | case GDT_Float64: |
7243 | 0 | ReadInternal<double>(count, bufferStride, bufferDataType, |
7244 | 0 | pDstBuffer, pTempBuffer, oTmpBufferDT, |
7245 | 0 | tmpBufferStrideVector); |
7246 | 0 | break; |
7247 | 0 | case GDT_Unknown: |
7248 | 0 | case GDT_CInt16: |
7249 | 0 | case GDT_CInt32: |
7250 | 0 | case GDT_CFloat16: |
7251 | 0 | case GDT_CFloat32: |
7252 | 0 | case GDT_CFloat64: |
7253 | 0 | case GDT_TypeCount: |
7254 | 0 | CPLAssert(false); |
7255 | 0 | break; |
7256 | 0 | } |
7257 | | |
7258 | 0 | VSIFree(pTempBuffer); |
7259 | |
|
7260 | 0 | return true; |
7261 | 0 | } |
7262 | | |
7263 | | /************************************************************************/ |
7264 | | /* IsValidForDT() */ |
7265 | | /************************************************************************/ |
7266 | | |
7267 | | template <typename Type> static bool IsValidForDT(double dfVal) |
7268 | 0 | { |
7269 | 0 | if (std::isnan(dfVal)) |
7270 | 0 | return false; |
7271 | 0 | if (dfVal < static_cast<double>(cpl::NumericLimits<Type>::lowest())) |
7272 | 0 | return false; |
7273 | 0 | if (dfVal > static_cast<double>(cpl::NumericLimits<Type>::max())) |
7274 | 0 | return false; |
7275 | 0 | return static_cast<double>(static_cast<Type>(dfVal)) == dfVal; |
7276 | 0 | } Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<unsigned char>(double) Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<signed char>(double) Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<unsigned short>(double) Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<short>(double) Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<unsigned int>(double) Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<int>(double) Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<unsigned long>(double) Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<long>(double) Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<cpl::Float16>(double) Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<float>(double) |
7277 | | |
7278 | | template <> bool IsValidForDT<double>(double) |
7279 | 0 | { |
7280 | 0 | return true; |
7281 | 0 | } |
7282 | | |
7283 | | /************************************************************************/ |
7284 | | /* IsNan() */ |
7285 | | /************************************************************************/ |
7286 | | |
7287 | | template <typename Type> inline bool IsNan(Type) |
7288 | 0 | { |
7289 | 0 | return false; |
7290 | 0 | } Unexecuted instantiation: bool IsNan<unsigned char>(unsigned char) Unexecuted instantiation: bool IsNan<signed char>(signed char) Unexecuted instantiation: bool IsNan<unsigned short>(unsigned short) Unexecuted instantiation: bool IsNan<short>(short) Unexecuted instantiation: bool IsNan<unsigned int>(unsigned int) Unexecuted instantiation: bool IsNan<int>(int) Unexecuted instantiation: bool IsNan<unsigned long>(unsigned long) Unexecuted instantiation: bool IsNan<long>(long) Unexecuted instantiation: bool IsNan<cpl::Float16>(cpl::Float16) |
7291 | | |
7292 | | template <> bool IsNan<double>(double val) |
7293 | 0 | { |
7294 | 0 | return std::isnan(val); |
7295 | 0 | } |
7296 | | |
7297 | | template <> bool IsNan<float>(float val) |
7298 | 0 | { |
7299 | 0 | return std::isnan(val); |
7300 | 0 | } |
7301 | | |
7302 | | /************************************************************************/ |
7303 | | /* ReadInternal() */ |
7304 | | /************************************************************************/ |
7305 | | |
7306 | | template <typename Type> |
7307 | | void GDALMDArrayMask::ReadInternal( |
7308 | | const size_t *count, const GPtrDiff_t *bufferStride, |
7309 | | const GDALExtendedDataType &bufferDataType, void *pDstBuffer, |
7310 | | const void *pTempBuffer, const GDALExtendedDataType &oTmpBufferDT, |
7311 | | const std::vector<GPtrDiff_t> &tmpBufferStrideVector) const |
7312 | 0 | { |
7313 | 0 | const size_t nDims = GetDimensionCount(); |
7314 | |
|
7315 | 0 | const auto castValue = [](bool &bHasVal, double dfVal) -> Type |
7316 | 0 | { |
7317 | 0 | if (bHasVal) |
7318 | 0 | { |
7319 | 0 | if (IsValidForDT<Type>(dfVal)) |
7320 | 0 | { |
7321 | 0 | return static_cast<Type>(dfVal); |
7322 | 0 | } |
7323 | 0 | else |
7324 | 0 | { |
7325 | 0 | bHasVal = false; |
7326 | 0 | } |
7327 | 0 | } |
7328 | 0 | return 0; |
7329 | 0 | }; Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<signed char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<cpl::Float16>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<float>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<double>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const |
7330 | |
|
7331 | 0 | const void *pSrcRawNoDataValue = m_poParent->GetRawNoDataValue(); |
7332 | 0 | bool bHasNodataValue = pSrcRawNoDataValue != nullptr; |
7333 | 0 | const Type nNoDataValue = |
7334 | 0 | castValue(bHasNodataValue, m_poParent->GetNoDataValueAsDouble()); |
7335 | 0 | bool bHasMissingValue = m_bHasMissingValue; |
7336 | 0 | const Type nMissingValue = castValue(bHasMissingValue, m_dfMissingValue); |
7337 | 0 | bool bHasFillValue = m_bHasFillValue; |
7338 | 0 | const Type nFillValue = castValue(bHasFillValue, m_dfFillValue); |
7339 | 0 | bool bHasValidMin = m_bHasValidMin; |
7340 | 0 | const Type nValidMin = castValue(bHasValidMin, m_dfValidMin); |
7341 | 0 | bool bHasValidMax = m_bHasValidMax; |
7342 | 0 | const Type nValidMax = castValue(bHasValidMax, m_dfValidMax); |
7343 | 0 | const bool bHasValidFlags = |
7344 | 0 | !m_anValidFlagValues.empty() || !m_anValidFlagMasks.empty(); |
7345 | |
|
7346 | 0 | const auto IsValidFlag = [this](Type v) |
7347 | 0 | { |
7348 | 0 | if (!m_anValidFlagValues.empty() && !m_anValidFlagMasks.empty()) |
7349 | 0 | { |
7350 | 0 | for (size_t i = 0; i < m_anValidFlagValues.size(); ++i) |
7351 | 0 | { |
7352 | 0 | if ((static_cast<uint32_t>(v) & m_anValidFlagMasks[i]) == |
7353 | 0 | m_anValidFlagValues[i]) |
7354 | 0 | { |
7355 | 0 | return true; |
7356 | 0 | } |
7357 | 0 | } |
7358 | 0 | } |
7359 | 0 | else if (!m_anValidFlagValues.empty()) |
7360 | 0 | { |
7361 | 0 | for (size_t i = 0; i < m_anValidFlagValues.size(); ++i) |
7362 | 0 | { |
7363 | 0 | if (static_cast<uint32_t>(v) == m_anValidFlagValues[i]) |
7364 | 0 | { |
7365 | 0 | return true; |
7366 | 0 | } |
7367 | 0 | } |
7368 | 0 | } |
7369 | 0 | else /* if( !m_anValidFlagMasks.empty() ) */ |
7370 | 0 | { |
7371 | 0 | for (size_t i = 0; i < m_anValidFlagMasks.size(); ++i) |
7372 | 0 | { |
7373 | 0 | if ((static_cast<uint32_t>(v) & m_anValidFlagMasks[i]) != 0) |
7374 | 0 | { |
7375 | 0 | return true; |
7376 | 0 | } |
7377 | 0 | } |
7378 | 0 | } |
7379 | 0 | return false; |
7380 | 0 | }; Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(unsigned char)#1}::operator()(unsigned char) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<signed char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(signed char)#1}::operator()(signed char) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(unsigned short)#1}::operator()(unsigned short) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(short)#1}::operator()(short) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(unsigned int)#1}::operator()(unsigned int) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(int)#1}::operator()(int) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(unsigned long)#1}::operator()(unsigned long) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(long)#1}::operator()(long) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<cpl::Float16>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(cpl::Float16)#1}::operator()(cpl::Float16) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<float>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(float)#1}::operator()(float) constUnexecuted instantiation: GDALMDArrayMask::ReadInternal<double>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(double)#1}::operator()(double) const |
7381 | |
|
7382 | 0 | #define GET_MASK_FOR_SAMPLE(v) \ |
7383 | 0 | static_cast<GByte>(!IsNan(v) && !(bHasNodataValue && v == nNoDataValue) && \ |
7384 | 0 | !(bHasMissingValue && v == nMissingValue) && \ |
7385 | 0 | !(bHasFillValue && v == nFillValue) && \ |
7386 | 0 | !(bHasValidMin && v < nValidMin) && \ |
7387 | 0 | !(bHasValidMax && v > nValidMax) && \ |
7388 | 0 | (!bHasValidFlags || IsValidFlag(v))); |
7389 | |
|
7390 | 0 | const bool bBufferDataTypeIsByte = bufferDataType == m_dt; |
7391 | | /* Optimized case: Byte output and output buffer is contiguous */ |
7392 | 0 | if (bBufferDataTypeIsByte) |
7393 | 0 | { |
7394 | 0 | bool bContiguous = true; |
7395 | 0 | for (size_t i = 0; i < nDims; i++) |
7396 | 0 | { |
7397 | 0 | if (bufferStride[i] != tmpBufferStrideVector[i]) |
7398 | 0 | { |
7399 | 0 | bContiguous = false; |
7400 | 0 | break; |
7401 | 0 | } |
7402 | 0 | } |
7403 | 0 | if (bContiguous) |
7404 | 0 | { |
7405 | 0 | size_t nElts = 1; |
7406 | 0 | for (size_t i = 0; i < nDims; i++) |
7407 | 0 | nElts *= count[i]; |
7408 | |
|
7409 | 0 | for (size_t i = 0; i < nElts; i++) |
7410 | 0 | { |
7411 | 0 | const Type *pSrc = static_cast<const Type *>(pTempBuffer) + i; |
7412 | 0 | static_cast<GByte *>(pDstBuffer)[i] = |
7413 | 0 | GET_MASK_FOR_SAMPLE(*pSrc); |
7414 | 0 | } |
7415 | 0 | return; |
7416 | 0 | } |
7417 | 0 | } |
7418 | | |
7419 | 0 | const size_t nTmpBufferDTSize = oTmpBufferDT.GetSize(); |
7420 | |
|
7421 | 0 | struct Stack |
7422 | 0 | { |
7423 | 0 | size_t nIters = 0; |
7424 | 0 | const GByte *src_ptr = nullptr; |
7425 | 0 | GByte *dst_ptr = nullptr; |
7426 | 0 | GPtrDiff_t src_inc_offset = 0; |
7427 | 0 | GPtrDiff_t dst_inc_offset = 0; |
7428 | 0 | }; |
7429 | |
|
7430 | 0 | std::vector<Stack> stack(std::max(static_cast<size_t>(1), nDims)); |
7431 | 0 | const size_t nBufferDTSize = bufferDataType.GetSize(); |
7432 | 0 | for (size_t i = 0; i < nDims; i++) |
7433 | 0 | { |
7434 | 0 | stack[i].src_inc_offset = static_cast<GPtrDiff_t>( |
7435 | 0 | tmpBufferStrideVector[i] * nTmpBufferDTSize); |
7436 | 0 | stack[i].dst_inc_offset = |
7437 | 0 | static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize); |
7438 | 0 | } |
7439 | 0 | stack[0].src_ptr = static_cast<const GByte *>(pTempBuffer); |
7440 | 0 | stack[0].dst_ptr = static_cast<GByte *>(pDstBuffer); |
7441 | |
|
7442 | 0 | size_t dimIdx = 0; |
7443 | 0 | const size_t nDimsMinus1 = nDims > 0 ? nDims - 1 : 0; |
7444 | 0 | GByte abyZeroOrOne[2][16]; // 16 is sizeof GDT_CFloat64 |
7445 | 0 | CPLAssert(nBufferDTSize <= 16); |
7446 | 0 | for (GByte flag = 0; flag <= 1; flag++) |
7447 | 0 | { |
7448 | 0 | GDALCopyWords64(&flag, m_dt.GetNumericDataType(), 0, abyZeroOrOne[flag], |
7449 | 0 | bufferDataType.GetNumericDataType(), 0, 1); |
7450 | 0 | } |
7451 | |
|
7452 | 0 | lbl_next_depth: |
7453 | 0 | if (dimIdx == nDimsMinus1) |
7454 | 0 | { |
7455 | 0 | auto nIters = nDims > 0 ? count[dimIdx] : 1; |
7456 | 0 | const GByte *src_ptr = stack[dimIdx].src_ptr; |
7457 | 0 | GByte *dst_ptr = stack[dimIdx].dst_ptr; |
7458 | |
|
7459 | 0 | while (true) |
7460 | 0 | { |
7461 | 0 | const Type *pSrc = reinterpret_cast<const Type *>(src_ptr); |
7462 | 0 | const GByte flag = GET_MASK_FOR_SAMPLE(*pSrc); |
7463 | |
|
7464 | 0 | if (bBufferDataTypeIsByte) |
7465 | 0 | { |
7466 | 0 | *dst_ptr = flag; |
7467 | 0 | } |
7468 | 0 | else |
7469 | 0 | { |
7470 | 0 | memcpy(dst_ptr, abyZeroOrOne[flag], nBufferDTSize); |
7471 | 0 | } |
7472 | |
|
7473 | 0 | if ((--nIters) == 0) |
7474 | 0 | break; |
7475 | 0 | src_ptr += stack[dimIdx].src_inc_offset; |
7476 | 0 | dst_ptr += stack[dimIdx].dst_inc_offset; |
7477 | 0 | } |
7478 | 0 | } |
7479 | 0 | else |
7480 | 0 | { |
7481 | 0 | stack[dimIdx].nIters = count[dimIdx]; |
7482 | 0 | while (true) |
7483 | 0 | { |
7484 | 0 | dimIdx++; |
7485 | 0 | stack[dimIdx].src_ptr = stack[dimIdx - 1].src_ptr; |
7486 | 0 | stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr; |
7487 | 0 | goto lbl_next_depth; |
7488 | 0 | lbl_return_to_caller: |
7489 | 0 | dimIdx--; |
7490 | 0 | if ((--stack[dimIdx].nIters) == 0) |
7491 | 0 | break; |
7492 | 0 | stack[dimIdx].src_ptr += stack[dimIdx].src_inc_offset; |
7493 | 0 | stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset; |
7494 | 0 | } |
7495 | 0 | } |
7496 | 0 | if (dimIdx > 0) |
7497 | 0 | goto lbl_return_to_caller; |
7498 | 0 | } Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<unsigned char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<signed char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<unsigned short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<unsigned int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<unsigned long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<cpl::Float16>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<float>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<double>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const |
7499 | | |
7500 | | /************************************************************************/ |
7501 | | /* GetMask() */ |
7502 | | /************************************************************************/ |
7503 | | |
7504 | | /** Return an array that is a mask for the current array |
7505 | | |
7506 | | This array will be of type Byte, with values set to 0 to indicate invalid |
7507 | | pixels of the current array, and values set to 1 to indicate valid pixels. |
7508 | | |
7509 | | The generic implementation honours the NoDataValue, as well as various |
7510 | | netCDF CF attributes: missing_value, _FillValue, valid_min, valid_max |
7511 | | and valid_range. |
7512 | | |
7513 | | Starting with GDAL 3.8, option UNMASK_FLAGS=flag_meaning_1[,flag_meaning_2,...] |
7514 | | can be used to specify strings of the "flag_meanings" attribute |
7515 | | (cf https://cfconventions.org/cf-conventions/cf-conventions.html#flags) |
7516 | | for which pixels matching any of those flags will be set at 1 in the mask array, |
7517 | | and pixels matching none of those flags will be set at 0. |
7518 | | For example, let's consider the following netCDF variable defined with: |
7519 | | \verbatim |
7520 | | l2p_flags:valid_min = 0s ; |
7521 | | l2p_flags:valid_max = 256s ; |
7522 | | l2p_flags:flag_meanings = "microwave land ice lake river reserved_for_future_use unused_currently unused_currently unused_currently" ; |
7523 | | l2p_flags:flag_masks = 1s, 2s, 4s, 8s, 16s, 32s, 64s, 128s, 256s ; |
7524 | | \endverbatim |
7525 | | |
7526 | | GetMask(["UNMASK_FLAGS=microwave,land"]) will return an array such that: |
7527 | | - for pixel values *outside* valid_range [0,256], the mask value will be 0. |
7528 | | - for a pixel value with bit 0 or bit 1 at 1 within [0,256], the mask value |
7529 | | will be 1. |
7530 | | - for a pixel value with bit 0 and bit 1 at 0 within [0,256], the mask value |
7531 | | will be 0. |
7532 | | |
7533 | | This is the same as the C function GDALMDArrayGetMask(). |
7534 | | |
7535 | | @param papszOptions NULL-terminated list of options, or NULL. |
7536 | | |
7537 | | @return a new array, that holds a reference to the original one, and thus is |
7538 | | a view of it (not a copy), or nullptr in case of error. |
7539 | | */ |
7540 | | std::shared_ptr<GDALMDArray> |
7541 | | GDALMDArray::GetMask(CSLConstList papszOptions) const |
7542 | 0 | { |
7543 | 0 | auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock()); |
7544 | 0 | if (!self) |
7545 | 0 | { |
7546 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
7547 | 0 | "Driver implementation issue: m_pSelf not set !"); |
7548 | 0 | return nullptr; |
7549 | 0 | } |
7550 | 0 | if (GetDataType().GetClass() != GEDTC_NUMERIC) |
7551 | 0 | { |
7552 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
7553 | 0 | "GetMask() only supports numeric data type"); |
7554 | 0 | return nullptr; |
7555 | 0 | } |
7556 | 0 | return GDALMDArrayMask::Create(self, papszOptions); |
7557 | 0 | } |
7558 | | |
7559 | | /************************************************************************/ |
7560 | | /* IsRegularlySpaced() */ |
7561 | | /************************************************************************/ |
7562 | | |
7563 | | /** Returns whether an array is a 1D regularly spaced array. |
7564 | | * |
7565 | | * @param[out] dfStart First value in the array |
7566 | | * @param[out] dfIncrement Increment/spacing between consecutive values. |
7567 | | * @return true if the array is regularly spaced. |
7568 | | */ |
7569 | | bool GDALMDArray::IsRegularlySpaced(double &dfStart, double &dfIncrement) const |
7570 | 0 | { |
7571 | 0 | dfStart = 0; |
7572 | 0 | dfIncrement = 0; |
7573 | 0 | if (GetDimensionCount() != 1 || GetDataType().GetClass() != GEDTC_NUMERIC) |
7574 | 0 | return false; |
7575 | 0 | const auto nSize = GetDimensions()[0]->GetSize(); |
7576 | 0 | if (nSize <= 1 || nSize > 10 * 1000 * 1000) |
7577 | 0 | return false; |
7578 | | |
7579 | 0 | size_t nCount = static_cast<size_t>(nSize); |
7580 | 0 | std::vector<double> adfTmp; |
7581 | 0 | try |
7582 | 0 | { |
7583 | 0 | adfTmp.resize(nCount); |
7584 | 0 | } |
7585 | 0 | catch (const std::exception &) |
7586 | 0 | { |
7587 | 0 | return false; |
7588 | 0 | } |
7589 | | |
7590 | 0 | GUInt64 anStart[1] = {0}; |
7591 | 0 | size_t anCount[1] = {nCount}; |
7592 | |
|
7593 | 0 | const auto IsRegularlySpacedInternal = |
7594 | 0 | [&dfStart, &dfIncrement, &anCount, &adfTmp]() |
7595 | 0 | { |
7596 | 0 | dfStart = adfTmp[0]; |
7597 | 0 | dfIncrement = (adfTmp[anCount[0] - 1] - adfTmp[0]) / (anCount[0] - 1); |
7598 | 0 | if (dfIncrement == 0) |
7599 | 0 | { |
7600 | 0 | return false; |
7601 | 0 | } |
7602 | 0 | for (size_t i = 1; i < anCount[0]; i++) |
7603 | 0 | { |
7604 | 0 | if (fabs((adfTmp[i] - adfTmp[i - 1]) - dfIncrement) > |
7605 | 0 | 1e-3 * fabs(dfIncrement)) |
7606 | 0 | { |
7607 | 0 | return false; |
7608 | 0 | } |
7609 | 0 | } |
7610 | 0 | return true; |
7611 | 0 | }; |
7612 | | |
7613 | | // First try with the first block(s). This can avoid excessive processing |
7614 | | // time, for example with Zarr datasets. |
7615 | | // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=37636 and |
7616 | | // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=39273 |
7617 | 0 | const auto nBlockSize = GetBlockSize()[0]; |
7618 | 0 | if (nCount >= 5 && nBlockSize <= nCount / 2) |
7619 | 0 | { |
7620 | 0 | size_t nReducedCount = |
7621 | 0 | std::max<size_t>(3, static_cast<size_t>(nBlockSize)); |
7622 | 0 | while (nReducedCount < 256 && nReducedCount <= (nCount - 2) / 2) |
7623 | 0 | nReducedCount *= 2; |
7624 | 0 | anCount[0] = nReducedCount; |
7625 | 0 | if (!Read(anStart, anCount, nullptr, nullptr, |
7626 | 0 | GDALExtendedDataType::Create(GDT_Float64), &adfTmp[0])) |
7627 | 0 | { |
7628 | 0 | return false; |
7629 | 0 | } |
7630 | 0 | if (!IsRegularlySpacedInternal()) |
7631 | 0 | { |
7632 | 0 | return false; |
7633 | 0 | } |
7634 | | |
7635 | | // Get next values |
7636 | 0 | anStart[0] = nReducedCount; |
7637 | 0 | anCount[0] = nCount - nReducedCount; |
7638 | 0 | } |
7639 | | |
7640 | 0 | if (!Read(anStart, anCount, nullptr, nullptr, |
7641 | 0 | GDALExtendedDataType::Create(GDT_Float64), |
7642 | 0 | &adfTmp[static_cast<size_t>(anStart[0])])) |
7643 | 0 | { |
7644 | 0 | return false; |
7645 | 0 | } |
7646 | | |
7647 | 0 | return IsRegularlySpacedInternal(); |
7648 | 0 | } |
7649 | | |
7650 | | /************************************************************************/ |
7651 | | /* GuessGeoTransform() */ |
7652 | | /************************************************************************/ |
7653 | | |
7654 | | /** Returns whether 2 specified dimensions form a geotransform |
7655 | | * |
7656 | | * @param nDimX Index of the X axis. |
7657 | | * @param nDimY Index of the Y axis. |
7658 | | * @param bPixelIsPoint Whether the geotransform should be returned |
7659 | | * with the pixel-is-point (pixel-center) convention |
7660 | | * (bPixelIsPoint = true), or with the pixel-is-area |
7661 | | * (top left corner convention) |
7662 | | * (bPixelIsPoint = false) |
7663 | | * @param[out] gt Computed geotransform |
7664 | | * @return true if a geotransform could be computed. |
7665 | | */ |
7666 | | bool GDALMDArray::GuessGeoTransform(size_t nDimX, size_t nDimY, |
7667 | | bool bPixelIsPoint, |
7668 | | GDALGeoTransform >) const |
7669 | 0 | { |
7670 | 0 | const auto &dims(GetDimensions()); |
7671 | 0 | auto poVarX = dims[nDimX]->GetIndexingVariable(); |
7672 | 0 | auto poVarY = dims[nDimY]->GetIndexingVariable(); |
7673 | 0 | double dfXStart = 0.0; |
7674 | 0 | double dfXSpacing = 0.0; |
7675 | 0 | double dfYStart = 0.0; |
7676 | 0 | double dfYSpacing = 0.0; |
7677 | 0 | if (poVarX && poVarX->GetDimensionCount() == 1 && |
7678 | 0 | poVarX->GetDimensions()[0]->GetSize() == dims[nDimX]->GetSize() && |
7679 | 0 | poVarY && poVarY->GetDimensionCount() == 1 && |
7680 | 0 | poVarY->GetDimensions()[0]->GetSize() == dims[nDimY]->GetSize() && |
7681 | 0 | poVarX->IsRegularlySpaced(dfXStart, dfXSpacing) && |
7682 | 0 | poVarY->IsRegularlySpaced(dfYStart, dfYSpacing)) |
7683 | 0 | { |
7684 | 0 | gt[0] = dfXStart - (bPixelIsPoint ? 0 : dfXSpacing / 2); |
7685 | 0 | gt[1] = dfXSpacing; |
7686 | 0 | gt[2] = 0; |
7687 | 0 | gt[3] = dfYStart - (bPixelIsPoint ? 0 : dfYSpacing / 2); |
7688 | 0 | gt[4] = 0; |
7689 | 0 | gt[5] = dfYSpacing; |
7690 | 0 | return true; |
7691 | 0 | } |
7692 | 0 | return false; |
7693 | 0 | } |
7694 | | |
7695 | | /** Returns whether 2 specified dimensions form a geotransform |
7696 | | * |
7697 | | * @param nDimX Index of the X axis. |
7698 | | * @param nDimY Index of the Y axis. |
7699 | | * @param bPixelIsPoint Whether the geotransform should be returned |
7700 | | * with the pixel-is-point (pixel-center) convention |
7701 | | * (bPixelIsPoint = true), or with the pixel-is-area |
7702 | | * (top left corner convention) |
7703 | | * (bPixelIsPoint = false) |
7704 | | * @param[out] adfGeoTransform Computed geotransform |
7705 | | * @return true if a geotransform could be computed. |
7706 | | */ |
7707 | | bool GDALMDArray::GuessGeoTransform(size_t nDimX, size_t nDimY, |
7708 | | bool bPixelIsPoint, |
7709 | | double adfGeoTransform[6]) const |
7710 | 0 | { |
7711 | 0 | GDALGeoTransform *gt = |
7712 | 0 | reinterpret_cast<GDALGeoTransform *>(adfGeoTransform); |
7713 | 0 | return GuessGeoTransform(nDimX, nDimY, bPixelIsPoint, *gt); |
7714 | 0 | } |
7715 | | |
7716 | | /************************************************************************/ |
7717 | | /* GDALMDArrayResampled */ |
7718 | | /************************************************************************/ |
7719 | | |
7720 | | class GDALMDArrayResampledDataset; |
7721 | | |
7722 | | class GDALMDArrayResampledDatasetRasterBand final : public GDALRasterBand |
7723 | | { |
7724 | | protected: |
7725 | | CPLErr IReadBlock(int, int, void *) override; |
7726 | | CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, |
7727 | | int nYSize, void *pData, int nBufXSize, int nBufYSize, |
7728 | | GDALDataType eBufType, GSpacing nPixelSpaceBuf, |
7729 | | GSpacing nLineSpaceBuf, |
7730 | | GDALRasterIOExtraArg *psExtraArg) override; |
7731 | | |
7732 | | public: |
7733 | | explicit GDALMDArrayResampledDatasetRasterBand( |
7734 | | GDALMDArrayResampledDataset *poDSIn); |
7735 | | |
7736 | | double GetNoDataValue(int *pbHasNoData) override; |
7737 | | }; |
7738 | | |
7739 | | class GDALMDArrayResampledDataset final : public GDALPamDataset |
7740 | | { |
7741 | | friend class GDALMDArrayResampled; |
7742 | | friend class GDALMDArrayResampledDatasetRasterBand; |
7743 | | |
7744 | | std::shared_ptr<GDALMDArray> m_poArray; |
7745 | | const size_t m_iXDim; |
7746 | | const size_t m_iYDim; |
7747 | | GDALGeoTransform m_gt{}; |
7748 | | bool m_bHasGT = false; |
7749 | | mutable std::shared_ptr<OGRSpatialReference> m_poSRS{}; |
7750 | | |
7751 | | std::vector<GUInt64> m_anOffset{}; |
7752 | | std::vector<size_t> m_anCount{}; |
7753 | | std::vector<GPtrDiff_t> m_anStride{}; |
7754 | | |
7755 | | std::string m_osFilenameLong{}; |
7756 | | std::string m_osFilenameLat{}; |
7757 | | |
7758 | | public: |
7759 | | GDALMDArrayResampledDataset(const std::shared_ptr<GDALMDArray> &array, |
7760 | | size_t iXDim, size_t iYDim) |
7761 | 0 | : m_poArray(array), m_iXDim(iXDim), m_iYDim(iYDim), |
7762 | 0 | m_anOffset(m_poArray->GetDimensionCount(), 0), |
7763 | 0 | m_anCount(m_poArray->GetDimensionCount(), 1), |
7764 | 0 | m_anStride(m_poArray->GetDimensionCount(), 0) |
7765 | 0 | { |
7766 | 0 | const auto &dims(m_poArray->GetDimensions()); |
7767 | |
|
7768 | 0 | nRasterYSize = static_cast<int>( |
7769 | 0 | std::min(static_cast<GUInt64>(INT_MAX), dims[iYDim]->GetSize())); |
7770 | 0 | nRasterXSize = static_cast<int>( |
7771 | 0 | std::min(static_cast<GUInt64>(INT_MAX), dims[iXDim]->GetSize())); |
7772 | |
|
7773 | 0 | m_bHasGT = m_poArray->GuessGeoTransform(m_iXDim, m_iYDim, false, m_gt); |
7774 | |
|
7775 | 0 | SetBand(1, new GDALMDArrayResampledDatasetRasterBand(this)); |
7776 | 0 | } |
7777 | | |
7778 | | ~GDALMDArrayResampledDataset() override; |
7779 | | |
7780 | | CPLErr GetGeoTransform(GDALGeoTransform >) const override |
7781 | 0 | { |
7782 | 0 | gt = m_gt; |
7783 | 0 | return m_bHasGT ? CE_None : CE_Failure; |
7784 | 0 | } |
7785 | | |
7786 | | const OGRSpatialReference *GetSpatialRef() const override |
7787 | 0 | { |
7788 | 0 | m_poSRS = m_poArray->GetSpatialRef(); |
7789 | 0 | if (m_poSRS) |
7790 | 0 | { |
7791 | 0 | m_poSRS.reset(m_poSRS->Clone()); |
7792 | 0 | auto axisMapping = m_poSRS->GetDataAxisToSRSAxisMapping(); |
7793 | 0 | for (auto &m : axisMapping) |
7794 | 0 | { |
7795 | 0 | if (m == static_cast<int>(m_iXDim) + 1) |
7796 | 0 | m = 1; |
7797 | 0 | else if (m == static_cast<int>(m_iYDim) + 1) |
7798 | 0 | m = 2; |
7799 | 0 | } |
7800 | 0 | m_poSRS->SetDataAxisToSRSAxisMapping(axisMapping); |
7801 | 0 | } |
7802 | 0 | return m_poSRS.get(); |
7803 | 0 | } |
7804 | | |
7805 | | void SetGeolocationArray(const std::string &osFilenameLong, |
7806 | | const std::string &osFilenameLat) |
7807 | 0 | { |
7808 | 0 | m_osFilenameLong = osFilenameLong; |
7809 | 0 | m_osFilenameLat = osFilenameLat; |
7810 | 0 | CPLStringList aosGeoLoc; |
7811 | 0 | aosGeoLoc.SetNameValue("LINE_OFFSET", "0"); |
7812 | 0 | aosGeoLoc.SetNameValue("LINE_STEP", "1"); |
7813 | 0 | aosGeoLoc.SetNameValue("PIXEL_OFFSET", "0"); |
7814 | 0 | aosGeoLoc.SetNameValue("PIXEL_STEP", "1"); |
7815 | 0 | aosGeoLoc.SetNameValue("SRS", SRS_WKT_WGS84_LAT_LONG); // FIXME? |
7816 | 0 | aosGeoLoc.SetNameValue("X_BAND", "1"); |
7817 | 0 | aosGeoLoc.SetNameValue("X_DATASET", m_osFilenameLong.c_str()); |
7818 | 0 | aosGeoLoc.SetNameValue("Y_BAND", "1"); |
7819 | 0 | aosGeoLoc.SetNameValue("Y_DATASET", m_osFilenameLat.c_str()); |
7820 | 0 | aosGeoLoc.SetNameValue("GEOREFERENCING_CONVENTION", "PIXEL_CENTER"); |
7821 | 0 | SetMetadata(aosGeoLoc.List(), "GEOLOCATION"); |
7822 | 0 | } |
7823 | | }; |
7824 | | |
7825 | | GDALMDArrayResampledDataset::~GDALMDArrayResampledDataset() |
7826 | 0 | { |
7827 | 0 | if (!m_osFilenameLong.empty()) |
7828 | 0 | VSIUnlink(m_osFilenameLong.c_str()); |
7829 | 0 | if (!m_osFilenameLat.empty()) |
7830 | 0 | VSIUnlink(m_osFilenameLat.c_str()); |
7831 | 0 | } |
7832 | | |
7833 | | /************************************************************************/ |
7834 | | /* GDALMDArrayResampledDatasetRasterBand() */ |
7835 | | /************************************************************************/ |
7836 | | |
7837 | | GDALMDArrayResampledDatasetRasterBand::GDALMDArrayResampledDatasetRasterBand( |
7838 | | GDALMDArrayResampledDataset *poDSIn) |
7839 | 0 | { |
7840 | 0 | const auto &poArray(poDSIn->m_poArray); |
7841 | 0 | const auto blockSize(poArray->GetBlockSize()); |
7842 | 0 | nBlockYSize = (blockSize[poDSIn->m_iYDim]) |
7843 | 0 | ? static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX), |
7844 | 0 | blockSize[poDSIn->m_iYDim])) |
7845 | 0 | : 1; |
7846 | 0 | nBlockXSize = blockSize[poDSIn->m_iXDim] |
7847 | 0 | ? static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX), |
7848 | 0 | blockSize[poDSIn->m_iXDim])) |
7849 | 0 | : poDSIn->GetRasterXSize(); |
7850 | 0 | eDataType = poArray->GetDataType().GetNumericDataType(); |
7851 | 0 | eAccess = poDSIn->eAccess; |
7852 | 0 | } |
7853 | | |
7854 | | /************************************************************************/ |
7855 | | /* GetNoDataValue() */ |
7856 | | /************************************************************************/ |
7857 | | |
7858 | | double GDALMDArrayResampledDatasetRasterBand::GetNoDataValue(int *pbHasNoData) |
7859 | 0 | { |
7860 | 0 | auto l_poDS(cpl::down_cast<GDALMDArrayResampledDataset *>(poDS)); |
7861 | 0 | const auto &poArray(l_poDS->m_poArray); |
7862 | 0 | bool bHasNodata = false; |
7863 | 0 | double dfRes = poArray->GetNoDataValueAsDouble(&bHasNodata); |
7864 | 0 | if (pbHasNoData) |
7865 | 0 | *pbHasNoData = bHasNodata; |
7866 | 0 | return dfRes; |
7867 | 0 | } |
7868 | | |
7869 | | /************************************************************************/ |
7870 | | /* IReadBlock() */ |
7871 | | /************************************************************************/ |
7872 | | |
7873 | | CPLErr GDALMDArrayResampledDatasetRasterBand::IReadBlock(int nBlockXOff, |
7874 | | int nBlockYOff, |
7875 | | void *pImage) |
7876 | 0 | { |
7877 | 0 | const int nDTSize(GDALGetDataTypeSizeBytes(eDataType)); |
7878 | 0 | const int nXOff = nBlockXOff * nBlockXSize; |
7879 | 0 | const int nYOff = nBlockYOff * nBlockYSize; |
7880 | 0 | const int nReqXSize = std::min(nRasterXSize - nXOff, nBlockXSize); |
7881 | 0 | const int nReqYSize = std::min(nRasterYSize - nYOff, nBlockYSize); |
7882 | 0 | GDALRasterIOExtraArg sExtraArg; |
7883 | 0 | INIT_RASTERIO_EXTRA_ARG(sExtraArg); |
7884 | 0 | return IRasterIO(GF_Read, nXOff, nYOff, nReqXSize, nReqYSize, pImage, |
7885 | 0 | nReqXSize, nReqYSize, eDataType, nDTSize, |
7886 | 0 | static_cast<GSpacing>(nDTSize) * nBlockXSize, &sExtraArg); |
7887 | 0 | } |
7888 | | |
7889 | | /************************************************************************/ |
7890 | | /* IRasterIO() */ |
7891 | | /************************************************************************/ |
7892 | | |
7893 | | CPLErr GDALMDArrayResampledDatasetRasterBand::IRasterIO( |
7894 | | GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize, |
7895 | | void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType, |
7896 | | GSpacing nPixelSpaceBuf, GSpacing nLineSpaceBuf, |
7897 | | GDALRasterIOExtraArg *psExtraArg) |
7898 | 0 | { |
7899 | 0 | auto l_poDS(cpl::down_cast<GDALMDArrayResampledDataset *>(poDS)); |
7900 | 0 | const auto &poArray(l_poDS->m_poArray); |
7901 | 0 | const int nBufferDTSize(GDALGetDataTypeSizeBytes(eBufType)); |
7902 | 0 | if (eRWFlag == GF_Read && nXSize == nBufXSize && nYSize == nBufYSize && |
7903 | 0 | nBufferDTSize > 0 && (nPixelSpaceBuf % nBufferDTSize) == 0 && |
7904 | 0 | (nLineSpaceBuf % nBufferDTSize) == 0) |
7905 | 0 | { |
7906 | 0 | l_poDS->m_anOffset[l_poDS->m_iXDim] = static_cast<GUInt64>(nXOff); |
7907 | 0 | l_poDS->m_anCount[l_poDS->m_iXDim] = static_cast<size_t>(nXSize); |
7908 | 0 | l_poDS->m_anStride[l_poDS->m_iXDim] = |
7909 | 0 | static_cast<GPtrDiff_t>(nPixelSpaceBuf / nBufferDTSize); |
7910 | |
|
7911 | 0 | l_poDS->m_anOffset[l_poDS->m_iYDim] = static_cast<GUInt64>(nYOff); |
7912 | 0 | l_poDS->m_anCount[l_poDS->m_iYDim] = static_cast<size_t>(nYSize); |
7913 | 0 | l_poDS->m_anStride[l_poDS->m_iYDim] = |
7914 | 0 | static_cast<GPtrDiff_t>(nLineSpaceBuf / nBufferDTSize); |
7915 | |
|
7916 | 0 | return poArray->Read(l_poDS->m_anOffset.data(), |
7917 | 0 | l_poDS->m_anCount.data(), nullptr, |
7918 | 0 | l_poDS->m_anStride.data(), |
7919 | 0 | GDALExtendedDataType::Create(eBufType), pData) |
7920 | 0 | ? CE_None |
7921 | 0 | : CE_Failure; |
7922 | 0 | } |
7923 | 0 | return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, |
7924 | 0 | pData, nBufXSize, nBufYSize, eBufType, |
7925 | 0 | nPixelSpaceBuf, nLineSpaceBuf, psExtraArg); |
7926 | 0 | } |
7927 | | |
7928 | | class GDALMDArrayResampled final : public GDALPamMDArray |
7929 | | { |
7930 | | private: |
7931 | | std::shared_ptr<GDALMDArray> m_poParent{}; |
7932 | | std::vector<std::shared_ptr<GDALDimension>> m_apoDims; |
7933 | | std::vector<GUInt64> m_anBlockSize; |
7934 | | GDALExtendedDataType m_dt; |
7935 | | std::shared_ptr<OGRSpatialReference> m_poSRS{}; |
7936 | | std::shared_ptr<GDALMDArray> m_poVarX{}; |
7937 | | std::shared_ptr<GDALMDArray> m_poVarY{}; |
7938 | | std::unique_ptr<GDALMDArrayResampledDataset> m_poParentDS{}; |
7939 | | std::unique_ptr<GDALDataset> m_poReprojectedDS{}; |
7940 | | |
7941 | | protected: |
7942 | | GDALMDArrayResampled( |
7943 | | const std::shared_ptr<GDALMDArray> &poParent, |
7944 | | const std::vector<std::shared_ptr<GDALDimension>> &apoDims, |
7945 | | const std::vector<GUInt64> &anBlockSize) |
7946 | 0 | : GDALAbstractMDArray(std::string(), |
7947 | 0 | "Resampled view of " + poParent->GetFullName()), |
7948 | 0 | GDALPamMDArray( |
7949 | 0 | std::string(), "Resampled view of " + poParent->GetFullName(), |
7950 | 0 | GDALPamMultiDim::GetPAM(poParent), poParent->GetContext()), |
7951 | 0 | m_poParent(std::move(poParent)), m_apoDims(apoDims), |
7952 | 0 | m_anBlockSize(anBlockSize), m_dt(m_poParent->GetDataType()) |
7953 | 0 | { |
7954 | 0 | CPLAssert(apoDims.size() == m_poParent->GetDimensionCount()); |
7955 | 0 | CPLAssert(anBlockSize.size() == m_poParent->GetDimensionCount()); |
7956 | 0 | } |
7957 | | |
7958 | | bool IRead(const GUInt64 *arrayStartIdx, const size_t *count, |
7959 | | const GInt64 *arrayStep, const GPtrDiff_t *bufferStride, |
7960 | | const GDALExtendedDataType &bufferDataType, |
7961 | | void *pDstBuffer) const override; |
7962 | | |
7963 | | public: |
7964 | | static std::shared_ptr<GDALMDArray> |
7965 | | Create(const std::shared_ptr<GDALMDArray> &poParent, |
7966 | | const std::vector<std::shared_ptr<GDALDimension>> &apoNewDims, |
7967 | | GDALRIOResampleAlg resampleAlg, |
7968 | | const OGRSpatialReference *poTargetSRS, CSLConstList papszOptions); |
7969 | | |
7970 | | ~GDALMDArrayResampled() override |
7971 | 0 | { |
7972 | | // First close the warped VRT |
7973 | 0 | m_poReprojectedDS.reset(); |
7974 | 0 | m_poParentDS.reset(); |
7975 | 0 | } |
7976 | | |
7977 | | bool IsWritable() const override |
7978 | 0 | { |
7979 | 0 | return false; |
7980 | 0 | } |
7981 | | |
7982 | | const std::string &GetFilename() const override |
7983 | 0 | { |
7984 | 0 | return m_poParent->GetFilename(); |
7985 | 0 | } |
7986 | | |
7987 | | const std::vector<std::shared_ptr<GDALDimension>> & |
7988 | | GetDimensions() const override |
7989 | 0 | { |
7990 | 0 | return m_apoDims; |
7991 | 0 | } |
7992 | | |
7993 | | const GDALExtendedDataType &GetDataType() const override |
7994 | 0 | { |
7995 | 0 | return m_dt; |
7996 | 0 | } |
7997 | | |
7998 | | std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override |
7999 | 0 | { |
8000 | 0 | return m_poSRS; |
8001 | 0 | } |
8002 | | |
8003 | | std::vector<GUInt64> GetBlockSize() const override |
8004 | 0 | { |
8005 | 0 | return m_anBlockSize; |
8006 | 0 | } |
8007 | | |
8008 | | std::shared_ptr<GDALAttribute> |
8009 | | GetAttribute(const std::string &osName) const override |
8010 | 0 | { |
8011 | 0 | return m_poParent->GetAttribute(osName); |
8012 | 0 | } |
8013 | | |
8014 | | std::vector<std::shared_ptr<GDALAttribute>> |
8015 | | GetAttributes(CSLConstList papszOptions = nullptr) const override |
8016 | 0 | { |
8017 | 0 | return m_poParent->GetAttributes(papszOptions); |
8018 | 0 | } |
8019 | | |
8020 | | const std::string &GetUnit() const override |
8021 | 0 | { |
8022 | 0 | return m_poParent->GetUnit(); |
8023 | 0 | } |
8024 | | |
8025 | | const void *GetRawNoDataValue() const override |
8026 | 0 | { |
8027 | 0 | return m_poParent->GetRawNoDataValue(); |
8028 | 0 | } |
8029 | | |
8030 | | double GetOffset(bool *pbHasOffset, |
8031 | | GDALDataType *peStorageType) const override |
8032 | 0 | { |
8033 | 0 | return m_poParent->GetOffset(pbHasOffset, peStorageType); |
8034 | 0 | } |
8035 | | |
8036 | | double GetScale(bool *pbHasScale, |
8037 | | GDALDataType *peStorageType) const override |
8038 | 0 | { |
8039 | 0 | return m_poParent->GetScale(pbHasScale, peStorageType); |
8040 | 0 | } |
8041 | | }; |
8042 | | |
8043 | | /************************************************************************/ |
8044 | | /* GDALMDArrayResampled::Create() */ |
8045 | | /************************************************************************/ |
8046 | | |
8047 | | std::shared_ptr<GDALMDArray> GDALMDArrayResampled::Create( |
8048 | | const std::shared_ptr<GDALMDArray> &poParent, |
8049 | | const std::vector<std::shared_ptr<GDALDimension>> &apoNewDimsIn, |
8050 | | GDALRIOResampleAlg resampleAlg, const OGRSpatialReference *poTargetSRS, |
8051 | | CSLConstList /* papszOptions */) |
8052 | 0 | { |
8053 | 0 | const char *pszResampleAlg = "nearest"; |
8054 | 0 | bool unsupported = false; |
8055 | 0 | switch (resampleAlg) |
8056 | 0 | { |
8057 | 0 | case GRIORA_NearestNeighbour: |
8058 | 0 | pszResampleAlg = "nearest"; |
8059 | 0 | break; |
8060 | 0 | case GRIORA_Bilinear: |
8061 | 0 | pszResampleAlg = "bilinear"; |
8062 | 0 | break; |
8063 | 0 | case GRIORA_Cubic: |
8064 | 0 | pszResampleAlg = "cubic"; |
8065 | 0 | break; |
8066 | 0 | case GRIORA_CubicSpline: |
8067 | 0 | pszResampleAlg = "cubicspline"; |
8068 | 0 | break; |
8069 | 0 | case GRIORA_Lanczos: |
8070 | 0 | pszResampleAlg = "lanczos"; |
8071 | 0 | break; |
8072 | 0 | case GRIORA_Average: |
8073 | 0 | pszResampleAlg = "average"; |
8074 | 0 | break; |
8075 | 0 | case GRIORA_Mode: |
8076 | 0 | pszResampleAlg = "mode"; |
8077 | 0 | break; |
8078 | 0 | case GRIORA_Gauss: |
8079 | 0 | unsupported = true; |
8080 | 0 | break; |
8081 | 0 | case GRIORA_RESERVED_START: |
8082 | 0 | unsupported = true; |
8083 | 0 | break; |
8084 | 0 | case GRIORA_RESERVED_END: |
8085 | 0 | unsupported = true; |
8086 | 0 | break; |
8087 | 0 | case GRIORA_RMS: |
8088 | 0 | pszResampleAlg = "rms"; |
8089 | 0 | break; |
8090 | 0 | } |
8091 | 0 | if (unsupported) |
8092 | 0 | { |
8093 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
8094 | 0 | "Unsupported resample method for GetResampled()"); |
8095 | 0 | return nullptr; |
8096 | 0 | } |
8097 | | |
8098 | 0 | if (poParent->GetDimensionCount() < 2) |
8099 | 0 | { |
8100 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
8101 | 0 | "GetResampled() only supports 2 dimensions or more"); |
8102 | 0 | return nullptr; |
8103 | 0 | } |
8104 | | |
8105 | 0 | const auto &aoParentDims = poParent->GetDimensions(); |
8106 | 0 | if (apoNewDimsIn.size() != aoParentDims.size()) |
8107 | 0 | { |
8108 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
8109 | 0 | "GetResampled(): apoNewDims size should be the same as " |
8110 | 0 | "GetDimensionCount()"); |
8111 | 0 | return nullptr; |
8112 | 0 | } |
8113 | | |
8114 | 0 | std::vector<std::shared_ptr<GDALDimension>> apoNewDims; |
8115 | 0 | apoNewDims.reserve(apoNewDimsIn.size()); |
8116 | |
|
8117 | 0 | std::vector<GUInt64> anBlockSize; |
8118 | 0 | anBlockSize.reserve(apoNewDimsIn.size()); |
8119 | 0 | const auto &anParentBlockSize = poParent->GetBlockSize(); |
8120 | |
|
8121 | 0 | auto apoParentDims = poParent->GetDimensions(); |
8122 | | // Special case for NASA EMIT datasets |
8123 | 0 | const bool bYXBandOrder = (apoParentDims.size() == 3 && |
8124 | 0 | apoParentDims[0]->GetName() == "downtrack" && |
8125 | 0 | apoParentDims[1]->GetName() == "crosstrack" && |
8126 | 0 | apoParentDims[2]->GetName() == "bands"); |
8127 | |
|
8128 | 0 | const size_t iYDimParent = |
8129 | 0 | bYXBandOrder ? 0 : poParent->GetDimensionCount() - 2; |
8130 | 0 | const size_t iXDimParent = |
8131 | 0 | bYXBandOrder ? 1 : poParent->GetDimensionCount() - 1; |
8132 | |
|
8133 | 0 | for (unsigned i = 0; i < apoNewDimsIn.size(); ++i) |
8134 | 0 | { |
8135 | 0 | if (i == iYDimParent || i == iXDimParent) |
8136 | 0 | continue; |
8137 | 0 | if (apoNewDimsIn[i] == nullptr) |
8138 | 0 | { |
8139 | 0 | apoNewDims.emplace_back(aoParentDims[i]); |
8140 | 0 | } |
8141 | 0 | else if (apoNewDimsIn[i]->GetSize() != aoParentDims[i]->GetSize() || |
8142 | 0 | apoNewDimsIn[i]->GetName() != aoParentDims[i]->GetName()) |
8143 | 0 | { |
8144 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
8145 | 0 | "GetResampled(): apoNewDims[%u] should be the same " |
8146 | 0 | "as its parent", |
8147 | 0 | i); |
8148 | 0 | return nullptr; |
8149 | 0 | } |
8150 | 0 | else |
8151 | 0 | { |
8152 | 0 | apoNewDims.emplace_back(aoParentDims[i]); |
8153 | 0 | } |
8154 | 0 | anBlockSize.emplace_back(anParentBlockSize[i]); |
8155 | 0 | } |
8156 | | |
8157 | 0 | std::unique_ptr<GDALMDArrayResampledDataset> poParentDS( |
8158 | 0 | new GDALMDArrayResampledDataset(poParent, iXDimParent, iYDimParent)); |
8159 | |
|
8160 | 0 | double dfXStart = 0.0; |
8161 | 0 | double dfXSpacing = 0.0; |
8162 | 0 | bool gotXSpacing = false; |
8163 | 0 | auto poNewDimX = apoNewDimsIn[iXDimParent]; |
8164 | 0 | if (poNewDimX) |
8165 | 0 | { |
8166 | 0 | if (poNewDimX->GetSize() > static_cast<GUInt64>(INT_MAX)) |
8167 | 0 | { |
8168 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
8169 | 0 | "Too big size for X dimension"); |
8170 | 0 | return nullptr; |
8171 | 0 | } |
8172 | 0 | auto var = poNewDimX->GetIndexingVariable(); |
8173 | 0 | if (var) |
8174 | 0 | { |
8175 | 0 | if (var->GetDimensionCount() != 1 || |
8176 | 0 | var->GetDimensions()[0]->GetSize() != poNewDimX->GetSize() || |
8177 | 0 | !var->IsRegularlySpaced(dfXStart, dfXSpacing)) |
8178 | 0 | { |
8179 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
8180 | 0 | "New X dimension should be indexed by a regularly " |
8181 | 0 | "spaced variable"); |
8182 | 0 | return nullptr; |
8183 | 0 | } |
8184 | 0 | gotXSpacing = true; |
8185 | 0 | } |
8186 | 0 | } |
8187 | | |
8188 | 0 | double dfYStart = 0.0; |
8189 | 0 | double dfYSpacing = 0.0; |
8190 | 0 | auto poNewDimY = apoNewDimsIn[iYDimParent]; |
8191 | 0 | bool gotYSpacing = false; |
8192 | 0 | if (poNewDimY) |
8193 | 0 | { |
8194 | 0 | if (poNewDimY->GetSize() > static_cast<GUInt64>(INT_MAX)) |
8195 | 0 | { |
8196 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
8197 | 0 | "Too big size for Y dimension"); |
8198 | 0 | return nullptr; |
8199 | 0 | } |
8200 | 0 | auto var = poNewDimY->GetIndexingVariable(); |
8201 | 0 | if (var) |
8202 | 0 | { |
8203 | 0 | if (var->GetDimensionCount() != 1 || |
8204 | 0 | var->GetDimensions()[0]->GetSize() != poNewDimY->GetSize() || |
8205 | 0 | !var->IsRegularlySpaced(dfYStart, dfYSpacing)) |
8206 | 0 | { |
8207 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
8208 | 0 | "New Y dimension should be indexed by a regularly " |
8209 | 0 | "spaced variable"); |
8210 | 0 | return nullptr; |
8211 | 0 | } |
8212 | 0 | gotYSpacing = true; |
8213 | 0 | } |
8214 | 0 | } |
8215 | | |
8216 | | // This limitation could probably be removed |
8217 | 0 | if ((gotXSpacing && !gotYSpacing) || (!gotXSpacing && gotYSpacing)) |
8218 | 0 | { |
8219 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
8220 | 0 | "Either none of new X or Y dimension should have an indexing " |
8221 | 0 | "variable, or both should both should have one."); |
8222 | 0 | return nullptr; |
8223 | 0 | } |
8224 | | |
8225 | 0 | std::string osDstWKT; |
8226 | 0 | if (poTargetSRS) |
8227 | 0 | { |
8228 | 0 | char *pszDstWKT = nullptr; |
8229 | 0 | if (poTargetSRS->exportToWkt(&pszDstWKT) != OGRERR_NONE) |
8230 | 0 | { |
8231 | 0 | CPLFree(pszDstWKT); |
8232 | 0 | return nullptr; |
8233 | 0 | } |
8234 | 0 | osDstWKT = pszDstWKT; |
8235 | 0 | CPLFree(pszDstWKT); |
8236 | 0 | } |
8237 | | |
8238 | | // Use coordinate variables for geolocation array |
8239 | 0 | const auto apoCoordinateVars = poParent->GetCoordinateVariables(); |
8240 | 0 | bool useGeolocationArray = false; |
8241 | 0 | if (apoCoordinateVars.size() >= 2) |
8242 | 0 | { |
8243 | 0 | std::shared_ptr<GDALMDArray> poLongVar; |
8244 | 0 | std::shared_ptr<GDALMDArray> poLatVar; |
8245 | 0 | for (const auto &poCoordVar : apoCoordinateVars) |
8246 | 0 | { |
8247 | 0 | const auto &osName = poCoordVar->GetName(); |
8248 | 0 | const auto poAttr = poCoordVar->GetAttribute("standard_name"); |
8249 | 0 | std::string osStandardName; |
8250 | 0 | if (poAttr && poAttr->GetDataType().GetClass() == GEDTC_STRING && |
8251 | 0 | poAttr->GetDimensionCount() == 0) |
8252 | 0 | { |
8253 | 0 | const char *pszStandardName = poAttr->ReadAsString(); |
8254 | 0 | if (pszStandardName) |
8255 | 0 | osStandardName = pszStandardName; |
8256 | 0 | } |
8257 | 0 | if (osName == "lon" || osName == "longitude" || |
8258 | 0 | osName == "Longitude" || osStandardName == "longitude") |
8259 | 0 | { |
8260 | 0 | poLongVar = poCoordVar; |
8261 | 0 | } |
8262 | 0 | else if (osName == "lat" || osName == "latitude" || |
8263 | 0 | osName == "Latitude" || osStandardName == "latitude") |
8264 | 0 | { |
8265 | 0 | poLatVar = poCoordVar; |
8266 | 0 | } |
8267 | 0 | } |
8268 | 0 | if (poLatVar != nullptr && poLongVar != nullptr) |
8269 | 0 | { |
8270 | 0 | const auto longDimCount = poLongVar->GetDimensionCount(); |
8271 | 0 | const auto &longDims = poLongVar->GetDimensions(); |
8272 | 0 | const auto latDimCount = poLatVar->GetDimensionCount(); |
8273 | 0 | const auto &latDims = poLatVar->GetDimensions(); |
8274 | 0 | const auto xDimSize = aoParentDims[iXDimParent]->GetSize(); |
8275 | 0 | const auto yDimSize = aoParentDims[iYDimParent]->GetSize(); |
8276 | 0 | if (longDimCount == 1 && longDims[0]->GetSize() == xDimSize && |
8277 | 0 | latDimCount == 1 && latDims[0]->GetSize() == yDimSize) |
8278 | 0 | { |
8279 | | // Geolocation arrays are 1D, and of consistent size with |
8280 | | // the variable |
8281 | 0 | useGeolocationArray = true; |
8282 | 0 | } |
8283 | 0 | else if ((longDimCount == 2 || |
8284 | 0 | (longDimCount == 3 && longDims[0]->GetSize() == 1)) && |
8285 | 0 | longDims[longDimCount - 2]->GetSize() == yDimSize && |
8286 | 0 | longDims[longDimCount - 1]->GetSize() == xDimSize && |
8287 | 0 | (latDimCount == 2 || |
8288 | 0 | (latDimCount == 3 && latDims[0]->GetSize() == 1)) && |
8289 | 0 | latDims[latDimCount - 2]->GetSize() == yDimSize && |
8290 | 0 | latDims[latDimCount - 1]->GetSize() == xDimSize) |
8291 | | |
8292 | 0 | { |
8293 | | // Geolocation arrays are 2D (or 3D with first dimension of |
8294 | | // size 1, as found in Sentinel 5P products), and of consistent |
8295 | | // size with the variable |
8296 | 0 | useGeolocationArray = true; |
8297 | 0 | } |
8298 | 0 | else |
8299 | 0 | { |
8300 | 0 | CPLDebug( |
8301 | 0 | "GDAL", |
8302 | 0 | "Longitude and latitude coordinate variables found, " |
8303 | 0 | "but their characteristics are not compatible of using " |
8304 | 0 | "them as geolocation arrays"); |
8305 | 0 | } |
8306 | 0 | if (useGeolocationArray) |
8307 | 0 | { |
8308 | 0 | CPLDebug("GDAL", |
8309 | 0 | "Setting geolocation array from variables %s and %s", |
8310 | 0 | poLongVar->GetName().c_str(), |
8311 | 0 | poLatVar->GetName().c_str()); |
8312 | 0 | const std::string osFilenameLong = |
8313 | 0 | VSIMemGenerateHiddenFilename("longitude.tif"); |
8314 | 0 | const std::string osFilenameLat = |
8315 | 0 | VSIMemGenerateHiddenFilename("latitude.tif"); |
8316 | 0 | std::unique_ptr<GDALDataset> poTmpLongDS( |
8317 | 0 | longDimCount == 1 |
8318 | 0 | ? poLongVar->AsClassicDataset(0, 0) |
8319 | 0 | : poLongVar->AsClassicDataset(longDimCount - 1, |
8320 | 0 | longDimCount - 2)); |
8321 | 0 | auto hTIFFLongDS = GDALTranslate( |
8322 | 0 | osFilenameLong.c_str(), |
8323 | 0 | GDALDataset::ToHandle(poTmpLongDS.get()), nullptr, nullptr); |
8324 | 0 | std::unique_ptr<GDALDataset> poTmpLatDS( |
8325 | 0 | latDimCount == 1 ? poLatVar->AsClassicDataset(0, 0) |
8326 | 0 | : poLatVar->AsClassicDataset( |
8327 | 0 | latDimCount - 1, latDimCount - 2)); |
8328 | 0 | auto hTIFFLatDS = GDALTranslate( |
8329 | 0 | osFilenameLat.c_str(), |
8330 | 0 | GDALDataset::ToHandle(poTmpLatDS.get()), nullptr, nullptr); |
8331 | 0 | const bool bError = |
8332 | 0 | (hTIFFLatDS == nullptr || hTIFFLongDS == nullptr); |
8333 | 0 | GDALClose(hTIFFLongDS); |
8334 | 0 | GDALClose(hTIFFLatDS); |
8335 | 0 | if (bError) |
8336 | 0 | { |
8337 | 0 | VSIUnlink(osFilenameLong.c_str()); |
8338 | 0 | VSIUnlink(osFilenameLat.c_str()); |
8339 | 0 | return nullptr; |
8340 | 0 | } |
8341 | | |
8342 | 0 | poParentDS->SetGeolocationArray(osFilenameLong, osFilenameLat); |
8343 | 0 | } |
8344 | 0 | } |
8345 | 0 | else |
8346 | 0 | { |
8347 | 0 | CPLDebug("GDAL", |
8348 | 0 | "Coordinate variables available for %s, but " |
8349 | 0 | "longitude and/or latitude variables were not identified", |
8350 | 0 | poParent->GetName().c_str()); |
8351 | 0 | } |
8352 | 0 | } |
8353 | | |
8354 | | // Build gdalwarp arguments |
8355 | 0 | CPLStringList aosArgv; |
8356 | |
|
8357 | 0 | aosArgv.AddString("-of"); |
8358 | 0 | aosArgv.AddString("VRT"); |
8359 | |
|
8360 | 0 | aosArgv.AddString("-r"); |
8361 | 0 | aosArgv.AddString(pszResampleAlg); |
8362 | |
|
8363 | 0 | if (!osDstWKT.empty()) |
8364 | 0 | { |
8365 | 0 | aosArgv.AddString("-t_srs"); |
8366 | 0 | aosArgv.AddString(osDstWKT.c_str()); |
8367 | 0 | } |
8368 | |
|
8369 | 0 | if (useGeolocationArray) |
8370 | 0 | aosArgv.AddString("-geoloc"); |
8371 | |
|
8372 | 0 | if (gotXSpacing && gotYSpacing) |
8373 | 0 | { |
8374 | 0 | const double dfXMin = dfXStart - dfXSpacing / 2; |
8375 | 0 | const double dfXMax = |
8376 | 0 | dfXMin + dfXSpacing * static_cast<double>(poNewDimX->GetSize()); |
8377 | 0 | const double dfYMax = dfYStart - dfYSpacing / 2; |
8378 | 0 | const double dfYMin = |
8379 | 0 | dfYMax + dfYSpacing * static_cast<double>(poNewDimY->GetSize()); |
8380 | 0 | aosArgv.AddString("-te"); |
8381 | 0 | aosArgv.AddString(CPLSPrintf("%.17g", dfXMin)); |
8382 | 0 | aosArgv.AddString(CPLSPrintf("%.17g", dfYMin)); |
8383 | 0 | aosArgv.AddString(CPLSPrintf("%.17g", dfXMax)); |
8384 | 0 | aosArgv.AddString(CPLSPrintf("%.17g", dfYMax)); |
8385 | 0 | } |
8386 | |
|
8387 | 0 | if (poNewDimX && poNewDimY) |
8388 | 0 | { |
8389 | 0 | aosArgv.AddString("-ts"); |
8390 | 0 | aosArgv.AddString( |
8391 | 0 | CPLSPrintf("%d", static_cast<int>(poNewDimX->GetSize()))); |
8392 | 0 | aosArgv.AddString( |
8393 | 0 | CPLSPrintf("%d", static_cast<int>(poNewDimY->GetSize()))); |
8394 | 0 | } |
8395 | 0 | else if (poNewDimX) |
8396 | 0 | { |
8397 | 0 | aosArgv.AddString("-ts"); |
8398 | 0 | aosArgv.AddString( |
8399 | 0 | CPLSPrintf("%d", static_cast<int>(poNewDimX->GetSize()))); |
8400 | 0 | aosArgv.AddString("0"); |
8401 | 0 | } |
8402 | 0 | else if (poNewDimY) |
8403 | 0 | { |
8404 | 0 | aosArgv.AddString("-ts"); |
8405 | 0 | aosArgv.AddString("0"); |
8406 | 0 | aosArgv.AddString( |
8407 | 0 | CPLSPrintf("%d", static_cast<int>(poNewDimY->GetSize()))); |
8408 | 0 | } |
8409 | | |
8410 | | // Create a warped VRT dataset |
8411 | 0 | GDALWarpAppOptions *psOptions = |
8412 | 0 | GDALWarpAppOptionsNew(aosArgv.List(), nullptr); |
8413 | 0 | GDALDatasetH hSrcDS = GDALDataset::ToHandle(poParentDS.get()); |
8414 | 0 | std::unique_ptr<GDALDataset> poReprojectedDS(GDALDataset::FromHandle( |
8415 | 0 | GDALWarp("", nullptr, 1, &hSrcDS, psOptions, nullptr))); |
8416 | 0 | GDALWarpAppOptionsFree(psOptions); |
8417 | 0 | if (poReprojectedDS == nullptr) |
8418 | 0 | return nullptr; |
8419 | | |
8420 | 0 | int nBlockXSize; |
8421 | 0 | int nBlockYSize; |
8422 | 0 | poReprojectedDS->GetRasterBand(1)->GetBlockSize(&nBlockXSize, &nBlockYSize); |
8423 | 0 | anBlockSize.emplace_back(nBlockYSize); |
8424 | 0 | anBlockSize.emplace_back(nBlockXSize); |
8425 | |
|
8426 | 0 | GDALGeoTransform gt; |
8427 | 0 | CPLErr eErr = poReprojectedDS->GetGeoTransform(gt); |
8428 | 0 | CPLAssert(eErr == CE_None); |
8429 | 0 | CPL_IGNORE_RET_VAL(eErr); |
8430 | |
|
8431 | 0 | auto poDimY = std::make_shared<GDALDimensionWeakIndexingVar>( |
8432 | 0 | std::string(), "dimY", GDAL_DIM_TYPE_HORIZONTAL_Y, "NORTH", |
8433 | 0 | poReprojectedDS->GetRasterYSize()); |
8434 | 0 | auto varY = GDALMDArrayRegularlySpaced::Create( |
8435 | 0 | std::string(), poDimY->GetName(), poDimY, gt[3] + gt[5] / 2, gt[5], 0); |
8436 | 0 | poDimY->SetIndexingVariable(varY); |
8437 | |
|
8438 | 0 | auto poDimX = std::make_shared<GDALDimensionWeakIndexingVar>( |
8439 | 0 | std::string(), "dimX", GDAL_DIM_TYPE_HORIZONTAL_X, "EAST", |
8440 | 0 | poReprojectedDS->GetRasterXSize()); |
8441 | 0 | auto varX = GDALMDArrayRegularlySpaced::Create( |
8442 | 0 | std::string(), poDimX->GetName(), poDimX, gt[0] + gt[1] / 2, gt[1], 0); |
8443 | 0 | poDimX->SetIndexingVariable(varX); |
8444 | |
|
8445 | 0 | apoNewDims.emplace_back(poDimY); |
8446 | 0 | apoNewDims.emplace_back(poDimX); |
8447 | 0 | auto newAr(std::shared_ptr<GDALMDArrayResampled>( |
8448 | 0 | new GDALMDArrayResampled(poParent, apoNewDims, anBlockSize))); |
8449 | 0 | newAr->SetSelf(newAr); |
8450 | 0 | if (poTargetSRS) |
8451 | 0 | { |
8452 | 0 | newAr->m_poSRS.reset(poTargetSRS->Clone()); |
8453 | 0 | } |
8454 | 0 | else |
8455 | 0 | { |
8456 | 0 | newAr->m_poSRS = poParent->GetSpatialRef(); |
8457 | 0 | } |
8458 | 0 | newAr->m_poVarX = varX; |
8459 | 0 | newAr->m_poVarY = varY; |
8460 | 0 | newAr->m_poReprojectedDS = std::move(poReprojectedDS); |
8461 | 0 | newAr->m_poParentDS = std::move(poParentDS); |
8462 | | |
8463 | | // If the input array is y,x,band ordered, the above newAr is |
8464 | | // actually band,y,x ordered as it is more convenient for |
8465 | | // GDALMDArrayResampled::IRead() implementation. But transpose that |
8466 | | // array to the order of the input array |
8467 | 0 | if (bYXBandOrder) |
8468 | 0 | return newAr->Transpose(std::vector<int>{1, 2, 0}); |
8469 | | |
8470 | 0 | return newAr; |
8471 | 0 | } |
8472 | | |
8473 | | /************************************************************************/ |
8474 | | /* GDALMDArrayResampled::IRead() */ |
8475 | | /************************************************************************/ |
8476 | | |
8477 | | bool GDALMDArrayResampled::IRead(const GUInt64 *arrayStartIdx, |
8478 | | const size_t *count, const GInt64 *arrayStep, |
8479 | | const GPtrDiff_t *bufferStride, |
8480 | | const GDALExtendedDataType &bufferDataType, |
8481 | | void *pDstBuffer) const |
8482 | 0 | { |
8483 | 0 | if (bufferDataType.GetClass() != GEDTC_NUMERIC) |
8484 | 0 | return false; |
8485 | | |
8486 | 0 | struct Stack |
8487 | 0 | { |
8488 | 0 | size_t nIters = 0; |
8489 | 0 | GByte *dst_ptr = nullptr; |
8490 | 0 | GPtrDiff_t dst_inc_offset = 0; |
8491 | 0 | }; |
8492 | |
|
8493 | 0 | const auto nDims = GetDimensionCount(); |
8494 | 0 | std::vector<Stack> stack(nDims + 1); // +1 to avoid -Wnull-dereference |
8495 | 0 | const size_t nBufferDTSize = bufferDataType.GetSize(); |
8496 | 0 | for (size_t i = 0; i < nDims; i++) |
8497 | 0 | { |
8498 | 0 | stack[i].dst_inc_offset = |
8499 | 0 | static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize); |
8500 | 0 | } |
8501 | 0 | stack[0].dst_ptr = static_cast<GByte *>(pDstBuffer); |
8502 | |
|
8503 | 0 | size_t dimIdx = 0; |
8504 | 0 | const size_t iDimY = nDims - 2; |
8505 | 0 | const size_t iDimX = nDims - 1; |
8506 | | // Use an array to avoid a false positive warning from CLang Static |
8507 | | // Analyzer about flushCaches being never read |
8508 | 0 | bool flushCaches[] = {false}; |
8509 | 0 | const bool bYXBandOrder = |
8510 | 0 | m_poParentDS->m_iYDim == 0 && m_poParentDS->m_iXDim == 1; |
8511 | |
|
8512 | 0 | lbl_next_depth: |
8513 | 0 | if (dimIdx == iDimY) |
8514 | 0 | { |
8515 | 0 | if (flushCaches[0]) |
8516 | 0 | { |
8517 | 0 | flushCaches[0] = false; |
8518 | | // When changing of 2D slice, flush GDAL 2D buffers |
8519 | 0 | m_poParentDS->FlushCache(false); |
8520 | 0 | m_poReprojectedDS->FlushCache(false); |
8521 | 0 | } |
8522 | |
|
8523 | 0 | if (!GDALMDRasterIOFromBand(m_poReprojectedDS->GetRasterBand(1), |
8524 | 0 | GF_Read, iDimX, iDimY, arrayStartIdx, count, |
8525 | 0 | arrayStep, bufferStride, bufferDataType, |
8526 | 0 | stack[dimIdx].dst_ptr)) |
8527 | 0 | { |
8528 | 0 | return false; |
8529 | 0 | } |
8530 | 0 | } |
8531 | 0 | else |
8532 | 0 | { |
8533 | 0 | stack[dimIdx].nIters = count[dimIdx]; |
8534 | 0 | if (m_poParentDS->m_anOffset[bYXBandOrder ? 2 : dimIdx] != |
8535 | 0 | arrayStartIdx[dimIdx]) |
8536 | 0 | { |
8537 | 0 | flushCaches[0] = true; |
8538 | 0 | } |
8539 | 0 | m_poParentDS->m_anOffset[bYXBandOrder ? 2 : dimIdx] = |
8540 | 0 | arrayStartIdx[dimIdx]; |
8541 | 0 | while (true) |
8542 | 0 | { |
8543 | 0 | dimIdx++; |
8544 | 0 | stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr; |
8545 | 0 | goto lbl_next_depth; |
8546 | 0 | lbl_return_to_caller: |
8547 | 0 | dimIdx--; |
8548 | 0 | if ((--stack[dimIdx].nIters) == 0) |
8549 | 0 | break; |
8550 | 0 | flushCaches[0] = true; |
8551 | 0 | ++m_poParentDS->m_anOffset[bYXBandOrder ? 2 : dimIdx]; |
8552 | 0 | stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset; |
8553 | 0 | } |
8554 | 0 | } |
8555 | 0 | if (dimIdx > 0) |
8556 | 0 | goto lbl_return_to_caller; |
8557 | | |
8558 | 0 | return true; |
8559 | 0 | } |
8560 | | |
8561 | | /************************************************************************/ |
8562 | | /* GetResampled() */ |
8563 | | /************************************************************************/ |
8564 | | |
8565 | | /** Return an array that is a resampled / reprojected view of the current array |
8566 | | * |
8567 | | * This is the same as the C function GDALMDArrayGetResampled(). |
8568 | | * |
8569 | | * Currently this method can only resample along the last 2 dimensions, unless |
8570 | | * orthorectifying a NASA EMIT dataset. |
8571 | | * |
8572 | | * For NASA EMIT datasets, if apoNewDims[] and poTargetSRS is NULL, the |
8573 | | * geometry lookup table (GLT) is used by default for fast orthorectification. |
8574 | | * |
8575 | | * Options available are: |
8576 | | * <ul> |
8577 | | * <li>EMIT_ORTHORECTIFICATION=YES/NO: defaults to YES for a NASA EMIT dataset. |
8578 | | * Can be set to NO to use generic reprojection method. |
8579 | | * </li> |
8580 | | * <li>USE_GOOD_WAVELENGTHS=YES/NO: defaults to YES. Only used for EMIT |
8581 | | * orthorectification to take into account the value of the |
8582 | | * /sensor_band_parameters/good_wavelengths array to decide if slices of the |
8583 | | * current array along the band dimension are valid.</li> |
8584 | | * </ul> |
8585 | | * |
8586 | | * @param apoNewDims New dimensions. Its size should be GetDimensionCount(). |
8587 | | * apoNewDims[i] can be NULL to let the method automatically |
8588 | | * determine it. |
8589 | | * @param resampleAlg Resampling algorithm |
8590 | | * @param poTargetSRS Target SRS, or nullptr |
8591 | | * @param papszOptions NULL-terminated list of options, or NULL. |
8592 | | * |
8593 | | * @return a new array, that holds a reference to the original one, and thus is |
8594 | | * a view of it (not a copy), or nullptr in case of error. |
8595 | | * |
8596 | | * @since 3.4 |
8597 | | */ |
8598 | | std::shared_ptr<GDALMDArray> GDALMDArray::GetResampled( |
8599 | | const std::vector<std::shared_ptr<GDALDimension>> &apoNewDims, |
8600 | | GDALRIOResampleAlg resampleAlg, const OGRSpatialReference *poTargetSRS, |
8601 | | CSLConstList papszOptions) const |
8602 | 0 | { |
8603 | 0 | auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock()); |
8604 | 0 | if (!self) |
8605 | 0 | { |
8606 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
8607 | 0 | "Driver implementation issue: m_pSelf not set !"); |
8608 | 0 | return nullptr; |
8609 | 0 | } |
8610 | 0 | if (GetDataType().GetClass() != GEDTC_NUMERIC) |
8611 | 0 | { |
8612 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
8613 | 0 | "GetResampled() only supports numeric data type"); |
8614 | 0 | return nullptr; |
8615 | 0 | } |
8616 | | |
8617 | | // Special case for NASA EMIT datasets |
8618 | 0 | auto apoDims = GetDimensions(); |
8619 | 0 | if (poTargetSRS == nullptr && |
8620 | 0 | ((apoDims.size() == 3 && apoDims[0]->GetName() == "downtrack" && |
8621 | 0 | apoDims[1]->GetName() == "crosstrack" && |
8622 | 0 | apoDims[2]->GetName() == "bands" && |
8623 | 0 | (apoNewDims == std::vector<std::shared_ptr<GDALDimension>>(3) || |
8624 | 0 | apoNewDims == |
8625 | 0 | std::vector<std::shared_ptr<GDALDimension>>{nullptr, nullptr, |
8626 | 0 | apoDims[2]})) || |
8627 | 0 | (apoDims.size() == 2 && apoDims[0]->GetName() == "downtrack" && |
8628 | 0 | apoDims[1]->GetName() == "crosstrack" && |
8629 | 0 | apoNewDims == std::vector<std::shared_ptr<GDALDimension>>(2))) && |
8630 | 0 | CPLTestBool(CSLFetchNameValueDef(papszOptions, |
8631 | 0 | "EMIT_ORTHORECTIFICATION", "YES"))) |
8632 | 0 | { |
8633 | 0 | auto poRootGroup = GetRootGroup(); |
8634 | 0 | if (poRootGroup) |
8635 | 0 | { |
8636 | 0 | auto poAttrGeotransform = poRootGroup->GetAttribute("geotransform"); |
8637 | 0 | auto poLocationGroup = poRootGroup->OpenGroup("location"); |
8638 | 0 | if (poAttrGeotransform && |
8639 | 0 | poAttrGeotransform->GetDataType().GetClass() == GEDTC_NUMERIC && |
8640 | 0 | poAttrGeotransform->GetDimensionCount() == 1 && |
8641 | 0 | poAttrGeotransform->GetDimensionsSize()[0] == 6 && |
8642 | 0 | poLocationGroup) |
8643 | 0 | { |
8644 | 0 | auto poGLT_X = poLocationGroup->OpenMDArray("glt_x"); |
8645 | 0 | auto poGLT_Y = poLocationGroup->OpenMDArray("glt_y"); |
8646 | 0 | if (poGLT_X && poGLT_X->GetDimensionCount() == 2 && |
8647 | 0 | poGLT_X->GetDimensions()[0]->GetName() == "ortho_y" && |
8648 | 0 | poGLT_X->GetDimensions()[1]->GetName() == "ortho_x" && |
8649 | 0 | poGLT_Y && poGLT_Y->GetDimensionCount() == 2 && |
8650 | 0 | poGLT_Y->GetDimensions()[0]->GetName() == "ortho_y" && |
8651 | 0 | poGLT_Y->GetDimensions()[1]->GetName() == "ortho_x") |
8652 | 0 | { |
8653 | 0 | return CreateGLTOrthorectified( |
8654 | 0 | self, poRootGroup, poGLT_X, poGLT_Y, |
8655 | 0 | /* nGLTIndexOffset = */ -1, |
8656 | 0 | poAttrGeotransform->ReadAsDoubleArray(), papszOptions); |
8657 | 0 | } |
8658 | 0 | } |
8659 | 0 | } |
8660 | 0 | } |
8661 | | |
8662 | 0 | if (CPLTestBool(CSLFetchNameValueDef(papszOptions, |
8663 | 0 | "EMIT_ORTHORECTIFICATION", "NO"))) |
8664 | 0 | { |
8665 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
8666 | 0 | "EMIT_ORTHORECTIFICATION required, but dataset and/or " |
8667 | 0 | "parameters are not compatible with it"); |
8668 | 0 | return nullptr; |
8669 | 0 | } |
8670 | | |
8671 | 0 | return GDALMDArrayResampled::Create(self, apoNewDims, resampleAlg, |
8672 | 0 | poTargetSRS, papszOptions); |
8673 | 0 | } |
8674 | | |
8675 | | /************************************************************************/ |
8676 | | /* GDALDatasetFromArray() */ |
8677 | | /************************************************************************/ |
8678 | | |
8679 | | class GDALDatasetFromArray; |
8680 | | |
8681 | | namespace |
8682 | | { |
8683 | | struct MetadataItem |
8684 | | { |
8685 | | std::shared_ptr<GDALAbstractMDArray> poArray{}; |
8686 | | std::string osName{}; |
8687 | | std::string osDefinition{}; |
8688 | | bool bDefinitionUsesPctForG = false; |
8689 | | }; |
8690 | | |
8691 | | struct BandImageryMetadata |
8692 | | { |
8693 | | std::shared_ptr<GDALAbstractMDArray> poCentralWavelengthArray{}; |
8694 | | double dfCentralWavelengthToMicrometer = 1.0; |
8695 | | std::shared_ptr<GDALAbstractMDArray> poFWHMArray{}; |
8696 | | double dfFWHMToMicrometer = 1.0; |
8697 | | }; |
8698 | | |
8699 | | } // namespace |
8700 | | |
8701 | | class GDALRasterBandFromArray final : public GDALPamRasterBand |
8702 | | { |
8703 | | std::vector<GUInt64> m_anOffset{}; |
8704 | | std::vector<size_t> m_anCount{}; |
8705 | | std::vector<GPtrDiff_t> m_anStride{}; |
8706 | | |
8707 | | protected: |
8708 | | CPLErr IReadBlock(int, int, void *) override; |
8709 | | CPLErr IWriteBlock(int, int, void *) override; |
8710 | | CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, |
8711 | | int nYSize, void *pData, int nBufXSize, int nBufYSize, |
8712 | | GDALDataType eBufType, GSpacing nPixelSpaceBuf, |
8713 | | GSpacing nLineSpaceBuf, |
8714 | | GDALRasterIOExtraArg *psExtraArg) override; |
8715 | | |
8716 | | public: |
8717 | | explicit GDALRasterBandFromArray( |
8718 | | GDALDatasetFromArray *poDSIn, |
8719 | | const std::vector<GUInt64> &anOtherDimCoord, |
8720 | | const std::vector<std::vector<MetadataItem>> |
8721 | | &aoBandParameterMetadataItems, |
8722 | | const std::vector<BandImageryMetadata> &aoBandImageryMetadata, |
8723 | | double dfDelay, time_t nStartTime, bool &bHasWarned); |
8724 | | |
8725 | | double GetNoDataValue(int *pbHasNoData) override; |
8726 | | int64_t GetNoDataValueAsInt64(int *pbHasNoData) override; |
8727 | | uint64_t GetNoDataValueAsUInt64(int *pbHasNoData) override; |
8728 | | double GetOffset(int *pbHasOffset) override; |
8729 | | double GetScale(int *pbHasScale) override; |
8730 | | const char *GetUnitType() override; |
8731 | | GDALColorInterp GetColorInterpretation() override; |
8732 | | }; |
8733 | | |
8734 | | class GDALDatasetFromArray final : public GDALPamDataset |
8735 | | { |
8736 | | friend class GDALRasterBandFromArray; |
8737 | | |
8738 | | std::shared_ptr<GDALMDArray> m_poArray; |
8739 | | size_t m_iXDim; |
8740 | | size_t m_iYDim; |
8741 | | GDALGeoTransform m_gt{}; |
8742 | | bool m_bHasGT = false; |
8743 | | mutable std::shared_ptr<OGRSpatialReference> m_poSRS{}; |
8744 | | GDALMultiDomainMetadata m_oMDD{}; |
8745 | | std::string m_osOvrFilename{}; |
8746 | | |
8747 | | public: |
8748 | | GDALDatasetFromArray(const std::shared_ptr<GDALMDArray> &array, |
8749 | | size_t iXDim, size_t iYDim) |
8750 | 0 | : m_poArray(array), m_iXDim(iXDim), m_iYDim(iYDim) |
8751 | 0 | { |
8752 | | // Initialize an overview filename from the filename of the array |
8753 | | // and its name. |
8754 | 0 | const std::string &osFilename = m_poArray->GetFilename(); |
8755 | 0 | if (!osFilename.empty()) |
8756 | 0 | { |
8757 | 0 | m_osOvrFilename = osFilename; |
8758 | 0 | m_osOvrFilename += '.'; |
8759 | 0 | for (char ch : m_poArray->GetName()) |
8760 | 0 | { |
8761 | 0 | if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || |
8762 | 0 | (ch >= 'a' && ch <= 'z') || ch == '_') |
8763 | 0 | { |
8764 | 0 | m_osOvrFilename += ch; |
8765 | 0 | } |
8766 | 0 | else |
8767 | 0 | { |
8768 | 0 | m_osOvrFilename += '_'; |
8769 | 0 | } |
8770 | 0 | } |
8771 | 0 | m_osOvrFilename += ".ovr"; |
8772 | 0 | oOvManager.Initialize(this); |
8773 | 0 | } |
8774 | 0 | } |
8775 | | |
8776 | | static GDALDatasetFromArray * |
8777 | | Create(const std::shared_ptr<GDALMDArray> &array, size_t iXDim, |
8778 | | size_t iYDim, const std::shared_ptr<GDALGroup> &poRootGroup, |
8779 | | CSLConstList papszOptions); |
8780 | | |
8781 | | ~GDALDatasetFromArray() override; |
8782 | | |
8783 | | CPLErr Close() override |
8784 | 0 | { |
8785 | 0 | CPLErr eErr = CE_None; |
8786 | 0 | if (nOpenFlags != OPEN_FLAGS_CLOSED) |
8787 | 0 | { |
8788 | 0 | if (GDALDatasetFromArray::FlushCache(/*bAtClosing=*/true) != |
8789 | 0 | CE_None) |
8790 | 0 | eErr = CE_Failure; |
8791 | 0 | m_poArray.reset(); |
8792 | 0 | } |
8793 | 0 | return eErr; |
8794 | 0 | } |
8795 | | |
8796 | | CPLErr GetGeoTransform(GDALGeoTransform >) const override |
8797 | 0 | { |
8798 | 0 | gt = m_gt; |
8799 | 0 | return m_bHasGT ? CE_None : CE_Failure; |
8800 | 0 | } |
8801 | | |
8802 | | const OGRSpatialReference *GetSpatialRef() const override |
8803 | 0 | { |
8804 | 0 | if (m_poArray->GetDimensionCount() < 2) |
8805 | 0 | return nullptr; |
8806 | 0 | m_poSRS = m_poArray->GetSpatialRef(); |
8807 | 0 | if (m_poSRS) |
8808 | 0 | { |
8809 | 0 | m_poSRS.reset(m_poSRS->Clone()); |
8810 | 0 | auto axisMapping = m_poSRS->GetDataAxisToSRSAxisMapping(); |
8811 | 0 | for (auto &m : axisMapping) |
8812 | 0 | { |
8813 | 0 | if (m == static_cast<int>(m_iXDim) + 1) |
8814 | 0 | m = 1; |
8815 | 0 | else if (m == static_cast<int>(m_iYDim) + 1) |
8816 | 0 | m = 2; |
8817 | 0 | } |
8818 | 0 | m_poSRS->SetDataAxisToSRSAxisMapping(axisMapping); |
8819 | 0 | } |
8820 | 0 | return m_poSRS.get(); |
8821 | 0 | } |
8822 | | |
8823 | | CPLErr SetMetadata(char **papszMetadata, const char *pszDomain) override |
8824 | 0 | { |
8825 | 0 | return m_oMDD.SetMetadata(papszMetadata, pszDomain); |
8826 | 0 | } |
8827 | | |
8828 | | char **GetMetadata(const char *pszDomain) override |
8829 | 0 | { |
8830 | 0 | return m_oMDD.GetMetadata(pszDomain); |
8831 | 0 | } |
8832 | | |
8833 | | const char *GetMetadataItem(const char *pszName, |
8834 | | const char *pszDomain) override |
8835 | 0 | { |
8836 | 0 | if (!m_osOvrFilename.empty() && pszName && |
8837 | 0 | EQUAL(pszName, "OVERVIEW_FILE") && pszDomain && |
8838 | 0 | EQUAL(pszDomain, "OVERVIEWS")) |
8839 | 0 | { |
8840 | 0 | return m_osOvrFilename.c_str(); |
8841 | 0 | } |
8842 | 0 | return m_oMDD.GetMetadataItem(pszName, pszDomain); |
8843 | 0 | } |
8844 | | }; |
8845 | | |
8846 | | GDALDatasetFromArray::~GDALDatasetFromArray() |
8847 | 0 | { |
8848 | 0 | GDALDatasetFromArray::Close(); |
8849 | 0 | } |
8850 | | |
8851 | | /************************************************************************/ |
8852 | | /* GDALRasterBandFromArray() */ |
8853 | | /************************************************************************/ |
8854 | | |
8855 | | GDALRasterBandFromArray::GDALRasterBandFromArray( |
8856 | | GDALDatasetFromArray *poDSIn, const std::vector<GUInt64> &anOtherDimCoord, |
8857 | | const std::vector<std::vector<MetadataItem>> &aoBandParameterMetadataItems, |
8858 | | const std::vector<BandImageryMetadata> &aoBandImageryMetadata, |
8859 | | double dfDelay, time_t nStartTime, bool &bHasWarned) |
8860 | 0 | { |
8861 | 0 | const auto &poArray(poDSIn->m_poArray); |
8862 | 0 | const auto &dims(poArray->GetDimensions()); |
8863 | 0 | const auto nDimCount(dims.size()); |
8864 | 0 | const auto blockSize(poArray->GetBlockSize()); |
8865 | 0 | nBlockYSize = (nDimCount >= 2 && blockSize[poDSIn->m_iYDim]) |
8866 | 0 | ? static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX), |
8867 | 0 | blockSize[poDSIn->m_iYDim])) |
8868 | 0 | : 1; |
8869 | 0 | nBlockXSize = blockSize[poDSIn->m_iXDim] |
8870 | 0 | ? static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX), |
8871 | 0 | blockSize[poDSIn->m_iXDim])) |
8872 | 0 | : poDSIn->GetRasterXSize(); |
8873 | 0 | eDataType = poArray->GetDataType().GetNumericDataType(); |
8874 | 0 | eAccess = poDSIn->eAccess; |
8875 | 0 | m_anOffset.resize(nDimCount); |
8876 | 0 | m_anCount.resize(nDimCount, 1); |
8877 | 0 | m_anStride.resize(nDimCount); |
8878 | 0 | for (size_t i = 0, j = 0; i < nDimCount; ++i) |
8879 | 0 | { |
8880 | 0 | if (i != poDSIn->m_iXDim && !(nDimCount >= 2 && i == poDSIn->m_iYDim)) |
8881 | 0 | { |
8882 | 0 | std::string dimName(dims[i]->GetName()); |
8883 | 0 | GUInt64 nIndex = anOtherDimCoord[j]; |
8884 | | // Detect subset_{orig_dim_name}_{start}_{incr}_{size} names of |
8885 | | // subsetted dimensions as generated by GetView() |
8886 | 0 | if (STARTS_WITH(dimName.c_str(), "subset_")) |
8887 | 0 | { |
8888 | 0 | CPLStringList aosTokens( |
8889 | 0 | CSLTokenizeString2(dimName.c_str(), "_", 0)); |
8890 | 0 | if (aosTokens.size() == 5) |
8891 | 0 | { |
8892 | 0 | dimName = aosTokens[1]; |
8893 | 0 | const auto nStartDim = static_cast<GUInt64>(CPLScanUIntBig( |
8894 | 0 | aosTokens[2], static_cast<int>(strlen(aosTokens[2])))); |
8895 | 0 | const auto nIncrDim = CPLAtoGIntBig(aosTokens[3]); |
8896 | 0 | nIndex = nIncrDim > 0 ? nStartDim + nIndex * nIncrDim |
8897 | 0 | : nStartDim - (nIndex * -nIncrDim); |
8898 | 0 | } |
8899 | 0 | } |
8900 | 0 | if (nDimCount != 3 || dimName != "Band") |
8901 | 0 | { |
8902 | 0 | SetMetadataItem( |
8903 | 0 | CPLSPrintf("DIM_%s_INDEX", dimName.c_str()), |
8904 | 0 | CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(nIndex))); |
8905 | 0 | } |
8906 | |
|
8907 | 0 | auto indexingVar = dims[i]->GetIndexingVariable(); |
8908 | | |
8909 | | // If the indexing variable is also listed in band parameter arrays, |
8910 | | // then don't use our default formatting |
8911 | 0 | if (indexingVar) |
8912 | 0 | { |
8913 | 0 | for (const auto &oItem : aoBandParameterMetadataItems[j]) |
8914 | 0 | { |
8915 | 0 | if (oItem.poArray->GetFullName() == |
8916 | 0 | indexingVar->GetFullName()) |
8917 | 0 | { |
8918 | 0 | indexingVar.reset(); |
8919 | 0 | break; |
8920 | 0 | } |
8921 | 0 | } |
8922 | 0 | } |
8923 | |
|
8924 | 0 | if (indexingVar && indexingVar->GetDimensionCount() == 1 && |
8925 | 0 | indexingVar->GetDimensions()[0]->GetSize() == |
8926 | 0 | dims[i]->GetSize()) |
8927 | 0 | { |
8928 | 0 | if (dfDelay >= 0 && time(nullptr) - nStartTime > dfDelay) |
8929 | 0 | { |
8930 | 0 | if (!bHasWarned) |
8931 | 0 | { |
8932 | 0 | CPLError( |
8933 | 0 | CE_Warning, CPLE_AppDefined, |
8934 | 0 | "Maximum delay to load band metadata from " |
8935 | 0 | "dimension indexing variables has expired. " |
8936 | 0 | "Increase the value of the " |
8937 | 0 | "LOAD_EXTRA_DIM_METADATA_DELAY " |
8938 | 0 | "option of GDALMDArray::AsClassicDataset() " |
8939 | 0 | "(also accessible as the " |
8940 | 0 | "GDAL_LOAD_EXTRA_DIM_METADATA_DELAY " |
8941 | 0 | "configuration option), " |
8942 | 0 | "or set it to 'unlimited' for unlimited delay. "); |
8943 | 0 | bHasWarned = true; |
8944 | 0 | } |
8945 | 0 | } |
8946 | 0 | else |
8947 | 0 | { |
8948 | 0 | size_t nCount = 1; |
8949 | 0 | const auto &dt(indexingVar->GetDataType()); |
8950 | 0 | std::vector<GByte> abyTmp(dt.GetSize()); |
8951 | 0 | if (indexingVar->Read(&(anOtherDimCoord[j]), &nCount, |
8952 | 0 | nullptr, nullptr, dt, &abyTmp[0])) |
8953 | 0 | { |
8954 | 0 | char *pszTmp = nullptr; |
8955 | 0 | GDALExtendedDataType::CopyValue( |
8956 | 0 | &abyTmp[0], dt, &pszTmp, |
8957 | 0 | GDALExtendedDataType::CreateString()); |
8958 | 0 | dt.FreeDynamicMemory(abyTmp.data()); |
8959 | 0 | if (pszTmp) |
8960 | 0 | { |
8961 | 0 | SetMetadataItem( |
8962 | 0 | CPLSPrintf("DIM_%s_VALUE", dimName.c_str()), |
8963 | 0 | pszTmp); |
8964 | 0 | CPLFree(pszTmp); |
8965 | 0 | } |
8966 | |
|
8967 | 0 | const auto &unit(indexingVar->GetUnit()); |
8968 | 0 | if (!unit.empty()) |
8969 | 0 | { |
8970 | 0 | SetMetadataItem( |
8971 | 0 | CPLSPrintf("DIM_%s_UNIT", dimName.c_str()), |
8972 | 0 | unit.c_str()); |
8973 | 0 | } |
8974 | 0 | } |
8975 | 0 | } |
8976 | 0 | } |
8977 | |
|
8978 | 0 | for (const auto &oItem : aoBandParameterMetadataItems[j]) |
8979 | 0 | { |
8980 | 0 | CPLString osVal; |
8981 | |
|
8982 | 0 | size_t nCount = 1; |
8983 | 0 | const auto &dt(oItem.poArray->GetDataType()); |
8984 | 0 | if (oItem.bDefinitionUsesPctForG) |
8985 | 0 | { |
8986 | | // There is one and only one %[x][.y]f|g in osDefinition |
8987 | 0 | std::vector<GByte> abyTmp(dt.GetSize()); |
8988 | 0 | if (oItem.poArray->Read(&(anOtherDimCoord[j]), &nCount, |
8989 | 0 | nullptr, nullptr, dt, &abyTmp[0])) |
8990 | 0 | { |
8991 | 0 | double dfVal = 0; |
8992 | 0 | GDALExtendedDataType::CopyValue( |
8993 | 0 | &abyTmp[0], dt, &dfVal, |
8994 | 0 | GDALExtendedDataType::Create(GDT_Float64)); |
8995 | 0 | osVal.Printf(oItem.osDefinition.c_str(), dfVal); |
8996 | 0 | dt.FreeDynamicMemory(abyTmp.data()); |
8997 | 0 | } |
8998 | 0 | } |
8999 | 0 | else |
9000 | 0 | { |
9001 | | // There should be zero or one %s in osDefinition |
9002 | 0 | char *pszValue = nullptr; |
9003 | 0 | if (dt.GetClass() == GEDTC_STRING) |
9004 | 0 | { |
9005 | 0 | CPL_IGNORE_RET_VAL(oItem.poArray->Read( |
9006 | 0 | &(anOtherDimCoord[j]), &nCount, nullptr, nullptr, |
9007 | 0 | dt, &pszValue)); |
9008 | 0 | } |
9009 | 0 | else |
9010 | 0 | { |
9011 | 0 | std::vector<GByte> abyTmp(dt.GetSize()); |
9012 | 0 | if (oItem.poArray->Read(&(anOtherDimCoord[j]), &nCount, |
9013 | 0 | nullptr, nullptr, dt, |
9014 | 0 | &abyTmp[0])) |
9015 | 0 | { |
9016 | 0 | GDALExtendedDataType::CopyValue( |
9017 | 0 | &abyTmp[0], dt, &pszValue, |
9018 | 0 | GDALExtendedDataType::CreateString()); |
9019 | 0 | } |
9020 | 0 | } |
9021 | |
|
9022 | 0 | if (pszValue) |
9023 | 0 | { |
9024 | 0 | osVal.Printf(oItem.osDefinition.c_str(), pszValue); |
9025 | 0 | CPLFree(pszValue); |
9026 | 0 | } |
9027 | 0 | } |
9028 | 0 | if (!osVal.empty()) |
9029 | 0 | SetMetadataItem(oItem.osName.c_str(), osVal); |
9030 | 0 | } |
9031 | |
|
9032 | 0 | if (aoBandImageryMetadata[j].poCentralWavelengthArray) |
9033 | 0 | { |
9034 | 0 | auto &poCentralWavelengthArray = |
9035 | 0 | aoBandImageryMetadata[j].poCentralWavelengthArray; |
9036 | 0 | size_t nCount = 1; |
9037 | 0 | const auto &dt(poCentralWavelengthArray->GetDataType()); |
9038 | 0 | std::vector<GByte> abyTmp(dt.GetSize()); |
9039 | 0 | if (poCentralWavelengthArray->Read(&(anOtherDimCoord[j]), |
9040 | 0 | &nCount, nullptr, nullptr, |
9041 | 0 | dt, &abyTmp[0])) |
9042 | 0 | { |
9043 | 0 | double dfVal = 0; |
9044 | 0 | GDALExtendedDataType::CopyValue( |
9045 | 0 | &abyTmp[0], dt, &dfVal, |
9046 | 0 | GDALExtendedDataType::Create(GDT_Float64)); |
9047 | 0 | dt.FreeDynamicMemory(abyTmp.data()); |
9048 | 0 | SetMetadataItem( |
9049 | 0 | "CENTRAL_WAVELENGTH_UM", |
9050 | 0 | CPLSPrintf( |
9051 | 0 | "%g", dfVal * aoBandImageryMetadata[j] |
9052 | 0 | .dfCentralWavelengthToMicrometer), |
9053 | 0 | "IMAGERY"); |
9054 | 0 | } |
9055 | 0 | } |
9056 | |
|
9057 | 0 | if (aoBandImageryMetadata[j].poFWHMArray) |
9058 | 0 | { |
9059 | 0 | auto &poFWHMArray = aoBandImageryMetadata[j].poFWHMArray; |
9060 | 0 | size_t nCount = 1; |
9061 | 0 | const auto &dt(poFWHMArray->GetDataType()); |
9062 | 0 | std::vector<GByte> abyTmp(dt.GetSize()); |
9063 | 0 | if (poFWHMArray->Read(&(anOtherDimCoord[j]), &nCount, nullptr, |
9064 | 0 | nullptr, dt, &abyTmp[0])) |
9065 | 0 | { |
9066 | 0 | double dfVal = 0; |
9067 | 0 | GDALExtendedDataType::CopyValue( |
9068 | 0 | &abyTmp[0], dt, &dfVal, |
9069 | 0 | GDALExtendedDataType::Create(GDT_Float64)); |
9070 | 0 | dt.FreeDynamicMemory(abyTmp.data()); |
9071 | 0 | SetMetadataItem( |
9072 | 0 | "FWHM_UM", |
9073 | 0 | CPLSPrintf("%g", dfVal * aoBandImageryMetadata[j] |
9074 | 0 | .dfFWHMToMicrometer), |
9075 | 0 | "IMAGERY"); |
9076 | 0 | } |
9077 | 0 | } |
9078 | |
|
9079 | 0 | m_anOffset[i] = anOtherDimCoord[j]; |
9080 | 0 | j++; |
9081 | 0 | } |
9082 | 0 | } |
9083 | 0 | } |
9084 | | |
9085 | | /************************************************************************/ |
9086 | | /* GetNoDataValue() */ |
9087 | | /************************************************************************/ |
9088 | | |
9089 | | double GDALRasterBandFromArray::GetNoDataValue(int *pbHasNoData) |
9090 | 0 | { |
9091 | 0 | auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS)); |
9092 | 0 | const auto &poArray(l_poDS->m_poArray); |
9093 | 0 | bool bHasNodata = false; |
9094 | 0 | const auto res = poArray->GetNoDataValueAsDouble(&bHasNodata); |
9095 | 0 | if (pbHasNoData) |
9096 | 0 | *pbHasNoData = bHasNodata; |
9097 | 0 | return res; |
9098 | 0 | } |
9099 | | |
9100 | | /************************************************************************/ |
9101 | | /* GetNoDataValueAsInt64() */ |
9102 | | /************************************************************************/ |
9103 | | |
9104 | | int64_t GDALRasterBandFromArray::GetNoDataValueAsInt64(int *pbHasNoData) |
9105 | 0 | { |
9106 | 0 | auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS)); |
9107 | 0 | const auto &poArray(l_poDS->m_poArray); |
9108 | 0 | bool bHasNodata = false; |
9109 | 0 | const auto nodata = poArray->GetNoDataValueAsInt64(&bHasNodata); |
9110 | 0 | if (pbHasNoData) |
9111 | 0 | *pbHasNoData = bHasNodata; |
9112 | 0 | return nodata; |
9113 | 0 | } |
9114 | | |
9115 | | /************************************************************************/ |
9116 | | /* GetNoDataValueAsUInt64() */ |
9117 | | /************************************************************************/ |
9118 | | |
9119 | | uint64_t GDALRasterBandFromArray::GetNoDataValueAsUInt64(int *pbHasNoData) |
9120 | 0 | { |
9121 | 0 | auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS)); |
9122 | 0 | const auto &poArray(l_poDS->m_poArray); |
9123 | 0 | bool bHasNodata = false; |
9124 | 0 | const auto nodata = poArray->GetNoDataValueAsUInt64(&bHasNodata); |
9125 | 0 | if (pbHasNoData) |
9126 | 0 | *pbHasNoData = bHasNodata; |
9127 | 0 | return nodata; |
9128 | 0 | } |
9129 | | |
9130 | | /************************************************************************/ |
9131 | | /* GetOffset() */ |
9132 | | /************************************************************************/ |
9133 | | |
9134 | | double GDALRasterBandFromArray::GetOffset(int *pbHasOffset) |
9135 | 0 | { |
9136 | 0 | auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS)); |
9137 | 0 | const auto &poArray(l_poDS->m_poArray); |
9138 | 0 | bool bHasValue = false; |
9139 | 0 | double dfRes = poArray->GetOffset(&bHasValue); |
9140 | 0 | if (pbHasOffset) |
9141 | 0 | *pbHasOffset = bHasValue; |
9142 | 0 | return dfRes; |
9143 | 0 | } |
9144 | | |
9145 | | /************************************************************************/ |
9146 | | /* GetUnitType() */ |
9147 | | /************************************************************************/ |
9148 | | |
9149 | | const char *GDALRasterBandFromArray::GetUnitType() |
9150 | 0 | { |
9151 | 0 | auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS)); |
9152 | 0 | const auto &poArray(l_poDS->m_poArray); |
9153 | 0 | return poArray->GetUnit().c_str(); |
9154 | 0 | } |
9155 | | |
9156 | | /************************************************************************/ |
9157 | | /* GetScale() */ |
9158 | | /************************************************************************/ |
9159 | | |
9160 | | double GDALRasterBandFromArray::GetScale(int *pbHasScale) |
9161 | 0 | { |
9162 | 0 | auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS)); |
9163 | 0 | const auto &poArray(l_poDS->m_poArray); |
9164 | 0 | bool bHasValue = false; |
9165 | 0 | double dfRes = poArray->GetScale(&bHasValue); |
9166 | 0 | if (pbHasScale) |
9167 | 0 | *pbHasScale = bHasValue; |
9168 | 0 | return dfRes; |
9169 | 0 | } |
9170 | | |
9171 | | /************************************************************************/ |
9172 | | /* IReadBlock() */ |
9173 | | /************************************************************************/ |
9174 | | |
9175 | | CPLErr GDALRasterBandFromArray::IReadBlock(int nBlockXOff, int nBlockYOff, |
9176 | | void *pImage) |
9177 | 0 | { |
9178 | 0 | const int nDTSize(GDALGetDataTypeSizeBytes(eDataType)); |
9179 | 0 | const int nXOff = nBlockXOff * nBlockXSize; |
9180 | 0 | const int nYOff = nBlockYOff * nBlockYSize; |
9181 | 0 | const int nReqXSize = std::min(nRasterXSize - nXOff, nBlockXSize); |
9182 | 0 | const int nReqYSize = std::min(nRasterYSize - nYOff, nBlockYSize); |
9183 | 0 | GDALRasterIOExtraArg sExtraArg; |
9184 | 0 | INIT_RASTERIO_EXTRA_ARG(sExtraArg); |
9185 | 0 | return IRasterIO(GF_Read, nXOff, nYOff, nReqXSize, nReqYSize, pImage, |
9186 | 0 | nReqXSize, nReqYSize, eDataType, nDTSize, |
9187 | 0 | static_cast<GSpacing>(nDTSize) * nBlockXSize, &sExtraArg); |
9188 | 0 | } |
9189 | | |
9190 | | /************************************************************************/ |
9191 | | /* IWriteBlock() */ |
9192 | | /************************************************************************/ |
9193 | | |
9194 | | CPLErr GDALRasterBandFromArray::IWriteBlock(int nBlockXOff, int nBlockYOff, |
9195 | | void *pImage) |
9196 | 0 | { |
9197 | 0 | const int nDTSize(GDALGetDataTypeSizeBytes(eDataType)); |
9198 | 0 | const int nXOff = nBlockXOff * nBlockXSize; |
9199 | 0 | const int nYOff = nBlockYOff * nBlockYSize; |
9200 | 0 | const int nReqXSize = std::min(nRasterXSize - nXOff, nBlockXSize); |
9201 | 0 | const int nReqYSize = std::min(nRasterYSize - nYOff, nBlockYSize); |
9202 | 0 | GDALRasterIOExtraArg sExtraArg; |
9203 | 0 | INIT_RASTERIO_EXTRA_ARG(sExtraArg); |
9204 | 0 | return IRasterIO(GF_Write, nXOff, nYOff, nReqXSize, nReqYSize, pImage, |
9205 | 0 | nReqXSize, nReqYSize, eDataType, nDTSize, |
9206 | 0 | static_cast<GSpacing>(nDTSize) * nBlockXSize, &sExtraArg); |
9207 | 0 | } |
9208 | | |
9209 | | /************************************************************************/ |
9210 | | /* IRasterIO() */ |
9211 | | /************************************************************************/ |
9212 | | |
9213 | | CPLErr GDALRasterBandFromArray::IRasterIO(GDALRWFlag eRWFlag, int nXOff, |
9214 | | int nYOff, int nXSize, int nYSize, |
9215 | | void *pData, int nBufXSize, |
9216 | | int nBufYSize, GDALDataType eBufType, |
9217 | | GSpacing nPixelSpaceBuf, |
9218 | | GSpacing nLineSpaceBuf, |
9219 | | GDALRasterIOExtraArg *psExtraArg) |
9220 | 0 | { |
9221 | 0 | auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS)); |
9222 | 0 | const auto &poArray(l_poDS->m_poArray); |
9223 | 0 | const int nBufferDTSize(GDALGetDataTypeSizeBytes(eBufType)); |
9224 | 0 | if (nXSize == nBufXSize && nYSize == nBufYSize && nBufferDTSize > 0 && |
9225 | 0 | (nPixelSpaceBuf % nBufferDTSize) == 0 && |
9226 | 0 | (nLineSpaceBuf % nBufferDTSize) == 0) |
9227 | 0 | { |
9228 | 0 | m_anOffset[l_poDS->m_iXDim] = static_cast<GUInt64>(nXOff); |
9229 | 0 | m_anCount[l_poDS->m_iXDim] = static_cast<size_t>(nXSize); |
9230 | 0 | m_anStride[l_poDS->m_iXDim] = |
9231 | 0 | static_cast<GPtrDiff_t>(nPixelSpaceBuf / nBufferDTSize); |
9232 | 0 | if (poArray->GetDimensionCount() >= 2) |
9233 | 0 | { |
9234 | 0 | m_anOffset[l_poDS->m_iYDim] = static_cast<GUInt64>(nYOff); |
9235 | 0 | m_anCount[l_poDS->m_iYDim] = static_cast<size_t>(nYSize); |
9236 | 0 | m_anStride[l_poDS->m_iYDim] = |
9237 | 0 | static_cast<GPtrDiff_t>(nLineSpaceBuf / nBufferDTSize); |
9238 | 0 | } |
9239 | 0 | if (eRWFlag == GF_Read) |
9240 | 0 | { |
9241 | 0 | return poArray->Read(m_anOffset.data(), m_anCount.data(), nullptr, |
9242 | 0 | m_anStride.data(), |
9243 | 0 | GDALExtendedDataType::Create(eBufType), pData) |
9244 | 0 | ? CE_None |
9245 | 0 | : CE_Failure; |
9246 | 0 | } |
9247 | 0 | else |
9248 | 0 | { |
9249 | 0 | return poArray->Write(m_anOffset.data(), m_anCount.data(), nullptr, |
9250 | 0 | m_anStride.data(), |
9251 | 0 | GDALExtendedDataType::Create(eBufType), pData) |
9252 | 0 | ? CE_None |
9253 | 0 | : CE_Failure; |
9254 | 0 | } |
9255 | 0 | } |
9256 | 0 | return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, |
9257 | 0 | pData, nBufXSize, nBufYSize, eBufType, |
9258 | 0 | nPixelSpaceBuf, nLineSpaceBuf, psExtraArg); |
9259 | 0 | } |
9260 | | |
9261 | | /************************************************************************/ |
9262 | | /* GetColorInterpretation() */ |
9263 | | /************************************************************************/ |
9264 | | |
9265 | | GDALColorInterp GDALRasterBandFromArray::GetColorInterpretation() |
9266 | 0 | { |
9267 | 0 | auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS)); |
9268 | 0 | const auto &poArray(l_poDS->m_poArray); |
9269 | 0 | auto poAttr = poArray->GetAttribute("COLOR_INTERPRETATION"); |
9270 | 0 | if (poAttr && poAttr->GetDataType().GetClass() == GEDTC_STRING) |
9271 | 0 | { |
9272 | 0 | bool bOK = false; |
9273 | 0 | GUInt64 nStartIndex = 0; |
9274 | 0 | if (poArray->GetDimensionCount() == 2 && |
9275 | 0 | poAttr->GetDimensionCount() == 0) |
9276 | 0 | { |
9277 | 0 | bOK = true; |
9278 | 0 | } |
9279 | 0 | else if (poArray->GetDimensionCount() == 3) |
9280 | 0 | { |
9281 | 0 | uint64_t nExtraDimSamples = 1; |
9282 | 0 | const auto &apoDims = poArray->GetDimensions(); |
9283 | 0 | for (size_t i = 0; i < apoDims.size(); ++i) |
9284 | 0 | { |
9285 | 0 | if (i != l_poDS->m_iXDim && i != l_poDS->m_iYDim) |
9286 | 0 | nExtraDimSamples *= apoDims[i]->GetSize(); |
9287 | 0 | } |
9288 | 0 | if (poAttr->GetDimensionsSize() == |
9289 | 0 | std::vector<GUInt64>{static_cast<GUInt64>(nExtraDimSamples)}) |
9290 | 0 | { |
9291 | 0 | bOK = true; |
9292 | 0 | } |
9293 | 0 | nStartIndex = nBand - 1; |
9294 | 0 | } |
9295 | 0 | if (bOK) |
9296 | 0 | { |
9297 | 0 | const auto oStringDT = GDALExtendedDataType::CreateString(); |
9298 | 0 | const size_t nCount = 1; |
9299 | 0 | const GInt64 arrayStep = 1; |
9300 | 0 | const GPtrDiff_t bufferStride = 1; |
9301 | 0 | char *pszValue = nullptr; |
9302 | 0 | poAttr->Read(&nStartIndex, &nCount, &arrayStep, &bufferStride, |
9303 | 0 | oStringDT, &pszValue); |
9304 | 0 | if (pszValue) |
9305 | 0 | { |
9306 | 0 | const auto eColorInterp = |
9307 | 0 | GDALGetColorInterpretationByName(pszValue); |
9308 | 0 | CPLFree(pszValue); |
9309 | 0 | return eColorInterp; |
9310 | 0 | } |
9311 | 0 | } |
9312 | 0 | } |
9313 | 0 | return GCI_Undefined; |
9314 | 0 | } |
9315 | | |
9316 | | /************************************************************************/ |
9317 | | /* GDALDatasetFromArray::Create() */ |
9318 | | /************************************************************************/ |
9319 | | |
9320 | | GDALDatasetFromArray *GDALDatasetFromArray::Create( |
9321 | | const std::shared_ptr<GDALMDArray> &array, size_t iXDim, size_t iYDim, |
9322 | | const std::shared_ptr<GDALGroup> &poRootGroup, CSLConstList papszOptions) |
9323 | | |
9324 | 0 | { |
9325 | 0 | const auto nDimCount(array->GetDimensionCount()); |
9326 | 0 | if (nDimCount == 0) |
9327 | 0 | { |
9328 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
9329 | 0 | "Unsupported number of dimensions"); |
9330 | 0 | return nullptr; |
9331 | 0 | } |
9332 | 0 | if (array->GetDataType().GetClass() != GEDTC_NUMERIC || |
9333 | 0 | array->GetDataType().GetNumericDataType() == GDT_Unknown) |
9334 | 0 | { |
9335 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
9336 | 0 | "Only arrays with numeric data types " |
9337 | 0 | "can be exposed as classic GDALDataset"); |
9338 | 0 | return nullptr; |
9339 | 0 | } |
9340 | 0 | if (iXDim >= nDimCount || iYDim >= nDimCount || |
9341 | 0 | (nDimCount >= 2 && iXDim == iYDim)) |
9342 | 0 | { |
9343 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Invalid iXDim and/or iYDim"); |
9344 | 0 | return nullptr; |
9345 | 0 | } |
9346 | 0 | GUInt64 nTotalBands = 1; |
9347 | 0 | const auto &dims(array->GetDimensions()); |
9348 | 0 | for (size_t i = 0; i < nDimCount; ++i) |
9349 | 0 | { |
9350 | 0 | if (i != iXDim && !(nDimCount >= 2 && i == iYDim)) |
9351 | 0 | { |
9352 | 0 | if (dims[i]->GetSize() > 65536 / nTotalBands) |
9353 | 0 | { |
9354 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9355 | 0 | "Too many bands. Operate on a sliced view"); |
9356 | 0 | return nullptr; |
9357 | 0 | } |
9358 | 0 | nTotalBands *= dims[i]->GetSize(); |
9359 | 0 | } |
9360 | 0 | } |
9361 | | |
9362 | 0 | std::map<std::string, size_t> oMapArrayDimNameToExtraDimIdx; |
9363 | 0 | std::vector<size_t> oMapArrayExtraDimIdxToOriginalIdx; |
9364 | 0 | for (size_t i = 0, j = 0; i < nDimCount; ++i) |
9365 | 0 | { |
9366 | 0 | if (i != iXDim && !(nDimCount >= 2 && i == iYDim)) |
9367 | 0 | { |
9368 | 0 | oMapArrayDimNameToExtraDimIdx[dims[i]->GetName()] = j; |
9369 | 0 | oMapArrayExtraDimIdxToOriginalIdx.push_back(i); |
9370 | 0 | ++j; |
9371 | 0 | } |
9372 | 0 | } |
9373 | |
|
9374 | 0 | const size_t nNewDimCount = nDimCount >= 2 ? nDimCount - 2 : 0; |
9375 | |
|
9376 | 0 | const char *pszBandMetadata = |
9377 | 0 | CSLFetchNameValue(papszOptions, "BAND_METADATA"); |
9378 | 0 | std::vector<std::vector<MetadataItem>> aoBandParameterMetadataItems( |
9379 | 0 | nNewDimCount); |
9380 | 0 | if (pszBandMetadata) |
9381 | 0 | { |
9382 | 0 | if (!poRootGroup) |
9383 | 0 | { |
9384 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9385 | 0 | "Root group should be provided when BAND_METADATA is set"); |
9386 | 0 | return nullptr; |
9387 | 0 | } |
9388 | 0 | CPLJSONDocument oDoc; |
9389 | 0 | if (!oDoc.LoadMemory(pszBandMetadata)) |
9390 | 0 | { |
9391 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9392 | 0 | "Invalid JSON content for BAND_METADATA"); |
9393 | 0 | return nullptr; |
9394 | 0 | } |
9395 | 0 | auto oRoot = oDoc.GetRoot(); |
9396 | 0 | if (oRoot.GetType() != CPLJSONObject::Type::Array) |
9397 | 0 | { |
9398 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9399 | 0 | "Value of BAND_METADATA should be an array"); |
9400 | 0 | return nullptr; |
9401 | 0 | } |
9402 | | |
9403 | 0 | auto oArray = oRoot.ToArray(); |
9404 | 0 | for (int j = 0; j < oArray.Size(); ++j) |
9405 | 0 | { |
9406 | 0 | const auto oJsonItem = oArray[j]; |
9407 | 0 | MetadataItem oItem; |
9408 | 0 | size_t iExtraDimIdx = 0; |
9409 | |
|
9410 | 0 | const auto osBandArrayFullname = oJsonItem.GetString("array"); |
9411 | 0 | const auto osBandAttributeName = oJsonItem.GetString("attribute"); |
9412 | 0 | std::shared_ptr<GDALMDArray> poArray; |
9413 | 0 | std::shared_ptr<GDALAttribute> poAttribute; |
9414 | 0 | if (osBandArrayFullname.empty() && osBandAttributeName.empty()) |
9415 | 0 | { |
9416 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9417 | 0 | "BAND_METADATA[%d][\"array\"] or " |
9418 | 0 | "BAND_METADATA[%d][\"attribute\"] is missing", |
9419 | 0 | j, j); |
9420 | 0 | return nullptr; |
9421 | 0 | } |
9422 | 0 | else if (!osBandArrayFullname.empty() && |
9423 | 0 | !osBandAttributeName.empty()) |
9424 | 0 | { |
9425 | 0 | CPLError( |
9426 | 0 | CE_Failure, CPLE_AppDefined, |
9427 | 0 | "BAND_METADATA[%d][\"array\"] and " |
9428 | 0 | "BAND_METADATA[%d][\"attribute\"] are mutually exclusive", |
9429 | 0 | j, j); |
9430 | 0 | return nullptr; |
9431 | 0 | } |
9432 | 0 | else if (!osBandArrayFullname.empty()) |
9433 | 0 | { |
9434 | 0 | poArray = |
9435 | 0 | poRootGroup->OpenMDArrayFromFullname(osBandArrayFullname); |
9436 | 0 | if (!poArray) |
9437 | 0 | { |
9438 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9439 | 0 | "Array %s cannot be found", |
9440 | 0 | osBandArrayFullname.c_str()); |
9441 | 0 | return nullptr; |
9442 | 0 | } |
9443 | 0 | if (poArray->GetDimensionCount() != 1) |
9444 | 0 | { |
9445 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9446 | 0 | "Array %s is not a 1D array", |
9447 | 0 | osBandArrayFullname.c_str()); |
9448 | 0 | return nullptr; |
9449 | 0 | } |
9450 | 0 | const auto &osAuxArrayDimName = |
9451 | 0 | poArray->GetDimensions()[0]->GetName(); |
9452 | 0 | auto oIter = |
9453 | 0 | oMapArrayDimNameToExtraDimIdx.find(osAuxArrayDimName); |
9454 | 0 | if (oIter == oMapArrayDimNameToExtraDimIdx.end()) |
9455 | 0 | { |
9456 | 0 | CPLError( |
9457 | 0 | CE_Failure, CPLE_AppDefined, |
9458 | 0 | "Dimension %s of array %s is not a non-X/Y dimension " |
9459 | 0 | "of array %s", |
9460 | 0 | osAuxArrayDimName.c_str(), osBandArrayFullname.c_str(), |
9461 | 0 | array->GetName().c_str()); |
9462 | 0 | return nullptr; |
9463 | 0 | } |
9464 | 0 | iExtraDimIdx = oIter->second; |
9465 | 0 | CPLAssert(iExtraDimIdx < nNewDimCount); |
9466 | 0 | } |
9467 | 0 | else |
9468 | 0 | { |
9469 | 0 | CPLAssert(!osBandAttributeName.empty()); |
9470 | 0 | poAttribute = !osBandAttributeName.empty() && |
9471 | 0 | osBandAttributeName[0] == '/' |
9472 | 0 | ? poRootGroup->OpenAttributeFromFullname( |
9473 | 0 | osBandAttributeName) |
9474 | 0 | : array->GetAttribute(osBandAttributeName); |
9475 | 0 | if (!poAttribute) |
9476 | 0 | { |
9477 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9478 | 0 | "Attribute %s cannot be found", |
9479 | 0 | osBandAttributeName.c_str()); |
9480 | 0 | return nullptr; |
9481 | 0 | } |
9482 | 0 | const auto aoAttrDims = poAttribute->GetDimensionsSize(); |
9483 | 0 | if (aoAttrDims.size() != 1) |
9484 | 0 | { |
9485 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9486 | 0 | "Attribute %s is not a 1D array", |
9487 | 0 | osBandAttributeName.c_str()); |
9488 | 0 | return nullptr; |
9489 | 0 | } |
9490 | 0 | bool found = false; |
9491 | 0 | for (const auto &iter : oMapArrayDimNameToExtraDimIdx) |
9492 | 0 | { |
9493 | 0 | if (dims[oMapArrayExtraDimIdxToOriginalIdx[iter.second]] |
9494 | 0 | ->GetSize() == aoAttrDims[0]) |
9495 | 0 | { |
9496 | 0 | if (found) |
9497 | 0 | { |
9498 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9499 | 0 | "Several dimensions of %s have the same " |
9500 | 0 | "size as attribute %s. Cannot infer which " |
9501 | 0 | "one to bind to!", |
9502 | 0 | array->GetName().c_str(), |
9503 | 0 | osBandAttributeName.c_str()); |
9504 | 0 | return nullptr; |
9505 | 0 | } |
9506 | 0 | found = true; |
9507 | 0 | iExtraDimIdx = iter.second; |
9508 | 0 | } |
9509 | 0 | } |
9510 | 0 | if (!found) |
9511 | 0 | { |
9512 | 0 | CPLError( |
9513 | 0 | CE_Failure, CPLE_AppDefined, |
9514 | 0 | "No dimension of %s has the same size as attribute %s", |
9515 | 0 | array->GetName().c_str(), osBandAttributeName.c_str()); |
9516 | 0 | return nullptr; |
9517 | 0 | } |
9518 | 0 | } |
9519 | | |
9520 | 0 | oItem.osName = oJsonItem.GetString("item_name"); |
9521 | 0 | if (oItem.osName.empty()) |
9522 | 0 | { |
9523 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9524 | 0 | "BAND_METADATA[%d][\"item_name\"] is missing", j); |
9525 | 0 | return nullptr; |
9526 | 0 | } |
9527 | | |
9528 | 0 | const auto osDefinition = oJsonItem.GetString("item_value", "%s"); |
9529 | | |
9530 | | // Check correctness of definition |
9531 | 0 | bool bFirstNumericFormatter = true; |
9532 | 0 | std::string osModDefinition; |
9533 | 0 | bool bDefinitionUsesPctForG = false; |
9534 | 0 | for (size_t k = 0; k < osDefinition.size(); ++k) |
9535 | 0 | { |
9536 | 0 | if (osDefinition[k] == '%') |
9537 | 0 | { |
9538 | 0 | osModDefinition += osDefinition[k]; |
9539 | 0 | if (k + 1 == osDefinition.size()) |
9540 | 0 | { |
9541 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9542 | 0 | "Value of " |
9543 | 0 | "BAND_METADATA[%d][\"item_value\"] = " |
9544 | 0 | "%s is invalid at offset %d", |
9545 | 0 | j, osDefinition.c_str(), int(k)); |
9546 | 0 | return nullptr; |
9547 | 0 | } |
9548 | 0 | ++k; |
9549 | 0 | if (osDefinition[k] == '%') |
9550 | 0 | { |
9551 | 0 | osModDefinition += osDefinition[k]; |
9552 | 0 | continue; |
9553 | 0 | } |
9554 | 0 | if (!bFirstNumericFormatter) |
9555 | 0 | { |
9556 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9557 | 0 | "Value of " |
9558 | 0 | "BAND_METADATA[%d][\"item_value\"] = %s is " |
9559 | 0 | "invalid at offset %d: %%[x][.y]f|g or %%s " |
9560 | 0 | "formatters should be specified at most once", |
9561 | 0 | j, osDefinition.c_str(), int(k)); |
9562 | 0 | return nullptr; |
9563 | 0 | } |
9564 | 0 | bFirstNumericFormatter = false; |
9565 | 0 | for (; k < osDefinition.size(); ++k) |
9566 | 0 | { |
9567 | 0 | osModDefinition += osDefinition[k]; |
9568 | 0 | if (!((osDefinition[k] >= '0' && |
9569 | 0 | osDefinition[k] <= '9') || |
9570 | 0 | osDefinition[k] == '.')) |
9571 | 0 | break; |
9572 | 0 | } |
9573 | 0 | if (k == osDefinition.size() || |
9574 | 0 | (osDefinition[k] != 'f' && osDefinition[k] != 'g' && |
9575 | 0 | osDefinition[k] != 's')) |
9576 | 0 | { |
9577 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9578 | 0 | "Value of " |
9579 | 0 | "BAND_METADATA[%d][\"item_value\"] = " |
9580 | 0 | "%s is invalid at offset %d: only " |
9581 | 0 | "%%[x][.y]f|g or %%s formatters are accepted", |
9582 | 0 | j, osDefinition.c_str(), int(k)); |
9583 | 0 | return nullptr; |
9584 | 0 | } |
9585 | 0 | bDefinitionUsesPctForG = |
9586 | 0 | (osDefinition[k] == 'f' || osDefinition[k] == 'g'); |
9587 | 0 | if (bDefinitionUsesPctForG) |
9588 | 0 | { |
9589 | 0 | if (poArray && |
9590 | 0 | poArray->GetDataType().GetClass() != GEDTC_NUMERIC) |
9591 | 0 | { |
9592 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9593 | 0 | "Data type of %s array is not numeric", |
9594 | 0 | poArray->GetName().c_str()); |
9595 | 0 | return nullptr; |
9596 | 0 | } |
9597 | 0 | else if (poAttribute && |
9598 | 0 | poAttribute->GetDataType().GetClass() != |
9599 | 0 | GEDTC_NUMERIC) |
9600 | 0 | { |
9601 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9602 | 0 | "Data type of %s attribute is not numeric", |
9603 | 0 | poAttribute->GetFullName().c_str()); |
9604 | 0 | return nullptr; |
9605 | 0 | } |
9606 | 0 | } |
9607 | 0 | } |
9608 | 0 | else if (osDefinition[k] == '$' && |
9609 | 0 | k + 1 < osDefinition.size() && |
9610 | 0 | osDefinition[k + 1] == '{') |
9611 | 0 | { |
9612 | 0 | const auto nPos = osDefinition.find('}', k); |
9613 | 0 | if (nPos == std::string::npos) |
9614 | 0 | { |
9615 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9616 | 0 | "Value of " |
9617 | 0 | "BAND_METADATA[%d][\"item_value\"] = " |
9618 | 0 | "%s is invalid at offset %d", |
9619 | 0 | j, osDefinition.c_str(), int(k)); |
9620 | 0 | return nullptr; |
9621 | 0 | } |
9622 | 0 | const auto osAttrName = |
9623 | 0 | osDefinition.substr(k + 2, nPos - (k + 2)); |
9624 | 0 | std::shared_ptr<GDALAttribute> poAttr; |
9625 | 0 | if (poArray && !osAttrName.empty() && osAttrName[0] != '/') |
9626 | 0 | { |
9627 | 0 | poAttr = poArray->GetAttribute(osAttrName); |
9628 | 0 | if (!poAttr) |
9629 | 0 | { |
9630 | 0 | CPLError( |
9631 | 0 | CE_Failure, CPLE_AppDefined, |
9632 | 0 | "Value of " |
9633 | 0 | "BAND_METADATA[%d][\"item_value\"] = " |
9634 | 0 | "%s is invalid: %s is not an attribute of %s", |
9635 | 0 | j, osDefinition.c_str(), osAttrName.c_str(), |
9636 | 0 | poArray->GetName().c_str()); |
9637 | 0 | return nullptr; |
9638 | 0 | } |
9639 | 0 | } |
9640 | 0 | else |
9641 | 0 | { |
9642 | 0 | poAttr = |
9643 | 0 | poRootGroup->OpenAttributeFromFullname(osAttrName); |
9644 | 0 | if (!poAttr) |
9645 | 0 | { |
9646 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9647 | 0 | "Value of " |
9648 | 0 | "BAND_METADATA[%d][\"item_value\"] = " |
9649 | 0 | "%s is invalid: %s is not an attribute", |
9650 | 0 | j, osDefinition.c_str(), |
9651 | 0 | osAttrName.c_str()); |
9652 | 0 | return nullptr; |
9653 | 0 | } |
9654 | 0 | } |
9655 | 0 | k = nPos; |
9656 | 0 | const char *pszValue = poAttr->ReadAsString(); |
9657 | 0 | if (!pszValue) |
9658 | 0 | { |
9659 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9660 | 0 | "Cannot get value of attribute %s as a " |
9661 | 0 | "string", |
9662 | 0 | osAttrName.c_str()); |
9663 | 0 | return nullptr; |
9664 | 0 | } |
9665 | 0 | osModDefinition += pszValue; |
9666 | 0 | } |
9667 | 0 | else |
9668 | 0 | { |
9669 | 0 | osModDefinition += osDefinition[k]; |
9670 | 0 | } |
9671 | 0 | } |
9672 | | |
9673 | 0 | if (poArray) |
9674 | 0 | oItem.poArray = std::move(poArray); |
9675 | 0 | else |
9676 | 0 | oItem.poArray = std::move(poAttribute); |
9677 | 0 | oItem.osDefinition = std::move(osModDefinition); |
9678 | 0 | oItem.bDefinitionUsesPctForG = bDefinitionUsesPctForG; |
9679 | |
|
9680 | 0 | aoBandParameterMetadataItems[iExtraDimIdx].emplace_back( |
9681 | 0 | std::move(oItem)); |
9682 | 0 | } |
9683 | 0 | } |
9684 | | |
9685 | 0 | std::vector<BandImageryMetadata> aoBandImageryMetadata(nNewDimCount); |
9686 | 0 | const char *pszBandImageryMetadata = |
9687 | 0 | CSLFetchNameValue(papszOptions, "BAND_IMAGERY_METADATA"); |
9688 | 0 | if (pszBandImageryMetadata) |
9689 | 0 | { |
9690 | 0 | if (!poRootGroup) |
9691 | 0 | { |
9692 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9693 | 0 | "Root group should be provided when BAND_IMAGERY_METADATA " |
9694 | 0 | "is set"); |
9695 | 0 | return nullptr; |
9696 | 0 | } |
9697 | 0 | CPLJSONDocument oDoc; |
9698 | 0 | if (!oDoc.LoadMemory(pszBandImageryMetadata)) |
9699 | 0 | { |
9700 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9701 | 0 | "Invalid JSON content for BAND_IMAGERY_METADATA"); |
9702 | 0 | return nullptr; |
9703 | 0 | } |
9704 | 0 | auto oRoot = oDoc.GetRoot(); |
9705 | 0 | if (oRoot.GetType() != CPLJSONObject::Type::Object) |
9706 | 0 | { |
9707 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9708 | 0 | "Value of BAND_IMAGERY_METADATA should be an object"); |
9709 | 0 | return nullptr; |
9710 | 0 | } |
9711 | 0 | for (const auto &oJsonItem : oRoot.GetChildren()) |
9712 | 0 | { |
9713 | 0 | if (oJsonItem.GetName() == "CENTRAL_WAVELENGTH_UM" || |
9714 | 0 | oJsonItem.GetName() == "FWHM_UM") |
9715 | 0 | { |
9716 | 0 | const auto osBandArrayFullname = oJsonItem.GetString("array"); |
9717 | 0 | const auto osBandAttributeName = |
9718 | 0 | oJsonItem.GetString("attribute"); |
9719 | 0 | std::shared_ptr<GDALMDArray> poArray; |
9720 | 0 | std::shared_ptr<GDALAttribute> poAttribute; |
9721 | 0 | size_t iExtraDimIdx = 0; |
9722 | 0 | if (osBandArrayFullname.empty() && osBandAttributeName.empty()) |
9723 | 0 | { |
9724 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9725 | 0 | "BAND_IMAGERY_METADATA[\"%s\"][\"array\"] or " |
9726 | 0 | "BAND_IMAGERY_METADATA[\"%s\"][\"attribute\"] is " |
9727 | 0 | "missing", |
9728 | 0 | oJsonItem.GetName().c_str(), |
9729 | 0 | oJsonItem.GetName().c_str()); |
9730 | 0 | return nullptr; |
9731 | 0 | } |
9732 | 0 | else if (!osBandArrayFullname.empty() && |
9733 | 0 | !osBandAttributeName.empty()) |
9734 | 0 | { |
9735 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9736 | 0 | "BAND_IMAGERY_METADATA[\"%s\"][\"array\"] and " |
9737 | 0 | "BAND_IMAGERY_METADATA[\"%s\"][\"attribute\"] are " |
9738 | 0 | "mutually exclusive", |
9739 | 0 | oJsonItem.GetName().c_str(), |
9740 | 0 | oJsonItem.GetName().c_str()); |
9741 | 0 | return nullptr; |
9742 | 0 | } |
9743 | 0 | else if (!osBandArrayFullname.empty()) |
9744 | 0 | { |
9745 | 0 | poArray = poRootGroup->OpenMDArrayFromFullname( |
9746 | 0 | osBandArrayFullname); |
9747 | 0 | if (!poArray) |
9748 | 0 | { |
9749 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9750 | 0 | "Array %s cannot be found", |
9751 | 0 | osBandArrayFullname.c_str()); |
9752 | 0 | return nullptr; |
9753 | 0 | } |
9754 | 0 | if (poArray->GetDimensionCount() != 1) |
9755 | 0 | { |
9756 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9757 | 0 | "Array %s is not a 1D array", |
9758 | 0 | osBandArrayFullname.c_str()); |
9759 | 0 | return nullptr; |
9760 | 0 | } |
9761 | 0 | const auto &osAuxArrayDimName = |
9762 | 0 | poArray->GetDimensions()[0]->GetName(); |
9763 | 0 | auto oIter = |
9764 | 0 | oMapArrayDimNameToExtraDimIdx.find(osAuxArrayDimName); |
9765 | 0 | if (oIter == oMapArrayDimNameToExtraDimIdx.end()) |
9766 | 0 | { |
9767 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9768 | 0 | "Dimension \"%s\" of array \"%s\" is not a " |
9769 | 0 | "non-X/Y dimension of array \"%s\"", |
9770 | 0 | osAuxArrayDimName.c_str(), |
9771 | 0 | osBandArrayFullname.c_str(), |
9772 | 0 | array->GetName().c_str()); |
9773 | 0 | return nullptr; |
9774 | 0 | } |
9775 | 0 | iExtraDimIdx = oIter->second; |
9776 | 0 | CPLAssert(iExtraDimIdx < nNewDimCount); |
9777 | 0 | } |
9778 | 0 | else |
9779 | 0 | { |
9780 | 0 | poAttribute = |
9781 | 0 | !osBandAttributeName.empty() && |
9782 | 0 | osBandAttributeName[0] == '/' |
9783 | 0 | ? poRootGroup->OpenAttributeFromFullname( |
9784 | 0 | osBandAttributeName) |
9785 | 0 | : array->GetAttribute(osBandAttributeName); |
9786 | 0 | if (!poAttribute) |
9787 | 0 | { |
9788 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9789 | 0 | "Attribute %s cannot be found", |
9790 | 0 | osBandAttributeName.c_str()); |
9791 | 0 | return nullptr; |
9792 | 0 | } |
9793 | 0 | const auto aoAttrDims = poAttribute->GetDimensionsSize(); |
9794 | 0 | if (aoAttrDims.size() != 1) |
9795 | 0 | { |
9796 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9797 | 0 | "Attribute %s is not a 1D array", |
9798 | 0 | osBandAttributeName.c_str()); |
9799 | 0 | return nullptr; |
9800 | 0 | } |
9801 | 0 | bool found = false; |
9802 | 0 | for (const auto &iter : oMapArrayDimNameToExtraDimIdx) |
9803 | 0 | { |
9804 | 0 | if (dims[oMapArrayExtraDimIdxToOriginalIdx[iter.second]] |
9805 | 0 | ->GetSize() == aoAttrDims[0]) |
9806 | 0 | { |
9807 | 0 | if (found) |
9808 | 0 | { |
9809 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9810 | 0 | "Several dimensions of %s have the " |
9811 | 0 | "same size as attribute %s. Cannot " |
9812 | 0 | "infer which one to bind to!", |
9813 | 0 | array->GetName().c_str(), |
9814 | 0 | osBandAttributeName.c_str()); |
9815 | 0 | return nullptr; |
9816 | 0 | } |
9817 | 0 | found = true; |
9818 | 0 | iExtraDimIdx = iter.second; |
9819 | 0 | } |
9820 | 0 | } |
9821 | 0 | if (!found) |
9822 | 0 | { |
9823 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9824 | 0 | "No dimension of %s has the same size as " |
9825 | 0 | "attribute %s", |
9826 | 0 | array->GetName().c_str(), |
9827 | 0 | osBandAttributeName.c_str()); |
9828 | 0 | return nullptr; |
9829 | 0 | } |
9830 | 0 | } |
9831 | | |
9832 | 0 | std::string osUnit = oJsonItem.GetString("unit", "um"); |
9833 | 0 | if (STARTS_WITH(osUnit.c_str(), "${")) |
9834 | 0 | { |
9835 | 0 | if (osUnit.back() != '}') |
9836 | 0 | { |
9837 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9838 | 0 | "Value of " |
9839 | 0 | "BAND_IMAGERY_METADATA[\"%s\"][\"unit\"] = " |
9840 | 0 | "%s is invalid", |
9841 | 0 | oJsonItem.GetName().c_str(), osUnit.c_str()); |
9842 | 0 | return nullptr; |
9843 | 0 | } |
9844 | 0 | const auto osAttrName = osUnit.substr(2, osUnit.size() - 3); |
9845 | 0 | std::shared_ptr<GDALAttribute> poAttr; |
9846 | 0 | if (poArray && !osAttrName.empty() && osAttrName[0] != '/') |
9847 | 0 | { |
9848 | 0 | poAttr = poArray->GetAttribute(osAttrName); |
9849 | 0 | if (!poAttr) |
9850 | 0 | { |
9851 | 0 | CPLError( |
9852 | 0 | CE_Failure, CPLE_AppDefined, |
9853 | 0 | "Value of " |
9854 | 0 | "BAND_IMAGERY_METADATA[\"%s\"][\"unit\"] = " |
9855 | 0 | "%s is invalid: %s is not an attribute of %s", |
9856 | 0 | oJsonItem.GetName().c_str(), osUnit.c_str(), |
9857 | 0 | osAttrName.c_str(), |
9858 | 0 | osBandArrayFullname.c_str()); |
9859 | 0 | return nullptr; |
9860 | 0 | } |
9861 | 0 | } |
9862 | 0 | else |
9863 | 0 | { |
9864 | 0 | poAttr = |
9865 | 0 | poRootGroup->OpenAttributeFromFullname(osAttrName); |
9866 | 0 | if (!poAttr) |
9867 | 0 | { |
9868 | 0 | CPLError( |
9869 | 0 | CE_Failure, CPLE_AppDefined, |
9870 | 0 | "Value of " |
9871 | 0 | "BAND_IMAGERY_METADATA[\"%s\"][\"unit\"] = " |
9872 | 0 | "%s is invalid: %s is not an attribute", |
9873 | 0 | oJsonItem.GetName().c_str(), osUnit.c_str(), |
9874 | 0 | osAttrName.c_str()); |
9875 | 0 | return nullptr; |
9876 | 0 | } |
9877 | 0 | } |
9878 | | |
9879 | 0 | const char *pszValue = poAttr->ReadAsString(); |
9880 | 0 | if (!pszValue) |
9881 | 0 | { |
9882 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9883 | 0 | "Cannot get value of attribute %s of %s as a " |
9884 | 0 | "string", |
9885 | 0 | osAttrName.c_str(), |
9886 | 0 | osBandArrayFullname.c_str()); |
9887 | 0 | return nullptr; |
9888 | 0 | } |
9889 | 0 | osUnit = pszValue; |
9890 | 0 | } |
9891 | 0 | double dfConvToUM = 1.0; |
9892 | 0 | if (osUnit == "nm" || osUnit == "nanometre" || |
9893 | 0 | osUnit == "nanometres" || osUnit == "nanometer" || |
9894 | 0 | osUnit == "nanometers") |
9895 | 0 | { |
9896 | 0 | dfConvToUM = 1e-3; |
9897 | 0 | } |
9898 | 0 | else if (!(osUnit == "um" || osUnit == "micrometre" || |
9899 | 0 | osUnit == "micrometres" || osUnit == "micrometer" || |
9900 | 0 | osUnit == "micrometers")) |
9901 | 0 | { |
9902 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
9903 | 0 | "Unhandled value for " |
9904 | 0 | "BAND_IMAGERY_METADATA[\"%s\"][\"unit\"] = %s", |
9905 | 0 | oJsonItem.GetName().c_str(), osUnit.c_str()); |
9906 | 0 | return nullptr; |
9907 | 0 | } |
9908 | | |
9909 | 0 | BandImageryMetadata &item = aoBandImageryMetadata[iExtraDimIdx]; |
9910 | |
|
9911 | 0 | std::shared_ptr<GDALAbstractMDArray> abstractArray; |
9912 | 0 | if (poArray) |
9913 | 0 | abstractArray = std::move(poArray); |
9914 | 0 | else |
9915 | 0 | abstractArray = std::move(poAttribute); |
9916 | 0 | if (oJsonItem.GetName() == "CENTRAL_WAVELENGTH_UM") |
9917 | 0 | { |
9918 | 0 | item.poCentralWavelengthArray = std::move(abstractArray); |
9919 | 0 | item.dfCentralWavelengthToMicrometer = dfConvToUM; |
9920 | 0 | } |
9921 | 0 | else |
9922 | 0 | { |
9923 | 0 | item.poFWHMArray = std::move(abstractArray); |
9924 | 0 | item.dfFWHMToMicrometer = dfConvToUM; |
9925 | 0 | } |
9926 | 0 | } |
9927 | 0 | else |
9928 | 0 | { |
9929 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
9930 | 0 | "Ignored member \"%s\" in BAND_IMAGERY_METADATA", |
9931 | 0 | oJsonItem.GetName().c_str()); |
9932 | 0 | } |
9933 | 0 | } |
9934 | 0 | } |
9935 | | |
9936 | 0 | auto poDS = std::make_unique<GDALDatasetFromArray>(array, iXDim, iYDim); |
9937 | |
|
9938 | 0 | poDS->eAccess = array->IsWritable() ? GA_Update : GA_ReadOnly; |
9939 | |
|
9940 | 0 | poDS->nRasterYSize = |
9941 | 0 | nDimCount < 2 ? 1 |
9942 | 0 | : static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX), |
9943 | 0 | dims[iYDim]->GetSize())); |
9944 | 0 | poDS->nRasterXSize = static_cast<int>( |
9945 | 0 | std::min(static_cast<GUInt64>(INT_MAX), dims[iXDim]->GetSize())); |
9946 | |
|
9947 | 0 | std::vector<GUInt64> anOtherDimCoord(nNewDimCount); |
9948 | 0 | std::vector<GUInt64> anStackIters(nDimCount); |
9949 | 0 | std::vector<size_t> anMapNewToOld(nNewDimCount); |
9950 | 0 | for (size_t i = 0, j = 0; i < nDimCount; ++i) |
9951 | 0 | { |
9952 | 0 | if (i != iXDim && !(nDimCount >= 2 && i == iYDim)) |
9953 | 0 | { |
9954 | 0 | anMapNewToOld[j] = i; |
9955 | 0 | j++; |
9956 | 0 | } |
9957 | 0 | } |
9958 | |
|
9959 | 0 | poDS->m_bHasGT = array->GuessGeoTransform(iXDim, iYDim, false, poDS->m_gt); |
9960 | |
|
9961 | 0 | const auto attrs(array->GetAttributes()); |
9962 | 0 | for (const auto &attr : attrs) |
9963 | 0 | { |
9964 | 0 | if (attr->GetName() != "COLOR_INTERPRETATION") |
9965 | 0 | { |
9966 | 0 | auto stringArray = attr->ReadAsStringArray(); |
9967 | 0 | std::string val; |
9968 | 0 | if (stringArray.size() > 1) |
9969 | 0 | { |
9970 | 0 | val += '{'; |
9971 | 0 | } |
9972 | 0 | for (int i = 0; i < stringArray.size(); ++i) |
9973 | 0 | { |
9974 | 0 | if (i > 0) |
9975 | 0 | val += ','; |
9976 | 0 | val += stringArray[i]; |
9977 | 0 | } |
9978 | 0 | if (stringArray.size() > 1) |
9979 | 0 | { |
9980 | 0 | val += '}'; |
9981 | 0 | } |
9982 | 0 | poDS->m_oMDD.SetMetadataItem(attr->GetName().c_str(), val.c_str()); |
9983 | 0 | } |
9984 | 0 | } |
9985 | |
|
9986 | 0 | const char *pszDelay = CSLFetchNameValueDef( |
9987 | 0 | papszOptions, "LOAD_EXTRA_DIM_METADATA_DELAY", |
9988 | 0 | CPLGetConfigOption("GDAL_LOAD_EXTRA_DIM_METADATA_DELAY", "5")); |
9989 | 0 | const double dfDelay = |
9990 | 0 | EQUAL(pszDelay, "unlimited") ? -1 : CPLAtof(pszDelay); |
9991 | 0 | const auto nStartTime = time(nullptr); |
9992 | 0 | bool bHasWarned = false; |
9993 | | // Instantiate bands by iterating over non-XY variables |
9994 | 0 | size_t iDim = 0; |
9995 | 0 | int nCurBand = 1; |
9996 | 0 | lbl_next_depth: |
9997 | 0 | if (iDim < nNewDimCount) |
9998 | 0 | { |
9999 | 0 | anStackIters[iDim] = dims[anMapNewToOld[iDim]]->GetSize(); |
10000 | 0 | anOtherDimCoord[iDim] = 0; |
10001 | 0 | while (true) |
10002 | 0 | { |
10003 | 0 | ++iDim; |
10004 | 0 | goto lbl_next_depth; |
10005 | 0 | lbl_return_to_caller: |
10006 | 0 | --iDim; |
10007 | 0 | --anStackIters[iDim]; |
10008 | 0 | if (anStackIters[iDim] == 0) |
10009 | 0 | break; |
10010 | 0 | ++anOtherDimCoord[iDim]; |
10011 | 0 | } |
10012 | 0 | } |
10013 | 0 | else |
10014 | 0 | { |
10015 | 0 | poDS->SetBand(nCurBand, |
10016 | 0 | new GDALRasterBandFromArray( |
10017 | 0 | poDS.get(), anOtherDimCoord, |
10018 | 0 | aoBandParameterMetadataItems, aoBandImageryMetadata, |
10019 | 0 | dfDelay, nStartTime, bHasWarned)); |
10020 | 0 | ++nCurBand; |
10021 | 0 | } |
10022 | 0 | if (iDim > 0) |
10023 | 0 | goto lbl_return_to_caller; |
10024 | | |
10025 | 0 | if (!array->GetFilename().empty()) |
10026 | 0 | { |
10027 | 0 | poDS->SetPhysicalFilename(array->GetFilename().c_str()); |
10028 | 0 | std::string osDerivedDatasetName( |
10029 | 0 | CPLSPrintf("AsClassicDataset(%d,%d) view of %s", int(iXDim), |
10030 | 0 | int(iYDim), array->GetFullName().c_str())); |
10031 | 0 | if (!array->GetContext().empty()) |
10032 | 0 | { |
10033 | 0 | osDerivedDatasetName += " with context "; |
10034 | 0 | osDerivedDatasetName += array->GetContext(); |
10035 | 0 | } |
10036 | 0 | poDS->SetDerivedDatasetName(osDerivedDatasetName.c_str()); |
10037 | 0 | poDS->TryLoadXML(); |
10038 | |
|
10039 | 0 | for (const auto &[pszKey, pszValue] : |
10040 | 0 | cpl::IterateNameValue(static_cast<CSLConstList>( |
10041 | 0 | poDS->GDALPamDataset::GetMetadata()))) |
10042 | 0 | { |
10043 | 0 | poDS->m_oMDD.SetMetadataItem(pszKey, pszValue); |
10044 | 0 | } |
10045 | 0 | } |
10046 | |
|
10047 | 0 | return poDS.release(); |
10048 | 0 | } |
10049 | | |
10050 | | /************************************************************************/ |
10051 | | /* AsClassicDataset() */ |
10052 | | /************************************************************************/ |
10053 | | |
10054 | | /** Return a view of this array as a "classic" GDALDataset (ie 2D) |
10055 | | * |
10056 | | * In the case of > 2D arrays, additional dimensions will be represented as |
10057 | | * raster bands. |
10058 | | * |
10059 | | * The "reverse" method is GDALRasterBand::AsMDArray(). |
10060 | | * |
10061 | | * This is the same as the C function GDALMDArrayAsClassicDataset(). |
10062 | | * |
10063 | | * @param iXDim Index of the dimension that will be used as the X/width axis. |
10064 | | * @param iYDim Index of the dimension that will be used as the Y/height axis. |
10065 | | * Ignored if the dimension count is 1. |
10066 | | * @param poRootGroup (Added in GDAL 3.8) Root group. Used with the BAND_METADATA |
10067 | | * and BAND_IMAGERY_METADATA option. |
10068 | | * @param papszOptions (Added in GDAL 3.8) Null-terminated list of options, or |
10069 | | * nullptr. Current supported options are: |
10070 | | * <ul> |
10071 | | * <li>BAND_METADATA: JSON serialized array defining which |
10072 | | * arrays of the poRootGroup, indexed by non-X and Y |
10073 | | * dimensions, should be mapped as band metadata items. |
10074 | | * Each array item should be an object with the |
10075 | | * following members: |
10076 | | * - "array": full name of a band parameter array. |
10077 | | * Such array must be a one |
10078 | | * dimensional array, and its dimension must be one of |
10079 | | * the dimensions of the array on which the method is |
10080 | | * called (excluding the X and Y dimensions). |
10081 | | * - "attribute": name relative to *this array or full |
10082 | | * name of a single dimension numeric array whose size |
10083 | | * must be one of the dimensions of *this array |
10084 | | * (excluding the X and Y dimensions). |
10085 | | * "array" and "attribute" are mutually exclusive. |
10086 | | * - "item_name": band metadata item name |
10087 | | * - "item_value": (optional) String, where "%[x][.y]f", |
10088 | | * "%[x][.y]g" or "%s" printf-like formatting can be |
10089 | | * used to format the corresponding value of the |
10090 | | * parameter array. The percentage character should be |
10091 | | * repeated: "%%" |
10092 | | * "${attribute_name}" can also be used to include the |
10093 | | * value of an attribute for "array" when set and if |
10094 | | * not starting with '/'. Otherwise if starting with |
10095 | | * '/', it is the full path to the attribute. |
10096 | | * |
10097 | | * If "item_value" is not provided, a default formatting |
10098 | | * of the value will be applied. |
10099 | | * |
10100 | | * Example: |
10101 | | * [ |
10102 | | * { |
10103 | | * "array": "/sensor_band_parameters/wavelengths", |
10104 | | * "item_name": "WAVELENGTH", |
10105 | | * "item_value": "%.1f ${units}" |
10106 | | * }, |
10107 | | * { |
10108 | | * "array": "/sensor_band_parameters/fwhm", |
10109 | | * "item_name": "FWHM" |
10110 | | * }, |
10111 | | * { |
10112 | | * "array": "/sensor_band_parameters/fwhm", |
10113 | | * "item_name": "FWHM_UNIT", |
10114 | | * "item_value": "${units}" |
10115 | | * } |
10116 | | * ] |
10117 | | * |
10118 | | * Example for Planet Labs Tanager radiance products: |
10119 | | * [ |
10120 | | * { |
10121 | | * "attribute": "center_wavelengths", |
10122 | | * "item_name": "WAVELENGTH", |
10123 | | * "item_value": "%.1f ${center_wavelengths_units}" |
10124 | | * } |
10125 | | * ] |
10126 | | * |
10127 | | * </li> |
10128 | | * <li>BAND_IMAGERY_METADATA: (GDAL >= 3.11) |
10129 | | * JSON serialized object defining which arrays of the |
10130 | | * poRootGroup, indexed by non-X and Y dimensions, |
10131 | | * should be mapped as band metadata items in the |
10132 | | * band IMAGERY domain. |
10133 | | * The object currently accepts 2 members: |
10134 | | * - "CENTRAL_WAVELENGTH_UM": Central Wavelength in |
10135 | | * micrometers. |
10136 | | * - "FWHM_UM": Full-width half-maximum |
10137 | | * in micrometers. |
10138 | | * The value of each member should be an object with the |
10139 | | * following members: |
10140 | | * - "array": full name of a band parameter array. |
10141 | | * Such array must be a one dimensional array, and its |
10142 | | * dimension must be one of the dimensions of the |
10143 | | * array on which the method is called |
10144 | | * (excluding the X and Y dimensions). |
10145 | | * - "attribute": name relative to *this array or full |
10146 | | * name of a single dimension numeric array whose size |
10147 | | * must be one of the dimensions of *this array |
10148 | | * (excluding the X and Y dimensions). |
10149 | | * "array" and "attribute" are mutually exclusive, |
10150 | | * and one of them is required. |
10151 | | * - "unit": (optional) unit of the values pointed in |
10152 | | * the above array. |
10153 | | * Can be a literal string or a string of the form |
10154 | | * "${attribute_name}" to point to an attribute for |
10155 | | * "array" when set and if no starting |
10156 | | * with '/'. Otherwise if starting with '/', it is |
10157 | | * the full path to the attribute. |
10158 | | * Accepted values are "um", "micrometer" |
10159 | | * (with UK vs US spelling, singular or plural), "nm", |
10160 | | * "nanometer" (with UK vs US spelling, singular or |
10161 | | * plural) |
10162 | | * If not provided, micrometer is assumed. |
10163 | | * |
10164 | | * Example for EMIT datasets: |
10165 | | * { |
10166 | | * "CENTRAL_WAVELENGTH_UM": { |
10167 | | * "array": "/sensor_band_parameters/wavelengths", |
10168 | | * "unit": "${units}" |
10169 | | * }, |
10170 | | * "FWHM_UM": { |
10171 | | * "array": "/sensor_band_parameters/fwhm", |
10172 | | * "unit": "${units}" |
10173 | | * } |
10174 | | * } |
10175 | | * |
10176 | | * Example for Planet Labs Tanager radiance products: |
10177 | | * { |
10178 | | * "CENTRAL_WAVELENGTH_UM": { |
10179 | | * "attribute": "center_wavelengths", |
10180 | | * "unit": "${center_wavelengths_units}" |
10181 | | * }, |
10182 | | * "FWHM_UM": { |
10183 | | * "attribute": "fwhm", |
10184 | | * "unit": "${fwhm_units}" |
10185 | | * } |
10186 | | * } |
10187 | | * |
10188 | | * </li> |
10189 | | * <li>LOAD_EXTRA_DIM_METADATA_DELAY: Maximum delay in |
10190 | | * seconds allowed to set the DIM_{dimname}_VALUE band |
10191 | | * metadata items from the indexing variable of the |
10192 | | * dimensions. |
10193 | | * Default value is 5. 'unlimited' can be used to mean |
10194 | | * unlimited delay. Can also be defined globally with |
10195 | | * the GDAL_LOAD_EXTRA_DIM_METADATA_DELAY configuration |
10196 | | * option.</li> |
10197 | | * </ul> |
10198 | | * @return a new GDALDataset that must be freed with GDALClose(), or nullptr |
10199 | | */ |
10200 | | GDALDataset * |
10201 | | GDALMDArray::AsClassicDataset(size_t iXDim, size_t iYDim, |
10202 | | const std::shared_ptr<GDALGroup> &poRootGroup, |
10203 | | CSLConstList papszOptions) const |
10204 | 0 | { |
10205 | 0 | auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock()); |
10206 | 0 | if (!self) |
10207 | 0 | { |
10208 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
10209 | 0 | "Driver implementation issue: m_pSelf not set !"); |
10210 | 0 | return nullptr; |
10211 | 0 | } |
10212 | 0 | return GDALDatasetFromArray::Create(self, iXDim, iYDim, poRootGroup, |
10213 | 0 | papszOptions); |
10214 | 0 | } |
10215 | | |
10216 | | /************************************************************************/ |
10217 | | /* GetStatistics() */ |
10218 | | /************************************************************************/ |
10219 | | |
10220 | | /** |
10221 | | * \brief Fetch statistics. |
10222 | | * |
10223 | | * Returns the minimum, maximum, mean and standard deviation of all |
10224 | | * pixel values in this array. |
10225 | | * |
10226 | | * If bForce is FALSE results will only be returned if it can be done |
10227 | | * quickly (i.e. without scanning the data). If bForce is FALSE and |
10228 | | * results cannot be returned efficiently, the method will return CE_Warning |
10229 | | * but no warning will have been issued. This is a non-standard use of |
10230 | | * the CE_Warning return value to indicate "nothing done". |
10231 | | * |
10232 | | * When cached statistics are not available, and bForce is TRUE, |
10233 | | * ComputeStatistics() is called. |
10234 | | * |
10235 | | * Note that file formats using PAM (Persistent Auxiliary Metadata) services |
10236 | | * will generally cache statistics in the .aux.xml file allowing fast fetch |
10237 | | * after the first request. |
10238 | | * |
10239 | | * Cached statistics can be cleared with GDALDataset::ClearStatistics(). |
10240 | | * |
10241 | | * This method is the same as the C function GDALMDArrayGetStatistics(). |
10242 | | * |
10243 | | * @param bApproxOK Currently ignored. In the future, should be set to true |
10244 | | * if statistics on the whole array are wished, or to false if a subset of it |
10245 | | * may be used. |
10246 | | * |
10247 | | * @param bForce If false statistics will only be returned if it can |
10248 | | * be done without rescanning the image. |
10249 | | * |
10250 | | * @param pdfMin Location into which to load image minimum (may be NULL). |
10251 | | * |
10252 | | * @param pdfMax Location into which to load image maximum (may be NULL).- |
10253 | | * |
10254 | | * @param pdfMean Location into which to load image mean (may be NULL). |
10255 | | * |
10256 | | * @param pdfStdDev Location into which to load image standard deviation |
10257 | | * (may be NULL). |
10258 | | * |
10259 | | * @param pnValidCount Number of samples whose value is different from the |
10260 | | * nodata value. (may be NULL) |
10261 | | * |
10262 | | * @param pfnProgress a function to call to report progress, or NULL. |
10263 | | * |
10264 | | * @param pProgressData application data to pass to the progress function. |
10265 | | * |
10266 | | * @return CE_None on success, CE_Warning if no values returned, |
10267 | | * CE_Failure if an error occurs. |
10268 | | * |
10269 | | * @since GDAL 3.2 |
10270 | | */ |
10271 | | |
10272 | | CPLErr GDALMDArray::GetStatistics(bool bApproxOK, bool bForce, double *pdfMin, |
10273 | | double *pdfMax, double *pdfMean, |
10274 | | double *pdfStdDev, GUInt64 *pnValidCount, |
10275 | | GDALProgressFunc pfnProgress, |
10276 | | void *pProgressData) |
10277 | 0 | { |
10278 | 0 | if (!bForce) |
10279 | 0 | return CE_Warning; |
10280 | | |
10281 | 0 | return ComputeStatistics(bApproxOK, pdfMin, pdfMax, pdfMean, pdfStdDev, |
10282 | 0 | pnValidCount, pfnProgress, pProgressData, nullptr) |
10283 | 0 | ? CE_None |
10284 | 0 | : CE_Failure; |
10285 | 0 | } |
10286 | | |
10287 | | /************************************************************************/ |
10288 | | /* ComputeStatistics() */ |
10289 | | /************************************************************************/ |
10290 | | |
10291 | | /** |
10292 | | * \brief Compute statistics. |
10293 | | * |
10294 | | * Returns the minimum, maximum, mean and standard deviation of all |
10295 | | * pixel values in this array. |
10296 | | * |
10297 | | * Pixels taken into account in statistics are those whose mask value |
10298 | | * (as determined by GetMask()) is non-zero. |
10299 | | * |
10300 | | * Once computed, the statistics will generally be "set" back on the |
10301 | | * owing dataset. |
10302 | | * |
10303 | | * Cached statistics can be cleared with GDALDataset::ClearStatistics(). |
10304 | | * |
10305 | | * This method is the same as the C functions GDALMDArrayComputeStatistics(). |
10306 | | * and GDALMDArrayComputeStatisticsEx(). |
10307 | | * |
10308 | | * @param bApproxOK Currently ignored. In the future, should be set to true |
10309 | | * if statistics on the whole array are wished, or to false if a subset of it |
10310 | | * may be used. |
10311 | | * |
10312 | | * @param pdfMin Location into which to load image minimum (may be NULL). |
10313 | | * |
10314 | | * @param pdfMax Location into which to load image maximum (may be NULL).- |
10315 | | * |
10316 | | * @param pdfMean Location into which to load image mean (may be NULL). |
10317 | | * |
10318 | | * @param pdfStdDev Location into which to load image standard deviation |
10319 | | * (may be NULL). |
10320 | | * |
10321 | | * @param pnValidCount Number of samples whose value is different from the |
10322 | | * nodata value. (may be NULL) |
10323 | | * |
10324 | | * @param pfnProgress a function to call to report progress, or NULL. |
10325 | | * |
10326 | | * @param pProgressData application data to pass to the progress function. |
10327 | | * |
10328 | | * @param papszOptions NULL-terminated list of options, of NULL. Added in 3.8. |
10329 | | * Options are driver specific. For now the netCDF and Zarr |
10330 | | * drivers recognize UPDATE_METADATA=YES, whose effect is |
10331 | | * to add or update the actual_range attribute with the |
10332 | | * computed min/max, only if done on the full array, in non |
10333 | | * approximate mode, and the dataset is opened in update |
10334 | | * mode. |
10335 | | * |
10336 | | * @return true on success |
10337 | | * |
10338 | | * @since GDAL 3.2 |
10339 | | */ |
10340 | | |
10341 | | bool GDALMDArray::ComputeStatistics(bool bApproxOK, double *pdfMin, |
10342 | | double *pdfMax, double *pdfMean, |
10343 | | double *pdfStdDev, GUInt64 *pnValidCount, |
10344 | | GDALProgressFunc pfnProgress, |
10345 | | void *pProgressData, |
10346 | | CSLConstList papszOptions) |
10347 | 0 | { |
10348 | 0 | struct StatsPerChunkType |
10349 | 0 | { |
10350 | 0 | const GDALMDArray *array = nullptr; |
10351 | 0 | std::shared_ptr<GDALMDArray> poMask{}; |
10352 | 0 | double dfMin = cpl::NumericLimits<double>::max(); |
10353 | 0 | double dfMax = -cpl::NumericLimits<double>::max(); |
10354 | 0 | double dfMean = 0.0; |
10355 | 0 | double dfM2 = 0.0; |
10356 | 0 | GUInt64 nValidCount = 0; |
10357 | 0 | std::vector<GByte> abyData{}; |
10358 | 0 | std::vector<double> adfData{}; |
10359 | 0 | std::vector<GByte> abyMaskData{}; |
10360 | 0 | GDALProgressFunc pfnProgress = nullptr; |
10361 | 0 | void *pProgressData = nullptr; |
10362 | 0 | }; |
10363 | |
|
10364 | 0 | const auto PerChunkFunc = [](GDALAbstractMDArray *, |
10365 | 0 | const GUInt64 *chunkArrayStartIdx, |
10366 | 0 | const size_t *chunkCount, GUInt64 iCurChunk, |
10367 | 0 | GUInt64 nChunkCount, void *pUserData) |
10368 | 0 | { |
10369 | 0 | StatsPerChunkType *data = static_cast<StatsPerChunkType *>(pUserData); |
10370 | 0 | const GDALMDArray *array = data->array; |
10371 | 0 | const GDALMDArray *poMask = data->poMask.get(); |
10372 | 0 | const size_t nDims = array->GetDimensionCount(); |
10373 | 0 | size_t nVals = 1; |
10374 | 0 | for (size_t i = 0; i < nDims; i++) |
10375 | 0 | nVals *= chunkCount[i]; |
10376 | | |
10377 | | // Get mask |
10378 | 0 | data->abyMaskData.resize(nVals); |
10379 | 0 | if (!(poMask->Read(chunkArrayStartIdx, chunkCount, nullptr, nullptr, |
10380 | 0 | poMask->GetDataType(), &data->abyMaskData[0]))) |
10381 | 0 | { |
10382 | 0 | return false; |
10383 | 0 | } |
10384 | | |
10385 | | // Get data |
10386 | 0 | const auto &oType = array->GetDataType(); |
10387 | 0 | if (oType.GetNumericDataType() == GDT_Float64) |
10388 | 0 | { |
10389 | 0 | data->adfData.resize(nVals); |
10390 | 0 | if (!array->Read(chunkArrayStartIdx, chunkCount, nullptr, nullptr, |
10391 | 0 | oType, &data->adfData[0])) |
10392 | 0 | { |
10393 | 0 | return false; |
10394 | 0 | } |
10395 | 0 | } |
10396 | 0 | else |
10397 | 0 | { |
10398 | 0 | data->abyData.resize(nVals * oType.GetSize()); |
10399 | 0 | if (!array->Read(chunkArrayStartIdx, chunkCount, nullptr, nullptr, |
10400 | 0 | oType, &data->abyData[0])) |
10401 | 0 | { |
10402 | 0 | return false; |
10403 | 0 | } |
10404 | 0 | data->adfData.resize(nVals); |
10405 | 0 | GDALCopyWords64(&data->abyData[0], oType.GetNumericDataType(), |
10406 | 0 | static_cast<int>(oType.GetSize()), |
10407 | 0 | &data->adfData[0], GDT_Float64, |
10408 | 0 | static_cast<int>(sizeof(double)), |
10409 | 0 | static_cast<GPtrDiff_t>(nVals)); |
10410 | 0 | } |
10411 | 0 | for (size_t i = 0; i < nVals; i++) |
10412 | 0 | { |
10413 | 0 | if (data->abyMaskData[i]) |
10414 | 0 | { |
10415 | 0 | const double dfValue = data->adfData[i]; |
10416 | 0 | data->dfMin = std::min(data->dfMin, dfValue); |
10417 | 0 | data->dfMax = std::max(data->dfMax, dfValue); |
10418 | 0 | data->nValidCount++; |
10419 | 0 | const double dfDelta = dfValue - data->dfMean; |
10420 | 0 | data->dfMean += dfDelta / data->nValidCount; |
10421 | 0 | data->dfM2 += dfDelta * (dfValue - data->dfMean); |
10422 | 0 | } |
10423 | 0 | } |
10424 | 0 | if (data->pfnProgress && |
10425 | 0 | !data->pfnProgress(static_cast<double>(iCurChunk + 1) / nChunkCount, |
10426 | 0 | "", data->pProgressData)) |
10427 | 0 | { |
10428 | 0 | return false; |
10429 | 0 | } |
10430 | 0 | return true; |
10431 | 0 | }; |
10432 | |
|
10433 | 0 | const auto &oType = GetDataType(); |
10434 | 0 | if (oType.GetClass() != GEDTC_NUMERIC || |
10435 | 0 | GDALDataTypeIsComplex(oType.GetNumericDataType())) |
10436 | 0 | { |
10437 | 0 | CPLError( |
10438 | 0 | CE_Failure, CPLE_NotSupported, |
10439 | 0 | "Statistics can only be computed on non-complex numeric data type"); |
10440 | 0 | return false; |
10441 | 0 | } |
10442 | | |
10443 | 0 | const size_t nDims = GetDimensionCount(); |
10444 | 0 | std::vector<GUInt64> arrayStartIdx(nDims); |
10445 | 0 | std::vector<GUInt64> count(nDims); |
10446 | 0 | const auto &poDims = GetDimensions(); |
10447 | 0 | for (size_t i = 0; i < nDims; i++) |
10448 | 0 | { |
10449 | 0 | count[i] = poDims[i]->GetSize(); |
10450 | 0 | } |
10451 | 0 | const char *pszSwathSize = CPLGetConfigOption("GDAL_SWATH_SIZE", nullptr); |
10452 | 0 | const size_t nMaxChunkSize = |
10453 | 0 | pszSwathSize |
10454 | 0 | ? static_cast<size_t>( |
10455 | 0 | std::min(GIntBig(std::numeric_limits<size_t>::max() / 2), |
10456 | 0 | CPLAtoGIntBig(pszSwathSize))) |
10457 | 0 | : static_cast<size_t>( |
10458 | 0 | std::min(GIntBig(std::numeric_limits<size_t>::max() / 2), |
10459 | 0 | GDALGetCacheMax64() / 4)); |
10460 | 0 | StatsPerChunkType sData; |
10461 | 0 | sData.array = this; |
10462 | 0 | sData.poMask = GetMask(nullptr); |
10463 | 0 | if (sData.poMask == nullptr) |
10464 | 0 | { |
10465 | 0 | return false; |
10466 | 0 | } |
10467 | 0 | sData.pfnProgress = pfnProgress; |
10468 | 0 | sData.pProgressData = pProgressData; |
10469 | 0 | if (!ProcessPerChunk(arrayStartIdx.data(), count.data(), |
10470 | 0 | GetProcessingChunkSize(nMaxChunkSize).data(), |
10471 | 0 | PerChunkFunc, &sData)) |
10472 | 0 | { |
10473 | 0 | return false; |
10474 | 0 | } |
10475 | | |
10476 | 0 | if (pdfMin) |
10477 | 0 | *pdfMin = sData.dfMin; |
10478 | |
|
10479 | 0 | if (pdfMax) |
10480 | 0 | *pdfMax = sData.dfMax; |
10481 | |
|
10482 | 0 | if (pdfMean) |
10483 | 0 | *pdfMean = sData.dfMean; |
10484 | |
|
10485 | 0 | const double dfStdDev = |
10486 | 0 | sData.nValidCount > 0 ? sqrt(sData.dfM2 / sData.nValidCount) : 0.0; |
10487 | 0 | if (pdfStdDev) |
10488 | 0 | *pdfStdDev = dfStdDev; |
10489 | |
|
10490 | 0 | if (pnValidCount) |
10491 | 0 | *pnValidCount = sData.nValidCount; |
10492 | |
|
10493 | 0 | SetStatistics(bApproxOK, sData.dfMin, sData.dfMax, sData.dfMean, dfStdDev, |
10494 | 0 | sData.nValidCount, papszOptions); |
10495 | |
|
10496 | 0 | return true; |
10497 | 0 | } |
10498 | | |
10499 | | /************************************************************************/ |
10500 | | /* SetStatistics() */ |
10501 | | /************************************************************************/ |
10502 | | //! @cond Doxygen_Suppress |
10503 | | bool GDALMDArray::SetStatistics(bool /* bApproxStats */, double /* dfMin */, |
10504 | | double /* dfMax */, double /* dfMean */, |
10505 | | double /* dfStdDev */, |
10506 | | GUInt64 /* nValidCount */, |
10507 | | CSLConstList /* papszOptions */) |
10508 | 0 | { |
10509 | 0 | CPLDebug("GDAL", "Cannot save statistics on a non-PAM MDArray"); |
10510 | 0 | return false; |
10511 | 0 | } |
10512 | | |
10513 | | //! @endcond |
10514 | | |
10515 | | /************************************************************************/ |
10516 | | /* ClearStatistics() */ |
10517 | | /************************************************************************/ |
10518 | | |
10519 | | /** |
10520 | | * \brief Clear statistics. |
10521 | | * |
10522 | | * @since GDAL 3.4 |
10523 | | */ |
10524 | | void GDALMDArray::ClearStatistics() |
10525 | 0 | { |
10526 | 0 | } |
10527 | | |
10528 | | /************************************************************************/ |
10529 | | /* GetCoordinateVariables() */ |
10530 | | /************************************************************************/ |
10531 | | |
10532 | | /** |
10533 | | * \brief Return coordinate variables. |
10534 | | * |
10535 | | * Coordinate variables are an alternate way of indexing an array that can |
10536 | | * be sometimes used. For example, an array collected through remote sensing |
10537 | | * might be indexed by (scanline, pixel). But there can be |
10538 | | * a longitude and latitude arrays alongside that are also both indexed by |
10539 | | * (scanline, pixel), and are referenced from operational arrays for |
10540 | | * reprojection purposes. |
10541 | | * |
10542 | | * For netCDF, this will return the arrays referenced by the "coordinates" |
10543 | | * attribute. |
10544 | | * |
10545 | | * This method is the same as the C function |
10546 | | * GDALMDArrayGetCoordinateVariables(). |
10547 | | * |
10548 | | * @return a vector of arrays |
10549 | | * |
10550 | | * @since GDAL 3.4 |
10551 | | */ |
10552 | | |
10553 | | std::vector<std::shared_ptr<GDALMDArray>> |
10554 | | GDALMDArray::GetCoordinateVariables() const |
10555 | 0 | { |
10556 | 0 | return {}; |
10557 | 0 | } |
10558 | | |
10559 | | /************************************************************************/ |
10560 | | /* ~GDALExtendedDataType() */ |
10561 | | /************************************************************************/ |
10562 | | |
10563 | 0 | GDALExtendedDataType::~GDALExtendedDataType() = default; |
10564 | | |
10565 | | /************************************************************************/ |
10566 | | /* GDALExtendedDataType() */ |
10567 | | /************************************************************************/ |
10568 | | |
10569 | | GDALExtendedDataType::GDALExtendedDataType(size_t nMaxStringLength, |
10570 | | GDALExtendedDataTypeSubType eSubType) |
10571 | 0 | : m_eClass(GEDTC_STRING), m_eSubType(eSubType), m_nSize(sizeof(char *)), |
10572 | 0 | m_nMaxStringLength(nMaxStringLength) |
10573 | 0 | { |
10574 | 0 | } |
10575 | | |
10576 | | /************************************************************************/ |
10577 | | /* GDALExtendedDataType() */ |
10578 | | /************************************************************************/ |
10579 | | |
10580 | | GDALExtendedDataType::GDALExtendedDataType(GDALDataType eType) |
10581 | 0 | : m_eClass(GEDTC_NUMERIC), m_eNumericDT(eType), |
10582 | 0 | m_nSize(GDALGetDataTypeSizeBytes(eType)) |
10583 | 0 | { |
10584 | 0 | } |
10585 | | |
10586 | | /************************************************************************/ |
10587 | | /* GDALExtendedDataType() */ |
10588 | | /************************************************************************/ |
10589 | | |
10590 | | GDALExtendedDataType::GDALExtendedDataType( |
10591 | | const std::string &osName, GDALDataType eBaseType, |
10592 | | std::unique_ptr<GDALRasterAttributeTable> poRAT) |
10593 | 0 | : m_osName(osName), m_eClass(GEDTC_NUMERIC), m_eNumericDT(eBaseType), |
10594 | 0 | m_nSize(GDALGetDataTypeSizeBytes(eBaseType)), m_poRAT(std::move(poRAT)) |
10595 | 0 | { |
10596 | 0 | } |
10597 | | |
10598 | | /************************************************************************/ |
10599 | | /* GDALExtendedDataType() */ |
10600 | | /************************************************************************/ |
10601 | | |
10602 | | GDALExtendedDataType::GDALExtendedDataType( |
10603 | | const std::string &osName, size_t nTotalSize, |
10604 | | std::vector<std::unique_ptr<GDALEDTComponent>> &&components) |
10605 | 0 | : m_osName(osName), m_eClass(GEDTC_COMPOUND), |
10606 | 0 | m_aoComponents(std::move(components)), m_nSize(nTotalSize) |
10607 | 0 | { |
10608 | 0 | } |
10609 | | |
10610 | | /************************************************************************/ |
10611 | | /* GDALExtendedDataType() */ |
10612 | | /************************************************************************/ |
10613 | | |
10614 | | /** Move constructor. */ |
10615 | 0 | GDALExtendedDataType::GDALExtendedDataType(GDALExtendedDataType &&) = default; |
10616 | | |
10617 | | /************************************************************************/ |
10618 | | /* GDALExtendedDataType() */ |
10619 | | /************************************************************************/ |
10620 | | |
10621 | | /** Copy constructor. */ |
10622 | | GDALExtendedDataType::GDALExtendedDataType(const GDALExtendedDataType &other) |
10623 | 0 | : m_osName(other.m_osName), m_eClass(other.m_eClass), |
10624 | 0 | m_eSubType(other.m_eSubType), m_eNumericDT(other.m_eNumericDT), |
10625 | 0 | m_nSize(other.m_nSize), m_nMaxStringLength(other.m_nMaxStringLength), |
10626 | 0 | m_poRAT(other.m_poRAT ? other.m_poRAT->Clone() : nullptr) |
10627 | 0 | { |
10628 | 0 | if (m_eClass == GEDTC_COMPOUND) |
10629 | 0 | { |
10630 | 0 | for (const auto &elt : other.m_aoComponents) |
10631 | 0 | { |
10632 | 0 | m_aoComponents.emplace_back(new GDALEDTComponent(*elt)); |
10633 | 0 | } |
10634 | 0 | } |
10635 | 0 | } |
10636 | | |
10637 | | /************************************************************************/ |
10638 | | /* operator= () */ |
10639 | | /************************************************************************/ |
10640 | | |
10641 | | /** Copy assignment. */ |
10642 | | GDALExtendedDataType & |
10643 | | GDALExtendedDataType::operator=(const GDALExtendedDataType &other) |
10644 | 0 | { |
10645 | 0 | if (this != &other) |
10646 | 0 | { |
10647 | 0 | m_osName = other.m_osName; |
10648 | 0 | m_eClass = other.m_eClass; |
10649 | 0 | m_eSubType = other.m_eSubType; |
10650 | 0 | m_eNumericDT = other.m_eNumericDT; |
10651 | 0 | m_nSize = other.m_nSize; |
10652 | 0 | m_nMaxStringLength = other.m_nMaxStringLength; |
10653 | 0 | m_poRAT.reset(other.m_poRAT ? other.m_poRAT->Clone() : nullptr); |
10654 | 0 | m_aoComponents.clear(); |
10655 | 0 | if (m_eClass == GEDTC_COMPOUND) |
10656 | 0 | { |
10657 | 0 | for (const auto &elt : other.m_aoComponents) |
10658 | 0 | { |
10659 | 0 | m_aoComponents.emplace_back(new GDALEDTComponent(*elt)); |
10660 | 0 | } |
10661 | 0 | } |
10662 | 0 | } |
10663 | 0 | return *this; |
10664 | 0 | } |
10665 | | |
10666 | | /************************************************************************/ |
10667 | | /* operator= () */ |
10668 | | /************************************************************************/ |
10669 | | |
10670 | | /** Move assignment. */ |
10671 | | GDALExtendedDataType & |
10672 | 0 | GDALExtendedDataType::operator=(GDALExtendedDataType &&other) = default; |
10673 | | |
10674 | | /************************************************************************/ |
10675 | | /* Create() */ |
10676 | | /************************************************************************/ |
10677 | | |
10678 | | /** Return a new GDALExtendedDataType of class GEDTC_NUMERIC. |
10679 | | * |
10680 | | * This is the same as the C function GDALExtendedDataTypeCreate() |
10681 | | * |
10682 | | * @param eType Numeric data type. Must be different from GDT_Unknown and |
10683 | | * GDT_TypeCount |
10684 | | */ |
10685 | | GDALExtendedDataType GDALExtendedDataType::Create(GDALDataType eType) |
10686 | 0 | { |
10687 | 0 | return GDALExtendedDataType(eType); |
10688 | 0 | } |
10689 | | |
10690 | | /************************************************************************/ |
10691 | | /* Create() */ |
10692 | | /************************************************************************/ |
10693 | | |
10694 | | /** Return a new GDALExtendedDataType from a raster attribute table. |
10695 | | * |
10696 | | * @param osName Type name |
10697 | | * @param eBaseType Base integer data type. |
10698 | | * @param poRAT Raster attribute table. Must not be NULL. |
10699 | | * @since 3.12 |
10700 | | */ |
10701 | | GDALExtendedDataType |
10702 | | GDALExtendedDataType::Create(const std::string &osName, GDALDataType eBaseType, |
10703 | | std::unique_ptr<GDALRasterAttributeTable> poRAT) |
10704 | 0 | { |
10705 | 0 | return GDALExtendedDataType(osName, eBaseType, std::move(poRAT)); |
10706 | 0 | } |
10707 | | |
10708 | | /************************************************************************/ |
10709 | | /* Create() */ |
10710 | | /************************************************************************/ |
10711 | | |
10712 | | /** Return a new GDALExtendedDataType of class GEDTC_COMPOUND. |
10713 | | * |
10714 | | * This is the same as the C function GDALExtendedDataTypeCreateCompound() |
10715 | | * |
10716 | | * @param osName Type name. |
10717 | | * @param nTotalSize Total size of the type in bytes. |
10718 | | * Should be large enough to store all components. |
10719 | | * @param components Components of the compound type. |
10720 | | */ |
10721 | | GDALExtendedDataType GDALExtendedDataType::Create( |
10722 | | const std::string &osName, size_t nTotalSize, |
10723 | | std::vector<std::unique_ptr<GDALEDTComponent>> &&components) |
10724 | 0 | { |
10725 | 0 | size_t nLastOffset = 0; |
10726 | | // Some arbitrary threshold to avoid potential integer overflows |
10727 | 0 | if (nTotalSize > static_cast<size_t>(std::numeric_limits<int>::max() / 2)) |
10728 | 0 | { |
10729 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid offset/size"); |
10730 | 0 | return GDALExtendedDataType(GDT_Unknown); |
10731 | 0 | } |
10732 | 0 | for (const auto &comp : components) |
10733 | 0 | { |
10734 | | // Check alignment too ? |
10735 | 0 | if (comp->GetOffset() < nLastOffset) |
10736 | 0 | { |
10737 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid offset/size"); |
10738 | 0 | return GDALExtendedDataType(GDT_Unknown); |
10739 | 0 | } |
10740 | 0 | nLastOffset = comp->GetOffset() + comp->GetType().GetSize(); |
10741 | 0 | } |
10742 | 0 | if (nTotalSize < nLastOffset) |
10743 | 0 | { |
10744 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid offset/size"); |
10745 | 0 | return GDALExtendedDataType(GDT_Unknown); |
10746 | 0 | } |
10747 | 0 | if (nTotalSize == 0 || components.empty()) |
10748 | 0 | { |
10749 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Empty compound not allowed"); |
10750 | 0 | return GDALExtendedDataType(GDT_Unknown); |
10751 | 0 | } |
10752 | 0 | return GDALExtendedDataType(osName, nTotalSize, std::move(components)); |
10753 | 0 | } |
10754 | | |
10755 | | /************************************************************************/ |
10756 | | /* Create() */ |
10757 | | /************************************************************************/ |
10758 | | |
10759 | | /** Return a new GDALExtendedDataType of class GEDTC_STRING. |
10760 | | * |
10761 | | * This is the same as the C function GDALExtendedDataTypeCreateString(). |
10762 | | * |
10763 | | * @param nMaxStringLength maximum length of a string in bytes. 0 if |
10764 | | * unknown/unlimited |
10765 | | * @param eSubType Subtype. |
10766 | | */ |
10767 | | GDALExtendedDataType |
10768 | | GDALExtendedDataType::CreateString(size_t nMaxStringLength, |
10769 | | GDALExtendedDataTypeSubType eSubType) |
10770 | 0 | { |
10771 | 0 | return GDALExtendedDataType(nMaxStringLength, eSubType); |
10772 | 0 | } |
10773 | | |
10774 | | /************************************************************************/ |
10775 | | /* operator==() */ |
10776 | | /************************************************************************/ |
10777 | | |
10778 | | /** Equality operator. |
10779 | | * |
10780 | | * This is the same as the C function GDALExtendedDataTypeEquals(). |
10781 | | */ |
10782 | | bool GDALExtendedDataType::operator==(const GDALExtendedDataType &other) const |
10783 | 0 | { |
10784 | 0 | if (m_eClass != other.m_eClass || m_eSubType != other.m_eSubType || |
10785 | 0 | m_nSize != other.m_nSize || m_osName != other.m_osName) |
10786 | 0 | { |
10787 | 0 | return false; |
10788 | 0 | } |
10789 | 0 | if (m_eClass == GEDTC_NUMERIC) |
10790 | 0 | { |
10791 | 0 | return m_eNumericDT == other.m_eNumericDT; |
10792 | 0 | } |
10793 | 0 | if (m_eClass == GEDTC_STRING) |
10794 | 0 | { |
10795 | 0 | return true; |
10796 | 0 | } |
10797 | 0 | CPLAssert(m_eClass == GEDTC_COMPOUND); |
10798 | 0 | if (m_aoComponents.size() != other.m_aoComponents.size()) |
10799 | 0 | { |
10800 | 0 | return false; |
10801 | 0 | } |
10802 | 0 | for (size_t i = 0; i < m_aoComponents.size(); i++) |
10803 | 0 | { |
10804 | 0 | if (!(*m_aoComponents[i] == *other.m_aoComponents[i])) |
10805 | 0 | { |
10806 | 0 | return false; |
10807 | 0 | } |
10808 | 0 | } |
10809 | 0 | return true; |
10810 | 0 | } |
10811 | | |
10812 | | /************************************************************************/ |
10813 | | /* CanConvertTo() */ |
10814 | | /************************************************************************/ |
10815 | | |
10816 | | /** Return whether this data type can be converted to the other one. |
10817 | | * |
10818 | | * This is the same as the C function GDALExtendedDataTypeCanConvertTo(). |
10819 | | * |
10820 | | * @param other Target data type for the conversion being considered. |
10821 | | */ |
10822 | | bool GDALExtendedDataType::CanConvertTo(const GDALExtendedDataType &other) const |
10823 | 0 | { |
10824 | 0 | if (m_eClass == GEDTC_NUMERIC) |
10825 | 0 | { |
10826 | 0 | if (m_eNumericDT == GDT_Unknown) |
10827 | 0 | return false; |
10828 | 0 | if (other.m_eClass == GEDTC_NUMERIC && |
10829 | 0 | other.m_eNumericDT == GDT_Unknown) |
10830 | 0 | return false; |
10831 | 0 | return other.m_eClass == GEDTC_NUMERIC || |
10832 | 0 | other.m_eClass == GEDTC_STRING; |
10833 | 0 | } |
10834 | 0 | if (m_eClass == GEDTC_STRING) |
10835 | 0 | { |
10836 | 0 | return other.m_eClass == m_eClass; |
10837 | 0 | } |
10838 | 0 | CPLAssert(m_eClass == GEDTC_COMPOUND); |
10839 | 0 | if (other.m_eClass != GEDTC_COMPOUND) |
10840 | 0 | return false; |
10841 | 0 | std::map<std::string, const std::unique_ptr<GDALEDTComponent> *> |
10842 | 0 | srcComponents; |
10843 | 0 | for (const auto &srcComp : m_aoComponents) |
10844 | 0 | { |
10845 | 0 | srcComponents[srcComp->GetName()] = &srcComp; |
10846 | 0 | } |
10847 | 0 | for (const auto &dstComp : other.m_aoComponents) |
10848 | 0 | { |
10849 | 0 | auto oIter = srcComponents.find(dstComp->GetName()); |
10850 | 0 | if (oIter == srcComponents.end()) |
10851 | 0 | return false; |
10852 | 0 | if (!(*(oIter->second))->GetType().CanConvertTo(dstComp->GetType())) |
10853 | 0 | return false; |
10854 | 0 | } |
10855 | 0 | return true; |
10856 | 0 | } |
10857 | | |
10858 | | /************************************************************************/ |
10859 | | /* NeedsFreeDynamicMemory() */ |
10860 | | /************************************************************************/ |
10861 | | |
10862 | | /** Return whether the data type holds dynamically allocated memory, that |
10863 | | * needs to be freed with FreeDynamicMemory(). |
10864 | | * |
10865 | | */ |
10866 | | bool GDALExtendedDataType::NeedsFreeDynamicMemory() const |
10867 | 0 | { |
10868 | 0 | switch (m_eClass) |
10869 | 0 | { |
10870 | 0 | case GEDTC_STRING: |
10871 | 0 | return true; |
10872 | | |
10873 | 0 | case GEDTC_NUMERIC: |
10874 | 0 | return false; |
10875 | | |
10876 | 0 | case GEDTC_COMPOUND: |
10877 | 0 | { |
10878 | 0 | for (const auto &comp : m_aoComponents) |
10879 | 0 | { |
10880 | 0 | if (comp->GetType().NeedsFreeDynamicMemory()) |
10881 | 0 | return true; |
10882 | 0 | } |
10883 | 0 | } |
10884 | 0 | } |
10885 | 0 | return false; |
10886 | 0 | } |
10887 | | |
10888 | | /************************************************************************/ |
10889 | | /* FreeDynamicMemory() */ |
10890 | | /************************************************************************/ |
10891 | | |
10892 | | /** Release the dynamic memory (strings typically) from a raw value. |
10893 | | * |
10894 | | * This is the same as the C function GDALExtendedDataTypeFreeDynamicMemory(). |
10895 | | * |
10896 | | * @param pBuffer Raw buffer of a single element of an attribute or array value. |
10897 | | */ |
10898 | | void GDALExtendedDataType::FreeDynamicMemory(void *pBuffer) const |
10899 | 0 | { |
10900 | 0 | switch (m_eClass) |
10901 | 0 | { |
10902 | 0 | case GEDTC_STRING: |
10903 | 0 | { |
10904 | 0 | char *pszStr; |
10905 | 0 | memcpy(&pszStr, pBuffer, sizeof(char *)); |
10906 | 0 | if (pszStr) |
10907 | 0 | { |
10908 | 0 | VSIFree(pszStr); |
10909 | 0 | } |
10910 | 0 | break; |
10911 | 0 | } |
10912 | | |
10913 | 0 | case GEDTC_NUMERIC: |
10914 | 0 | { |
10915 | 0 | break; |
10916 | 0 | } |
10917 | | |
10918 | 0 | case GEDTC_COMPOUND: |
10919 | 0 | { |
10920 | 0 | GByte *pabyBuffer = static_cast<GByte *>(pBuffer); |
10921 | 0 | for (const auto &comp : m_aoComponents) |
10922 | 0 | { |
10923 | 0 | comp->GetType().FreeDynamicMemory(pabyBuffer + |
10924 | 0 | comp->GetOffset()); |
10925 | 0 | } |
10926 | 0 | break; |
10927 | 0 | } |
10928 | 0 | } |
10929 | 0 | } |
10930 | | |
10931 | | /************************************************************************/ |
10932 | | /* ~GDALEDTComponent() */ |
10933 | | /************************************************************************/ |
10934 | | |
10935 | 0 | GDALEDTComponent::~GDALEDTComponent() = default; |
10936 | | |
10937 | | /************************************************************************/ |
10938 | | /* GDALEDTComponent() */ |
10939 | | /************************************************************************/ |
10940 | | |
10941 | | /** constructor of a GDALEDTComponent |
10942 | | * |
10943 | | * This is the same as the C function GDALEDTComponendCreate() |
10944 | | * |
10945 | | * @param name Component name |
10946 | | * @param offset Offset in byte of the component in the compound data type. |
10947 | | * In case of nesting of compound data type, this should be |
10948 | | * the offset to the immediate belonging data type, not to the |
10949 | | * higher level one. |
10950 | | * @param type Component data type. |
10951 | | */ |
10952 | | GDALEDTComponent::GDALEDTComponent(const std::string &name, size_t offset, |
10953 | | const GDALExtendedDataType &type) |
10954 | 0 | : m_osName(name), m_nOffset(offset), m_oType(type) |
10955 | 0 | { |
10956 | 0 | } |
10957 | | |
10958 | | /************************************************************************/ |
10959 | | /* GDALEDTComponent() */ |
10960 | | /************************************************************************/ |
10961 | | |
10962 | | /** Copy constructor. */ |
10963 | 0 | GDALEDTComponent::GDALEDTComponent(const GDALEDTComponent &) = default; |
10964 | | |
10965 | | /************************************************************************/ |
10966 | | /* operator==() */ |
10967 | | /************************************************************************/ |
10968 | | |
10969 | | /** Equality operator. |
10970 | | */ |
10971 | | bool GDALEDTComponent::operator==(const GDALEDTComponent &other) const |
10972 | 0 | { |
10973 | 0 | return m_osName == other.m_osName && m_nOffset == other.m_nOffset && |
10974 | 0 | m_oType == other.m_oType; |
10975 | 0 | } |
10976 | | |
10977 | | /************************************************************************/ |
10978 | | /* ~GDALDimension() */ |
10979 | | /************************************************************************/ |
10980 | | |
10981 | 0 | GDALDimension::~GDALDimension() = default; |
10982 | | |
10983 | | /************************************************************************/ |
10984 | | /* GDALDimension() */ |
10985 | | /************************************************************************/ |
10986 | | |
10987 | | //! @cond Doxygen_Suppress |
10988 | | /** Constructor. |
10989 | | * |
10990 | | * @param osParentName Parent name |
10991 | | * @param osName name |
10992 | | * @param osType type. See GetType(). |
10993 | | * @param osDirection direction. See GetDirection(). |
10994 | | * @param nSize size. |
10995 | | */ |
10996 | | GDALDimension::GDALDimension(const std::string &osParentName, |
10997 | | const std::string &osName, |
10998 | | const std::string &osType, |
10999 | | const std::string &osDirection, GUInt64 nSize) |
11000 | 0 | : m_osName(osName), |
11001 | | m_osFullName( |
11002 | 0 | !osParentName.empty() |
11003 | 0 | ? ((osParentName == "/" ? "/" : osParentName + "/") + osName) |
11004 | 0 | : osName), |
11005 | 0 | m_osType(osType), m_osDirection(osDirection), m_nSize(nSize) |
11006 | 0 | { |
11007 | 0 | } |
11008 | | |
11009 | | //! @endcond |
11010 | | |
11011 | | /************************************************************************/ |
11012 | | /* GetIndexingVariable() */ |
11013 | | /************************************************************************/ |
11014 | | |
11015 | | /** Return the variable that is used to index the dimension (if there is one). |
11016 | | * |
11017 | | * This is the array, typically one-dimensional, describing the values taken |
11018 | | * by the dimension. |
11019 | | */ |
11020 | | std::shared_ptr<GDALMDArray> GDALDimension::GetIndexingVariable() const |
11021 | 0 | { |
11022 | 0 | return nullptr; |
11023 | 0 | } |
11024 | | |
11025 | | /************************************************************************/ |
11026 | | /* SetIndexingVariable() */ |
11027 | | /************************************************************************/ |
11028 | | |
11029 | | /** Set the variable that is used to index the dimension. |
11030 | | * |
11031 | | * This is the array, typically one-dimensional, describing the values taken |
11032 | | * by the dimension. |
11033 | | * |
11034 | | * Optionally implemented by drivers. |
11035 | | * |
11036 | | * Drivers known to implement it: MEM. |
11037 | | * |
11038 | | * @param poArray Variable to use to index the dimension. |
11039 | | * @return true in case of success. |
11040 | | */ |
11041 | | bool GDALDimension::SetIndexingVariable( |
11042 | | CPL_UNUSED std::shared_ptr<GDALMDArray> poArray) |
11043 | 0 | { |
11044 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
11045 | 0 | "SetIndexingVariable() not implemented"); |
11046 | 0 | return false; |
11047 | 0 | } |
11048 | | |
11049 | | /************************************************************************/ |
11050 | | /* Rename() */ |
11051 | | /************************************************************************/ |
11052 | | |
11053 | | /** Rename the dimension. |
11054 | | * |
11055 | | * This is not implemented by all drivers. |
11056 | | * |
11057 | | * Drivers known to implement it: MEM, netCDF, ZARR. |
11058 | | * |
11059 | | * This is the same as the C function GDALDimensionRename(). |
11060 | | * |
11061 | | * @param osNewName New name. |
11062 | | * |
11063 | | * @return true in case of success |
11064 | | * @since GDAL 3.8 |
11065 | | */ |
11066 | | bool GDALDimension::Rename(CPL_UNUSED const std::string &osNewName) |
11067 | 0 | { |
11068 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Rename() not implemented"); |
11069 | 0 | return false; |
11070 | 0 | } |
11071 | | |
11072 | | /************************************************************************/ |
11073 | | /* BaseRename() */ |
11074 | | /************************************************************************/ |
11075 | | |
11076 | | //! @cond Doxygen_Suppress |
11077 | | void GDALDimension::BaseRename(const std::string &osNewName) |
11078 | 0 | { |
11079 | 0 | m_osFullName.resize(m_osFullName.size() - m_osName.size()); |
11080 | 0 | m_osFullName += osNewName; |
11081 | 0 | m_osName = osNewName; |
11082 | 0 | } |
11083 | | |
11084 | | //! @endcond |
11085 | | |
11086 | | //! @cond Doxygen_Suppress |
11087 | | /************************************************************************/ |
11088 | | /* ParentRenamed() */ |
11089 | | /************************************************************************/ |
11090 | | |
11091 | | void GDALDimension::ParentRenamed(const std::string &osNewParentFullName) |
11092 | 0 | { |
11093 | 0 | m_osFullName = osNewParentFullName; |
11094 | 0 | m_osFullName += "/"; |
11095 | 0 | m_osFullName += m_osName; |
11096 | 0 | } |
11097 | | |
11098 | | //! @endcond |
11099 | | |
11100 | | //! @cond Doxygen_Suppress |
11101 | | /************************************************************************/ |
11102 | | /* ParentDeleted() */ |
11103 | | /************************************************************************/ |
11104 | | |
11105 | | void GDALDimension::ParentDeleted() |
11106 | 0 | { |
11107 | 0 | } |
11108 | | |
11109 | | //! @endcond |
11110 | | |
11111 | | /************************************************************************/ |
11112 | | /************************************************************************/ |
11113 | | /************************************************************************/ |
11114 | | /* C API */ |
11115 | | /************************************************************************/ |
11116 | | /************************************************************************/ |
11117 | | /************************************************************************/ |
11118 | | |
11119 | | /************************************************************************/ |
11120 | | /* GDALExtendedDataTypeCreate() */ |
11121 | | /************************************************************************/ |
11122 | | |
11123 | | /** Return a new GDALExtendedDataType of class GEDTC_NUMERIC. |
11124 | | * |
11125 | | * This is the same as the C++ method GDALExtendedDataType::Create() |
11126 | | * |
11127 | | * The returned handle should be freed with GDALExtendedDataTypeRelease(). |
11128 | | * |
11129 | | * @param eType Numeric data type. Must be different from GDT_Unknown and |
11130 | | * GDT_TypeCount |
11131 | | * |
11132 | | * @return a new GDALExtendedDataTypeH handle, or nullptr. |
11133 | | */ |
11134 | | GDALExtendedDataTypeH GDALExtendedDataTypeCreate(GDALDataType eType) |
11135 | 0 | { |
11136 | 0 | if (CPL_UNLIKELY(eType == GDT_Unknown || eType == GDT_TypeCount)) |
11137 | 0 | { |
11138 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
11139 | 0 | "Illegal GDT_Unknown/GDT_TypeCount argument"); |
11140 | 0 | return nullptr; |
11141 | 0 | } |
11142 | 0 | return new GDALExtendedDataTypeHS( |
11143 | 0 | new GDALExtendedDataType(GDALExtendedDataType::Create(eType))); |
11144 | 0 | } |
11145 | | |
11146 | | /************************************************************************/ |
11147 | | /* GDALExtendedDataTypeCreateString() */ |
11148 | | /************************************************************************/ |
11149 | | |
11150 | | /** Return a new GDALExtendedDataType of class GEDTC_STRING. |
11151 | | * |
11152 | | * This is the same as the C++ method GDALExtendedDataType::CreateString() |
11153 | | * |
11154 | | * The returned handle should be freed with GDALExtendedDataTypeRelease(). |
11155 | | * |
11156 | | * @return a new GDALExtendedDataTypeH handle, or nullptr. |
11157 | | */ |
11158 | | GDALExtendedDataTypeH GDALExtendedDataTypeCreateString(size_t nMaxStringLength) |
11159 | 0 | { |
11160 | 0 | return new GDALExtendedDataTypeHS(new GDALExtendedDataType( |
11161 | 0 | GDALExtendedDataType::CreateString(nMaxStringLength))); |
11162 | 0 | } |
11163 | | |
11164 | | /************************************************************************/ |
11165 | | /* GDALExtendedDataTypeCreateStringEx() */ |
11166 | | /************************************************************************/ |
11167 | | |
11168 | | /** Return a new GDALExtendedDataType of class GEDTC_STRING. |
11169 | | * |
11170 | | * This is the same as the C++ method GDALExtendedDataType::CreateString() |
11171 | | * |
11172 | | * The returned handle should be freed with GDALExtendedDataTypeRelease(). |
11173 | | * |
11174 | | * @return a new GDALExtendedDataTypeH handle, or nullptr. |
11175 | | * @since GDAL 3.4 |
11176 | | */ |
11177 | | GDALExtendedDataTypeH |
11178 | | GDALExtendedDataTypeCreateStringEx(size_t nMaxStringLength, |
11179 | | GDALExtendedDataTypeSubType eSubType) |
11180 | 0 | { |
11181 | 0 | return new GDALExtendedDataTypeHS(new GDALExtendedDataType( |
11182 | 0 | GDALExtendedDataType::CreateString(nMaxStringLength, eSubType))); |
11183 | 0 | } |
11184 | | |
11185 | | /************************************************************************/ |
11186 | | /* GDALExtendedDataTypeCreateCompound() */ |
11187 | | /************************************************************************/ |
11188 | | |
11189 | | /** Return a new GDALExtendedDataType of class GEDTC_COMPOUND. |
11190 | | * |
11191 | | * This is the same as the C++ method GDALExtendedDataType::Create(const |
11192 | | * std::string&, size_t, std::vector<std::unique_ptr<GDALEDTComponent>>&&) |
11193 | | * |
11194 | | * The returned handle should be freed with GDALExtendedDataTypeRelease(). |
11195 | | * |
11196 | | * @param pszName Type name. |
11197 | | * @param nTotalSize Total size of the type in bytes. |
11198 | | * Should be large enough to store all components. |
11199 | | * @param nComponents Number of components in comps array. |
11200 | | * @param comps Components. |
11201 | | * @return a new GDALExtendedDataTypeH handle, or nullptr. |
11202 | | */ |
11203 | | GDALExtendedDataTypeH |
11204 | | GDALExtendedDataTypeCreateCompound(const char *pszName, size_t nTotalSize, |
11205 | | size_t nComponents, |
11206 | | const GDALEDTComponentH *comps) |
11207 | 0 | { |
11208 | 0 | std::vector<std::unique_ptr<GDALEDTComponent>> compsCpp; |
11209 | 0 | for (size_t i = 0; i < nComponents; i++) |
11210 | 0 | { |
11211 | 0 | compsCpp.emplace_back( |
11212 | 0 | std::make_unique<GDALEDTComponent>(*(comps[i]->m_poImpl.get()))); |
11213 | 0 | } |
11214 | 0 | auto dt = GDALExtendedDataType::Create(pszName ? pszName : "", nTotalSize, |
11215 | 0 | std::move(compsCpp)); |
11216 | 0 | if (dt.GetClass() != GEDTC_COMPOUND) |
11217 | 0 | return nullptr; |
11218 | 0 | return new GDALExtendedDataTypeHS(new GDALExtendedDataType(std::move(dt))); |
11219 | 0 | } |
11220 | | |
11221 | | /************************************************************************/ |
11222 | | /* GDALExtendedDataTypeRelease() */ |
11223 | | /************************************************************************/ |
11224 | | |
11225 | | /** Release the GDAL in-memory object associated with a GDALExtendedDataTypeH. |
11226 | | * |
11227 | | * Note: when applied on a object coming from a driver, this does not |
11228 | | * destroy the object in the file, database, etc... |
11229 | | */ |
11230 | | void GDALExtendedDataTypeRelease(GDALExtendedDataTypeH hEDT) |
11231 | 0 | { |
11232 | 0 | delete hEDT; |
11233 | 0 | } |
11234 | | |
11235 | | /************************************************************************/ |
11236 | | /* GDALExtendedDataTypeGetName() */ |
11237 | | /************************************************************************/ |
11238 | | |
11239 | | /** Return type name. |
11240 | | * |
11241 | | * This is the same as the C++ method GDALExtendedDataType::GetName() |
11242 | | */ |
11243 | | const char *GDALExtendedDataTypeGetName(GDALExtendedDataTypeH hEDT) |
11244 | 0 | { |
11245 | 0 | VALIDATE_POINTER1(hEDT, __func__, ""); |
11246 | 0 | return hEDT->m_poImpl->GetName().c_str(); |
11247 | 0 | } |
11248 | | |
11249 | | /************************************************************************/ |
11250 | | /* GDALExtendedDataTypeGetClass() */ |
11251 | | /************************************************************************/ |
11252 | | |
11253 | | /** Return type class. |
11254 | | * |
11255 | | * This is the same as the C++ method GDALExtendedDataType::GetClass() |
11256 | | */ |
11257 | | GDALExtendedDataTypeClass |
11258 | | GDALExtendedDataTypeGetClass(GDALExtendedDataTypeH hEDT) |
11259 | 0 | { |
11260 | 0 | VALIDATE_POINTER1(hEDT, __func__, GEDTC_NUMERIC); |
11261 | 0 | return hEDT->m_poImpl->GetClass(); |
11262 | 0 | } |
11263 | | |
11264 | | /************************************************************************/ |
11265 | | /* GDALExtendedDataTypeGetNumericDataType() */ |
11266 | | /************************************************************************/ |
11267 | | |
11268 | | /** Return numeric data type (only valid when GetClass() == GEDTC_NUMERIC) |
11269 | | * |
11270 | | * This is the same as the C++ method GDALExtendedDataType::GetNumericDataType() |
11271 | | */ |
11272 | | GDALDataType GDALExtendedDataTypeGetNumericDataType(GDALExtendedDataTypeH hEDT) |
11273 | 0 | { |
11274 | 0 | VALIDATE_POINTER1(hEDT, __func__, GDT_Unknown); |
11275 | 0 | return hEDT->m_poImpl->GetNumericDataType(); |
11276 | 0 | } |
11277 | | |
11278 | | /************************************************************************/ |
11279 | | /* GDALExtendedDataTypeGetSize() */ |
11280 | | /************************************************************************/ |
11281 | | |
11282 | | /** Return data type size in bytes. |
11283 | | * |
11284 | | * This is the same as the C++ method GDALExtendedDataType::GetSize() |
11285 | | */ |
11286 | | size_t GDALExtendedDataTypeGetSize(GDALExtendedDataTypeH hEDT) |
11287 | 0 | { |
11288 | 0 | VALIDATE_POINTER1(hEDT, __func__, 0); |
11289 | 0 | return hEDT->m_poImpl->GetSize(); |
11290 | 0 | } |
11291 | | |
11292 | | /************************************************************************/ |
11293 | | /* GDALExtendedDataTypeGetMaxStringLength() */ |
11294 | | /************************************************************************/ |
11295 | | |
11296 | | /** Return the maximum length of a string in bytes. |
11297 | | * |
11298 | | * 0 indicates unknown/unlimited string. |
11299 | | * |
11300 | | * This is the same as the C++ method GDALExtendedDataType::GetMaxStringLength() |
11301 | | */ |
11302 | | size_t GDALExtendedDataTypeGetMaxStringLength(GDALExtendedDataTypeH hEDT) |
11303 | 0 | { |
11304 | 0 | VALIDATE_POINTER1(hEDT, __func__, 0); |
11305 | 0 | return hEDT->m_poImpl->GetMaxStringLength(); |
11306 | 0 | } |
11307 | | |
11308 | | /************************************************************************/ |
11309 | | /* GDALExtendedDataTypeCanConvertTo() */ |
11310 | | /************************************************************************/ |
11311 | | |
11312 | | /** Return whether this data type can be converted to the other one. |
11313 | | * |
11314 | | * This is the same as the C function GDALExtendedDataType::CanConvertTo() |
11315 | | * |
11316 | | * @param hSourceEDT Source data type for the conversion being considered. |
11317 | | * @param hTargetEDT Target data type for the conversion being considered. |
11318 | | * @return TRUE if hSourceEDT can be convert to hTargetEDT. FALSE otherwise. |
11319 | | */ |
11320 | | int GDALExtendedDataTypeCanConvertTo(GDALExtendedDataTypeH hSourceEDT, |
11321 | | GDALExtendedDataTypeH hTargetEDT) |
11322 | 0 | { |
11323 | 0 | VALIDATE_POINTER1(hSourceEDT, __func__, FALSE); |
11324 | 0 | VALIDATE_POINTER1(hTargetEDT, __func__, FALSE); |
11325 | 0 | return hSourceEDT->m_poImpl->CanConvertTo(*(hTargetEDT->m_poImpl)); |
11326 | 0 | } |
11327 | | |
11328 | | /************************************************************************/ |
11329 | | /* GDALExtendedDataTypeEquals() */ |
11330 | | /************************************************************************/ |
11331 | | |
11332 | | /** Return whether this data type is equal to another one. |
11333 | | * |
11334 | | * This is the same as the C++ method GDALExtendedDataType::operator==() |
11335 | | * |
11336 | | * @param hFirstEDT First data type. |
11337 | | * @param hSecondEDT Second data type. |
11338 | | * @return TRUE if they are equal. FALSE otherwise. |
11339 | | */ |
11340 | | int GDALExtendedDataTypeEquals(GDALExtendedDataTypeH hFirstEDT, |
11341 | | GDALExtendedDataTypeH hSecondEDT) |
11342 | 0 | { |
11343 | 0 | VALIDATE_POINTER1(hFirstEDT, __func__, FALSE); |
11344 | 0 | VALIDATE_POINTER1(hSecondEDT, __func__, FALSE); |
11345 | 0 | return *(hFirstEDT->m_poImpl) == *(hSecondEDT->m_poImpl); |
11346 | 0 | } |
11347 | | |
11348 | | /************************************************************************/ |
11349 | | /* GDALExtendedDataTypeGetSubType() */ |
11350 | | /************************************************************************/ |
11351 | | |
11352 | | /** Return the subtype of a type. |
11353 | | * |
11354 | | * This is the same as the C++ method GDALExtendedDataType::GetSubType() |
11355 | | * |
11356 | | * @param hEDT Data type. |
11357 | | * @return subtype. |
11358 | | * @since 3.4 |
11359 | | */ |
11360 | | GDALExtendedDataTypeSubType |
11361 | | GDALExtendedDataTypeGetSubType(GDALExtendedDataTypeH hEDT) |
11362 | 0 | { |
11363 | 0 | VALIDATE_POINTER1(hEDT, __func__, GEDTST_NONE); |
11364 | 0 | return hEDT->m_poImpl->GetSubType(); |
11365 | 0 | } |
11366 | | |
11367 | | /************************************************************************/ |
11368 | | /* GDALExtendedDataTypeGetRAT() */ |
11369 | | /************************************************************************/ |
11370 | | |
11371 | | /** Return associated raster attribute table, when there is one. |
11372 | | * |
11373 | | * * For the netCDF driver, the RAT will capture enumerated types, with |
11374 | | * a "value" column with an integer value and a "name" column with the |
11375 | | * associated name. |
11376 | | * This is the same as the C++ method GDALExtendedDataType::GetRAT() |
11377 | | * |
11378 | | * @param hEDT Data type. |
11379 | | * @return raster attribute (owned by GDALExtendedDataTypeH), or NULL |
11380 | | * @since 3.12 |
11381 | | */ |
11382 | | GDALRasterAttributeTableH GDALExtendedDataTypeGetRAT(GDALExtendedDataTypeH hEDT) |
11383 | 0 | { |
11384 | 0 | VALIDATE_POINTER1(hEDT, __func__, nullptr); |
11385 | 0 | return GDALRasterAttributeTable::ToHandle( |
11386 | 0 | const_cast<GDALRasterAttributeTable *>(hEDT->m_poImpl->GetRAT())); |
11387 | 0 | } |
11388 | | |
11389 | | /************************************************************************/ |
11390 | | /* GDALExtendedDataTypeGetComponents() */ |
11391 | | /************************************************************************/ |
11392 | | |
11393 | | /** Return the components of the data type (only valid when GetClass() == |
11394 | | * GEDTC_COMPOUND) |
11395 | | * |
11396 | | * The returned array and its content must be freed with |
11397 | | * GDALExtendedDataTypeFreeComponents(). If only the array itself needs to be |
11398 | | * freed, CPLFree() should be called (and GDALExtendedDataTypeRelease() on |
11399 | | * individual array members). |
11400 | | * |
11401 | | * This is the same as the C++ method GDALExtendedDataType::GetComponents() |
11402 | | * |
11403 | | * @param hEDT Data type |
11404 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
11405 | | * @return an array of *pnCount components. |
11406 | | */ |
11407 | | GDALEDTComponentH *GDALExtendedDataTypeGetComponents(GDALExtendedDataTypeH hEDT, |
11408 | | size_t *pnCount) |
11409 | 0 | { |
11410 | 0 | VALIDATE_POINTER1(hEDT, __func__, nullptr); |
11411 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
11412 | 0 | const auto &components = hEDT->m_poImpl->GetComponents(); |
11413 | 0 | auto ret = static_cast<GDALEDTComponentH *>( |
11414 | 0 | CPLMalloc(sizeof(GDALEDTComponentH) * components.size())); |
11415 | 0 | for (size_t i = 0; i < components.size(); i++) |
11416 | 0 | { |
11417 | 0 | ret[i] = new GDALEDTComponentHS(*components[i].get()); |
11418 | 0 | } |
11419 | 0 | *pnCount = components.size(); |
11420 | 0 | return ret; |
11421 | 0 | } |
11422 | | |
11423 | | /************************************************************************/ |
11424 | | /* GDALExtendedDataTypeFreeComponents() */ |
11425 | | /************************************************************************/ |
11426 | | |
11427 | | /** Free the return of GDALExtendedDataTypeGetComponents(). |
11428 | | * |
11429 | | * @param components return value of GDALExtendedDataTypeGetComponents() |
11430 | | * @param nCount *pnCount value returned by GDALExtendedDataTypeGetComponents() |
11431 | | */ |
11432 | | void GDALExtendedDataTypeFreeComponents(GDALEDTComponentH *components, |
11433 | | size_t nCount) |
11434 | 0 | { |
11435 | 0 | for (size_t i = 0; i < nCount; i++) |
11436 | 0 | { |
11437 | 0 | delete components[i]; |
11438 | 0 | } |
11439 | 0 | CPLFree(components); |
11440 | 0 | } |
11441 | | |
11442 | | /************************************************************************/ |
11443 | | /* GDALEDTComponentCreate() */ |
11444 | | /************************************************************************/ |
11445 | | |
11446 | | /** Create a new GDALEDTComponent. |
11447 | | * |
11448 | | * The returned value must be freed with GDALEDTComponentRelease(). |
11449 | | * |
11450 | | * This is the same as the C++ constructor GDALEDTComponent::GDALEDTComponent(). |
11451 | | */ |
11452 | | GDALEDTComponentH GDALEDTComponentCreate(const char *pszName, size_t nOffset, |
11453 | | GDALExtendedDataTypeH hType) |
11454 | 0 | { |
11455 | 0 | VALIDATE_POINTER1(pszName, __func__, nullptr); |
11456 | 0 | VALIDATE_POINTER1(hType, __func__, nullptr); |
11457 | 0 | return new GDALEDTComponentHS( |
11458 | 0 | GDALEDTComponent(pszName, nOffset, *(hType->m_poImpl.get()))); |
11459 | 0 | } |
11460 | | |
11461 | | /************************************************************************/ |
11462 | | /* GDALEDTComponentRelease() */ |
11463 | | /************************************************************************/ |
11464 | | |
11465 | | /** Release the GDAL in-memory object associated with a GDALEDTComponentH. |
11466 | | * |
11467 | | * Note: when applied on a object coming from a driver, this does not |
11468 | | * destroy the object in the file, database, etc... |
11469 | | */ |
11470 | | void GDALEDTComponentRelease(GDALEDTComponentH hComp) |
11471 | 0 | { |
11472 | 0 | delete hComp; |
11473 | 0 | } |
11474 | | |
11475 | | /************************************************************************/ |
11476 | | /* GDALEDTComponentGetName() */ |
11477 | | /************************************************************************/ |
11478 | | |
11479 | | /** Return the name. |
11480 | | * |
11481 | | * The returned pointer is valid until hComp is released. |
11482 | | * |
11483 | | * This is the same as the C++ method GDALEDTComponent::GetName(). |
11484 | | */ |
11485 | | const char *GDALEDTComponentGetName(GDALEDTComponentH hComp) |
11486 | 0 | { |
11487 | 0 | VALIDATE_POINTER1(hComp, __func__, nullptr); |
11488 | 0 | return hComp->m_poImpl->GetName().c_str(); |
11489 | 0 | } |
11490 | | |
11491 | | /************************************************************************/ |
11492 | | /* GDALEDTComponentGetOffset() */ |
11493 | | /************************************************************************/ |
11494 | | |
11495 | | /** Return the offset (in bytes) of the component in the compound data type. |
11496 | | * |
11497 | | * This is the same as the C++ method GDALEDTComponent::GetOffset(). |
11498 | | */ |
11499 | | size_t GDALEDTComponentGetOffset(GDALEDTComponentH hComp) |
11500 | 0 | { |
11501 | 0 | VALIDATE_POINTER1(hComp, __func__, 0); |
11502 | 0 | return hComp->m_poImpl->GetOffset(); |
11503 | 0 | } |
11504 | | |
11505 | | /************************************************************************/ |
11506 | | /* GDALEDTComponentGetType() */ |
11507 | | /************************************************************************/ |
11508 | | |
11509 | | /** Return the data type of the component. |
11510 | | * |
11511 | | * This is the same as the C++ method GDALEDTComponent::GetType(). |
11512 | | */ |
11513 | | GDALExtendedDataTypeH GDALEDTComponentGetType(GDALEDTComponentH hComp) |
11514 | 0 | { |
11515 | 0 | VALIDATE_POINTER1(hComp, __func__, nullptr); |
11516 | 0 | return new GDALExtendedDataTypeHS( |
11517 | 0 | new GDALExtendedDataType(hComp->m_poImpl->GetType())); |
11518 | 0 | } |
11519 | | |
11520 | | /************************************************************************/ |
11521 | | /* GDALGroupRelease() */ |
11522 | | /************************************************************************/ |
11523 | | |
11524 | | /** Release the GDAL in-memory object associated with a GDALGroupH. |
11525 | | * |
11526 | | * Note: when applied on a object coming from a driver, this does not |
11527 | | * destroy the object in the file, database, etc... |
11528 | | */ |
11529 | | void GDALGroupRelease(GDALGroupH hGroup) |
11530 | 0 | { |
11531 | 0 | delete hGroup; |
11532 | 0 | } |
11533 | | |
11534 | | /************************************************************************/ |
11535 | | /* GDALGroupGetName() */ |
11536 | | /************************************************************************/ |
11537 | | |
11538 | | /** Return the name of the group. |
11539 | | * |
11540 | | * The returned pointer is valid until hGroup is released. |
11541 | | * |
11542 | | * This is the same as the C++ method GDALGroup::GetName(). |
11543 | | */ |
11544 | | const char *GDALGroupGetName(GDALGroupH hGroup) |
11545 | 0 | { |
11546 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11547 | 0 | return hGroup->m_poImpl->GetName().c_str(); |
11548 | 0 | } |
11549 | | |
11550 | | /************************************************************************/ |
11551 | | /* GDALGroupGetFullName() */ |
11552 | | /************************************************************************/ |
11553 | | |
11554 | | /** Return the full name of the group. |
11555 | | * |
11556 | | * The returned pointer is valid until hGroup is released. |
11557 | | * |
11558 | | * This is the same as the C++ method GDALGroup::GetFullName(). |
11559 | | */ |
11560 | | const char *GDALGroupGetFullName(GDALGroupH hGroup) |
11561 | 0 | { |
11562 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11563 | 0 | return hGroup->m_poImpl->GetFullName().c_str(); |
11564 | 0 | } |
11565 | | |
11566 | | /************************************************************************/ |
11567 | | /* GDALGroupGetMDArrayNames() */ |
11568 | | /************************************************************************/ |
11569 | | |
11570 | | /** Return the list of multidimensional array names contained in this group. |
11571 | | * |
11572 | | * This is the same as the C++ method GDALGroup::GetGroupNames(). |
11573 | | * |
11574 | | * @return the array names, to be freed with CSLDestroy() |
11575 | | */ |
11576 | | char **GDALGroupGetMDArrayNames(GDALGroupH hGroup, CSLConstList papszOptions) |
11577 | 0 | { |
11578 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11579 | 0 | auto names = hGroup->m_poImpl->GetMDArrayNames(papszOptions); |
11580 | 0 | CPLStringList res; |
11581 | 0 | for (const auto &name : names) |
11582 | 0 | { |
11583 | 0 | res.AddString(name.c_str()); |
11584 | 0 | } |
11585 | 0 | return res.StealList(); |
11586 | 0 | } |
11587 | | |
11588 | | /************************************************************************/ |
11589 | | /* GDALGroupGetMDArrayFullNamesRecursive() */ |
11590 | | /************************************************************************/ |
11591 | | |
11592 | | /** Return the list of multidimensional array full names contained in this |
11593 | | * group and its subgroups. |
11594 | | * |
11595 | | * This is the same as the C++ method GDALGroup::GetMDArrayFullNamesRecursive(). |
11596 | | * |
11597 | | * @return the array names, to be freed with CSLDestroy() |
11598 | | * |
11599 | | * @since 3.11 |
11600 | | */ |
11601 | | char **GDALGroupGetMDArrayFullNamesRecursive(GDALGroupH hGroup, |
11602 | | CSLConstList papszGroupOptions, |
11603 | | CSLConstList papszArrayOptions) |
11604 | 0 | { |
11605 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11606 | 0 | auto names = hGroup->m_poImpl->GetMDArrayFullNamesRecursive( |
11607 | 0 | papszGroupOptions, papszArrayOptions); |
11608 | 0 | CPLStringList res; |
11609 | 0 | for (const auto &name : names) |
11610 | 0 | { |
11611 | 0 | res.AddString(name.c_str()); |
11612 | 0 | } |
11613 | 0 | return res.StealList(); |
11614 | 0 | } |
11615 | | |
11616 | | /************************************************************************/ |
11617 | | /* GDALGroupOpenMDArray() */ |
11618 | | /************************************************************************/ |
11619 | | |
11620 | | /** Open and return a multidimensional array. |
11621 | | * |
11622 | | * This is the same as the C++ method GDALGroup::OpenMDArray(). |
11623 | | * |
11624 | | * @return the array, to be freed with GDALMDArrayRelease(), or nullptr. |
11625 | | */ |
11626 | | GDALMDArrayH GDALGroupOpenMDArray(GDALGroupH hGroup, const char *pszMDArrayName, |
11627 | | CSLConstList papszOptions) |
11628 | 0 | { |
11629 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11630 | 0 | VALIDATE_POINTER1(pszMDArrayName, __func__, nullptr); |
11631 | 0 | auto array = hGroup->m_poImpl->OpenMDArray(std::string(pszMDArrayName), |
11632 | 0 | papszOptions); |
11633 | 0 | if (!array) |
11634 | 0 | return nullptr; |
11635 | 0 | return new GDALMDArrayHS(array); |
11636 | 0 | } |
11637 | | |
11638 | | /************************************************************************/ |
11639 | | /* GDALGroupOpenMDArrayFromFullname() */ |
11640 | | /************************************************************************/ |
11641 | | |
11642 | | /** Open and return a multidimensional array from its fully qualified name. |
11643 | | * |
11644 | | * This is the same as the C++ method GDALGroup::OpenMDArrayFromFullname(). |
11645 | | * |
11646 | | * @return the array, to be freed with GDALMDArrayRelease(), or nullptr. |
11647 | | * |
11648 | | * @since GDAL 3.2 |
11649 | | */ |
11650 | | GDALMDArrayH GDALGroupOpenMDArrayFromFullname(GDALGroupH hGroup, |
11651 | | const char *pszFullname, |
11652 | | CSLConstList papszOptions) |
11653 | 0 | { |
11654 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11655 | 0 | VALIDATE_POINTER1(pszFullname, __func__, nullptr); |
11656 | 0 | auto array = hGroup->m_poImpl->OpenMDArrayFromFullname( |
11657 | 0 | std::string(pszFullname), papszOptions); |
11658 | 0 | if (!array) |
11659 | 0 | return nullptr; |
11660 | 0 | return new GDALMDArrayHS(array); |
11661 | 0 | } |
11662 | | |
11663 | | /************************************************************************/ |
11664 | | /* GDALGroupResolveMDArray() */ |
11665 | | /************************************************************************/ |
11666 | | |
11667 | | /** Locate an array in a group and its subgroups by name. |
11668 | | * |
11669 | | * See GDALGroup::ResolveMDArray() for description of the behavior. |
11670 | | * @since GDAL 3.2 |
11671 | | */ |
11672 | | GDALMDArrayH GDALGroupResolveMDArray(GDALGroupH hGroup, const char *pszName, |
11673 | | const char *pszStartingPoint, |
11674 | | CSLConstList papszOptions) |
11675 | 0 | { |
11676 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11677 | 0 | VALIDATE_POINTER1(pszName, __func__, nullptr); |
11678 | 0 | VALIDATE_POINTER1(pszStartingPoint, __func__, nullptr); |
11679 | 0 | auto array = hGroup->m_poImpl->ResolveMDArray( |
11680 | 0 | std::string(pszName), std::string(pszStartingPoint), papszOptions); |
11681 | 0 | if (!array) |
11682 | 0 | return nullptr; |
11683 | 0 | return new GDALMDArrayHS(array); |
11684 | 0 | } |
11685 | | |
11686 | | /************************************************************************/ |
11687 | | /* GDALGroupGetGroupNames() */ |
11688 | | /************************************************************************/ |
11689 | | |
11690 | | /** Return the list of sub-groups contained in this group. |
11691 | | * |
11692 | | * This is the same as the C++ method GDALGroup::GetGroupNames(). |
11693 | | * |
11694 | | * @return the group names, to be freed with CSLDestroy() |
11695 | | */ |
11696 | | char **GDALGroupGetGroupNames(GDALGroupH hGroup, CSLConstList papszOptions) |
11697 | 0 | { |
11698 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11699 | 0 | auto names = hGroup->m_poImpl->GetGroupNames(papszOptions); |
11700 | 0 | CPLStringList res; |
11701 | 0 | for (const auto &name : names) |
11702 | 0 | { |
11703 | 0 | res.AddString(name.c_str()); |
11704 | 0 | } |
11705 | 0 | return res.StealList(); |
11706 | 0 | } |
11707 | | |
11708 | | /************************************************************************/ |
11709 | | /* GDALGroupOpenGroup() */ |
11710 | | /************************************************************************/ |
11711 | | |
11712 | | /** Open and return a sub-group. |
11713 | | * |
11714 | | * This is the same as the C++ method GDALGroup::OpenGroup(). |
11715 | | * |
11716 | | * @return the sub-group, to be freed with GDALGroupRelease(), or nullptr. |
11717 | | */ |
11718 | | GDALGroupH GDALGroupOpenGroup(GDALGroupH hGroup, const char *pszSubGroupName, |
11719 | | CSLConstList papszOptions) |
11720 | 0 | { |
11721 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11722 | 0 | VALIDATE_POINTER1(pszSubGroupName, __func__, nullptr); |
11723 | 0 | auto subGroup = |
11724 | 0 | hGroup->m_poImpl->OpenGroup(std::string(pszSubGroupName), papszOptions); |
11725 | 0 | if (!subGroup) |
11726 | 0 | return nullptr; |
11727 | 0 | return new GDALGroupHS(subGroup); |
11728 | 0 | } |
11729 | | |
11730 | | /************************************************************************/ |
11731 | | /* GDALGroupGetVectorLayerNames() */ |
11732 | | /************************************************************************/ |
11733 | | |
11734 | | /** Return the list of layer names contained in this group. |
11735 | | * |
11736 | | * This is the same as the C++ method GDALGroup::GetVectorLayerNames(). |
11737 | | * |
11738 | | * @return the group names, to be freed with CSLDestroy() |
11739 | | * @since 3.4 |
11740 | | */ |
11741 | | char **GDALGroupGetVectorLayerNames(GDALGroupH hGroup, |
11742 | | CSLConstList papszOptions) |
11743 | 0 | { |
11744 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11745 | 0 | auto names = hGroup->m_poImpl->GetVectorLayerNames(papszOptions); |
11746 | 0 | CPLStringList res; |
11747 | 0 | for (const auto &name : names) |
11748 | 0 | { |
11749 | 0 | res.AddString(name.c_str()); |
11750 | 0 | } |
11751 | 0 | return res.StealList(); |
11752 | 0 | } |
11753 | | |
11754 | | /************************************************************************/ |
11755 | | /* GDALGroupOpenVectorLayer() */ |
11756 | | /************************************************************************/ |
11757 | | |
11758 | | /** Open and return a vector layer. |
11759 | | * |
11760 | | * This is the same as the C++ method GDALGroup::OpenVectorLayer(). |
11761 | | * |
11762 | | * Note that the vector layer is owned by its parent GDALDatasetH, and thus |
11763 | | * the returned handled if only valid while the parent GDALDatasetH is kept |
11764 | | * opened. |
11765 | | * |
11766 | | * @return the vector layer, or nullptr. |
11767 | | * @since 3.4 |
11768 | | */ |
11769 | | OGRLayerH GDALGroupOpenVectorLayer(GDALGroupH hGroup, |
11770 | | const char *pszVectorLayerName, |
11771 | | CSLConstList papszOptions) |
11772 | 0 | { |
11773 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11774 | 0 | VALIDATE_POINTER1(pszVectorLayerName, __func__, nullptr); |
11775 | 0 | return OGRLayer::ToHandle(hGroup->m_poImpl->OpenVectorLayer( |
11776 | 0 | std::string(pszVectorLayerName), papszOptions)); |
11777 | 0 | } |
11778 | | |
11779 | | /************************************************************************/ |
11780 | | /* GDALGroupOpenMDArrayFromFullname() */ |
11781 | | /************************************************************************/ |
11782 | | |
11783 | | /** Open and return a sub-group from its fully qualified name. |
11784 | | * |
11785 | | * This is the same as the C++ method GDALGroup::OpenGroupFromFullname(). |
11786 | | * |
11787 | | * @return the sub-group, to be freed with GDALGroupRelease(), or nullptr. |
11788 | | * |
11789 | | * @since GDAL 3.2 |
11790 | | */ |
11791 | | GDALGroupH GDALGroupOpenGroupFromFullname(GDALGroupH hGroup, |
11792 | | const char *pszFullname, |
11793 | | CSLConstList papszOptions) |
11794 | 0 | { |
11795 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11796 | 0 | VALIDATE_POINTER1(pszFullname, __func__, nullptr); |
11797 | 0 | auto subGroup = hGroup->m_poImpl->OpenGroupFromFullname( |
11798 | 0 | std::string(pszFullname), papszOptions); |
11799 | 0 | if (!subGroup) |
11800 | 0 | return nullptr; |
11801 | 0 | return new GDALGroupHS(subGroup); |
11802 | 0 | } |
11803 | | |
11804 | | /************************************************************************/ |
11805 | | /* GDALGroupGetDimensions() */ |
11806 | | /************************************************************************/ |
11807 | | |
11808 | | /** Return the list of dimensions contained in this group and used by its |
11809 | | * arrays. |
11810 | | * |
11811 | | * The returned array must be freed with GDALReleaseDimensions(). If only the |
11812 | | * array itself needs to be freed, CPLFree() should be called (and |
11813 | | * GDALDimensionRelease() on individual array members). |
11814 | | * |
11815 | | * This is the same as the C++ method GDALGroup::GetDimensions(). |
11816 | | * |
11817 | | * @param hGroup Group. |
11818 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
11819 | | * @param papszOptions Driver specific options determining how dimensions |
11820 | | * should be retrieved. Pass nullptr for default behavior. |
11821 | | * |
11822 | | * @return an array of *pnCount dimensions. |
11823 | | */ |
11824 | | GDALDimensionH *GDALGroupGetDimensions(GDALGroupH hGroup, size_t *pnCount, |
11825 | | CSLConstList papszOptions) |
11826 | 0 | { |
11827 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11828 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
11829 | 0 | auto dims = hGroup->m_poImpl->GetDimensions(papszOptions); |
11830 | 0 | auto ret = static_cast<GDALDimensionH *>( |
11831 | 0 | CPLMalloc(sizeof(GDALDimensionH) * dims.size())); |
11832 | 0 | for (size_t i = 0; i < dims.size(); i++) |
11833 | 0 | { |
11834 | 0 | ret[i] = new GDALDimensionHS(dims[i]); |
11835 | 0 | } |
11836 | 0 | *pnCount = dims.size(); |
11837 | 0 | return ret; |
11838 | 0 | } |
11839 | | |
11840 | | /************************************************************************/ |
11841 | | /* GDALGroupGetAttribute() */ |
11842 | | /************************************************************************/ |
11843 | | |
11844 | | /** Return an attribute by its name. |
11845 | | * |
11846 | | * This is the same as the C++ method GDALIHasAttribute::GetAttribute() |
11847 | | * |
11848 | | * The returned attribute must be freed with GDALAttributeRelease(). |
11849 | | */ |
11850 | | GDALAttributeH GDALGroupGetAttribute(GDALGroupH hGroup, const char *pszName) |
11851 | 0 | { |
11852 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11853 | 0 | VALIDATE_POINTER1(pszName, __func__, nullptr); |
11854 | 0 | auto attr = hGroup->m_poImpl->GetAttribute(std::string(pszName)); |
11855 | 0 | if (attr) |
11856 | 0 | return new GDALAttributeHS(attr); |
11857 | 0 | return nullptr; |
11858 | 0 | } |
11859 | | |
11860 | | /************************************************************************/ |
11861 | | /* GDALGroupGetAttributes() */ |
11862 | | /************************************************************************/ |
11863 | | |
11864 | | /** Return the list of attributes contained in this group. |
11865 | | * |
11866 | | * The returned array must be freed with GDALReleaseAttributes(). If only the |
11867 | | * array itself needs to be freed, CPLFree() should be called (and |
11868 | | * GDALAttributeRelease() on individual array members). |
11869 | | * |
11870 | | * This is the same as the C++ method GDALGroup::GetAttributes(). |
11871 | | * |
11872 | | * @param hGroup Group. |
11873 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
11874 | | * @param papszOptions Driver specific options determining how attributes |
11875 | | * should be retrieved. Pass nullptr for default behavior. |
11876 | | * |
11877 | | * @return an array of *pnCount attributes. |
11878 | | */ |
11879 | | GDALAttributeH *GDALGroupGetAttributes(GDALGroupH hGroup, size_t *pnCount, |
11880 | | CSLConstList papszOptions) |
11881 | 0 | { |
11882 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11883 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
11884 | 0 | auto attrs = hGroup->m_poImpl->GetAttributes(papszOptions); |
11885 | 0 | auto ret = static_cast<GDALAttributeH *>( |
11886 | 0 | CPLMalloc(sizeof(GDALAttributeH) * attrs.size())); |
11887 | 0 | for (size_t i = 0; i < attrs.size(); i++) |
11888 | 0 | { |
11889 | 0 | ret[i] = new GDALAttributeHS(attrs[i]); |
11890 | 0 | } |
11891 | 0 | *pnCount = attrs.size(); |
11892 | 0 | return ret; |
11893 | 0 | } |
11894 | | |
11895 | | /************************************************************************/ |
11896 | | /* GDALGroupGetStructuralInfo() */ |
11897 | | /************************************************************************/ |
11898 | | |
11899 | | /** Return structural information on the group. |
11900 | | * |
11901 | | * This may be the compression, etc.. |
11902 | | * |
11903 | | * The return value should not be freed and is valid until GDALGroup is |
11904 | | * released or this function called again. |
11905 | | * |
11906 | | * This is the same as the C++ method GDALGroup::GetStructuralInfo(). |
11907 | | */ |
11908 | | CSLConstList GDALGroupGetStructuralInfo(GDALGroupH hGroup) |
11909 | 0 | { |
11910 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11911 | 0 | return hGroup->m_poImpl->GetStructuralInfo(); |
11912 | 0 | } |
11913 | | |
11914 | | /************************************************************************/ |
11915 | | /* GDALGroupGetDataTypeCount() */ |
11916 | | /************************************************************************/ |
11917 | | |
11918 | | /** Return the number of data types associated with the group |
11919 | | * (typically enumerations). |
11920 | | * |
11921 | | * This is the same as the C++ method GDALGroup::GetDataTypes().size(). |
11922 | | * |
11923 | | * @since 3.12 |
11924 | | */ |
11925 | | size_t GDALGroupGetDataTypeCount(GDALGroupH hGroup) |
11926 | 0 | { |
11927 | 0 | VALIDATE_POINTER1(hGroup, __func__, 0); |
11928 | 0 | return hGroup->m_poImpl->GetDataTypes().size(); |
11929 | 0 | } |
11930 | | |
11931 | | /************************************************************************/ |
11932 | | /* GDALGroupGetDataType() */ |
11933 | | /************************************************************************/ |
11934 | | |
11935 | | /** Return one of the data types associated with the group. |
11936 | | * |
11937 | | * This is the same as the C++ method GDALGroup::GetDataTypes()[]. |
11938 | | * |
11939 | | * @return a type to release with GDALExtendedDataTypeRelease() once done, |
11940 | | * or nullptr in case of error. |
11941 | | * @since 3.12 |
11942 | | */ |
11943 | | GDALExtendedDataTypeH GDALGroupGetDataType(GDALGroupH hGroup, size_t nIdx) |
11944 | 0 | { |
11945 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11946 | 0 | if (nIdx >= hGroup->m_poImpl->GetDataTypes().size()) |
11947 | 0 | return nullptr; |
11948 | 0 | return new GDALExtendedDataTypeHS(new GDALExtendedDataType( |
11949 | 0 | *(hGroup->m_poImpl->GetDataTypes()[nIdx].get()))); |
11950 | 0 | } |
11951 | | |
11952 | | /************************************************************************/ |
11953 | | /* GDALReleaseAttributes() */ |
11954 | | /************************************************************************/ |
11955 | | |
11956 | | /** Free the return of GDALGroupGetAttributes() or GDALMDArrayGetAttributes() |
11957 | | * |
11958 | | * @param attributes return pointer of above methods |
11959 | | * @param nCount *pnCount value returned by above methods |
11960 | | */ |
11961 | | void GDALReleaseAttributes(GDALAttributeH *attributes, size_t nCount) |
11962 | 0 | { |
11963 | 0 | for (size_t i = 0; i < nCount; i++) |
11964 | 0 | { |
11965 | 0 | delete attributes[i]; |
11966 | 0 | } |
11967 | 0 | CPLFree(attributes); |
11968 | 0 | } |
11969 | | |
11970 | | /************************************************************************/ |
11971 | | /* GDALGroupCreateGroup() */ |
11972 | | /************************************************************************/ |
11973 | | |
11974 | | /** Create a sub-group within a group. |
11975 | | * |
11976 | | * This is the same as the C++ method GDALGroup::CreateGroup(). |
11977 | | * |
11978 | | * @return the sub-group, to be freed with GDALGroupRelease(), or nullptr. |
11979 | | */ |
11980 | | GDALGroupH GDALGroupCreateGroup(GDALGroupH hGroup, const char *pszSubGroupName, |
11981 | | CSLConstList papszOptions) |
11982 | 0 | { |
11983 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
11984 | 0 | VALIDATE_POINTER1(pszSubGroupName, __func__, nullptr); |
11985 | 0 | auto ret = hGroup->m_poImpl->CreateGroup(std::string(pszSubGroupName), |
11986 | 0 | papszOptions); |
11987 | 0 | if (!ret) |
11988 | 0 | return nullptr; |
11989 | 0 | return new GDALGroupHS(ret); |
11990 | 0 | } |
11991 | | |
11992 | | /************************************************************************/ |
11993 | | /* GDALGroupDeleteGroup() */ |
11994 | | /************************************************************************/ |
11995 | | |
11996 | | /** Delete a sub-group from a group. |
11997 | | * |
11998 | | * After this call, if a previously obtained instance of the deleted object |
11999 | | * is still alive, no method other than for freeing it should be invoked. |
12000 | | * |
12001 | | * This is the same as the C++ method GDALGroup::DeleteGroup(). |
12002 | | * |
12003 | | * @return true in case of success. |
12004 | | * @since GDAL 3.8 |
12005 | | */ |
12006 | | bool GDALGroupDeleteGroup(GDALGroupH hGroup, const char *pszSubGroupName, |
12007 | | CSLConstList papszOptions) |
12008 | 0 | { |
12009 | 0 | VALIDATE_POINTER1(hGroup, __func__, false); |
12010 | 0 | VALIDATE_POINTER1(pszSubGroupName, __func__, false); |
12011 | 0 | return hGroup->m_poImpl->DeleteGroup(std::string(pszSubGroupName), |
12012 | 0 | papszOptions); |
12013 | 0 | } |
12014 | | |
12015 | | /************************************************************************/ |
12016 | | /* GDALGroupCreateDimension() */ |
12017 | | /************************************************************************/ |
12018 | | |
12019 | | /** Create a dimension within a group. |
12020 | | * |
12021 | | * This is the same as the C++ method GDALGroup::CreateDimension(). |
12022 | | * |
12023 | | * @return the dimension, to be freed with GDALDimensionRelease(), or nullptr. |
12024 | | */ |
12025 | | GDALDimensionH GDALGroupCreateDimension(GDALGroupH hGroup, const char *pszName, |
12026 | | const char *pszType, |
12027 | | const char *pszDirection, GUInt64 nSize, |
12028 | | CSLConstList papszOptions) |
12029 | 0 | { |
12030 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
12031 | 0 | VALIDATE_POINTER1(pszName, __func__, nullptr); |
12032 | 0 | auto ret = hGroup->m_poImpl->CreateDimension( |
12033 | 0 | std::string(pszName), std::string(pszType ? pszType : ""), |
12034 | 0 | std::string(pszDirection ? pszDirection : ""), nSize, papszOptions); |
12035 | 0 | if (!ret) |
12036 | 0 | return nullptr; |
12037 | 0 | return new GDALDimensionHS(ret); |
12038 | 0 | } |
12039 | | |
12040 | | /************************************************************************/ |
12041 | | /* GDALGroupCreateMDArray() */ |
12042 | | /************************************************************************/ |
12043 | | |
12044 | | /** Create a multidimensional array within a group. |
12045 | | * |
12046 | | * This is the same as the C++ method GDALGroup::CreateMDArray(). |
12047 | | * |
12048 | | * @return the array, to be freed with GDALMDArrayRelease(), or nullptr. |
12049 | | */ |
12050 | | GDALMDArrayH GDALGroupCreateMDArray(GDALGroupH hGroup, const char *pszName, |
12051 | | size_t nDimensions, |
12052 | | GDALDimensionH *pahDimensions, |
12053 | | GDALExtendedDataTypeH hEDT, |
12054 | | CSLConstList papszOptions) |
12055 | 0 | { |
12056 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
12057 | 0 | VALIDATE_POINTER1(pszName, __func__, nullptr); |
12058 | 0 | VALIDATE_POINTER1(hEDT, __func__, nullptr); |
12059 | 0 | std::vector<std::shared_ptr<GDALDimension>> dims; |
12060 | 0 | dims.reserve(nDimensions); |
12061 | 0 | for (size_t i = 0; i < nDimensions; i++) |
12062 | 0 | dims.push_back(pahDimensions[i]->m_poImpl); |
12063 | 0 | auto ret = hGroup->m_poImpl->CreateMDArray(std::string(pszName), dims, |
12064 | 0 | *(hEDT->m_poImpl), papszOptions); |
12065 | 0 | if (!ret) |
12066 | 0 | return nullptr; |
12067 | 0 | return new GDALMDArrayHS(ret); |
12068 | 0 | } |
12069 | | |
12070 | | /************************************************************************/ |
12071 | | /* GDALGroupDeleteMDArray() */ |
12072 | | /************************************************************************/ |
12073 | | |
12074 | | /** Delete an array from a group. |
12075 | | * |
12076 | | * After this call, if a previously obtained instance of the deleted object |
12077 | | * is still alive, no method other than for freeing it should be invoked. |
12078 | | * |
12079 | | * This is the same as the C++ method GDALGroup::DeleteMDArray(). |
12080 | | * |
12081 | | * @return true in case of success. |
12082 | | * @since GDAL 3.8 |
12083 | | */ |
12084 | | bool GDALGroupDeleteMDArray(GDALGroupH hGroup, const char *pszName, |
12085 | | CSLConstList papszOptions) |
12086 | 0 | { |
12087 | 0 | VALIDATE_POINTER1(hGroup, __func__, false); |
12088 | 0 | VALIDATE_POINTER1(pszName, __func__, false); |
12089 | 0 | return hGroup->m_poImpl->DeleteMDArray(std::string(pszName), papszOptions); |
12090 | 0 | } |
12091 | | |
12092 | | /************************************************************************/ |
12093 | | /* GDALGroupCreateAttribute() */ |
12094 | | /************************************************************************/ |
12095 | | |
12096 | | /** Create a attribute within a group. |
12097 | | * |
12098 | | * This is the same as the C++ method GDALGroup::CreateAttribute(). |
12099 | | * |
12100 | | * @return the attribute, to be freed with GDALAttributeRelease(), or nullptr. |
12101 | | */ |
12102 | | GDALAttributeH GDALGroupCreateAttribute(GDALGroupH hGroup, const char *pszName, |
12103 | | size_t nDimensions, |
12104 | | const GUInt64 *panDimensions, |
12105 | | GDALExtendedDataTypeH hEDT, |
12106 | | CSLConstList papszOptions) |
12107 | 0 | { |
12108 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
12109 | 0 | VALIDATE_POINTER1(hEDT, __func__, nullptr); |
12110 | 0 | std::vector<GUInt64> dims; |
12111 | 0 | dims.reserve(nDimensions); |
12112 | 0 | for (size_t i = 0; i < nDimensions; i++) |
12113 | 0 | dims.push_back(panDimensions[i]); |
12114 | 0 | auto ret = hGroup->m_poImpl->CreateAttribute( |
12115 | 0 | std::string(pszName), dims, *(hEDT->m_poImpl), papszOptions); |
12116 | 0 | if (!ret) |
12117 | 0 | return nullptr; |
12118 | 0 | return new GDALAttributeHS(ret); |
12119 | 0 | } |
12120 | | |
12121 | | /************************************************************************/ |
12122 | | /* GDALGroupDeleteAttribute() */ |
12123 | | /************************************************************************/ |
12124 | | |
12125 | | /** Delete an attribute from a group. |
12126 | | * |
12127 | | * After this call, if a previously obtained instance of the deleted object |
12128 | | * is still alive, no method other than for freeing it should be invoked. |
12129 | | * |
12130 | | * This is the same as the C++ method GDALGroup::DeleteAttribute(). |
12131 | | * |
12132 | | * @return true in case of success. |
12133 | | * @since GDAL 3.8 |
12134 | | */ |
12135 | | bool GDALGroupDeleteAttribute(GDALGroupH hGroup, const char *pszName, |
12136 | | CSLConstList papszOptions) |
12137 | 0 | { |
12138 | 0 | VALIDATE_POINTER1(hGroup, __func__, false); |
12139 | 0 | VALIDATE_POINTER1(pszName, __func__, false); |
12140 | 0 | return hGroup->m_poImpl->DeleteAttribute(std::string(pszName), |
12141 | 0 | papszOptions); |
12142 | 0 | } |
12143 | | |
12144 | | /************************************************************************/ |
12145 | | /* GDALGroupRename() */ |
12146 | | /************************************************************************/ |
12147 | | |
12148 | | /** Rename the group. |
12149 | | * |
12150 | | * This is not implemented by all drivers. |
12151 | | * |
12152 | | * Drivers known to implement it: MEM, netCDF. |
12153 | | * |
12154 | | * This is the same as the C++ method GDALGroup::Rename() |
12155 | | * |
12156 | | * @return true in case of success |
12157 | | * @since GDAL 3.8 |
12158 | | */ |
12159 | | bool GDALGroupRename(GDALGroupH hGroup, const char *pszNewName) |
12160 | 0 | { |
12161 | 0 | VALIDATE_POINTER1(hGroup, __func__, false); |
12162 | 0 | VALIDATE_POINTER1(pszNewName, __func__, false); |
12163 | 0 | return hGroup->m_poImpl->Rename(pszNewName); |
12164 | 0 | } |
12165 | | |
12166 | | /************************************************************************/ |
12167 | | /* GDALGroupSubsetDimensionFromSelection() */ |
12168 | | /************************************************************************/ |
12169 | | |
12170 | | /** Return a virtual group whose one dimension has been subset according to a |
12171 | | * selection. |
12172 | | * |
12173 | | * This is the same as the C++ method GDALGroup::SubsetDimensionFromSelection(). |
12174 | | * |
12175 | | * @return a virtual group, to be freed with GDALGroupRelease(), or nullptr. |
12176 | | */ |
12177 | | GDALGroupH |
12178 | | GDALGroupSubsetDimensionFromSelection(GDALGroupH hGroup, |
12179 | | const char *pszSelection, |
12180 | | CPL_UNUSED CSLConstList papszOptions) |
12181 | 0 | { |
12182 | 0 | VALIDATE_POINTER1(hGroup, __func__, nullptr); |
12183 | 0 | VALIDATE_POINTER1(pszSelection, __func__, nullptr); |
12184 | 0 | auto hNewGroup = hGroup->m_poImpl->SubsetDimensionFromSelection( |
12185 | 0 | std::string(pszSelection)); |
12186 | 0 | if (!hNewGroup) |
12187 | 0 | return nullptr; |
12188 | 0 | return new GDALGroupHS(hNewGroup); |
12189 | 0 | } |
12190 | | |
12191 | | /************************************************************************/ |
12192 | | /* GDALMDArrayRelease() */ |
12193 | | /************************************************************************/ |
12194 | | |
12195 | | /** Release the GDAL in-memory object associated with a GDALMDArray. |
12196 | | * |
12197 | | * Note: when applied on a object coming from a driver, this does not |
12198 | | * destroy the object in the file, database, etc... |
12199 | | */ |
12200 | | void GDALMDArrayRelease(GDALMDArrayH hMDArray) |
12201 | 0 | { |
12202 | 0 | delete hMDArray; |
12203 | 0 | } |
12204 | | |
12205 | | /************************************************************************/ |
12206 | | /* GDALMDArrayGetName() */ |
12207 | | /************************************************************************/ |
12208 | | |
12209 | | /** Return array name. |
12210 | | * |
12211 | | * This is the same as the C++ method GDALMDArray::GetName() |
12212 | | */ |
12213 | | const char *GDALMDArrayGetName(GDALMDArrayH hArray) |
12214 | 0 | { |
12215 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12216 | 0 | return hArray->m_poImpl->GetName().c_str(); |
12217 | 0 | } |
12218 | | |
12219 | | /************************************************************************/ |
12220 | | /* GDALMDArrayGetFullName() */ |
12221 | | /************************************************************************/ |
12222 | | |
12223 | | /** Return array full name. |
12224 | | * |
12225 | | * This is the same as the C++ method GDALMDArray::GetFullName() |
12226 | | */ |
12227 | | const char *GDALMDArrayGetFullName(GDALMDArrayH hArray) |
12228 | 0 | { |
12229 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12230 | 0 | return hArray->m_poImpl->GetFullName().c_str(); |
12231 | 0 | } |
12232 | | |
12233 | | /************************************************************************/ |
12234 | | /* GDALMDArrayGetName() */ |
12235 | | /************************************************************************/ |
12236 | | |
12237 | | /** Return the total number of values in the array. |
12238 | | * |
12239 | | * This is the same as the C++ method |
12240 | | * GDALAbstractMDArray::GetTotalElementsCount() |
12241 | | */ |
12242 | | GUInt64 GDALMDArrayGetTotalElementsCount(GDALMDArrayH hArray) |
12243 | 0 | { |
12244 | 0 | VALIDATE_POINTER1(hArray, __func__, 0); |
12245 | 0 | return hArray->m_poImpl->GetTotalElementsCount(); |
12246 | 0 | } |
12247 | | |
12248 | | /************************************************************************/ |
12249 | | /* GDALMDArrayGetDimensionCount() */ |
12250 | | /************************************************************************/ |
12251 | | |
12252 | | /** Return the number of dimensions. |
12253 | | * |
12254 | | * This is the same as the C++ method GDALAbstractMDArray::GetDimensionCount() |
12255 | | */ |
12256 | | size_t GDALMDArrayGetDimensionCount(GDALMDArrayH hArray) |
12257 | 0 | { |
12258 | 0 | VALIDATE_POINTER1(hArray, __func__, 0); |
12259 | 0 | return hArray->m_poImpl->GetDimensionCount(); |
12260 | 0 | } |
12261 | | |
12262 | | /************************************************************************/ |
12263 | | /* GDALMDArrayGetDimensions() */ |
12264 | | /************************************************************************/ |
12265 | | |
12266 | | /** Return the dimensions of the array |
12267 | | * |
12268 | | * The returned array must be freed with GDALReleaseDimensions(). If only the |
12269 | | * array itself needs to be freed, CPLFree() should be called (and |
12270 | | * GDALDimensionRelease() on individual array members). |
12271 | | * |
12272 | | * This is the same as the C++ method GDALAbstractMDArray::GetDimensions() |
12273 | | * |
12274 | | * @param hArray Array. |
12275 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
12276 | | * |
12277 | | * @return an array of *pnCount dimensions. |
12278 | | */ |
12279 | | GDALDimensionH *GDALMDArrayGetDimensions(GDALMDArrayH hArray, size_t *pnCount) |
12280 | 0 | { |
12281 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12282 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
12283 | 0 | const auto &dims(hArray->m_poImpl->GetDimensions()); |
12284 | 0 | auto ret = static_cast<GDALDimensionH *>( |
12285 | 0 | CPLMalloc(sizeof(GDALDimensionH) * dims.size())); |
12286 | 0 | for (size_t i = 0; i < dims.size(); i++) |
12287 | 0 | { |
12288 | 0 | ret[i] = new GDALDimensionHS(dims[i]); |
12289 | 0 | } |
12290 | 0 | *pnCount = dims.size(); |
12291 | 0 | return ret; |
12292 | 0 | } |
12293 | | |
12294 | | /************************************************************************/ |
12295 | | /* GDALReleaseDimensions() */ |
12296 | | /************************************************************************/ |
12297 | | |
12298 | | /** Free the return of GDALGroupGetDimensions() or GDALMDArrayGetDimensions() |
12299 | | * |
12300 | | * @param dims return pointer of above methods |
12301 | | * @param nCount *pnCount value returned by above methods |
12302 | | */ |
12303 | | void GDALReleaseDimensions(GDALDimensionH *dims, size_t nCount) |
12304 | 0 | { |
12305 | 0 | for (size_t i = 0; i < nCount; i++) |
12306 | 0 | { |
12307 | 0 | delete dims[i]; |
12308 | 0 | } |
12309 | 0 | CPLFree(dims); |
12310 | 0 | } |
12311 | | |
12312 | | /************************************************************************/ |
12313 | | /* GDALMDArrayGetDataType() */ |
12314 | | /************************************************************************/ |
12315 | | |
12316 | | /** Return the data type |
12317 | | * |
12318 | | * The return must be freed with GDALExtendedDataTypeRelease(). |
12319 | | */ |
12320 | | GDALExtendedDataTypeH GDALMDArrayGetDataType(GDALMDArrayH hArray) |
12321 | 0 | { |
12322 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12323 | 0 | return new GDALExtendedDataTypeHS( |
12324 | 0 | new GDALExtendedDataType(hArray->m_poImpl->GetDataType())); |
12325 | 0 | } |
12326 | | |
12327 | | /************************************************************************/ |
12328 | | /* GDALMDArrayRead() */ |
12329 | | /************************************************************************/ |
12330 | | |
12331 | | /** Read part or totality of a multidimensional array. |
12332 | | * |
12333 | | * This is the same as the C++ method GDALAbstractMDArray::Read() |
12334 | | * |
12335 | | * @return TRUE in case of success. |
12336 | | */ |
12337 | | int GDALMDArrayRead(GDALMDArrayH hArray, const GUInt64 *arrayStartIdx, |
12338 | | const size_t *count, const GInt64 *arrayStep, |
12339 | | const GPtrDiff_t *bufferStride, |
12340 | | GDALExtendedDataTypeH bufferDataType, void *pDstBuffer, |
12341 | | const void *pDstBufferAllocStart, |
12342 | | size_t nDstBufferAllocSize) |
12343 | 0 | { |
12344 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12345 | 0 | if ((arrayStartIdx == nullptr || count == nullptr) && |
12346 | 0 | hArray->m_poImpl->GetDimensionCount() > 0) |
12347 | 0 | { |
12348 | 0 | VALIDATE_POINTER1(arrayStartIdx, __func__, FALSE); |
12349 | 0 | VALIDATE_POINTER1(count, __func__, FALSE); |
12350 | 0 | } |
12351 | 0 | VALIDATE_POINTER1(bufferDataType, __func__, FALSE); |
12352 | 0 | VALIDATE_POINTER1(pDstBuffer, __func__, FALSE); |
12353 | 0 | return hArray->m_poImpl->Read(arrayStartIdx, count, arrayStep, bufferStride, |
12354 | 0 | *(bufferDataType->m_poImpl), pDstBuffer, |
12355 | 0 | pDstBufferAllocStart, nDstBufferAllocSize); |
12356 | 0 | } |
12357 | | |
12358 | | /************************************************************************/ |
12359 | | /* GDALMDArrayWrite() */ |
12360 | | /************************************************************************/ |
12361 | | |
12362 | | /** Write part or totality of a multidimensional array. |
12363 | | * |
12364 | | * This is the same as the C++ method GDALAbstractMDArray::Write() |
12365 | | * |
12366 | | * @return TRUE in case of success. |
12367 | | */ |
12368 | | int GDALMDArrayWrite(GDALMDArrayH hArray, const GUInt64 *arrayStartIdx, |
12369 | | const size_t *count, const GInt64 *arrayStep, |
12370 | | const GPtrDiff_t *bufferStride, |
12371 | | GDALExtendedDataTypeH bufferDataType, |
12372 | | const void *pSrcBuffer, const void *pSrcBufferAllocStart, |
12373 | | size_t nSrcBufferAllocSize) |
12374 | 0 | { |
12375 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12376 | 0 | if ((arrayStartIdx == nullptr || count == nullptr) && |
12377 | 0 | hArray->m_poImpl->GetDimensionCount() > 0) |
12378 | 0 | { |
12379 | 0 | VALIDATE_POINTER1(arrayStartIdx, __func__, FALSE); |
12380 | 0 | VALIDATE_POINTER1(count, __func__, FALSE); |
12381 | 0 | } |
12382 | 0 | VALIDATE_POINTER1(bufferDataType, __func__, FALSE); |
12383 | 0 | VALIDATE_POINTER1(pSrcBuffer, __func__, FALSE); |
12384 | 0 | return hArray->m_poImpl->Write(arrayStartIdx, count, arrayStep, |
12385 | 0 | bufferStride, *(bufferDataType->m_poImpl), |
12386 | 0 | pSrcBuffer, pSrcBufferAllocStart, |
12387 | 0 | nSrcBufferAllocSize); |
12388 | 0 | } |
12389 | | |
12390 | | /************************************************************************/ |
12391 | | /* GDALMDArrayAdviseRead() */ |
12392 | | /************************************************************************/ |
12393 | | |
12394 | | /** Advise driver of upcoming read requests. |
12395 | | * |
12396 | | * This is the same as the C++ method GDALMDArray::AdviseRead() |
12397 | | * |
12398 | | * @return TRUE in case of success. |
12399 | | * |
12400 | | * @since GDAL 3.2 |
12401 | | */ |
12402 | | int GDALMDArrayAdviseRead(GDALMDArrayH hArray, const GUInt64 *arrayStartIdx, |
12403 | | const size_t *count) |
12404 | 0 | { |
12405 | 0 | return GDALMDArrayAdviseReadEx(hArray, arrayStartIdx, count, nullptr); |
12406 | 0 | } |
12407 | | |
12408 | | /************************************************************************/ |
12409 | | /* GDALMDArrayAdviseReadEx() */ |
12410 | | /************************************************************************/ |
12411 | | |
12412 | | /** Advise driver of upcoming read requests. |
12413 | | * |
12414 | | * This is the same as the C++ method GDALMDArray::AdviseRead() |
12415 | | * |
12416 | | * @return TRUE in case of success. |
12417 | | * |
12418 | | * @since GDAL 3.4 |
12419 | | */ |
12420 | | int GDALMDArrayAdviseReadEx(GDALMDArrayH hArray, const GUInt64 *arrayStartIdx, |
12421 | | const size_t *count, CSLConstList papszOptions) |
12422 | 0 | { |
12423 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12424 | 0 | return hArray->m_poImpl->AdviseRead(arrayStartIdx, count, papszOptions); |
12425 | 0 | } |
12426 | | |
12427 | | /************************************************************************/ |
12428 | | /* GDALMDArrayGetAttribute() */ |
12429 | | /************************************************************************/ |
12430 | | |
12431 | | /** Return an attribute by its name. |
12432 | | * |
12433 | | * This is the same as the C++ method GDALIHasAttribute::GetAttribute() |
12434 | | * |
12435 | | * The returned attribute must be freed with GDALAttributeRelease(). |
12436 | | */ |
12437 | | GDALAttributeH GDALMDArrayGetAttribute(GDALMDArrayH hArray, const char *pszName) |
12438 | 0 | { |
12439 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12440 | 0 | VALIDATE_POINTER1(pszName, __func__, nullptr); |
12441 | 0 | auto attr = hArray->m_poImpl->GetAttribute(std::string(pszName)); |
12442 | 0 | if (attr) |
12443 | 0 | return new GDALAttributeHS(attr); |
12444 | 0 | return nullptr; |
12445 | 0 | } |
12446 | | |
12447 | | /************************************************************************/ |
12448 | | /* GDALMDArrayGetAttributes() */ |
12449 | | /************************************************************************/ |
12450 | | |
12451 | | /** Return the list of attributes contained in this array. |
12452 | | * |
12453 | | * The returned array must be freed with GDALReleaseAttributes(). If only the |
12454 | | * array itself needs to be freed, CPLFree() should be called (and |
12455 | | * GDALAttributeRelease() on individual array members). |
12456 | | * |
12457 | | * This is the same as the C++ method GDALMDArray::GetAttributes(). |
12458 | | * |
12459 | | * @param hArray Array. |
12460 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
12461 | | * @param papszOptions Driver specific options determining how attributes |
12462 | | * should be retrieved. Pass nullptr for default behavior. |
12463 | | * |
12464 | | * @return an array of *pnCount attributes. |
12465 | | */ |
12466 | | GDALAttributeH *GDALMDArrayGetAttributes(GDALMDArrayH hArray, size_t *pnCount, |
12467 | | CSLConstList papszOptions) |
12468 | 0 | { |
12469 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12470 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
12471 | 0 | auto attrs = hArray->m_poImpl->GetAttributes(papszOptions); |
12472 | 0 | auto ret = static_cast<GDALAttributeH *>( |
12473 | 0 | CPLMalloc(sizeof(GDALAttributeH) * attrs.size())); |
12474 | 0 | for (size_t i = 0; i < attrs.size(); i++) |
12475 | 0 | { |
12476 | 0 | ret[i] = new GDALAttributeHS(attrs[i]); |
12477 | 0 | } |
12478 | 0 | *pnCount = attrs.size(); |
12479 | 0 | return ret; |
12480 | 0 | } |
12481 | | |
12482 | | /************************************************************************/ |
12483 | | /* GDALMDArrayCreateAttribute() */ |
12484 | | /************************************************************************/ |
12485 | | |
12486 | | /** Create a attribute within an array. |
12487 | | * |
12488 | | * This is the same as the C++ method GDALMDArray::CreateAttribute(). |
12489 | | * |
12490 | | * @return the attribute, to be freed with GDALAttributeRelease(), or nullptr. |
12491 | | */ |
12492 | | GDALAttributeH GDALMDArrayCreateAttribute(GDALMDArrayH hArray, |
12493 | | const char *pszName, |
12494 | | size_t nDimensions, |
12495 | | const GUInt64 *panDimensions, |
12496 | | GDALExtendedDataTypeH hEDT, |
12497 | | CSLConstList papszOptions) |
12498 | 0 | { |
12499 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12500 | 0 | VALIDATE_POINTER1(pszName, __func__, nullptr); |
12501 | 0 | VALIDATE_POINTER1(hEDT, __func__, nullptr); |
12502 | 0 | std::vector<GUInt64> dims; |
12503 | 0 | dims.reserve(nDimensions); |
12504 | 0 | for (size_t i = 0; i < nDimensions; i++) |
12505 | 0 | dims.push_back(panDimensions[i]); |
12506 | 0 | auto ret = hArray->m_poImpl->CreateAttribute( |
12507 | 0 | std::string(pszName), dims, *(hEDT->m_poImpl), papszOptions); |
12508 | 0 | if (!ret) |
12509 | 0 | return nullptr; |
12510 | 0 | return new GDALAttributeHS(ret); |
12511 | 0 | } |
12512 | | |
12513 | | /************************************************************************/ |
12514 | | /* GDALMDArrayDeleteAttribute() */ |
12515 | | /************************************************************************/ |
12516 | | |
12517 | | /** Delete an attribute from an array. |
12518 | | * |
12519 | | * After this call, if a previously obtained instance of the deleted object |
12520 | | * is still alive, no method other than for freeing it should be invoked. |
12521 | | * |
12522 | | * This is the same as the C++ method GDALMDArray::DeleteAttribute(). |
12523 | | * |
12524 | | * @return true in case of success. |
12525 | | * @since GDAL 3.8 |
12526 | | */ |
12527 | | bool GDALMDArrayDeleteAttribute(GDALMDArrayH hArray, const char *pszName, |
12528 | | CSLConstList papszOptions) |
12529 | 0 | { |
12530 | 0 | VALIDATE_POINTER1(hArray, __func__, false); |
12531 | 0 | VALIDATE_POINTER1(pszName, __func__, false); |
12532 | 0 | return hArray->m_poImpl->DeleteAttribute(std::string(pszName), |
12533 | 0 | papszOptions); |
12534 | 0 | } |
12535 | | |
12536 | | /************************************************************************/ |
12537 | | /* GDALMDArrayGetRawNoDataValue() */ |
12538 | | /************************************************************************/ |
12539 | | |
12540 | | /** Return the nodata value as a "raw" value. |
12541 | | * |
12542 | | * The value returned might be nullptr in case of no nodata value. When |
12543 | | * a nodata value is registered, a non-nullptr will be returned whose size in |
12544 | | * bytes is GetDataType().GetSize(). |
12545 | | * |
12546 | | * The returned value should not be modified or freed. |
12547 | | * |
12548 | | * This is the same as the ++ method GDALMDArray::GetRawNoDataValue(). |
12549 | | * |
12550 | | * @return nullptr or a pointer to GetDataType().GetSize() bytes. |
12551 | | */ |
12552 | | const void *GDALMDArrayGetRawNoDataValue(GDALMDArrayH hArray) |
12553 | 0 | { |
12554 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12555 | 0 | return hArray->m_poImpl->GetRawNoDataValue(); |
12556 | 0 | } |
12557 | | |
12558 | | /************************************************************************/ |
12559 | | /* GDALMDArrayGetNoDataValueAsDouble() */ |
12560 | | /************************************************************************/ |
12561 | | |
12562 | | /** Return the nodata value as a double. |
12563 | | * |
12564 | | * The value returned might be nullptr in case of no nodata value. When |
12565 | | * a nodata value is registered, a non-nullptr will be returned whose size in |
12566 | | * bytes is GetDataType().GetSize(). |
12567 | | * |
12568 | | * This is the same as the C++ method GDALMDArray::GetNoDataValueAsDouble(). |
12569 | | * |
12570 | | * @param hArray Array handle. |
12571 | | * @param pbHasNoDataValue Pointer to a output boolean that will be set to true |
12572 | | * if a nodata value exists and can be converted to double. Might be nullptr. |
12573 | | * |
12574 | | * @return the nodata value as a double. A 0.0 value might also indicate the |
12575 | | * absence of a nodata value or an error in the conversion (*pbHasNoDataValue |
12576 | | * will be set to false then). |
12577 | | */ |
12578 | | double GDALMDArrayGetNoDataValueAsDouble(GDALMDArrayH hArray, |
12579 | | int *pbHasNoDataValue) |
12580 | 0 | { |
12581 | 0 | VALIDATE_POINTER1(hArray, __func__, 0); |
12582 | 0 | bool bHasNodataValue = false; |
12583 | 0 | double ret = hArray->m_poImpl->GetNoDataValueAsDouble(&bHasNodataValue); |
12584 | 0 | if (pbHasNoDataValue) |
12585 | 0 | *pbHasNoDataValue = bHasNodataValue; |
12586 | 0 | return ret; |
12587 | 0 | } |
12588 | | |
12589 | | /************************************************************************/ |
12590 | | /* GDALMDArrayGetNoDataValueAsInt64() */ |
12591 | | /************************************************************************/ |
12592 | | |
12593 | | /** Return the nodata value as a Int64. |
12594 | | * |
12595 | | * This is the same as the C++ method GDALMDArray::GetNoDataValueAsInt64(). |
12596 | | * |
12597 | | * @param hArray Array handle. |
12598 | | * @param pbHasNoDataValue Pointer to a output boolean that will be set to true |
12599 | | * if a nodata value exists and can be converted to Int64. Might be nullptr. |
12600 | | * |
12601 | | * @return the nodata value as a Int64. |
12602 | | * @since GDAL 3.5 |
12603 | | */ |
12604 | | int64_t GDALMDArrayGetNoDataValueAsInt64(GDALMDArrayH hArray, |
12605 | | int *pbHasNoDataValue) |
12606 | 0 | { |
12607 | 0 | VALIDATE_POINTER1(hArray, __func__, 0); |
12608 | 0 | bool bHasNodataValue = false; |
12609 | 0 | const auto ret = hArray->m_poImpl->GetNoDataValueAsInt64(&bHasNodataValue); |
12610 | 0 | if (pbHasNoDataValue) |
12611 | 0 | *pbHasNoDataValue = bHasNodataValue; |
12612 | 0 | return ret; |
12613 | 0 | } |
12614 | | |
12615 | | /************************************************************************/ |
12616 | | /* GDALMDArrayGetNoDataValueAsUInt64() */ |
12617 | | /************************************************************************/ |
12618 | | |
12619 | | /** Return the nodata value as a UInt64. |
12620 | | * |
12621 | | * This is the same as the C++ method GDALMDArray::GetNoDataValueAsInt64(). |
12622 | | * |
12623 | | * @param hArray Array handle. |
12624 | | * @param pbHasNoDataValue Pointer to a output boolean that will be set to true |
12625 | | * if a nodata value exists and can be converted to UInt64. Might be nullptr. |
12626 | | * |
12627 | | * @return the nodata value as a UInt64. |
12628 | | * @since GDAL 3.5 |
12629 | | */ |
12630 | | uint64_t GDALMDArrayGetNoDataValueAsUInt64(GDALMDArrayH hArray, |
12631 | | int *pbHasNoDataValue) |
12632 | 0 | { |
12633 | 0 | VALIDATE_POINTER1(hArray, __func__, 0); |
12634 | 0 | bool bHasNodataValue = false; |
12635 | 0 | const auto ret = hArray->m_poImpl->GetNoDataValueAsUInt64(&bHasNodataValue); |
12636 | 0 | if (pbHasNoDataValue) |
12637 | 0 | *pbHasNoDataValue = bHasNodataValue; |
12638 | 0 | return ret; |
12639 | 0 | } |
12640 | | |
12641 | | /************************************************************************/ |
12642 | | /* GDALMDArraySetRawNoDataValue() */ |
12643 | | /************************************************************************/ |
12644 | | |
12645 | | /** Set the nodata value as a "raw" value. |
12646 | | * |
12647 | | * This is the same as the C++ method GDALMDArray::SetRawNoDataValue(const |
12648 | | * void*). |
12649 | | * |
12650 | | * @return TRUE in case of success. |
12651 | | */ |
12652 | | int GDALMDArraySetRawNoDataValue(GDALMDArrayH hArray, const void *pNoData) |
12653 | 0 | { |
12654 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12655 | 0 | return hArray->m_poImpl->SetRawNoDataValue(pNoData); |
12656 | 0 | } |
12657 | | |
12658 | | /************************************************************************/ |
12659 | | /* GDALMDArraySetNoDataValueAsDouble() */ |
12660 | | /************************************************************************/ |
12661 | | |
12662 | | /** Set the nodata value as a double. |
12663 | | * |
12664 | | * If the natural data type of the attribute/array is not double, type |
12665 | | * conversion will occur to the type returned by GetDataType(). |
12666 | | * |
12667 | | * This is the same as the C++ method GDALMDArray::SetNoDataValue(double). |
12668 | | * |
12669 | | * @return TRUE in case of success. |
12670 | | */ |
12671 | | int GDALMDArraySetNoDataValueAsDouble(GDALMDArrayH hArray, double dfNoDataValue) |
12672 | 0 | { |
12673 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12674 | 0 | return hArray->m_poImpl->SetNoDataValue(dfNoDataValue); |
12675 | 0 | } |
12676 | | |
12677 | | /************************************************************************/ |
12678 | | /* GDALMDArraySetNoDataValueAsInt64() */ |
12679 | | /************************************************************************/ |
12680 | | |
12681 | | /** Set the nodata value as a Int64. |
12682 | | * |
12683 | | * If the natural data type of the attribute/array is not Int64, type conversion |
12684 | | * will occur to the type returned by GetDataType(). |
12685 | | * |
12686 | | * This is the same as the C++ method GDALMDArray::SetNoDataValue(int64_t). |
12687 | | * |
12688 | | * @return TRUE in case of success. |
12689 | | * @since GDAL 3.5 |
12690 | | */ |
12691 | | int GDALMDArraySetNoDataValueAsInt64(GDALMDArrayH hArray, int64_t nNoDataValue) |
12692 | 0 | { |
12693 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12694 | 0 | return hArray->m_poImpl->SetNoDataValue(nNoDataValue); |
12695 | 0 | } |
12696 | | |
12697 | | /************************************************************************/ |
12698 | | /* GDALMDArraySetNoDataValueAsUInt64() */ |
12699 | | /************************************************************************/ |
12700 | | |
12701 | | /** Set the nodata value as a UInt64. |
12702 | | * |
12703 | | * If the natural data type of the attribute/array is not UInt64, type |
12704 | | * conversion will occur to the type returned by GetDataType(). |
12705 | | * |
12706 | | * This is the same as the C++ method GDALMDArray::SetNoDataValue(uint64_t). |
12707 | | * |
12708 | | * @return TRUE in case of success. |
12709 | | * @since GDAL 3.5 |
12710 | | */ |
12711 | | int GDALMDArraySetNoDataValueAsUInt64(GDALMDArrayH hArray, |
12712 | | uint64_t nNoDataValue) |
12713 | 0 | { |
12714 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12715 | 0 | return hArray->m_poImpl->SetNoDataValue(nNoDataValue); |
12716 | 0 | } |
12717 | | |
12718 | | /************************************************************************/ |
12719 | | /* GDALMDArrayResize() */ |
12720 | | /************************************************************************/ |
12721 | | |
12722 | | /** Resize an array to new dimensions. |
12723 | | * |
12724 | | * Not all drivers may allow this operation, and with restrictions (e.g. |
12725 | | * for netCDF, this is limited to growing of "unlimited" dimensions) |
12726 | | * |
12727 | | * Resizing a dimension used in other arrays will cause those other arrays |
12728 | | * to be resized. |
12729 | | * |
12730 | | * This is the same as the C++ method GDALMDArray::Resize(). |
12731 | | * |
12732 | | * @param hArray Array. |
12733 | | * @param panNewDimSizes Array of GetDimensionCount() values containing the |
12734 | | * new size of each indexing dimension. |
12735 | | * @param papszOptions Options. (Driver specific) |
12736 | | * @return true in case of success. |
12737 | | * @since GDAL 3.7 |
12738 | | */ |
12739 | | bool GDALMDArrayResize(GDALMDArrayH hArray, const GUInt64 *panNewDimSizes, |
12740 | | CSLConstList papszOptions) |
12741 | 0 | { |
12742 | 0 | VALIDATE_POINTER1(hArray, __func__, false); |
12743 | 0 | VALIDATE_POINTER1(panNewDimSizes, __func__, false); |
12744 | 0 | std::vector<GUInt64> anNewDimSizes(hArray->m_poImpl->GetDimensionCount()); |
12745 | 0 | for (size_t i = 0; i < anNewDimSizes.size(); ++i) |
12746 | 0 | { |
12747 | 0 | anNewDimSizes[i] = panNewDimSizes[i]; |
12748 | 0 | } |
12749 | 0 | return hArray->m_poImpl->Resize(anNewDimSizes, papszOptions); |
12750 | 0 | } |
12751 | | |
12752 | | /************************************************************************/ |
12753 | | /* GDALMDArraySetScale() */ |
12754 | | /************************************************************************/ |
12755 | | |
12756 | | /** Set the scale value to apply to raw values. |
12757 | | * |
12758 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
12759 | | * |
12760 | | * This is the same as the C++ method GDALMDArray::SetScale(). |
12761 | | * |
12762 | | * @return TRUE in case of success. |
12763 | | */ |
12764 | | int GDALMDArraySetScale(GDALMDArrayH hArray, double dfScale) |
12765 | 0 | { |
12766 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12767 | 0 | return hArray->m_poImpl->SetScale(dfScale); |
12768 | 0 | } |
12769 | | |
12770 | | /************************************************************************/ |
12771 | | /* GDALMDArraySetScaleEx() */ |
12772 | | /************************************************************************/ |
12773 | | |
12774 | | /** Set the scale value to apply to raw values. |
12775 | | * |
12776 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
12777 | | * |
12778 | | * This is the same as the C++ method GDALMDArray::SetScale(). |
12779 | | * |
12780 | | * @return TRUE in case of success. |
12781 | | * @since GDAL 3.3 |
12782 | | */ |
12783 | | int GDALMDArraySetScaleEx(GDALMDArrayH hArray, double dfScale, |
12784 | | GDALDataType eStorageType) |
12785 | 0 | { |
12786 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12787 | 0 | return hArray->m_poImpl->SetScale(dfScale, eStorageType); |
12788 | 0 | } |
12789 | | |
12790 | | /************************************************************************/ |
12791 | | /* GDALMDArraySetOffset() */ |
12792 | | /************************************************************************/ |
12793 | | |
12794 | | /** Set the scale value to apply to raw values. |
12795 | | * |
12796 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
12797 | | * |
12798 | | * This is the same as the C++ method GDALMDArray::SetOffset(). |
12799 | | * |
12800 | | * @return TRUE in case of success. |
12801 | | */ |
12802 | | int GDALMDArraySetOffset(GDALMDArrayH hArray, double dfOffset) |
12803 | 0 | { |
12804 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12805 | 0 | return hArray->m_poImpl->SetOffset(dfOffset); |
12806 | 0 | } |
12807 | | |
12808 | | /************************************************************************/ |
12809 | | /* GDALMDArraySetOffsetEx() */ |
12810 | | /************************************************************************/ |
12811 | | |
12812 | | /** Set the scale value to apply to raw values. |
12813 | | * |
12814 | | * unscaled_value = raw_value * GetOffset() + GetOffset() |
12815 | | * |
12816 | | * This is the same as the C++ method GDALMDArray::SetOffset(). |
12817 | | * |
12818 | | * @return TRUE in case of success. |
12819 | | * @since GDAL 3.3 |
12820 | | */ |
12821 | | int GDALMDArraySetOffsetEx(GDALMDArrayH hArray, double dfOffset, |
12822 | | GDALDataType eStorageType) |
12823 | 0 | { |
12824 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
12825 | 0 | return hArray->m_poImpl->SetOffset(dfOffset, eStorageType); |
12826 | 0 | } |
12827 | | |
12828 | | /************************************************************************/ |
12829 | | /* GDALMDArrayGetScale() */ |
12830 | | /************************************************************************/ |
12831 | | |
12832 | | /** Get the scale value to apply to raw values. |
12833 | | * |
12834 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
12835 | | * |
12836 | | * This is the same as the C++ method GDALMDArray::GetScale(). |
12837 | | * |
12838 | | * @return the scale value |
12839 | | */ |
12840 | | double GDALMDArrayGetScale(GDALMDArrayH hArray, int *pbHasValue) |
12841 | 0 | { |
12842 | 0 | VALIDATE_POINTER1(hArray, __func__, 0.0); |
12843 | 0 | bool bHasValue = false; |
12844 | 0 | double dfRet = hArray->m_poImpl->GetScale(&bHasValue); |
12845 | 0 | if (pbHasValue) |
12846 | 0 | *pbHasValue = bHasValue; |
12847 | 0 | return dfRet; |
12848 | 0 | } |
12849 | | |
12850 | | /************************************************************************/ |
12851 | | /* GDALMDArrayGetScaleEx() */ |
12852 | | /************************************************************************/ |
12853 | | |
12854 | | /** Get the scale value to apply to raw values. |
12855 | | * |
12856 | | * unscaled_value = raw_value * GetScale() + GetScale() |
12857 | | * |
12858 | | * This is the same as the C++ method GDALMDArray::GetScale(). |
12859 | | * |
12860 | | * @return the scale value |
12861 | | * @since GDAL 3.3 |
12862 | | */ |
12863 | | double GDALMDArrayGetScaleEx(GDALMDArrayH hArray, int *pbHasValue, |
12864 | | GDALDataType *peStorageType) |
12865 | 0 | { |
12866 | 0 | VALIDATE_POINTER1(hArray, __func__, 0.0); |
12867 | 0 | bool bHasValue = false; |
12868 | 0 | double dfRet = hArray->m_poImpl->GetScale(&bHasValue, peStorageType); |
12869 | 0 | if (pbHasValue) |
12870 | 0 | *pbHasValue = bHasValue; |
12871 | 0 | return dfRet; |
12872 | 0 | } |
12873 | | |
12874 | | /************************************************************************/ |
12875 | | /* GDALMDArrayGetOffset() */ |
12876 | | /************************************************************************/ |
12877 | | |
12878 | | /** Get the scale value to apply to raw values. |
12879 | | * |
12880 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
12881 | | * |
12882 | | * This is the same as the C++ method GDALMDArray::GetOffset(). |
12883 | | * |
12884 | | * @return the scale value |
12885 | | */ |
12886 | | double GDALMDArrayGetOffset(GDALMDArrayH hArray, int *pbHasValue) |
12887 | 0 | { |
12888 | 0 | VALIDATE_POINTER1(hArray, __func__, 0.0); |
12889 | 0 | bool bHasValue = false; |
12890 | 0 | double dfRet = hArray->m_poImpl->GetOffset(&bHasValue); |
12891 | 0 | if (pbHasValue) |
12892 | 0 | *pbHasValue = bHasValue; |
12893 | 0 | return dfRet; |
12894 | 0 | } |
12895 | | |
12896 | | /************************************************************************/ |
12897 | | /* GDALMDArrayGetOffsetEx() */ |
12898 | | /************************************************************************/ |
12899 | | |
12900 | | /** Get the scale value to apply to raw values. |
12901 | | * |
12902 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
12903 | | * |
12904 | | * This is the same as the C++ method GDALMDArray::GetOffset(). |
12905 | | * |
12906 | | * @return the scale value |
12907 | | * @since GDAL 3.3 |
12908 | | */ |
12909 | | double GDALMDArrayGetOffsetEx(GDALMDArrayH hArray, int *pbHasValue, |
12910 | | GDALDataType *peStorageType) |
12911 | 0 | { |
12912 | 0 | VALIDATE_POINTER1(hArray, __func__, 0.0); |
12913 | 0 | bool bHasValue = false; |
12914 | 0 | double dfRet = hArray->m_poImpl->GetOffset(&bHasValue, peStorageType); |
12915 | 0 | if (pbHasValue) |
12916 | 0 | *pbHasValue = bHasValue; |
12917 | 0 | return dfRet; |
12918 | 0 | } |
12919 | | |
12920 | | /************************************************************************/ |
12921 | | /* GDALMDArrayGetBlockSize() */ |
12922 | | /************************************************************************/ |
12923 | | |
12924 | | /** Return the "natural" block size of the array along all dimensions. |
12925 | | * |
12926 | | * Some drivers might organize the array in tiles/blocks and reading/writing |
12927 | | * aligned on those tile/block boundaries will be more efficient. |
12928 | | * |
12929 | | * The returned number of elements in the vector is the same as |
12930 | | * GetDimensionCount(). A value of 0 should be interpreted as no hint regarding |
12931 | | * the natural block size along the considered dimension. |
12932 | | * "Flat" arrays will typically return a vector of values set to 0. |
12933 | | * |
12934 | | * The default implementation will return a vector of values set to 0. |
12935 | | * |
12936 | | * This method is used by GetProcessingChunkSize(). |
12937 | | * |
12938 | | * Pedantic note: the returned type is GUInt64, so in the highly unlikeley |
12939 | | * theoretical case of a 32-bit platform, this might exceed its size_t |
12940 | | * allocation capabilities. |
12941 | | * |
12942 | | * This is the same as the C++ method GDALAbstractMDArray::GetBlockSize(). |
12943 | | * |
12944 | | * @return the block size, in number of elements along each dimension. |
12945 | | */ |
12946 | | GUInt64 *GDALMDArrayGetBlockSize(GDALMDArrayH hArray, size_t *pnCount) |
12947 | 0 | { |
12948 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12949 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
12950 | 0 | auto res = hArray->m_poImpl->GetBlockSize(); |
12951 | 0 | auto ret = static_cast<GUInt64 *>(CPLMalloc(sizeof(GUInt64) * res.size())); |
12952 | 0 | for (size_t i = 0; i < res.size(); i++) |
12953 | 0 | { |
12954 | 0 | ret[i] = res[i]; |
12955 | 0 | } |
12956 | 0 | *pnCount = res.size(); |
12957 | 0 | return ret; |
12958 | 0 | } |
12959 | | |
12960 | | /***********************************************************************/ |
12961 | | /* GDALMDArrayGetProcessingChunkSize() */ |
12962 | | /************************************************************************/ |
12963 | | |
12964 | | /** \brief Return an optimal chunk size for read/write operations, given the |
12965 | | * natural block size and memory constraints specified. |
12966 | | * |
12967 | | * This method will use GetBlockSize() to define a chunk whose dimensions are |
12968 | | * multiple of those returned by GetBlockSize() (unless the block define by |
12969 | | * GetBlockSize() is larger than nMaxChunkMemory, in which case it will be |
12970 | | * returned by this method). |
12971 | | * |
12972 | | * This is the same as the C++ method |
12973 | | * GDALAbstractMDArray::GetProcessingChunkSize(). |
12974 | | * |
12975 | | * @param hArray Array. |
12976 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
12977 | | * @param nMaxChunkMemory Maximum amount of memory, in bytes, to use for the |
12978 | | * chunk. |
12979 | | * |
12980 | | * @return the chunk size, in number of elements along each dimension. |
12981 | | */ |
12982 | | |
12983 | | size_t *GDALMDArrayGetProcessingChunkSize(GDALMDArrayH hArray, size_t *pnCount, |
12984 | | size_t nMaxChunkMemory) |
12985 | 0 | { |
12986 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
12987 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
12988 | 0 | auto res = hArray->m_poImpl->GetProcessingChunkSize(nMaxChunkMemory); |
12989 | 0 | auto ret = static_cast<size_t *>(CPLMalloc(sizeof(size_t) * res.size())); |
12990 | 0 | for (size_t i = 0; i < res.size(); i++) |
12991 | 0 | { |
12992 | 0 | ret[i] = res[i]; |
12993 | 0 | } |
12994 | 0 | *pnCount = res.size(); |
12995 | 0 | return ret; |
12996 | 0 | } |
12997 | | |
12998 | | /************************************************************************/ |
12999 | | /* GDALMDArrayGetStructuralInfo() */ |
13000 | | /************************************************************************/ |
13001 | | |
13002 | | /** Return structural information on the array. |
13003 | | * |
13004 | | * This may be the compression, etc.. |
13005 | | * |
13006 | | * The return value should not be freed and is valid until GDALMDArray is |
13007 | | * released or this function called again. |
13008 | | * |
13009 | | * This is the same as the C++ method GDALMDArray::GetStructuralInfo(). |
13010 | | */ |
13011 | | CSLConstList GDALMDArrayGetStructuralInfo(GDALMDArrayH hArray) |
13012 | 0 | { |
13013 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13014 | 0 | return hArray->m_poImpl->GetStructuralInfo(); |
13015 | 0 | } |
13016 | | |
13017 | | /************************************************************************/ |
13018 | | /* GDALMDArrayGetView() */ |
13019 | | /************************************************************************/ |
13020 | | |
13021 | | /** Return a view of the array using slicing or field access. |
13022 | | * |
13023 | | * The returned object should be released with GDALMDArrayRelease(). |
13024 | | * |
13025 | | * This is the same as the C++ method GDALMDArray::GetView(). |
13026 | | */ |
13027 | | GDALMDArrayH GDALMDArrayGetView(GDALMDArrayH hArray, const char *pszViewExpr) |
13028 | 0 | { |
13029 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13030 | 0 | VALIDATE_POINTER1(pszViewExpr, __func__, nullptr); |
13031 | 0 | auto sliced = hArray->m_poImpl->GetView(std::string(pszViewExpr)); |
13032 | 0 | if (!sliced) |
13033 | 0 | return nullptr; |
13034 | 0 | return new GDALMDArrayHS(sliced); |
13035 | 0 | } |
13036 | | |
13037 | | /************************************************************************/ |
13038 | | /* GDALMDArrayTranspose() */ |
13039 | | /************************************************************************/ |
13040 | | |
13041 | | /** Return a view of the array whose axis have been reordered. |
13042 | | * |
13043 | | * The returned object should be released with GDALMDArrayRelease(). |
13044 | | * |
13045 | | * This is the same as the C++ method GDALMDArray::Transpose(). |
13046 | | */ |
13047 | | GDALMDArrayH GDALMDArrayTranspose(GDALMDArrayH hArray, size_t nNewAxisCount, |
13048 | | const int *panMapNewAxisToOldAxis) |
13049 | 0 | { |
13050 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13051 | 0 | std::vector<int> anMapNewAxisToOldAxis(nNewAxisCount); |
13052 | 0 | if (nNewAxisCount) |
13053 | 0 | { |
13054 | 0 | memcpy(&anMapNewAxisToOldAxis[0], panMapNewAxisToOldAxis, |
13055 | 0 | nNewAxisCount * sizeof(int)); |
13056 | 0 | } |
13057 | 0 | auto reordered = hArray->m_poImpl->Transpose(anMapNewAxisToOldAxis); |
13058 | 0 | if (!reordered) |
13059 | 0 | return nullptr; |
13060 | 0 | return new GDALMDArrayHS(reordered); |
13061 | 0 | } |
13062 | | |
13063 | | /************************************************************************/ |
13064 | | /* GDALMDArrayGetUnscaled() */ |
13065 | | /************************************************************************/ |
13066 | | |
13067 | | /** Return an array that is the unscaled version of the current one. |
13068 | | * |
13069 | | * That is each value of the unscaled array will be |
13070 | | * unscaled_value = raw_value * GetScale() + GetOffset() |
13071 | | * |
13072 | | * Starting with GDAL 3.3, the Write() method is implemented and will convert |
13073 | | * from unscaled values to raw values. |
13074 | | * |
13075 | | * The returned object should be released with GDALMDArrayRelease(). |
13076 | | * |
13077 | | * This is the same as the C++ method GDALMDArray::GetUnscaled(). |
13078 | | */ |
13079 | | GDALMDArrayH GDALMDArrayGetUnscaled(GDALMDArrayH hArray) |
13080 | 0 | { |
13081 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13082 | 0 | auto unscaled = hArray->m_poImpl->GetUnscaled(); |
13083 | 0 | if (!unscaled) |
13084 | 0 | return nullptr; |
13085 | 0 | return new GDALMDArrayHS(unscaled); |
13086 | 0 | } |
13087 | | |
13088 | | /************************************************************************/ |
13089 | | /* GDALMDArrayGetMask() */ |
13090 | | /************************************************************************/ |
13091 | | |
13092 | | /** Return an array that is a mask for the current array |
13093 | | * |
13094 | | * This array will be of type Byte, with values set to 0 to indicate invalid |
13095 | | * pixels of the current array, and values set to 1 to indicate valid pixels. |
13096 | | * |
13097 | | * The returned object should be released with GDALMDArrayRelease(). |
13098 | | * |
13099 | | * This is the same as the C++ method GDALMDArray::GetMask(). |
13100 | | */ |
13101 | | GDALMDArrayH GDALMDArrayGetMask(GDALMDArrayH hArray, CSLConstList papszOptions) |
13102 | 0 | { |
13103 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13104 | 0 | auto unscaled = hArray->m_poImpl->GetMask(papszOptions); |
13105 | 0 | if (!unscaled) |
13106 | 0 | return nullptr; |
13107 | 0 | return new GDALMDArrayHS(unscaled); |
13108 | 0 | } |
13109 | | |
13110 | | /************************************************************************/ |
13111 | | /* GDALMDArrayGetResampled() */ |
13112 | | /************************************************************************/ |
13113 | | |
13114 | | /** Return an array that is a resampled / reprojected view of the current array |
13115 | | * |
13116 | | * This is the same as the C++ method GDALMDArray::GetResampled(). |
13117 | | * |
13118 | | * Currently this method can only resample along the last 2 dimensions, unless |
13119 | | * orthorectifying a NASA EMIT dataset. |
13120 | | * |
13121 | | * The returned object should be released with GDALMDArrayRelease(). |
13122 | | * |
13123 | | * @since 3.4 |
13124 | | */ |
13125 | | GDALMDArrayH GDALMDArrayGetResampled(GDALMDArrayH hArray, size_t nNewDimCount, |
13126 | | const GDALDimensionH *pahNewDims, |
13127 | | GDALRIOResampleAlg resampleAlg, |
13128 | | OGRSpatialReferenceH hTargetSRS, |
13129 | | CSLConstList papszOptions) |
13130 | 0 | { |
13131 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13132 | 0 | VALIDATE_POINTER1(pahNewDims, __func__, nullptr); |
13133 | 0 | std::vector<std::shared_ptr<GDALDimension>> apoNewDims(nNewDimCount); |
13134 | 0 | for (size_t i = 0; i < nNewDimCount; ++i) |
13135 | 0 | { |
13136 | 0 | if (pahNewDims[i]) |
13137 | 0 | apoNewDims[i] = pahNewDims[i]->m_poImpl; |
13138 | 0 | } |
13139 | 0 | auto poNewArray = hArray->m_poImpl->GetResampled( |
13140 | 0 | apoNewDims, resampleAlg, OGRSpatialReference::FromHandle(hTargetSRS), |
13141 | 0 | papszOptions); |
13142 | 0 | if (!poNewArray) |
13143 | 0 | return nullptr; |
13144 | 0 | return new GDALMDArrayHS(poNewArray); |
13145 | 0 | } |
13146 | | |
13147 | | /************************************************************************/ |
13148 | | /* GDALMDArraySetUnit() */ |
13149 | | /************************************************************************/ |
13150 | | |
13151 | | /** Set the variable unit. |
13152 | | * |
13153 | | * Values should conform as much as possible with those allowed by |
13154 | | * the NetCDF CF conventions: |
13155 | | * http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.html#units |
13156 | | * but others might be returned. |
13157 | | * |
13158 | | * Few examples are "meter", "degrees", "second", ... |
13159 | | * Empty value means unknown. |
13160 | | * |
13161 | | * This is the same as the C function GDALMDArraySetUnit() |
13162 | | * |
13163 | | * @param hArray array. |
13164 | | * @param pszUnit unit name. |
13165 | | * @return TRUE in case of success. |
13166 | | */ |
13167 | | int GDALMDArraySetUnit(GDALMDArrayH hArray, const char *pszUnit) |
13168 | 0 | { |
13169 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
13170 | 0 | return hArray->m_poImpl->SetUnit(pszUnit ? pszUnit : ""); |
13171 | 0 | } |
13172 | | |
13173 | | /************************************************************************/ |
13174 | | /* GDALMDArrayGetUnit() */ |
13175 | | /************************************************************************/ |
13176 | | |
13177 | | /** Return the array unit. |
13178 | | * |
13179 | | * Values should conform as much as possible with those allowed by |
13180 | | * the NetCDF CF conventions: |
13181 | | * http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.html#units |
13182 | | * but others might be returned. |
13183 | | * |
13184 | | * Few examples are "meter", "degrees", "second", ... |
13185 | | * Empty value means unknown. |
13186 | | * |
13187 | | * The return value should not be freed and is valid until GDALMDArray is |
13188 | | * released or this function called again. |
13189 | | * |
13190 | | * This is the same as the C++ method GDALMDArray::GetUnit(). |
13191 | | */ |
13192 | | const char *GDALMDArrayGetUnit(GDALMDArrayH hArray) |
13193 | 0 | { |
13194 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13195 | 0 | return hArray->m_poImpl->GetUnit().c_str(); |
13196 | 0 | } |
13197 | | |
13198 | | /************************************************************************/ |
13199 | | /* GDALMDArrayGetSpatialRef() */ |
13200 | | /************************************************************************/ |
13201 | | |
13202 | | /** Assign a spatial reference system object to the array. |
13203 | | * |
13204 | | * This is the same as the C++ method GDALMDArray::SetSpatialRef(). |
13205 | | * @return TRUE in case of success. |
13206 | | */ |
13207 | | int GDALMDArraySetSpatialRef(GDALMDArrayH hArray, OGRSpatialReferenceH hSRS) |
13208 | 0 | { |
13209 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
13210 | 0 | return hArray->m_poImpl->SetSpatialRef( |
13211 | 0 | OGRSpatialReference::FromHandle(hSRS)); |
13212 | 0 | } |
13213 | | |
13214 | | /************************************************************************/ |
13215 | | /* GDALMDArrayGetSpatialRef() */ |
13216 | | /************************************************************************/ |
13217 | | |
13218 | | /** Return the spatial reference system object associated with the array. |
13219 | | * |
13220 | | * This is the same as the C++ method GDALMDArray::GetSpatialRef(). |
13221 | | * |
13222 | | * The returned object must be freed with OSRDestroySpatialReference(). |
13223 | | */ |
13224 | | OGRSpatialReferenceH GDALMDArrayGetSpatialRef(GDALMDArrayH hArray) |
13225 | 0 | { |
13226 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13227 | 0 | auto poSRS = hArray->m_poImpl->GetSpatialRef(); |
13228 | 0 | return poSRS ? OGRSpatialReference::ToHandle(poSRS->Clone()) : nullptr; |
13229 | 0 | } |
13230 | | |
13231 | | /************************************************************************/ |
13232 | | /* GDALMDArrayGetStatistics() */ |
13233 | | /************************************************************************/ |
13234 | | |
13235 | | /** |
13236 | | * \brief Fetch statistics. |
13237 | | * |
13238 | | * This is the same as the C++ method GDALMDArray::GetStatistics(). |
13239 | | * |
13240 | | * @since GDAL 3.2 |
13241 | | */ |
13242 | | |
13243 | | CPLErr GDALMDArrayGetStatistics(GDALMDArrayH hArray, GDALDatasetH /*hDS*/, |
13244 | | int bApproxOK, int bForce, double *pdfMin, |
13245 | | double *pdfMax, double *pdfMean, |
13246 | | double *pdfStdDev, GUInt64 *pnValidCount, |
13247 | | GDALProgressFunc pfnProgress, |
13248 | | void *pProgressData) |
13249 | 0 | { |
13250 | 0 | VALIDATE_POINTER1(hArray, __func__, CE_Failure); |
13251 | 0 | return hArray->m_poImpl->GetStatistics( |
13252 | 0 | CPL_TO_BOOL(bApproxOK), CPL_TO_BOOL(bForce), pdfMin, pdfMax, pdfMean, |
13253 | 0 | pdfStdDev, pnValidCount, pfnProgress, pProgressData); |
13254 | 0 | } |
13255 | | |
13256 | | /************************************************************************/ |
13257 | | /* GDALMDArrayComputeStatistics() */ |
13258 | | /************************************************************************/ |
13259 | | |
13260 | | /** |
13261 | | * \brief Compute statistics. |
13262 | | * |
13263 | | * This is the same as the C++ method GDALMDArray::ComputeStatistics(). |
13264 | | * |
13265 | | * @since GDAL 3.2 |
13266 | | * @see GDALMDArrayComputeStatisticsEx() |
13267 | | */ |
13268 | | |
13269 | | int GDALMDArrayComputeStatistics(GDALMDArrayH hArray, GDALDatasetH /* hDS */, |
13270 | | int bApproxOK, double *pdfMin, double *pdfMax, |
13271 | | double *pdfMean, double *pdfStdDev, |
13272 | | GUInt64 *pnValidCount, |
13273 | | GDALProgressFunc pfnProgress, |
13274 | | void *pProgressData) |
13275 | 0 | { |
13276 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
13277 | 0 | return hArray->m_poImpl->ComputeStatistics( |
13278 | 0 | CPL_TO_BOOL(bApproxOK), pdfMin, pdfMax, pdfMean, pdfStdDev, |
13279 | 0 | pnValidCount, pfnProgress, pProgressData, nullptr); |
13280 | 0 | } |
13281 | | |
13282 | | /************************************************************************/ |
13283 | | /* GDALMDArrayComputeStatisticsEx() */ |
13284 | | /************************************************************************/ |
13285 | | |
13286 | | /** |
13287 | | * \brief Compute statistics. |
13288 | | * |
13289 | | * Same as GDALMDArrayComputeStatistics() with extra papszOptions argument. |
13290 | | * |
13291 | | * This is the same as the C++ method GDALMDArray::ComputeStatistics(). |
13292 | | * |
13293 | | * @since GDAL 3.8 |
13294 | | */ |
13295 | | |
13296 | | int GDALMDArrayComputeStatisticsEx(GDALMDArrayH hArray, GDALDatasetH /* hDS */, |
13297 | | int bApproxOK, double *pdfMin, |
13298 | | double *pdfMax, double *pdfMean, |
13299 | | double *pdfStdDev, GUInt64 *pnValidCount, |
13300 | | GDALProgressFunc pfnProgress, |
13301 | | void *pProgressData, |
13302 | | CSLConstList papszOptions) |
13303 | 0 | { |
13304 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
13305 | 0 | return hArray->m_poImpl->ComputeStatistics( |
13306 | 0 | CPL_TO_BOOL(bApproxOK), pdfMin, pdfMax, pdfMean, pdfStdDev, |
13307 | 0 | pnValidCount, pfnProgress, pProgressData, papszOptions); |
13308 | 0 | } |
13309 | | |
13310 | | /************************************************************************/ |
13311 | | /* GDALMDArrayGetCoordinateVariables() */ |
13312 | | /************************************************************************/ |
13313 | | |
13314 | | /** Return coordinate variables. |
13315 | | * |
13316 | | * The returned array must be freed with GDALReleaseArrays(). If only the array |
13317 | | * itself needs to be freed, CPLFree() should be called (and |
13318 | | * GDALMDArrayRelease() on individual array members). |
13319 | | * |
13320 | | * This is the same as the C++ method GDALMDArray::GetCoordinateVariables() |
13321 | | * |
13322 | | * @param hArray Array. |
13323 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
13324 | | * |
13325 | | * @return an array of *pnCount arrays. |
13326 | | * @since 3.4 |
13327 | | */ |
13328 | | GDALMDArrayH *GDALMDArrayGetCoordinateVariables(GDALMDArrayH hArray, |
13329 | | size_t *pnCount) |
13330 | 0 | { |
13331 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13332 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
13333 | 0 | const auto coordinates(hArray->m_poImpl->GetCoordinateVariables()); |
13334 | 0 | auto ret = static_cast<GDALMDArrayH *>( |
13335 | 0 | CPLMalloc(sizeof(GDALMDArrayH) * coordinates.size())); |
13336 | 0 | for (size_t i = 0; i < coordinates.size(); i++) |
13337 | 0 | { |
13338 | 0 | ret[i] = new GDALMDArrayHS(coordinates[i]); |
13339 | 0 | } |
13340 | 0 | *pnCount = coordinates.size(); |
13341 | 0 | return ret; |
13342 | 0 | } |
13343 | | |
13344 | | /************************************************************************/ |
13345 | | /* GDALMDArrayGetGridded() */ |
13346 | | /************************************************************************/ |
13347 | | |
13348 | | /** Return a gridded array from scattered point data, that is from an array |
13349 | | * whose last dimension is the indexing variable of X and Y arrays. |
13350 | | * |
13351 | | * The returned object should be released with GDALMDArrayRelease(). |
13352 | | * |
13353 | | * This is the same as the C++ method GDALMDArray::GetGridded(). |
13354 | | * |
13355 | | * @since GDAL 3.7 |
13356 | | */ |
13357 | | GDALMDArrayH GDALMDArrayGetGridded(GDALMDArrayH hArray, |
13358 | | const char *pszGridOptions, |
13359 | | GDALMDArrayH hXArray, GDALMDArrayH hYArray, |
13360 | | CSLConstList papszOptions) |
13361 | 0 | { |
13362 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
13363 | 0 | VALIDATE_POINTER1(pszGridOptions, __func__, nullptr); |
13364 | 0 | auto gridded = hArray->m_poImpl->GetGridded( |
13365 | 0 | pszGridOptions, hXArray ? hXArray->m_poImpl : nullptr, |
13366 | 0 | hYArray ? hYArray->m_poImpl : nullptr, papszOptions); |
13367 | 0 | if (!gridded) |
13368 | 0 | return nullptr; |
13369 | 0 | return new GDALMDArrayHS(gridded); |
13370 | 0 | } |
13371 | | |
13372 | | /************************************************************************/ |
13373 | | /* GDALMDArrayGetMeshGrid() */ |
13374 | | /************************************************************************/ |
13375 | | |
13376 | | /** Return a list of multidimensional arrays from a list of one-dimensional |
13377 | | * arrays. |
13378 | | * |
13379 | | * This is typically used to transform one-dimensional longitude, latitude |
13380 | | * arrays into 2D ones. |
13381 | | * |
13382 | | * More formally, for one-dimensional arrays x1, x2,..., xn with lengths |
13383 | | * Ni=len(xi), returns (N1, N2, ..., Nn) shaped arrays if indexing="ij" or |
13384 | | * (N2, N1, ..., Nn) shaped arrays if indexing="xy" with the elements of xi |
13385 | | * repeated to fill the matrix along the first dimension for x1, the second |
13386 | | * for x2 and so on. |
13387 | | * |
13388 | | * For example, if x = [1, 2], and y = [3, 4, 5], |
13389 | | * GetMeshGrid([x, y], ["INDEXING=xy"]) will return [xm, ym] such that |
13390 | | * xm=[[1, 2],[1, 2],[1, 2]] and ym=[[3, 3],[4, 4],[5, 5]], |
13391 | | * or more generally xm[any index][i] = x[i] and ym[i][any index]=y[i] |
13392 | | * |
13393 | | * and |
13394 | | * GetMeshGrid([x, y], ["INDEXING=ij"]) will return [xm, ym] such that |
13395 | | * xm=[[1, 1, 1],[2, 2, 2]] and ym=[[3, 4, 5],[3, 4, 5]], |
13396 | | * or more generally xm[i][any index] = x[i] and ym[any index][i]=y[i] |
13397 | | * |
13398 | | * The currently supported options are: |
13399 | | * <ul> |
13400 | | * <li>INDEXING=xy/ij: Cartesian ("xy", default) or matrix ("ij") indexing of |
13401 | | * output. |
13402 | | * </li> |
13403 | | * </ul> |
13404 | | * |
13405 | | * This is the same as |
13406 | | * <a href="https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html">numpy.meshgrid()</a> |
13407 | | * function. |
13408 | | * |
13409 | | * The returned array (of arrays) must be freed with GDALReleaseArrays(). |
13410 | | * If only the array itself needs to be freed, CPLFree() should be called |
13411 | | * (and GDALMDArrayRelease() on individual array members). |
13412 | | * |
13413 | | * This is the same as the C++ method GDALMDArray::GetMeshGrid() |
13414 | | * |
13415 | | * @param pahInputArrays Input arrays |
13416 | | * @param nCountInputArrays Number of input arrays |
13417 | | * @param pnCountOutputArrays Pointer to the number of values returned. Must NOT be NULL. |
13418 | | * @param papszOptions NULL, or NULL terminated list of options. |
13419 | | * |
13420 | | * @return an array of *pnCountOutputArrays arrays. |
13421 | | * @since 3.10 |
13422 | | */ |
13423 | | GDALMDArrayH *GDALMDArrayGetMeshGrid(const GDALMDArrayH *pahInputArrays, |
13424 | | size_t nCountInputArrays, |
13425 | | size_t *pnCountOutputArrays, |
13426 | | CSLConstList papszOptions) |
13427 | 0 | { |
13428 | 0 | VALIDATE_POINTER1(pahInputArrays, __func__, nullptr); |
13429 | 0 | VALIDATE_POINTER1(pnCountOutputArrays, __func__, nullptr); |
13430 | | |
13431 | 0 | std::vector<std::shared_ptr<GDALMDArray>> apoInputArrays; |
13432 | 0 | for (size_t i = 0; i < nCountInputArrays; ++i) |
13433 | 0 | apoInputArrays.push_back(pahInputArrays[i]->m_poImpl); |
13434 | |
|
13435 | 0 | const auto apoOutputArrays = |
13436 | 0 | GDALMDArray::GetMeshGrid(apoInputArrays, papszOptions); |
13437 | 0 | auto ret = static_cast<GDALMDArrayH *>( |
13438 | 0 | CPLMalloc(sizeof(GDALMDArrayH) * apoOutputArrays.size())); |
13439 | 0 | for (size_t i = 0; i < apoOutputArrays.size(); i++) |
13440 | 0 | { |
13441 | 0 | ret[i] = new GDALMDArrayHS(apoOutputArrays[i]); |
13442 | 0 | } |
13443 | 0 | *pnCountOutputArrays = apoOutputArrays.size(); |
13444 | 0 | return ret; |
13445 | 0 | } |
13446 | | |
13447 | | /************************************************************************/ |
13448 | | /* GDALReleaseArrays() */ |
13449 | | /************************************************************************/ |
13450 | | |
13451 | | /** Free the return of GDALMDArrayGetCoordinateVariables() |
13452 | | * |
13453 | | * @param arrays return pointer of above methods |
13454 | | * @param nCount *pnCount value returned by above methods |
13455 | | */ |
13456 | | void GDALReleaseArrays(GDALMDArrayH *arrays, size_t nCount) |
13457 | 0 | { |
13458 | 0 | for (size_t i = 0; i < nCount; i++) |
13459 | 0 | { |
13460 | 0 | delete arrays[i]; |
13461 | 0 | } |
13462 | 0 | CPLFree(arrays); |
13463 | 0 | } |
13464 | | |
13465 | | /************************************************************************/ |
13466 | | /* GDALMDArrayCache() */ |
13467 | | /************************************************************************/ |
13468 | | |
13469 | | /** |
13470 | | * \brief Cache the content of the array into an auxiliary filename. |
13471 | | * |
13472 | | * This is the same as the C++ method GDALMDArray::Cache(). |
13473 | | * |
13474 | | * @since GDAL 3.4 |
13475 | | */ |
13476 | | |
13477 | | int GDALMDArrayCache(GDALMDArrayH hArray, CSLConstList papszOptions) |
13478 | 0 | { |
13479 | 0 | VALIDATE_POINTER1(hArray, __func__, FALSE); |
13480 | 0 | return hArray->m_poImpl->Cache(papszOptions); |
13481 | 0 | } |
13482 | | |
13483 | | /************************************************************************/ |
13484 | | /* GDALMDArrayRename() */ |
13485 | | /************************************************************************/ |
13486 | | |
13487 | | /** Rename the array. |
13488 | | * |
13489 | | * This is not implemented by all drivers. |
13490 | | * |
13491 | | * Drivers known to implement it: MEM, netCDF, Zarr. |
13492 | | * |
13493 | | * This is the same as the C++ method GDALAbstractMDArray::Rename() |
13494 | | * |
13495 | | * @return true in case of success |
13496 | | * @since GDAL 3.8 |
13497 | | */ |
13498 | | bool GDALMDArrayRename(GDALMDArrayH hArray, const char *pszNewName) |
13499 | 0 | { |
13500 | 0 | VALIDATE_POINTER1(hArray, __func__, false); |
13501 | 0 | VALIDATE_POINTER1(pszNewName, __func__, false); |
13502 | 0 | return hArray->m_poImpl->Rename(pszNewName); |
13503 | 0 | } |
13504 | | |
13505 | | /************************************************************************/ |
13506 | | /* GDALAttributeRelease() */ |
13507 | | /************************************************************************/ |
13508 | | |
13509 | | /** Release the GDAL in-memory object associated with a GDALAttribute. |
13510 | | * |
13511 | | * Note: when applied on a object coming from a driver, this does not |
13512 | | * destroy the object in the file, database, etc... |
13513 | | */ |
13514 | | void GDALAttributeRelease(GDALAttributeH hAttr) |
13515 | 0 | { |
13516 | 0 | delete hAttr; |
13517 | 0 | } |
13518 | | |
13519 | | /************************************************************************/ |
13520 | | /* GDALAttributeGetName() */ |
13521 | | /************************************************************************/ |
13522 | | |
13523 | | /** Return the name of the attribute. |
13524 | | * |
13525 | | * The returned pointer is valid until hAttr is released. |
13526 | | * |
13527 | | * This is the same as the C++ method GDALAttribute::GetName(). |
13528 | | */ |
13529 | | const char *GDALAttributeGetName(GDALAttributeH hAttr) |
13530 | 0 | { |
13531 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13532 | 0 | return hAttr->m_poImpl->GetName().c_str(); |
13533 | 0 | } |
13534 | | |
13535 | | /************************************************************************/ |
13536 | | /* GDALAttributeGetFullName() */ |
13537 | | /************************************************************************/ |
13538 | | |
13539 | | /** Return the full name of the attribute. |
13540 | | * |
13541 | | * The returned pointer is valid until hAttr is released. |
13542 | | * |
13543 | | * This is the same as the C++ method GDALAttribute::GetFullName(). |
13544 | | */ |
13545 | | const char *GDALAttributeGetFullName(GDALAttributeH hAttr) |
13546 | 0 | { |
13547 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13548 | 0 | return hAttr->m_poImpl->GetFullName().c_str(); |
13549 | 0 | } |
13550 | | |
13551 | | /************************************************************************/ |
13552 | | /* GDALAttributeGetTotalElementsCount() */ |
13553 | | /************************************************************************/ |
13554 | | |
13555 | | /** Return the total number of values in the attribute. |
13556 | | * |
13557 | | * This is the same as the C++ method |
13558 | | * GDALAbstractMDArray::GetTotalElementsCount() |
13559 | | */ |
13560 | | GUInt64 GDALAttributeGetTotalElementsCount(GDALAttributeH hAttr) |
13561 | 0 | { |
13562 | 0 | VALIDATE_POINTER1(hAttr, __func__, 0); |
13563 | 0 | return hAttr->m_poImpl->GetTotalElementsCount(); |
13564 | 0 | } |
13565 | | |
13566 | | /************************************************************************/ |
13567 | | /* GDALAttributeGetDimensionCount() */ |
13568 | | /************************************************************************/ |
13569 | | |
13570 | | /** Return the number of dimensions. |
13571 | | * |
13572 | | * This is the same as the C++ method GDALAbstractMDArray::GetDimensionCount() |
13573 | | */ |
13574 | | size_t GDALAttributeGetDimensionCount(GDALAttributeH hAttr) |
13575 | 0 | { |
13576 | 0 | VALIDATE_POINTER1(hAttr, __func__, 0); |
13577 | 0 | return hAttr->m_poImpl->GetDimensionCount(); |
13578 | 0 | } |
13579 | | |
13580 | | /************************************************************************/ |
13581 | | /* GDALAttributeGetDimensionsSize() */ |
13582 | | /************************************************************************/ |
13583 | | |
13584 | | /** Return the dimension sizes of the attribute. |
13585 | | * |
13586 | | * The returned array must be freed with CPLFree() |
13587 | | * |
13588 | | * @param hAttr Attribute. |
13589 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
13590 | | * |
13591 | | * @return an array of *pnCount values. |
13592 | | */ |
13593 | | GUInt64 *GDALAttributeGetDimensionsSize(GDALAttributeH hAttr, size_t *pnCount) |
13594 | 0 | { |
13595 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13596 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
13597 | 0 | const auto &dims = hAttr->m_poImpl->GetDimensions(); |
13598 | 0 | auto ret = static_cast<GUInt64 *>(CPLMalloc(sizeof(GUInt64) * dims.size())); |
13599 | 0 | for (size_t i = 0; i < dims.size(); i++) |
13600 | 0 | { |
13601 | 0 | ret[i] = dims[i]->GetSize(); |
13602 | 0 | } |
13603 | 0 | *pnCount = dims.size(); |
13604 | 0 | return ret; |
13605 | 0 | } |
13606 | | |
13607 | | /************************************************************************/ |
13608 | | /* GDALAttributeGetDataType() */ |
13609 | | /************************************************************************/ |
13610 | | |
13611 | | /** Return the data type |
13612 | | * |
13613 | | * The return must be freed with GDALExtendedDataTypeRelease(). |
13614 | | */ |
13615 | | GDALExtendedDataTypeH GDALAttributeGetDataType(GDALAttributeH hAttr) |
13616 | 0 | { |
13617 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13618 | 0 | return new GDALExtendedDataTypeHS( |
13619 | 0 | new GDALExtendedDataType(hAttr->m_poImpl->GetDataType())); |
13620 | 0 | } |
13621 | | |
13622 | | /************************************************************************/ |
13623 | | /* GDALAttributeReadAsRaw() */ |
13624 | | /************************************************************************/ |
13625 | | |
13626 | | /** Return the raw value of an attribute. |
13627 | | * |
13628 | | * This is the same as the C++ method GDALAttribute::ReadAsRaw(). |
13629 | | * |
13630 | | * The returned buffer must be freed with GDALAttributeFreeRawResult() |
13631 | | * |
13632 | | * @param hAttr Attribute. |
13633 | | * @param pnSize Pointer to the number of bytes returned. Must NOT be NULL. |
13634 | | * |
13635 | | * @return a buffer of *pnSize bytes. |
13636 | | */ |
13637 | | GByte *GDALAttributeReadAsRaw(GDALAttributeH hAttr, size_t *pnSize) |
13638 | 0 | { |
13639 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13640 | 0 | VALIDATE_POINTER1(pnSize, __func__, nullptr); |
13641 | 0 | auto res(hAttr->m_poImpl->ReadAsRaw()); |
13642 | 0 | *pnSize = res.size(); |
13643 | 0 | auto ret = res.StealData(); |
13644 | 0 | if (!ret) |
13645 | 0 | { |
13646 | 0 | *pnSize = 0; |
13647 | 0 | return nullptr; |
13648 | 0 | } |
13649 | 0 | return ret; |
13650 | 0 | } |
13651 | | |
13652 | | /************************************************************************/ |
13653 | | /* GDALAttributeFreeRawResult() */ |
13654 | | /************************************************************************/ |
13655 | | |
13656 | | /** Free the return of GDALAttributeAsRaw() |
13657 | | */ |
13658 | | void GDALAttributeFreeRawResult(GDALAttributeH hAttr, GByte *raw, |
13659 | | CPL_UNUSED size_t nSize) |
13660 | 0 | { |
13661 | 0 | VALIDATE_POINTER0(hAttr, __func__); |
13662 | 0 | if (raw) |
13663 | 0 | { |
13664 | 0 | const auto &dt(hAttr->m_poImpl->GetDataType()); |
13665 | 0 | const auto nDTSize(dt.GetSize()); |
13666 | 0 | GByte *pabyPtr = raw; |
13667 | 0 | const auto nEltCount(hAttr->m_poImpl->GetTotalElementsCount()); |
13668 | 0 | CPLAssert(nSize == nDTSize * nEltCount); |
13669 | 0 | for (size_t i = 0; i < nEltCount; ++i) |
13670 | 0 | { |
13671 | 0 | dt.FreeDynamicMemory(pabyPtr); |
13672 | 0 | pabyPtr += nDTSize; |
13673 | 0 | } |
13674 | 0 | CPLFree(raw); |
13675 | 0 | } |
13676 | 0 | } |
13677 | | |
13678 | | /************************************************************************/ |
13679 | | /* GDALAttributeReadAsString() */ |
13680 | | /************************************************************************/ |
13681 | | |
13682 | | /** Return the value of an attribute as a string. |
13683 | | * |
13684 | | * The returned string should not be freed, and its lifetime does not |
13685 | | * excess a next call to ReadAsString() on the same object, or the deletion |
13686 | | * of the object itself. |
13687 | | * |
13688 | | * This function will only return the first element if there are several. |
13689 | | * |
13690 | | * This is the same as the C++ method GDALAttribute::ReadAsString() |
13691 | | * |
13692 | | * @return a string, or nullptr. |
13693 | | */ |
13694 | | const char *GDALAttributeReadAsString(GDALAttributeH hAttr) |
13695 | 0 | { |
13696 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13697 | 0 | return hAttr->m_poImpl->ReadAsString(); |
13698 | 0 | } |
13699 | | |
13700 | | /************************************************************************/ |
13701 | | /* GDALAttributeReadAsInt() */ |
13702 | | /************************************************************************/ |
13703 | | |
13704 | | /** Return the value of an attribute as a integer. |
13705 | | * |
13706 | | * This function will only return the first element if there are several. |
13707 | | * |
13708 | | * It can fail if its value can not be converted to integer. |
13709 | | * |
13710 | | * This is the same as the C++ method GDALAttribute::ReadAsInt() |
13711 | | * |
13712 | | * @return a integer, or INT_MIN in case of error. |
13713 | | */ |
13714 | | int GDALAttributeReadAsInt(GDALAttributeH hAttr) |
13715 | 0 | { |
13716 | 0 | VALIDATE_POINTER1(hAttr, __func__, 0); |
13717 | 0 | return hAttr->m_poImpl->ReadAsInt(); |
13718 | 0 | } |
13719 | | |
13720 | | /************************************************************************/ |
13721 | | /* GDALAttributeReadAsInt64() */ |
13722 | | /************************************************************************/ |
13723 | | |
13724 | | /** Return the value of an attribute as a int64_t. |
13725 | | * |
13726 | | * This function will only return the first element if there are several. |
13727 | | * |
13728 | | * It can fail if its value can not be converted to integer. |
13729 | | * |
13730 | | * This is the same as the C++ method GDALAttribute::ReadAsInt64() |
13731 | | * |
13732 | | * @return an int64_t, or INT64_MIN in case of error. |
13733 | | */ |
13734 | | int64_t GDALAttributeReadAsInt64(GDALAttributeH hAttr) |
13735 | 0 | { |
13736 | 0 | VALIDATE_POINTER1(hAttr, __func__, 0); |
13737 | 0 | return hAttr->m_poImpl->ReadAsInt64(); |
13738 | 0 | } |
13739 | | |
13740 | | /************************************************************************/ |
13741 | | /* GDALAttributeReadAsDouble() */ |
13742 | | /************************************************************************/ |
13743 | | |
13744 | | /** Return the value of an attribute as a double. |
13745 | | * |
13746 | | * This function will only return the first element if there are several. |
13747 | | * |
13748 | | * It can fail if its value can not be converted to double. |
13749 | | * |
13750 | | * This is the same as the C++ method GDALAttribute::ReadAsDouble() |
13751 | | * |
13752 | | * @return a double value. |
13753 | | */ |
13754 | | double GDALAttributeReadAsDouble(GDALAttributeH hAttr) |
13755 | 0 | { |
13756 | 0 | VALIDATE_POINTER1(hAttr, __func__, 0); |
13757 | 0 | return hAttr->m_poImpl->ReadAsDouble(); |
13758 | 0 | } |
13759 | | |
13760 | | /************************************************************************/ |
13761 | | /* GDALAttributeReadAsStringArray() */ |
13762 | | /************************************************************************/ |
13763 | | |
13764 | | /** Return the value of an attribute as an array of strings. |
13765 | | * |
13766 | | * This is the same as the C++ method GDALAttribute::ReadAsStringArray() |
13767 | | * |
13768 | | * The return value must be freed with CSLDestroy(). |
13769 | | */ |
13770 | | char **GDALAttributeReadAsStringArray(GDALAttributeH hAttr) |
13771 | 0 | { |
13772 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13773 | 0 | return hAttr->m_poImpl->ReadAsStringArray().StealList(); |
13774 | 0 | } |
13775 | | |
13776 | | /************************************************************************/ |
13777 | | /* GDALAttributeReadAsIntArray() */ |
13778 | | /************************************************************************/ |
13779 | | |
13780 | | /** Return the value of an attribute as an array of integers. |
13781 | | * |
13782 | | * This is the same as the C++ method GDALAttribute::ReadAsIntArray() |
13783 | | * |
13784 | | * @param hAttr Attribute |
13785 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
13786 | | * @return array to be freed with CPLFree(), or nullptr. |
13787 | | */ |
13788 | | int *GDALAttributeReadAsIntArray(GDALAttributeH hAttr, size_t *pnCount) |
13789 | 0 | { |
13790 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13791 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
13792 | 0 | *pnCount = 0; |
13793 | 0 | auto tmp(hAttr->m_poImpl->ReadAsIntArray()); |
13794 | 0 | if (tmp.empty()) |
13795 | 0 | return nullptr; |
13796 | 0 | auto ret = static_cast<int *>(VSI_MALLOC2_VERBOSE(tmp.size(), sizeof(int))); |
13797 | 0 | if (!ret) |
13798 | 0 | return nullptr; |
13799 | 0 | memcpy(ret, tmp.data(), tmp.size() * sizeof(int)); |
13800 | 0 | *pnCount = tmp.size(); |
13801 | 0 | return ret; |
13802 | 0 | } |
13803 | | |
13804 | | /************************************************************************/ |
13805 | | /* GDALAttributeReadAsInt64Array() */ |
13806 | | /************************************************************************/ |
13807 | | |
13808 | | /** Return the value of an attribute as an array of int64_t. |
13809 | | * |
13810 | | * This is the same as the C++ method GDALAttribute::ReadAsInt64Array() |
13811 | | * |
13812 | | * @param hAttr Attribute |
13813 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
13814 | | * @return array to be freed with CPLFree(), or nullptr. |
13815 | | */ |
13816 | | int64_t *GDALAttributeReadAsInt64Array(GDALAttributeH hAttr, size_t *pnCount) |
13817 | 0 | { |
13818 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13819 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
13820 | 0 | *pnCount = 0; |
13821 | 0 | auto tmp(hAttr->m_poImpl->ReadAsInt64Array()); |
13822 | 0 | if (tmp.empty()) |
13823 | 0 | return nullptr; |
13824 | 0 | auto ret = static_cast<int64_t *>( |
13825 | 0 | VSI_MALLOC2_VERBOSE(tmp.size(), sizeof(int64_t))); |
13826 | 0 | if (!ret) |
13827 | 0 | return nullptr; |
13828 | 0 | memcpy(ret, tmp.data(), tmp.size() * sizeof(int64_t)); |
13829 | 0 | *pnCount = tmp.size(); |
13830 | 0 | return ret; |
13831 | 0 | } |
13832 | | |
13833 | | /************************************************************************/ |
13834 | | /* GDALAttributeReadAsDoubleArray() */ |
13835 | | /************************************************************************/ |
13836 | | |
13837 | | /** Return the value of an attribute as an array of doubles. |
13838 | | * |
13839 | | * This is the same as the C++ method GDALAttribute::ReadAsDoubleArray() |
13840 | | * |
13841 | | * @param hAttr Attribute |
13842 | | * @param pnCount Pointer to the number of values returned. Must NOT be NULL. |
13843 | | * @return array to be freed with CPLFree(), or nullptr. |
13844 | | */ |
13845 | | double *GDALAttributeReadAsDoubleArray(GDALAttributeH hAttr, size_t *pnCount) |
13846 | 0 | { |
13847 | 0 | VALIDATE_POINTER1(hAttr, __func__, nullptr); |
13848 | 0 | VALIDATE_POINTER1(pnCount, __func__, nullptr); |
13849 | 0 | *pnCount = 0; |
13850 | 0 | auto tmp(hAttr->m_poImpl->ReadAsDoubleArray()); |
13851 | 0 | if (tmp.empty()) |
13852 | 0 | return nullptr; |
13853 | 0 | auto ret = |
13854 | 0 | static_cast<double *>(VSI_MALLOC2_VERBOSE(tmp.size(), sizeof(double))); |
13855 | 0 | if (!ret) |
13856 | 0 | return nullptr; |
13857 | 0 | memcpy(ret, tmp.data(), tmp.size() * sizeof(double)); |
13858 | 0 | *pnCount = tmp.size(); |
13859 | 0 | return ret; |
13860 | 0 | } |
13861 | | |
13862 | | /************************************************************************/ |
13863 | | /* GDALAttributeWriteRaw() */ |
13864 | | /************************************************************************/ |
13865 | | |
13866 | | /** Write an attribute from raw values expressed in GetDataType() |
13867 | | * |
13868 | | * The values should be provided in the type of GetDataType() and there should |
13869 | | * be exactly GetTotalElementsCount() of them. |
13870 | | * If GetDataType() is a string, each value should be a char* pointer. |
13871 | | * |
13872 | | * This is the same as the C++ method GDALAttribute::Write(const void*, size_t). |
13873 | | * |
13874 | | * @param hAttr Attribute |
13875 | | * @param pabyValue Buffer of nLen bytes. |
13876 | | * @param nLength Size of pabyValue in bytes. Should be equal to |
13877 | | * GetTotalElementsCount() * GetDataType().GetSize() |
13878 | | * @return TRUE in case of success. |
13879 | | */ |
13880 | | int GDALAttributeWriteRaw(GDALAttributeH hAttr, const void *pabyValue, |
13881 | | size_t nLength) |
13882 | 0 | { |
13883 | 0 | VALIDATE_POINTER1(hAttr, __func__, FALSE); |
13884 | 0 | return hAttr->m_poImpl->Write(pabyValue, nLength); |
13885 | 0 | } |
13886 | | |
13887 | | /************************************************************************/ |
13888 | | /* GDALAttributeWriteString() */ |
13889 | | /************************************************************************/ |
13890 | | |
13891 | | /** Write an attribute from a string value. |
13892 | | * |
13893 | | * Type conversion will be performed if needed. If the attribute contains |
13894 | | * multiple values, only the first one will be updated. |
13895 | | * |
13896 | | * This is the same as the C++ method GDALAttribute::Write(const char*) |
13897 | | * |
13898 | | * @param hAttr Attribute |
13899 | | * @param pszVal Pointer to a string. |
13900 | | * @return TRUE in case of success. |
13901 | | */ |
13902 | | int GDALAttributeWriteString(GDALAttributeH hAttr, const char *pszVal) |
13903 | 0 | { |
13904 | 0 | VALIDATE_POINTER1(hAttr, __func__, FALSE); |
13905 | 0 | return hAttr->m_poImpl->Write(pszVal); |
13906 | 0 | } |
13907 | | |
13908 | | /************************************************************************/ |
13909 | | /* GDALAttributeWriteInt() */ |
13910 | | /************************************************************************/ |
13911 | | |
13912 | | /** Write an attribute from a integer value. |
13913 | | * |
13914 | | * Type conversion will be performed if needed. If the attribute contains |
13915 | | * multiple values, only the first one will be updated. |
13916 | | * |
13917 | | * This is the same as the C++ method GDALAttribute::WriteInt() |
13918 | | * |
13919 | | * @param hAttr Attribute |
13920 | | * @param nVal Value. |
13921 | | * @return TRUE in case of success. |
13922 | | */ |
13923 | | int GDALAttributeWriteInt(GDALAttributeH hAttr, int nVal) |
13924 | 0 | { |
13925 | 0 | VALIDATE_POINTER1(hAttr, __func__, FALSE); |
13926 | 0 | return hAttr->m_poImpl->WriteInt(nVal); |
13927 | 0 | } |
13928 | | |
13929 | | /************************************************************************/ |
13930 | | /* GDALAttributeWriteInt64() */ |
13931 | | /************************************************************************/ |
13932 | | |
13933 | | /** Write an attribute from an int64_t value. |
13934 | | * |
13935 | | * Type conversion will be performed if needed. If the attribute contains |
13936 | | * multiple values, only the first one will be updated. |
13937 | | * |
13938 | | * This is the same as the C++ method GDALAttribute::WriteLong() |
13939 | | * |
13940 | | * @param hAttr Attribute |
13941 | | * @param nVal Value. |
13942 | | * @return TRUE in case of success. |
13943 | | */ |
13944 | | int GDALAttributeWriteInt64(GDALAttributeH hAttr, int64_t nVal) |
13945 | 0 | { |
13946 | 0 | VALIDATE_POINTER1(hAttr, __func__, FALSE); |
13947 | 0 | return hAttr->m_poImpl->WriteInt64(nVal); |
13948 | 0 | } |
13949 | | |
13950 | | /************************************************************************/ |
13951 | | /* GDALAttributeWriteDouble() */ |
13952 | | /************************************************************************/ |
13953 | | |
13954 | | /** Write an attribute from a double value. |
13955 | | * |
13956 | | * Type conversion will be performed if needed. If the attribute contains |
13957 | | * multiple values, only the first one will be updated. |
13958 | | * |
13959 | | * This is the same as the C++ method GDALAttribute::Write(double); |
13960 | | * |
13961 | | * @param hAttr Attribute |
13962 | | * @param dfVal Value. |
13963 | | * |
13964 | | * @return TRUE in case of success. |
13965 | | */ |
13966 | | int GDALAttributeWriteDouble(GDALAttributeH hAttr, double dfVal) |
13967 | 0 | { |
13968 | 0 | VALIDATE_POINTER1(hAttr, __func__, FALSE); |
13969 | 0 | return hAttr->m_poImpl->Write(dfVal); |
13970 | 0 | } |
13971 | | |
13972 | | /************************************************************************/ |
13973 | | /* GDALAttributeWriteStringArray() */ |
13974 | | /************************************************************************/ |
13975 | | |
13976 | | /** Write an attribute from an array of strings. |
13977 | | * |
13978 | | * Type conversion will be performed if needed. |
13979 | | * |
13980 | | * Exactly GetTotalElementsCount() strings must be provided |
13981 | | * |
13982 | | * This is the same as the C++ method GDALAttribute::Write(CSLConstList) |
13983 | | * |
13984 | | * @param hAttr Attribute |
13985 | | * @param papszValues Array of strings. |
13986 | | * @return TRUE in case of success. |
13987 | | */ |
13988 | | int GDALAttributeWriteStringArray(GDALAttributeH hAttr, |
13989 | | CSLConstList papszValues) |
13990 | 0 | { |
13991 | 0 | VALIDATE_POINTER1(hAttr, __func__, FALSE); |
13992 | 0 | return hAttr->m_poImpl->Write(papszValues); |
13993 | 0 | } |
13994 | | |
13995 | | /************************************************************************/ |
13996 | | /* GDALAttributeWriteIntArray() */ |
13997 | | /************************************************************************/ |
13998 | | |
13999 | | /** Write an attribute from an array of int. |
14000 | | * |
14001 | | * Type conversion will be performed if needed. |
14002 | | * |
14003 | | * Exactly GetTotalElementsCount() strings must be provided |
14004 | | * |
14005 | | * This is the same as the C++ method GDALAttribute::Write(const int *, |
14006 | | * size_t) |
14007 | | * |
14008 | | * @param hAttr Attribute |
14009 | | * @param panValues Array of int. |
14010 | | * @param nCount Should be equal to GetTotalElementsCount(). |
14011 | | * @return TRUE in case of success. |
14012 | | */ |
14013 | | int GDALAttributeWriteIntArray(GDALAttributeH hAttr, const int *panValues, |
14014 | | size_t nCount) |
14015 | 0 | { |
14016 | 0 | VALIDATE_POINTER1(hAttr, __func__, FALSE); |
14017 | 0 | return hAttr->m_poImpl->Write(panValues, nCount); |
14018 | 0 | } |
14019 | | |
14020 | | /************************************************************************/ |
14021 | | /* GDALAttributeWriteInt64Array() */ |
14022 | | /************************************************************************/ |
14023 | | |
14024 | | /** Write an attribute from an array of int64_t. |
14025 | | * |
14026 | | * Type conversion will be performed if needed. |
14027 | | * |
14028 | | * Exactly GetTotalElementsCount() strings must be provided |
14029 | | * |
14030 | | * This is the same as the C++ method GDALAttribute::Write(const int64_t *, |
14031 | | * size_t) |
14032 | | * |
14033 | | * @param hAttr Attribute |
14034 | | * @param panValues Array of int64_t. |
14035 | | * @param nCount Should be equal to GetTotalElementsCount(). |
14036 | | * @return TRUE in case of success. |
14037 | | */ |
14038 | | int GDALAttributeWriteInt64Array(GDALAttributeH hAttr, const int64_t *panValues, |
14039 | | size_t nCount) |
14040 | 0 | { |
14041 | 0 | VALIDATE_POINTER1(hAttr, __func__, FALSE); |
14042 | 0 | return hAttr->m_poImpl->Write(panValues, nCount); |
14043 | 0 | } |
14044 | | |
14045 | | /************************************************************************/ |
14046 | | /* GDALAttributeWriteDoubleArray() */ |
14047 | | /************************************************************************/ |
14048 | | |
14049 | | /** Write an attribute from an array of double. |
14050 | | * |
14051 | | * Type conversion will be performed if needed. |
14052 | | * |
14053 | | * Exactly GetTotalElementsCount() strings must be provided |
14054 | | * |
14055 | | * This is the same as the C++ method GDALAttribute::Write(const double *, |
14056 | | * size_t) |
14057 | | * |
14058 | | * @param hAttr Attribute |
14059 | | * @param padfValues Array of double. |
14060 | | * @param nCount Should be equal to GetTotalElementsCount(). |
14061 | | * @return TRUE in case of success. |
14062 | | */ |
14063 | | int GDALAttributeWriteDoubleArray(GDALAttributeH hAttr, |
14064 | | const double *padfValues, size_t nCount) |
14065 | 0 | { |
14066 | 0 | VALIDATE_POINTER1(hAttr, __func__, FALSE); |
14067 | 0 | return hAttr->m_poImpl->Write(padfValues, nCount); |
14068 | 0 | } |
14069 | | |
14070 | | /************************************************************************/ |
14071 | | /* GDALAttributeRename() */ |
14072 | | /************************************************************************/ |
14073 | | |
14074 | | /** Rename the attribute. |
14075 | | * |
14076 | | * This is not implemented by all drivers. |
14077 | | * |
14078 | | * Drivers known to implement it: MEM, netCDF. |
14079 | | * |
14080 | | * This is the same as the C++ method GDALAbstractMDArray::Rename() |
14081 | | * |
14082 | | * @return true in case of success |
14083 | | * @since GDAL 3.8 |
14084 | | */ |
14085 | | bool GDALAttributeRename(GDALAttributeH hAttr, const char *pszNewName) |
14086 | 0 | { |
14087 | 0 | VALIDATE_POINTER1(hAttr, __func__, false); |
14088 | 0 | VALIDATE_POINTER1(pszNewName, __func__, false); |
14089 | 0 | return hAttr->m_poImpl->Rename(pszNewName); |
14090 | 0 | } |
14091 | | |
14092 | | /************************************************************************/ |
14093 | | /* GDALDimensionRelease() */ |
14094 | | /************************************************************************/ |
14095 | | |
14096 | | /** Release the GDAL in-memory object associated with a GDALDimension. |
14097 | | * |
14098 | | * Note: when applied on a object coming from a driver, this does not |
14099 | | * destroy the object in the file, database, etc... |
14100 | | */ |
14101 | | void GDALDimensionRelease(GDALDimensionH hDim) |
14102 | 0 | { |
14103 | 0 | delete hDim; |
14104 | 0 | } |
14105 | | |
14106 | | /************************************************************************/ |
14107 | | /* GDALDimensionGetName() */ |
14108 | | /************************************************************************/ |
14109 | | |
14110 | | /** Return dimension name. |
14111 | | * |
14112 | | * This is the same as the C++ method GDALDimension::GetName() |
14113 | | */ |
14114 | | const char *GDALDimensionGetName(GDALDimensionH hDim) |
14115 | 0 | { |
14116 | 0 | VALIDATE_POINTER1(hDim, __func__, nullptr); |
14117 | 0 | return hDim->m_poImpl->GetName().c_str(); |
14118 | 0 | } |
14119 | | |
14120 | | /************************************************************************/ |
14121 | | /* GDALDimensionGetFullName() */ |
14122 | | /************************************************************************/ |
14123 | | |
14124 | | /** Return dimension full name. |
14125 | | * |
14126 | | * This is the same as the C++ method GDALDimension::GetFullName() |
14127 | | */ |
14128 | | const char *GDALDimensionGetFullName(GDALDimensionH hDim) |
14129 | 0 | { |
14130 | 0 | VALIDATE_POINTER1(hDim, __func__, nullptr); |
14131 | 0 | return hDim->m_poImpl->GetFullName().c_str(); |
14132 | 0 | } |
14133 | | |
14134 | | /************************************************************************/ |
14135 | | /* GDALDimensionGetType() */ |
14136 | | /************************************************************************/ |
14137 | | |
14138 | | /** Return dimension type. |
14139 | | * |
14140 | | * This is the same as the C++ method GDALDimension::GetType() |
14141 | | */ |
14142 | | const char *GDALDimensionGetType(GDALDimensionH hDim) |
14143 | 0 | { |
14144 | 0 | VALIDATE_POINTER1(hDim, __func__, nullptr); |
14145 | 0 | return hDim->m_poImpl->GetType().c_str(); |
14146 | 0 | } |
14147 | | |
14148 | | /************************************************************************/ |
14149 | | /* GDALDimensionGetDirection() */ |
14150 | | /************************************************************************/ |
14151 | | |
14152 | | /** Return dimension direction. |
14153 | | * |
14154 | | * This is the same as the C++ method GDALDimension::GetDirection() |
14155 | | */ |
14156 | | const char *GDALDimensionGetDirection(GDALDimensionH hDim) |
14157 | 0 | { |
14158 | 0 | VALIDATE_POINTER1(hDim, __func__, nullptr); |
14159 | 0 | return hDim->m_poImpl->GetDirection().c_str(); |
14160 | 0 | } |
14161 | | |
14162 | | /************************************************************************/ |
14163 | | /* GDALDimensionGetSize() */ |
14164 | | /************************************************************************/ |
14165 | | |
14166 | | /** Return the size, that is the number of values along the dimension. |
14167 | | * |
14168 | | * This is the same as the C++ method GDALDimension::GetSize() |
14169 | | */ |
14170 | | GUInt64 GDALDimensionGetSize(GDALDimensionH hDim) |
14171 | 0 | { |
14172 | 0 | VALIDATE_POINTER1(hDim, __func__, 0); |
14173 | 0 | return hDim->m_poImpl->GetSize(); |
14174 | 0 | } |
14175 | | |
14176 | | /************************************************************************/ |
14177 | | /* GDALDimensionGetIndexingVariable() */ |
14178 | | /************************************************************************/ |
14179 | | |
14180 | | /** Return the variable that is used to index the dimension (if there is one). |
14181 | | * |
14182 | | * This is the array, typically one-dimensional, describing the values taken |
14183 | | * by the dimension. |
14184 | | * |
14185 | | * The returned value should be freed with GDALMDArrayRelease(). |
14186 | | * |
14187 | | * This is the same as the C++ method GDALDimension::GetIndexingVariable() |
14188 | | */ |
14189 | | GDALMDArrayH GDALDimensionGetIndexingVariable(GDALDimensionH hDim) |
14190 | 0 | { |
14191 | 0 | VALIDATE_POINTER1(hDim, __func__, nullptr); |
14192 | 0 | auto var(hDim->m_poImpl->GetIndexingVariable()); |
14193 | 0 | if (!var) |
14194 | 0 | return nullptr; |
14195 | 0 | return new GDALMDArrayHS(var); |
14196 | 0 | } |
14197 | | |
14198 | | /************************************************************************/ |
14199 | | /* GDALDimensionSetIndexingVariable() */ |
14200 | | /************************************************************************/ |
14201 | | |
14202 | | /** Set the variable that is used to index the dimension. |
14203 | | * |
14204 | | * This is the array, typically one-dimensional, describing the values taken |
14205 | | * by the dimension. |
14206 | | * |
14207 | | * This is the same as the C++ method GDALDimension::SetIndexingVariable() |
14208 | | * |
14209 | | * @return TRUE in case of success. |
14210 | | */ |
14211 | | int GDALDimensionSetIndexingVariable(GDALDimensionH hDim, GDALMDArrayH hArray) |
14212 | 0 | { |
14213 | 0 | VALIDATE_POINTER1(hDim, __func__, FALSE); |
14214 | 0 | return hDim->m_poImpl->SetIndexingVariable(hArray ? hArray->m_poImpl |
14215 | 0 | : nullptr); |
14216 | 0 | } |
14217 | | |
14218 | | /************************************************************************/ |
14219 | | /* GDALDimensionRename() */ |
14220 | | /************************************************************************/ |
14221 | | |
14222 | | /** Rename the dimension. |
14223 | | * |
14224 | | * This is not implemented by all drivers. |
14225 | | * |
14226 | | * Drivers known to implement it: MEM, netCDF. |
14227 | | * |
14228 | | * This is the same as the C++ method GDALDimension::Rename() |
14229 | | * |
14230 | | * @return true in case of success |
14231 | | * @since GDAL 3.8 |
14232 | | */ |
14233 | | bool GDALDimensionRename(GDALDimensionH hDim, const char *pszNewName) |
14234 | 0 | { |
14235 | 0 | VALIDATE_POINTER1(hDim, __func__, false); |
14236 | 0 | VALIDATE_POINTER1(pszNewName, __func__, false); |
14237 | 0 | return hDim->m_poImpl->Rename(pszNewName); |
14238 | 0 | } |
14239 | | |
14240 | | /************************************************************************/ |
14241 | | /* GDALDatasetGetRootGroup() */ |
14242 | | /************************************************************************/ |
14243 | | |
14244 | | /** Return the root GDALGroup of this dataset. |
14245 | | * |
14246 | | * Only valid for multidimensional datasets. |
14247 | | * |
14248 | | * The returned value must be freed with GDALGroupRelease(). |
14249 | | * |
14250 | | * This is the same as the C++ method GDALDataset::GetRootGroup(). |
14251 | | * |
14252 | | * @since GDAL 3.1 |
14253 | | */ |
14254 | | GDALGroupH GDALDatasetGetRootGroup(GDALDatasetH hDS) |
14255 | 0 | { |
14256 | 0 | VALIDATE_POINTER1(hDS, __func__, nullptr); |
14257 | 0 | auto poGroup(GDALDataset::FromHandle(hDS)->GetRootGroup()); |
14258 | 0 | return poGroup ? new GDALGroupHS(poGroup) : nullptr; |
14259 | 0 | } |
14260 | | |
14261 | | /************************************************************************/ |
14262 | | /* GDALRasterBandAsMDArray() */ |
14263 | | /************************************************************************/ |
14264 | | |
14265 | | /** Return a view of this raster band as a 2D multidimensional GDALMDArray. |
14266 | | * |
14267 | | * The band must be linked to a GDALDataset. If this dataset is not already |
14268 | | * marked as shared, it will be, so that the returned array holds a reference |
14269 | | * to it. |
14270 | | * |
14271 | | * If the dataset has a geotransform attached, the X and Y dimensions of the |
14272 | | * returned array will have an associated indexing variable. |
14273 | | * |
14274 | | * The returned pointer must be released with GDALMDArrayRelease(). |
14275 | | * |
14276 | | * This is the same as the C++ method GDALRasterBand::AsMDArray(). |
14277 | | * |
14278 | | * @return a new array, or NULL. |
14279 | | * |
14280 | | * @since GDAL 3.1 |
14281 | | */ |
14282 | | GDALMDArrayH GDALRasterBandAsMDArray(GDALRasterBandH hBand) |
14283 | 0 | { |
14284 | 0 | VALIDATE_POINTER1(hBand, __func__, nullptr); |
14285 | 0 | auto poArray(GDALRasterBand::FromHandle(hBand)->AsMDArray()); |
14286 | 0 | if (!poArray) |
14287 | 0 | return nullptr; |
14288 | 0 | return new GDALMDArrayHS(poArray); |
14289 | 0 | } |
14290 | | |
14291 | | /************************************************************************/ |
14292 | | /* GDALDatasetAsMDArray() */ |
14293 | | /************************************************************************/ |
14294 | | |
14295 | | /** Return a view of this dataset as a 3D multidimensional GDALMDArray. |
14296 | | * |
14297 | | * If this dataset is not already marked as shared, it will be, so that the |
14298 | | * returned array holds a reference to it. |
14299 | | * |
14300 | | * If the dataset has a geotransform attached, the X and Y dimensions of the |
14301 | | * returned array will have an associated indexing variable. |
14302 | | * |
14303 | | * The currently supported list of options is: |
14304 | | * <ul> |
14305 | | * <li>DIM_ORDER=<order> where order can be "AUTO", "Band,Y,X" or "Y,X,Band". |
14306 | | * "Band,Y,X" means that the first (slowest changing) dimension is Band |
14307 | | * and the last (fastest changing direction) is X |
14308 | | * "Y,X,Band" means that the first (slowest changing) dimension is Y |
14309 | | * and the last (fastest changing direction) is Band. |
14310 | | * "AUTO" (the default) selects "Band,Y,X" for single band datasets, or takes |
14311 | | * into account the INTERLEAVE metadata item in the IMAGE_STRUCTURE domain. |
14312 | | * If it equals BAND, then "Band,Y,X" is used. Otherwise (if it equals PIXEL), |
14313 | | * "Y,X,Band" is use. |
14314 | | * </li> |
14315 | | * <li>BAND_INDEXING_VAR_ITEM={Description}|{None}|{Index}|{ColorInterpretation}|<BandMetadataItem>: |
14316 | | * item from which to build the band indexing variable. |
14317 | | * <ul> |
14318 | | * <li>"{Description}", the default, means to use the band description (or "Band index" if empty).</li> |
14319 | | * <li>"{None}" means that no band indexing variable must be created.</li> |
14320 | | * <li>"{Index}" means that the band index (starting at one) is used.</li> |
14321 | | * <li>"{ColorInterpretation}" means that the band color interpretation is used (i.e. "Red", "Green", "Blue").</li> |
14322 | | * <li><BandMetadataItem> is the name of a band metadata item to use.</li> |
14323 | | * </ul> |
14324 | | * </li> |
14325 | | * <li>BAND_INDEXING_VAR_TYPE=String|Real|Integer: the data type of the band |
14326 | | * indexing variable, when BAND_INDEXING_VAR_ITEM corresponds to a band metadata item. |
14327 | | * Defaults to String. |
14328 | | * </li> |
14329 | | * <li>BAND_DIM_NAME=<string>: Name of the band dimension. |
14330 | | * Defaults to "Band". |
14331 | | * </li> |
14332 | | * <li>X_DIM_NAME=<string>: Name of the X dimension. Defaults to "X". |
14333 | | * </li> |
14334 | | * <li>Y_DIM_NAME=<string>: Name of the Y dimension. Defaults to "Y". |
14335 | | * </li> |
14336 | | * </ul> |
14337 | | * |
14338 | | * The returned pointer must be released with GDALMDArrayRelease(). |
14339 | | * |
14340 | | * The "reverse" methods are GDALRasterBand::AsMDArray() and |
14341 | | * GDALDataset::AsMDArray() |
14342 | | * |
14343 | | * This is the same as the C++ method GDALDataset::AsMDArray(). |
14344 | | * |
14345 | | * @param hDS Dataset handle. |
14346 | | * @param papszOptions Null-terminated list of strings, or nullptr. |
14347 | | * @return a new array, or NULL. |
14348 | | * |
14349 | | * @since GDAL 3.12 |
14350 | | */ |
14351 | | GDALMDArrayH GDALDatasetAsMDArray(GDALDatasetH hDS, CSLConstList papszOptions) |
14352 | 0 | { |
14353 | 0 | VALIDATE_POINTER1(hDS, __func__, nullptr); |
14354 | 0 | auto poArray(GDALDataset::FromHandle(hDS)->AsMDArray(papszOptions)); |
14355 | 0 | if (!poArray) |
14356 | 0 | return nullptr; |
14357 | 0 | return new GDALMDArrayHS(poArray); |
14358 | 0 | } |
14359 | | |
14360 | | /************************************************************************/ |
14361 | | /* GDALMDArrayAsClassicDataset() */ |
14362 | | /************************************************************************/ |
14363 | | |
14364 | | /** Return a view of this array as a "classic" GDALDataset (ie 2D) |
14365 | | * |
14366 | | * Only 2D or more arrays are supported. |
14367 | | * |
14368 | | * In the case of > 2D arrays, additional dimensions will be represented as |
14369 | | * raster bands. |
14370 | | * |
14371 | | * The "reverse" methods are GDALRasterBand::AsMDArray() and |
14372 | | * GDALDataset::AsMDArray() |
14373 | | * |
14374 | | * This is the same as the C++ method GDALMDArray::AsClassicDataset(). |
14375 | | * |
14376 | | * @param hArray Array. |
14377 | | * @param iXDim Index of the dimension that will be used as the X/width axis. |
14378 | | * @param iYDim Index of the dimension that will be used as the Y/height axis. |
14379 | | * @return a new GDALDataset that must be freed with GDALClose(), or nullptr |
14380 | | */ |
14381 | | GDALDatasetH GDALMDArrayAsClassicDataset(GDALMDArrayH hArray, size_t iXDim, |
14382 | | size_t iYDim) |
14383 | 0 | { |
14384 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
14385 | 0 | return GDALDataset::ToHandle( |
14386 | 0 | hArray->m_poImpl->AsClassicDataset(iXDim, iYDim)); |
14387 | 0 | } |
14388 | | |
14389 | | /************************************************************************/ |
14390 | | /* GDALMDArrayAsClassicDatasetEx() */ |
14391 | | /************************************************************************/ |
14392 | | |
14393 | | /** Return a view of this array as a "classic" GDALDataset (ie 2D) |
14394 | | * |
14395 | | * Only 2D or more arrays are supported. |
14396 | | * |
14397 | | * In the case of > 2D arrays, additional dimensions will be represented as |
14398 | | * raster bands. |
14399 | | * |
14400 | | * The "reverse" method is GDALRasterBand::AsMDArray(). |
14401 | | * |
14402 | | * This is the same as the C++ method GDALMDArray::AsClassicDataset(). |
14403 | | * @param hArray Array. |
14404 | | * @param iXDim Index of the dimension that will be used as the X/width axis. |
14405 | | * @param iYDim Index of the dimension that will be used as the Y/height axis. |
14406 | | * Ignored if the dimension count is 1. |
14407 | | * @param hRootGroup Root group, or NULL. Used with the BAND_METADATA and |
14408 | | * BAND_IMAGERY_METADATA option. |
14409 | | * @param papszOptions Cf GDALMDArray::AsClassicDataset() |
14410 | | * @return a new GDALDataset that must be freed with GDALClose(), or nullptr |
14411 | | * @since GDAL 3.8 |
14412 | | */ |
14413 | | GDALDatasetH GDALMDArrayAsClassicDatasetEx(GDALMDArrayH hArray, size_t iXDim, |
14414 | | size_t iYDim, GDALGroupH hRootGroup, |
14415 | | CSLConstList papszOptions) |
14416 | 0 | { |
14417 | 0 | VALIDATE_POINTER1(hArray, __func__, nullptr); |
14418 | 0 | return GDALDataset::ToHandle(hArray->m_poImpl->AsClassicDataset( |
14419 | 0 | iXDim, iYDim, hRootGroup ? hRootGroup->m_poImpl : nullptr, |
14420 | 0 | papszOptions)); |
14421 | 0 | } |
14422 | | |
14423 | | //! @cond Doxygen_Suppress |
14424 | | |
14425 | | GDALAttributeString::GDALAttributeString(const std::string &osParentName, |
14426 | | const std::string &osName, |
14427 | | const std::string &osValue, |
14428 | | GDALExtendedDataTypeSubType eSubType) |
14429 | 0 | : GDALAbstractMDArray(osParentName, osName), |
14430 | 0 | GDALAttribute(osParentName, osName), |
14431 | 0 | m_dt(GDALExtendedDataType::CreateString(0, eSubType)), m_osValue(osValue) |
14432 | 0 | { |
14433 | 0 | } Unexecuted instantiation: GDALAttributeString::GDALAttributeString(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, GDALExtendedDataTypeSubType) Unexecuted instantiation: GDALAttributeString::GDALAttributeString(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, GDALExtendedDataTypeSubType) |
14434 | | |
14435 | | const std::vector<std::shared_ptr<GDALDimension>> & |
14436 | | GDALAttributeString::GetDimensions() const |
14437 | 0 | { |
14438 | 0 | return m_dims; |
14439 | 0 | } |
14440 | | |
14441 | | const GDALExtendedDataType &GDALAttributeString::GetDataType() const |
14442 | 0 | { |
14443 | 0 | return m_dt; |
14444 | 0 | } |
14445 | | |
14446 | | bool GDALAttributeString::IRead(const GUInt64 *, const size_t *, const GInt64 *, |
14447 | | const GPtrDiff_t *, |
14448 | | const GDALExtendedDataType &bufferDataType, |
14449 | | void *pDstBuffer) const |
14450 | 0 | { |
14451 | 0 | if (bufferDataType.GetClass() != GEDTC_STRING) |
14452 | 0 | return false; |
14453 | 0 | char *pszStr = static_cast<char *>(VSIMalloc(m_osValue.size() + 1)); |
14454 | 0 | if (!pszStr) |
14455 | 0 | return false; |
14456 | 0 | memcpy(pszStr, m_osValue.c_str(), m_osValue.size() + 1); |
14457 | 0 | *static_cast<char **>(pDstBuffer) = pszStr; |
14458 | 0 | return true; |
14459 | 0 | } |
14460 | | |
14461 | | GDALAttributeNumeric::GDALAttributeNumeric(const std::string &osParentName, |
14462 | | const std::string &osName, |
14463 | | double dfValue) |
14464 | 0 | : GDALAbstractMDArray(osParentName, osName), |
14465 | 0 | GDALAttribute(osParentName, osName), |
14466 | 0 | m_dt(GDALExtendedDataType::Create(GDT_Float64)), m_dfValue(dfValue) |
14467 | 0 | { |
14468 | 0 | } Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, double) Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, double) |
14469 | | |
14470 | | GDALAttributeNumeric::GDALAttributeNumeric(const std::string &osParentName, |
14471 | | const std::string &osName, |
14472 | | int nValue) |
14473 | 0 | : GDALAbstractMDArray(osParentName, osName), |
14474 | 0 | GDALAttribute(osParentName, osName), |
14475 | 0 | m_dt(GDALExtendedDataType::Create(GDT_Int32)), m_nValue(nValue) |
14476 | 0 | { |
14477 | 0 | } Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int) Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int) |
14478 | | |
14479 | | GDALAttributeNumeric::GDALAttributeNumeric(const std::string &osParentName, |
14480 | | const std::string &osName, |
14481 | | const std::vector<GUInt32> &anValues) |
14482 | 0 | : GDALAbstractMDArray(osParentName, osName), |
14483 | 0 | GDALAttribute(osParentName, osName), |
14484 | 0 | m_dt(GDALExtendedDataType::Create(GDT_UInt32)), m_anValuesUInt32(anValues) |
14485 | 0 | { |
14486 | 0 | m_dims.push_back(std::make_shared<GDALDimension>( |
14487 | 0 | std::string(), "dim0", std::string(), std::string(), |
14488 | 0 | m_anValuesUInt32.size())); |
14489 | 0 | } Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&) Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&) |
14490 | | |
14491 | | const std::vector<std::shared_ptr<GDALDimension>> & |
14492 | | GDALAttributeNumeric::GetDimensions() const |
14493 | 0 | { |
14494 | 0 | return m_dims; |
14495 | 0 | } |
14496 | | |
14497 | | const GDALExtendedDataType &GDALAttributeNumeric::GetDataType() const |
14498 | 0 | { |
14499 | 0 | return m_dt; |
14500 | 0 | } |
14501 | | |
14502 | | bool GDALAttributeNumeric::IRead(const GUInt64 *arrayStartIdx, |
14503 | | const size_t *count, const GInt64 *arrayStep, |
14504 | | const GPtrDiff_t *bufferStride, |
14505 | | const GDALExtendedDataType &bufferDataType, |
14506 | | void *pDstBuffer) const |
14507 | 0 | { |
14508 | 0 | if (m_dims.empty()) |
14509 | 0 | { |
14510 | 0 | if (m_dt.GetNumericDataType() == GDT_Float64) |
14511 | 0 | GDALExtendedDataType::CopyValue(&m_dfValue, m_dt, pDstBuffer, |
14512 | 0 | bufferDataType); |
14513 | 0 | else |
14514 | 0 | { |
14515 | 0 | CPLAssert(m_dt.GetNumericDataType() == GDT_Int32); |
14516 | 0 | GDALExtendedDataType::CopyValue(&m_nValue, m_dt, pDstBuffer, |
14517 | 0 | bufferDataType); |
14518 | 0 | } |
14519 | 0 | } |
14520 | 0 | else |
14521 | 0 | { |
14522 | 0 | CPLAssert(m_dt.GetNumericDataType() == GDT_UInt32); |
14523 | 0 | GByte *pabyDstBuffer = static_cast<GByte *>(pDstBuffer); |
14524 | 0 | for (size_t i = 0; i < count[0]; ++i) |
14525 | 0 | { |
14526 | 0 | GDALExtendedDataType::CopyValue( |
14527 | 0 | &m_anValuesUInt32[static_cast<size_t>(arrayStartIdx[0] + |
14528 | 0 | i * arrayStep[0])], |
14529 | 0 | m_dt, pabyDstBuffer, bufferDataType); |
14530 | 0 | pabyDstBuffer += bufferDataType.GetSize() * bufferStride[0]; |
14531 | 0 | } |
14532 | 0 | } |
14533 | 0 | return true; |
14534 | 0 | } |
14535 | | |
14536 | | GDALMDArrayRegularlySpaced::GDALMDArrayRegularlySpaced( |
14537 | | const std::string &osParentName, const std::string &osName, |
14538 | | const std::shared_ptr<GDALDimension> &poDim, double dfStart, |
14539 | | double dfIncrement, double dfOffsetInIncrement) |
14540 | 0 | : GDALAbstractMDArray(osParentName, osName), |
14541 | 0 | GDALMDArray(osParentName, osName), m_dfStart(dfStart), |
14542 | 0 | m_dfIncrement(dfIncrement), |
14543 | 0 | m_dfOffsetInIncrement(dfOffsetInIncrement), m_dims{poDim} |
14544 | 0 | { |
14545 | 0 | } Unexecuted instantiation: GDALMDArrayRegularlySpaced::GDALMDArrayRegularlySpaced(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<GDALDimension> const&, double, double, double) Unexecuted instantiation: GDALMDArrayRegularlySpaced::GDALMDArrayRegularlySpaced(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<GDALDimension> const&, double, double, double) |
14546 | | |
14547 | | std::shared_ptr<GDALMDArrayRegularlySpaced> GDALMDArrayRegularlySpaced::Create( |
14548 | | const std::string &osParentName, const std::string &osName, |
14549 | | const std::shared_ptr<GDALDimension> &poDim, double dfStart, |
14550 | | double dfIncrement, double dfOffsetInIncrement) |
14551 | 0 | { |
14552 | 0 | auto poArray = std::make_shared<GDALMDArrayRegularlySpaced>( |
14553 | 0 | osParentName, osName, poDim, dfStart, dfIncrement, dfOffsetInIncrement); |
14554 | 0 | poArray->SetSelf(poArray); |
14555 | 0 | return poArray; |
14556 | 0 | } |
14557 | | |
14558 | | const std::vector<std::shared_ptr<GDALDimension>> & |
14559 | | GDALMDArrayRegularlySpaced::GetDimensions() const |
14560 | 0 | { |
14561 | 0 | return m_dims; |
14562 | 0 | } |
14563 | | |
14564 | | const GDALExtendedDataType &GDALMDArrayRegularlySpaced::GetDataType() const |
14565 | 0 | { |
14566 | 0 | return m_dt; |
14567 | 0 | } |
14568 | | |
14569 | | std::vector<std::shared_ptr<GDALAttribute>> |
14570 | | GDALMDArrayRegularlySpaced::GetAttributes(CSLConstList) const |
14571 | 0 | { |
14572 | 0 | return m_attributes; |
14573 | 0 | } |
14574 | | |
14575 | | void GDALMDArrayRegularlySpaced::AddAttribute( |
14576 | | const std::shared_ptr<GDALAttribute> &poAttr) |
14577 | 0 | { |
14578 | 0 | m_attributes.emplace_back(poAttr); |
14579 | 0 | } |
14580 | | |
14581 | | bool GDALMDArrayRegularlySpaced::IRead( |
14582 | | const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep, |
14583 | | const GPtrDiff_t *bufferStride, const GDALExtendedDataType &bufferDataType, |
14584 | | void *pDstBuffer) const |
14585 | 0 | { |
14586 | 0 | GByte *pabyDstBuffer = static_cast<GByte *>(pDstBuffer); |
14587 | 0 | for (size_t i = 0; i < count[0]; i++) |
14588 | 0 | { |
14589 | 0 | const double dfVal = |
14590 | 0 | m_dfStart + |
14591 | 0 | (arrayStartIdx[0] + i * static_cast<double>(arrayStep[0]) + |
14592 | 0 | m_dfOffsetInIncrement) * |
14593 | 0 | m_dfIncrement; |
14594 | 0 | GDALExtendedDataType::CopyValue(&dfVal, m_dt, pabyDstBuffer, |
14595 | 0 | bufferDataType); |
14596 | 0 | pabyDstBuffer += bufferStride[0] * bufferDataType.GetSize(); |
14597 | 0 | } |
14598 | 0 | return true; |
14599 | 0 | } |
14600 | | |
14601 | | GDALDimensionWeakIndexingVar::GDALDimensionWeakIndexingVar( |
14602 | | const std::string &osParentName, const std::string &osName, |
14603 | | const std::string &osType, const std::string &osDirection, GUInt64 nSize) |
14604 | 0 | : GDALDimension(osParentName, osName, osType, osDirection, nSize) |
14605 | 0 | { |
14606 | 0 | } |
14607 | | |
14608 | | std::shared_ptr<GDALMDArray> |
14609 | | GDALDimensionWeakIndexingVar::GetIndexingVariable() const |
14610 | 0 | { |
14611 | 0 | return m_poIndexingVariable.lock(); |
14612 | 0 | } |
14613 | | |
14614 | | // cppcheck-suppress passedByValue |
14615 | | bool GDALDimensionWeakIndexingVar::SetIndexingVariable( |
14616 | | std::shared_ptr<GDALMDArray> poIndexingVariable) |
14617 | 0 | { |
14618 | 0 | m_poIndexingVariable = poIndexingVariable; |
14619 | 0 | return true; |
14620 | 0 | } |
14621 | | |
14622 | | void GDALDimensionWeakIndexingVar::SetSize(GUInt64 nNewSize) |
14623 | 0 | { |
14624 | 0 | m_nSize = nNewSize; |
14625 | 0 | } |
14626 | | |
14627 | | /************************************************************************/ |
14628 | | /* GDALPamMultiDim::Private */ |
14629 | | /************************************************************************/ |
14630 | | |
14631 | | struct GDALPamMultiDim::Private |
14632 | | { |
14633 | | std::string m_osFilename{}; |
14634 | | std::string m_osPamFilename{}; |
14635 | | |
14636 | | struct Statistics |
14637 | | { |
14638 | | bool bHasStats = false; |
14639 | | bool bApproxStats = false; |
14640 | | double dfMin = 0; |
14641 | | double dfMax = 0; |
14642 | | double dfMean = 0; |
14643 | | double dfStdDev = 0; |
14644 | | GUInt64 nValidCount = 0; |
14645 | | }; |
14646 | | |
14647 | | struct ArrayInfo |
14648 | | { |
14649 | | std::shared_ptr<OGRSpatialReference> poSRS{}; |
14650 | | // cppcheck-suppress unusedStructMember |
14651 | | Statistics stats{}; |
14652 | | }; |
14653 | | |
14654 | | typedef std::pair<std::string, std::string> NameContext; |
14655 | | std::map<NameContext, ArrayInfo> m_oMapArray{}; |
14656 | | std::vector<CPLXMLTreeCloser> m_apoOtherNodes{}; |
14657 | | bool m_bDirty = false; |
14658 | | bool m_bLoaded = false; |
14659 | | }; |
14660 | | |
14661 | | /************************************************************************/ |
14662 | | /* GDALPamMultiDim */ |
14663 | | /************************************************************************/ |
14664 | | |
14665 | | GDALPamMultiDim::GDALPamMultiDim(const std::string &osFilename) |
14666 | 0 | : d(new Private()) |
14667 | 0 | { |
14668 | 0 | d->m_osFilename = osFilename; |
14669 | 0 | } |
14670 | | |
14671 | | /************************************************************************/ |
14672 | | /* GDALPamMultiDim::~GDALPamMultiDim() */ |
14673 | | /************************************************************************/ |
14674 | | |
14675 | | GDALPamMultiDim::~GDALPamMultiDim() |
14676 | 0 | { |
14677 | 0 | if (d->m_bDirty) |
14678 | 0 | Save(); |
14679 | 0 | } |
14680 | | |
14681 | | /************************************************************************/ |
14682 | | /* GDALPamMultiDim::Load() */ |
14683 | | /************************************************************************/ |
14684 | | |
14685 | | void GDALPamMultiDim::Load() |
14686 | 0 | { |
14687 | 0 | if (d->m_bLoaded) |
14688 | 0 | return; |
14689 | 0 | d->m_bLoaded = true; |
14690 | |
|
14691 | 0 | const char *pszProxyPam = PamGetProxy(d->m_osFilename.c_str()); |
14692 | 0 | d->m_osPamFilename = |
14693 | 0 | pszProxyPam ? std::string(pszProxyPam) : d->m_osFilename + ".aux.xml"; |
14694 | 0 | CPLXMLTreeCloser oTree(nullptr); |
14695 | 0 | { |
14696 | 0 | CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); |
14697 | 0 | oTree.reset(CPLParseXMLFile(d->m_osPamFilename.c_str())); |
14698 | 0 | } |
14699 | 0 | if (!oTree) |
14700 | 0 | { |
14701 | 0 | return; |
14702 | 0 | } |
14703 | 0 | const auto poPAMMultiDim = CPLGetXMLNode(oTree.get(), "=PAMDataset"); |
14704 | 0 | if (!poPAMMultiDim) |
14705 | 0 | return; |
14706 | 0 | for (CPLXMLNode *psIter = poPAMMultiDim->psChild; psIter; |
14707 | 0 | psIter = psIter->psNext) |
14708 | 0 | { |
14709 | 0 | if (psIter->eType == CXT_Element && |
14710 | 0 | strcmp(psIter->pszValue, "Array") == 0) |
14711 | 0 | { |
14712 | 0 | const char *pszName = CPLGetXMLValue(psIter, "name", nullptr); |
14713 | 0 | if (!pszName) |
14714 | 0 | continue; |
14715 | 0 | const char *pszContext = CPLGetXMLValue(psIter, "context", ""); |
14716 | 0 | const auto oKey = |
14717 | 0 | std::pair<std::string, std::string>(pszName, pszContext); |
14718 | | |
14719 | | /* -------------------------------------------------------------------- |
14720 | | */ |
14721 | | /* Check for an SRS node. */ |
14722 | | /* -------------------------------------------------------------------- |
14723 | | */ |
14724 | 0 | const CPLXMLNode *psSRSNode = CPLGetXMLNode(psIter, "SRS"); |
14725 | 0 | if (psSRSNode) |
14726 | 0 | { |
14727 | 0 | std::shared_ptr<OGRSpatialReference> poSRS = |
14728 | 0 | std::make_shared<OGRSpatialReference>(); |
14729 | 0 | poSRS->SetFromUserInput( |
14730 | 0 | CPLGetXMLValue(psSRSNode, nullptr, ""), |
14731 | 0 | OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS); |
14732 | 0 | const char *pszMapping = CPLGetXMLValue( |
14733 | 0 | psSRSNode, "dataAxisToSRSAxisMapping", nullptr); |
14734 | 0 | if (pszMapping) |
14735 | 0 | { |
14736 | 0 | char **papszTokens = |
14737 | 0 | CSLTokenizeStringComplex(pszMapping, ",", FALSE, FALSE); |
14738 | 0 | std::vector<int> anMapping; |
14739 | 0 | for (int i = 0; papszTokens && papszTokens[i]; i++) |
14740 | 0 | { |
14741 | 0 | anMapping.push_back(atoi(papszTokens[i])); |
14742 | 0 | } |
14743 | 0 | CSLDestroy(papszTokens); |
14744 | 0 | poSRS->SetDataAxisToSRSAxisMapping(anMapping); |
14745 | 0 | } |
14746 | 0 | else |
14747 | 0 | { |
14748 | 0 | poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
14749 | 0 | } |
14750 | |
|
14751 | 0 | const char *pszCoordinateEpoch = |
14752 | 0 | CPLGetXMLValue(psSRSNode, "coordinateEpoch", nullptr); |
14753 | 0 | if (pszCoordinateEpoch) |
14754 | 0 | poSRS->SetCoordinateEpoch(CPLAtof(pszCoordinateEpoch)); |
14755 | |
|
14756 | 0 | d->m_oMapArray[oKey].poSRS = std::move(poSRS); |
14757 | 0 | } |
14758 | |
|
14759 | 0 | const CPLXMLNode *psStatistics = |
14760 | 0 | CPLGetXMLNode(psIter, "Statistics"); |
14761 | 0 | if (psStatistics) |
14762 | 0 | { |
14763 | 0 | Private::Statistics sStats; |
14764 | 0 | sStats.bHasStats = true; |
14765 | 0 | sStats.bApproxStats = CPLTestBool( |
14766 | 0 | CPLGetXMLValue(psStatistics, "ApproxStats", "false")); |
14767 | 0 | sStats.dfMin = |
14768 | 0 | CPLAtofM(CPLGetXMLValue(psStatistics, "Minimum", "0")); |
14769 | 0 | sStats.dfMax = |
14770 | 0 | CPLAtofM(CPLGetXMLValue(psStatistics, "Maximum", "0")); |
14771 | 0 | sStats.dfMean = |
14772 | 0 | CPLAtofM(CPLGetXMLValue(psStatistics, "Mean", "0")); |
14773 | 0 | sStats.dfStdDev = |
14774 | 0 | CPLAtofM(CPLGetXMLValue(psStatistics, "StdDev", "0")); |
14775 | 0 | sStats.nValidCount = static_cast<GUInt64>(CPLAtoGIntBig( |
14776 | 0 | CPLGetXMLValue(psStatistics, "ValidSampleCount", "0"))); |
14777 | 0 | d->m_oMapArray[oKey].stats = sStats; |
14778 | 0 | } |
14779 | 0 | } |
14780 | 0 | else |
14781 | 0 | { |
14782 | 0 | CPLXMLNode *psNextBackup = psIter->psNext; |
14783 | 0 | psIter->psNext = nullptr; |
14784 | 0 | d->m_apoOtherNodes.emplace_back( |
14785 | 0 | CPLXMLTreeCloser(CPLCloneXMLTree(psIter))); |
14786 | 0 | psIter->psNext = psNextBackup; |
14787 | 0 | } |
14788 | 0 | } |
14789 | 0 | } |
14790 | | |
14791 | | /************************************************************************/ |
14792 | | /* GDALPamMultiDim::Save() */ |
14793 | | /************************************************************************/ |
14794 | | |
14795 | | void GDALPamMultiDim::Save() |
14796 | 0 | { |
14797 | 0 | CPLXMLTreeCloser oTree( |
14798 | 0 | CPLCreateXMLNode(nullptr, CXT_Element, "PAMDataset")); |
14799 | 0 | for (const auto &poOtherNode : d->m_apoOtherNodes) |
14800 | 0 | { |
14801 | 0 | CPLAddXMLChild(oTree.get(), CPLCloneXMLTree(poOtherNode.get())); |
14802 | 0 | } |
14803 | 0 | for (const auto &kv : d->m_oMapArray) |
14804 | 0 | { |
14805 | 0 | CPLXMLNode *psArrayNode = |
14806 | 0 | CPLCreateXMLNode(oTree.get(), CXT_Element, "Array"); |
14807 | 0 | CPLAddXMLAttributeAndValue(psArrayNode, "name", kv.first.first.c_str()); |
14808 | 0 | if (!kv.first.second.empty()) |
14809 | 0 | { |
14810 | 0 | CPLAddXMLAttributeAndValue(psArrayNode, "context", |
14811 | 0 | kv.first.second.c_str()); |
14812 | 0 | } |
14813 | 0 | if (kv.second.poSRS) |
14814 | 0 | { |
14815 | 0 | char *pszWKT = nullptr; |
14816 | 0 | { |
14817 | 0 | CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); |
14818 | 0 | const char *const apszOptions[] = {"FORMAT=WKT2", nullptr}; |
14819 | 0 | kv.second.poSRS->exportToWkt(&pszWKT, apszOptions); |
14820 | 0 | } |
14821 | 0 | CPLXMLNode *psSRSNode = |
14822 | 0 | CPLCreateXMLElementAndValue(psArrayNode, "SRS", pszWKT); |
14823 | 0 | CPLFree(pszWKT); |
14824 | 0 | const auto &mapping = |
14825 | 0 | kv.second.poSRS->GetDataAxisToSRSAxisMapping(); |
14826 | 0 | CPLString osMapping; |
14827 | 0 | for (size_t i = 0; i < mapping.size(); ++i) |
14828 | 0 | { |
14829 | 0 | if (!osMapping.empty()) |
14830 | 0 | osMapping += ","; |
14831 | 0 | osMapping += CPLSPrintf("%d", mapping[i]); |
14832 | 0 | } |
14833 | 0 | CPLAddXMLAttributeAndValue(psSRSNode, "dataAxisToSRSAxisMapping", |
14834 | 0 | osMapping.c_str()); |
14835 | |
|
14836 | 0 | const double dfCoordinateEpoch = |
14837 | 0 | kv.second.poSRS->GetCoordinateEpoch(); |
14838 | 0 | if (dfCoordinateEpoch > 0) |
14839 | 0 | { |
14840 | 0 | std::string osCoordinateEpoch = |
14841 | 0 | CPLSPrintf("%f", dfCoordinateEpoch); |
14842 | 0 | if (osCoordinateEpoch.find('.') != std::string::npos) |
14843 | 0 | { |
14844 | 0 | while (osCoordinateEpoch.back() == '0') |
14845 | 0 | osCoordinateEpoch.resize(osCoordinateEpoch.size() - 1); |
14846 | 0 | } |
14847 | 0 | CPLAddXMLAttributeAndValue(psSRSNode, "coordinateEpoch", |
14848 | 0 | osCoordinateEpoch.c_str()); |
14849 | 0 | } |
14850 | 0 | } |
14851 | |
|
14852 | 0 | if (kv.second.stats.bHasStats) |
14853 | 0 | { |
14854 | 0 | CPLXMLNode *psMDArray = |
14855 | 0 | CPLCreateXMLNode(psArrayNode, CXT_Element, "Statistics"); |
14856 | 0 | CPLCreateXMLElementAndValue(psMDArray, "ApproxStats", |
14857 | 0 | kv.second.stats.bApproxStats ? "1" |
14858 | 0 | : "0"); |
14859 | 0 | CPLCreateXMLElementAndValue( |
14860 | 0 | psMDArray, "Minimum", |
14861 | 0 | CPLSPrintf("%.17g", kv.second.stats.dfMin)); |
14862 | 0 | CPLCreateXMLElementAndValue( |
14863 | 0 | psMDArray, "Maximum", |
14864 | 0 | CPLSPrintf("%.17g", kv.second.stats.dfMax)); |
14865 | 0 | CPLCreateXMLElementAndValue( |
14866 | 0 | psMDArray, "Mean", CPLSPrintf("%.17g", kv.second.stats.dfMean)); |
14867 | 0 | CPLCreateXMLElementAndValue( |
14868 | 0 | psMDArray, "StdDev", |
14869 | 0 | CPLSPrintf("%.17g", kv.second.stats.dfStdDev)); |
14870 | 0 | CPLCreateXMLElementAndValue( |
14871 | 0 | psMDArray, "ValidSampleCount", |
14872 | 0 | CPLSPrintf(CPL_FRMT_GUIB, kv.second.stats.nValidCount)); |
14873 | 0 | } |
14874 | 0 | } |
14875 | |
|
14876 | 0 | int bSaved; |
14877 | 0 | CPLErrorAccumulator oErrorAccumulator; |
14878 | 0 | { |
14879 | 0 | auto oAccumulator = oErrorAccumulator.InstallForCurrentScope(); |
14880 | 0 | CPL_IGNORE_RET_VAL(oAccumulator); |
14881 | 0 | bSaved = |
14882 | 0 | CPLSerializeXMLTreeToFile(oTree.get(), d->m_osPamFilename.c_str()); |
14883 | 0 | } |
14884 | |
|
14885 | 0 | const char *pszNewPam = nullptr; |
14886 | 0 | if (!bSaved && PamGetProxy(d->m_osFilename.c_str()) == nullptr && |
14887 | 0 | ((pszNewPam = PamAllocateProxy(d->m_osFilename.c_str())) != nullptr)) |
14888 | 0 | { |
14889 | 0 | CPLErrorReset(); |
14890 | 0 | CPLSerializeXMLTreeToFile(oTree.get(), pszNewPam); |
14891 | 0 | } |
14892 | 0 | else |
14893 | 0 | { |
14894 | 0 | oErrorAccumulator.ReplayErrors(); |
14895 | 0 | } |
14896 | 0 | } |
14897 | | |
14898 | | /************************************************************************/ |
14899 | | /* GDALPamMultiDim::GetSpatialRef() */ |
14900 | | /************************************************************************/ |
14901 | | |
14902 | | std::shared_ptr<OGRSpatialReference> |
14903 | | GDALPamMultiDim::GetSpatialRef(const std::string &osArrayFullName, |
14904 | | const std::string &osContext) |
14905 | 0 | { |
14906 | 0 | Load(); |
14907 | 0 | auto oIter = |
14908 | 0 | d->m_oMapArray.find(std::make_pair(osArrayFullName, osContext)); |
14909 | 0 | if (oIter != d->m_oMapArray.end()) |
14910 | 0 | return oIter->second.poSRS; |
14911 | 0 | return nullptr; |
14912 | 0 | } |
14913 | | |
14914 | | /************************************************************************/ |
14915 | | /* GDALPamMultiDim::SetSpatialRef() */ |
14916 | | /************************************************************************/ |
14917 | | |
14918 | | void GDALPamMultiDim::SetSpatialRef(const std::string &osArrayFullName, |
14919 | | const std::string &osContext, |
14920 | | const OGRSpatialReference *poSRS) |
14921 | 0 | { |
14922 | 0 | Load(); |
14923 | 0 | d->m_bDirty = true; |
14924 | 0 | if (poSRS && !poSRS->IsEmpty()) |
14925 | 0 | d->m_oMapArray[std::make_pair(osArrayFullName, osContext)].poSRS.reset( |
14926 | 0 | poSRS->Clone()); |
14927 | 0 | else |
14928 | 0 | d->m_oMapArray[std::make_pair(osArrayFullName, osContext)] |
14929 | 0 | .poSRS.reset(); |
14930 | 0 | } |
14931 | | |
14932 | | /************************************************************************/ |
14933 | | /* GetStatistics() */ |
14934 | | /************************************************************************/ |
14935 | | |
14936 | | CPLErr GDALPamMultiDim::GetStatistics(const std::string &osArrayFullName, |
14937 | | const std::string &osContext, |
14938 | | bool bApproxOK, double *pdfMin, |
14939 | | double *pdfMax, double *pdfMean, |
14940 | | double *pdfStdDev, GUInt64 *pnValidCount) |
14941 | 0 | { |
14942 | 0 | Load(); |
14943 | 0 | auto oIter = |
14944 | 0 | d->m_oMapArray.find(std::make_pair(osArrayFullName, osContext)); |
14945 | 0 | if (oIter == d->m_oMapArray.end()) |
14946 | 0 | return CE_Failure; |
14947 | 0 | const auto &stats = oIter->second.stats; |
14948 | 0 | if (!stats.bHasStats) |
14949 | 0 | return CE_Failure; |
14950 | 0 | if (!bApproxOK && stats.bApproxStats) |
14951 | 0 | return CE_Failure; |
14952 | 0 | if (pdfMin) |
14953 | 0 | *pdfMin = stats.dfMin; |
14954 | 0 | if (pdfMax) |
14955 | 0 | *pdfMax = stats.dfMax; |
14956 | 0 | if (pdfMean) |
14957 | 0 | *pdfMean = stats.dfMean; |
14958 | 0 | if (pdfStdDev) |
14959 | 0 | *pdfStdDev = stats.dfStdDev; |
14960 | 0 | if (pnValidCount) |
14961 | 0 | *pnValidCount = stats.nValidCount; |
14962 | 0 | return CE_None; |
14963 | 0 | } |
14964 | | |
14965 | | /************************************************************************/ |
14966 | | /* SetStatistics() */ |
14967 | | /************************************************************************/ |
14968 | | |
14969 | | void GDALPamMultiDim::SetStatistics(const std::string &osArrayFullName, |
14970 | | const std::string &osContext, |
14971 | | bool bApproxStats, double dfMin, |
14972 | | double dfMax, double dfMean, |
14973 | | double dfStdDev, GUInt64 nValidCount) |
14974 | 0 | { |
14975 | 0 | Load(); |
14976 | 0 | d->m_bDirty = true; |
14977 | 0 | auto &stats = |
14978 | 0 | d->m_oMapArray[std::make_pair(osArrayFullName, osContext)].stats; |
14979 | 0 | stats.bHasStats = true; |
14980 | 0 | stats.bApproxStats = bApproxStats; |
14981 | 0 | stats.dfMin = dfMin; |
14982 | 0 | stats.dfMax = dfMax; |
14983 | 0 | stats.dfMean = dfMean; |
14984 | 0 | stats.dfStdDev = dfStdDev; |
14985 | 0 | stats.nValidCount = nValidCount; |
14986 | 0 | } |
14987 | | |
14988 | | /************************************************************************/ |
14989 | | /* ClearStatistics() */ |
14990 | | /************************************************************************/ |
14991 | | |
14992 | | void GDALPamMultiDim::ClearStatistics(const std::string &osArrayFullName, |
14993 | | const std::string &osContext) |
14994 | 0 | { |
14995 | 0 | Load(); |
14996 | 0 | d->m_bDirty = true; |
14997 | 0 | d->m_oMapArray[std::make_pair(osArrayFullName, osContext)].stats.bHasStats = |
14998 | 0 | false; |
14999 | 0 | } |
15000 | | |
15001 | | /************************************************************************/ |
15002 | | /* ClearStatistics() */ |
15003 | | /************************************************************************/ |
15004 | | |
15005 | | void GDALPamMultiDim::ClearStatistics() |
15006 | 0 | { |
15007 | 0 | Load(); |
15008 | 0 | d->m_bDirty = true; |
15009 | 0 | for (auto &kv : d->m_oMapArray) |
15010 | 0 | kv.second.stats.bHasStats = false; |
15011 | 0 | } |
15012 | | |
15013 | | /************************************************************************/ |
15014 | | /* GetPAM() */ |
15015 | | /************************************************************************/ |
15016 | | |
15017 | | /*static*/ std::shared_ptr<GDALPamMultiDim> |
15018 | | GDALPamMultiDim::GetPAM(const std::shared_ptr<GDALMDArray> &poParent) |
15019 | 0 | { |
15020 | 0 | auto poPamArray = dynamic_cast<GDALPamMDArray *>(poParent.get()); |
15021 | 0 | if (poPamArray) |
15022 | 0 | return poPamArray->GetPAM(); |
15023 | 0 | return nullptr; |
15024 | 0 | } |
15025 | | |
15026 | | /************************************************************************/ |
15027 | | /* GDALPamMDArray */ |
15028 | | /************************************************************************/ |
15029 | | |
15030 | | GDALPamMDArray::GDALPamMDArray(const std::string &osParentName, |
15031 | | const std::string &osName, |
15032 | | const std::shared_ptr<GDALPamMultiDim> &poPam, |
15033 | | const std::string &osContext) |
15034 | | : |
15035 | | #if !defined(COMPILER_WARNS_ABOUT_ABSTRACT_VBASE_INIT) |
15036 | | GDALAbstractMDArray(osParentName, osName), |
15037 | | #endif |
15038 | 0 | GDALMDArray(osParentName, osName, osContext), m_poPam(poPam) |
15039 | 0 | { |
15040 | 0 | } |
15041 | | |
15042 | | /************************************************************************/ |
15043 | | /* GDALPamMDArray::SetSpatialRef() */ |
15044 | | /************************************************************************/ |
15045 | | |
15046 | | bool GDALPamMDArray::SetSpatialRef(const OGRSpatialReference *poSRS) |
15047 | 0 | { |
15048 | 0 | if (!m_poPam) |
15049 | 0 | return false; |
15050 | 0 | m_poPam->SetSpatialRef(GetFullName(), GetContext(), poSRS); |
15051 | 0 | return true; |
15052 | 0 | } |
15053 | | |
15054 | | /************************************************************************/ |
15055 | | /* GDALPamMDArray::GetSpatialRef() */ |
15056 | | /************************************************************************/ |
15057 | | |
15058 | | std::shared_ptr<OGRSpatialReference> GDALPamMDArray::GetSpatialRef() const |
15059 | 0 | { |
15060 | 0 | if (!m_poPam) |
15061 | 0 | return nullptr; |
15062 | 0 | return m_poPam->GetSpatialRef(GetFullName(), GetContext()); |
15063 | 0 | } |
15064 | | |
15065 | | /************************************************************************/ |
15066 | | /* GetStatistics() */ |
15067 | | /************************************************************************/ |
15068 | | |
15069 | | CPLErr GDALPamMDArray::GetStatistics(bool bApproxOK, bool bForce, |
15070 | | double *pdfMin, double *pdfMax, |
15071 | | double *pdfMean, double *pdfStdDev, |
15072 | | GUInt64 *pnValidCount, |
15073 | | GDALProgressFunc pfnProgress, |
15074 | | void *pProgressData) |
15075 | 0 | { |
15076 | 0 | if (m_poPam && m_poPam->GetStatistics(GetFullName(), GetContext(), |
15077 | 0 | bApproxOK, pdfMin, pdfMax, pdfMean, |
15078 | 0 | pdfStdDev, pnValidCount) == CE_None) |
15079 | 0 | { |
15080 | 0 | return CE_None; |
15081 | 0 | } |
15082 | 0 | if (!bForce) |
15083 | 0 | return CE_Warning; |
15084 | | |
15085 | 0 | return GDALMDArray::GetStatistics(bApproxOK, bForce, pdfMin, pdfMax, |
15086 | 0 | pdfMean, pdfStdDev, pnValidCount, |
15087 | 0 | pfnProgress, pProgressData); |
15088 | 0 | } |
15089 | | |
15090 | | /************************************************************************/ |
15091 | | /* SetStatistics() */ |
15092 | | /************************************************************************/ |
15093 | | |
15094 | | bool GDALPamMDArray::SetStatistics(bool bApproxStats, double dfMin, |
15095 | | double dfMax, double dfMean, double dfStdDev, |
15096 | | GUInt64 nValidCount, |
15097 | | CSLConstList /* papszOptions */) |
15098 | 0 | { |
15099 | 0 | if (!m_poPam) |
15100 | 0 | return false; |
15101 | 0 | m_poPam->SetStatistics(GetFullName(), GetContext(), bApproxStats, dfMin, |
15102 | 0 | dfMax, dfMean, dfStdDev, nValidCount); |
15103 | 0 | return true; |
15104 | 0 | } |
15105 | | |
15106 | | /************************************************************************/ |
15107 | | /* ClearStatistics() */ |
15108 | | /************************************************************************/ |
15109 | | |
15110 | | void GDALPamMDArray::ClearStatistics() |
15111 | 0 | { |
15112 | 0 | if (!m_poPam) |
15113 | 0 | return; |
15114 | 0 | m_poPam->ClearStatistics(GetFullName(), GetContext()); |
15115 | 0 | } |
15116 | | |
15117 | | /************************************************************************/ |
15118 | | /* GDALMDIAsAttribute::GetDimensions() */ |
15119 | | /************************************************************************/ |
15120 | | |
15121 | | const std::vector<std::shared_ptr<GDALDimension>> & |
15122 | | GDALMDIAsAttribute::GetDimensions() const |
15123 | 0 | { |
15124 | 0 | return m_dims; |
15125 | 0 | } |
15126 | | |
15127 | | /************************************************************************/ |
15128 | | /* GDALMDArrayRawBlockInfo::~GDALMDArrayRawBlockInfo() */ |
15129 | | /************************************************************************/ |
15130 | | |
15131 | | GDALMDArrayRawBlockInfo::~GDALMDArrayRawBlockInfo() |
15132 | 0 | { |
15133 | 0 | clear(); |
15134 | 0 | } |
15135 | | |
15136 | | /************************************************************************/ |
15137 | | /* GDALMDArrayRawBlockInfo::clear() */ |
15138 | | /************************************************************************/ |
15139 | | |
15140 | | void GDALMDArrayRawBlockInfo::clear() |
15141 | 0 | { |
15142 | 0 | CPLFree(pszFilename); |
15143 | 0 | pszFilename = nullptr; |
15144 | 0 | CSLDestroy(papszInfo); |
15145 | 0 | papszInfo = nullptr; |
15146 | 0 | nOffset = 0; |
15147 | 0 | nSize = 0; |
15148 | 0 | CPLFree(pabyInlineData); |
15149 | 0 | pabyInlineData = nullptr; |
15150 | 0 | } |
15151 | | |
15152 | | /************************************************************************/ |
15153 | | /* GDALMDArrayRawBlockInfo::GDALMDArrayRawBlockInfo() */ |
15154 | | /************************************************************************/ |
15155 | | |
15156 | | GDALMDArrayRawBlockInfo::GDALMDArrayRawBlockInfo( |
15157 | | const GDALMDArrayRawBlockInfo &other) |
15158 | 0 | : pszFilename(other.pszFilename ? CPLStrdup(other.pszFilename) : nullptr), |
15159 | 0 | nOffset(other.nOffset), nSize(other.nSize), |
15160 | 0 | papszInfo(CSLDuplicate(other.papszInfo)), pabyInlineData(nullptr) |
15161 | 0 | { |
15162 | 0 | if (other.pabyInlineData) |
15163 | 0 | { |
15164 | 0 | pabyInlineData = static_cast<GByte *>( |
15165 | 0 | VSI_MALLOC_VERBOSE(static_cast<size_t>(other.nSize))); |
15166 | 0 | if (pabyInlineData) |
15167 | 0 | memcpy(pabyInlineData, other.pabyInlineData, |
15168 | 0 | static_cast<size_t>(other.nSize)); |
15169 | 0 | } |
15170 | 0 | } |
15171 | | |
15172 | | /************************************************************************/ |
15173 | | /* GDALMDArrayRawBlockInfo::operator=() */ |
15174 | | /************************************************************************/ |
15175 | | |
15176 | | GDALMDArrayRawBlockInfo & |
15177 | | GDALMDArrayRawBlockInfo::operator=(const GDALMDArrayRawBlockInfo &other) |
15178 | 0 | { |
15179 | 0 | if (this != &other) |
15180 | 0 | { |
15181 | 0 | CPLFree(pszFilename); |
15182 | 0 | pszFilename = |
15183 | 0 | other.pszFilename ? CPLStrdup(other.pszFilename) : nullptr; |
15184 | 0 | nOffset = other.nOffset; |
15185 | 0 | nSize = other.nSize; |
15186 | 0 | CSLDestroy(papszInfo); |
15187 | 0 | papszInfo = CSLDuplicate(other.papszInfo); |
15188 | 0 | CPLFree(pabyInlineData); |
15189 | 0 | pabyInlineData = nullptr; |
15190 | 0 | if (other.pabyInlineData) |
15191 | 0 | { |
15192 | 0 | pabyInlineData = static_cast<GByte *>( |
15193 | 0 | VSI_MALLOC_VERBOSE(static_cast<size_t>(other.nSize))); |
15194 | 0 | if (pabyInlineData) |
15195 | 0 | memcpy(pabyInlineData, other.pabyInlineData, |
15196 | 0 | static_cast<size_t>(other.nSize)); |
15197 | 0 | } |
15198 | 0 | } |
15199 | 0 | return *this; |
15200 | 0 | } |
15201 | | |
15202 | | /************************************************************************/ |
15203 | | /* GDALMDArrayRawBlockInfo::GDALMDArrayRawBlockInfo() */ |
15204 | | /************************************************************************/ |
15205 | | |
15206 | | GDALMDArrayRawBlockInfo::GDALMDArrayRawBlockInfo( |
15207 | | GDALMDArrayRawBlockInfo &&other) |
15208 | 0 | : pszFilename(other.pszFilename), nOffset(other.nOffset), |
15209 | 0 | nSize(other.nSize), papszInfo(other.papszInfo), |
15210 | 0 | pabyInlineData(other.pabyInlineData) |
15211 | 0 | { |
15212 | 0 | other.pszFilename = nullptr; |
15213 | 0 | other.papszInfo = nullptr; |
15214 | 0 | other.pabyInlineData = nullptr; |
15215 | 0 | } |
15216 | | |
15217 | | /************************************************************************/ |
15218 | | /* GDALMDArrayRawBlockInfo::operator=() */ |
15219 | | /************************************************************************/ |
15220 | | |
15221 | | GDALMDArrayRawBlockInfo & |
15222 | | GDALMDArrayRawBlockInfo::operator=(GDALMDArrayRawBlockInfo &&other) |
15223 | 0 | { |
15224 | 0 | if (this != &other) |
15225 | 0 | { |
15226 | 0 | std::swap(pszFilename, other.pszFilename); |
15227 | 0 | nOffset = other.nOffset; |
15228 | 0 | nSize = other.nSize; |
15229 | 0 | std::swap(papszInfo, other.papszInfo); |
15230 | 0 | std::swap(pabyInlineData, other.pabyInlineData); |
15231 | 0 | } |
15232 | 0 | return *this; |
15233 | 0 | } |
15234 | | |
15235 | | //! @endcond |
15236 | | |
15237 | | /************************************************************************/ |
15238 | | /* GDALMDArray::GetRawBlockInfo() */ |
15239 | | /************************************************************************/ |
15240 | | |
15241 | | /** Return information on a raw block. |
15242 | | * |
15243 | | * The block coordinates must be between 0 and |
15244 | | * (GetDimensions()[i]->GetSize() / GetBlockSize()[i]) - 1, for all i between |
15245 | | * 0 and GetDimensionCount()-1. |
15246 | | * |
15247 | | * If the queried block has valid coordinates but is missing in the dataset, |
15248 | | * all fields of info will be set to 0/nullptr, but the function will return |
15249 | | * true. |
15250 | | * |
15251 | | * This method is only implemented by a subset of drivers. The base |
15252 | | * implementation just returns false and empty info. |
15253 | | * |
15254 | | * The values returned in psBlockInfo->papszInfo are driver dependent. |
15255 | | * |
15256 | | * For multi-byte data types, drivers should return a "ENDIANNESS" key whose |
15257 | | * value is "LITTLE" or "BIG". |
15258 | | * |
15259 | | * For HDF5 and netCDF 4, the potential keys are "COMPRESSION" (possible values |
15260 | | * "DEFLATE" or "SZIP") and "FILTER" (if several filters, names are |
15261 | | * comma-separated) |
15262 | | * |
15263 | | * For ZARR, the potential keys are "COMPRESSOR" (value is the JSON encoded |
15264 | | * content from the array definition), "FILTERS" (for Zarr V2, value is JSON |
15265 | | * encoded content) and "TRANSPOSE_ORDER" (value is a string like |
15266 | | * "[idx0,...,idxN]" with the permutation). |
15267 | | * |
15268 | | * For VRT, the potential keys are the ones of the underlying source(s). Note |
15269 | | * that GetRawBlockInfo() on VRT only works when the VRT declares a block size, |
15270 | | * that for each queried VRT block, there is one and only one source that |
15271 | | * is used to fill the VRT block and that the block size of this source is |
15272 | | * exactly the one of the VRT block. |
15273 | | * |
15274 | | * This is the same as C function GDALMDArrayGetRawBlockInfo(). |
15275 | | * |
15276 | | * @param panBlockCoordinates array of GetDimensionCount() values with the block |
15277 | | * coordinates. |
15278 | | * @param[out] info structure to fill with block information. |
15279 | | * @return true in case of success, or false if an error occurs. |
15280 | | * @since 3.12 |
15281 | | */ |
15282 | | bool GDALMDArray::GetRawBlockInfo(const uint64_t *panBlockCoordinates, |
15283 | | GDALMDArrayRawBlockInfo &info) const |
15284 | 0 | { |
15285 | 0 | (void)panBlockCoordinates; |
15286 | 0 | info.clear(); |
15287 | 0 | return false; |
15288 | 0 | } |
15289 | | |
15290 | | /************************************************************************/ |
15291 | | /* GDALMDArrayGetRawBlockInfo() */ |
15292 | | /************************************************************************/ |
15293 | | |
15294 | | /** Return information on a raw block. |
15295 | | * |
15296 | | * The block coordinates must be between 0 and |
15297 | | * (GetDimensions()[i]->GetSize() / GetBlockSize()[i]) - 1, for all i between |
15298 | | * 0 and GetDimensionCount()-1. |
15299 | | * |
15300 | | * If the queried block has valid coordinates but is missing in the dataset, |
15301 | | * all fields of info will be set to 0/nullptr, but the function will return |
15302 | | * true. |
15303 | | * |
15304 | | * This method is only implemented by a subset of drivers. The base |
15305 | | * implementation just returns false and empty info. |
15306 | | * |
15307 | | * The values returned in psBlockInfo->papszInfo are driver dependent. |
15308 | | * |
15309 | | * For multi-byte data types, drivers should return a "ENDIANNESS" key whose |
15310 | | * value is "LITTLE" or "BIG". |
15311 | | * |
15312 | | * For HDF5 and netCDF 4, the potential keys are "COMPRESSION" (possible values |
15313 | | * "DEFLATE" or "SZIP") and "FILTER" (if several filters, names are |
15314 | | * comma-separated) |
15315 | | * |
15316 | | * For ZARR, the potential keys are "COMPRESSOR" (value is the JSON encoded |
15317 | | * content from the array definition), "FILTERS" (for Zarr V2, value is JSON |
15318 | | * encoded content) and "TRANSPOSE_ORDER" (value is a string like |
15319 | | * "[idx0,...,idxN]" with the permutation). |
15320 | | * |
15321 | | * For VRT, the potential keys are the ones of the underlying source(s). Note |
15322 | | * that GetRawBlockInfo() on VRT only works when the VRT declares a block size, |
15323 | | * that for each queried VRT block, there is one and only one source that |
15324 | | * is used to fill the VRT block and that the block size of this source is |
15325 | | * exactly the one of the VRT block. |
15326 | | * |
15327 | | * This is the same as C++ method GDALMDArray::GetRawBlockInfo(). |
15328 | | * |
15329 | | * @param hArray handle to array. |
15330 | | * @param panBlockCoordinates array of GetDimensionCount() values with the block |
15331 | | * coordinates. |
15332 | | * @param[out] psBlockInfo structure to fill with block information. |
15333 | | * Must be allocated with GDALMDArrayRawBlockInfoCreate(), |
15334 | | * and freed with GDALMDArrayRawBlockInfoRelease(). |
15335 | | * @return true in case of success, or false if an error occurs. |
15336 | | * @since 3.12 |
15337 | | */ |
15338 | | bool GDALMDArrayGetRawBlockInfo(GDALMDArrayH hArray, |
15339 | | const uint64_t *panBlockCoordinates, |
15340 | | GDALMDArrayRawBlockInfo *psBlockInfo) |
15341 | 0 | { |
15342 | 0 | VALIDATE_POINTER1(hArray, __func__, false); |
15343 | 0 | VALIDATE_POINTER1(panBlockCoordinates, __func__, false); |
15344 | 0 | VALIDATE_POINTER1(psBlockInfo, __func__, false); |
15345 | 0 | return hArray->m_poImpl->GetRawBlockInfo(panBlockCoordinates, *psBlockInfo); |
15346 | 0 | } |
15347 | | |
15348 | | /************************************************************************/ |
15349 | | /* GDALMDArrayRawBlockInfoCreate() */ |
15350 | | /************************************************************************/ |
15351 | | |
15352 | | /** Allocate a new instance of GDALMDArrayRawBlockInfo. |
15353 | | * |
15354 | | * Returned pointer must be freed with GDALMDArrayRawBlockInfoRelease(). |
15355 | | * |
15356 | | * @since 3.12 |
15357 | | */ |
15358 | | GDALMDArrayRawBlockInfo *GDALMDArrayRawBlockInfoCreate(void) |
15359 | 0 | { |
15360 | 0 | return new GDALMDArrayRawBlockInfo(); |
15361 | 0 | } |
15362 | | |
15363 | | /************************************************************************/ |
15364 | | /* GDALMDArrayRawBlockInfoRelease() */ |
15365 | | /************************************************************************/ |
15366 | | |
15367 | | /** Free an instance of GDALMDArrayRawBlockInfo. |
15368 | | * |
15369 | | * @since 3.12 |
15370 | | */ |
15371 | | void GDALMDArrayRawBlockInfoRelease(GDALMDArrayRawBlockInfo *psBlockInfo) |
15372 | 0 | { |
15373 | 0 | delete psBlockInfo; |
15374 | 0 | } |