Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: Virtual GDAL Datasets
4 : * Purpose: Declaration of virtual gdal dataset classes.
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2001, Frank Warmerdam <warmerdam@pobox.com>
9 : * Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #ifndef VIRTUALDATASET_H_INCLUDED
15 : #define VIRTUALDATASET_H_INCLUDED
16 :
17 : #ifndef DOXYGEN_SKIP
18 :
19 : #include "cpl_hash_set.h"
20 : #include "cpl_minixml.h"
21 : #include "gdal_driver.h"
22 : #include "gdal_multidim.h"
23 : #include "gdal_pam.h"
24 : #include "gdal_vrt.h"
25 : #include "gdal_rat.h"
26 :
27 : #include <atomic>
28 : #include <deque>
29 : #include <functional>
30 : #include <map>
31 : #include <memory>
32 : #include <mutex>
33 : #include <vector>
34 :
35 : CPLErr GDALRegisterDefaultPixelFunc();
36 : void GDALVRTRegisterDefaultProcessedDatasetFuncs();
37 : CPLString CPL_DLL VRTSerializeNoData(double dfVal, GDALDataType eDataType,
38 : int nPrecision);
39 :
40 : #if 0
41 : int VRTWarpedOverviewTransform( void *pTransformArg, int bDstToSrc,
42 : int nPointCount,
43 : double *padfX, double *padfY, double *padfZ,
44 : int *panSuccess );
45 : void* VRTDeserializeWarpedOverviewTransformer( CPLXMLNode *psTree );
46 : #endif
47 :
48 : /************************************************************************/
49 : /* VRTOverviewInfo() */
50 : /************************************************************************/
51 : class VRTOverviewInfo
52 : {
53 : CPL_DISALLOW_COPY_ASSIGN(VRTOverviewInfo)
54 :
55 : public:
56 : CPLString osFilename{};
57 : int nBand = 0;
58 : GDALRasterBand *poBand = nullptr;
59 : int bTriedToOpen = FALSE;
60 :
61 16 : VRTOverviewInfo() = default;
62 :
63 0 : VRTOverviewInfo(VRTOverviewInfo &&oOther) noexcept
64 0 : : osFilename(std::move(oOther.osFilename)), nBand(oOther.nBand),
65 0 : poBand(oOther.poBand), bTriedToOpen(oOther.bTriedToOpen)
66 : {
67 0 : oOther.poBand = nullptr;
68 0 : }
69 :
70 16 : ~VRTOverviewInfo()
71 16 : {
72 16 : CloseDataset();
73 16 : }
74 :
75 32 : bool CloseDataset()
76 : {
77 32 : if (poBand == nullptr)
78 23 : return false;
79 :
80 9 : GDALDataset *poDS = poBand->GetDataset();
81 : // Nullify now, to prevent recursion in some cases !
82 9 : poBand = nullptr;
83 9 : if (poDS->GetShared())
84 9 : GDALClose(/* (GDALDatasetH) */ poDS);
85 : else
86 0 : poDS->Dereference();
87 :
88 9 : return true;
89 : }
90 : };
91 :
92 : /************************************************************************/
93 : /* VRTMapSharedResources */
94 : /************************************************************************/
95 :
96 : /** Map of shared datasets */
97 : class CPL_DLL VRTMapSharedResources
98 : {
99 : public:
100 6953 : VRTMapSharedResources() = default;
101 :
102 : /** Return a dataset from its key */
103 : GDALDataset *Get(const std::string &osKey) const;
104 :
105 : /** Inserts a dataset. It must be kept alive while this
106 : * VRTMapSharedResources is alive.
107 : */
108 : void Insert(const std::string &osKey, GDALDataset *poDS);
109 :
110 : /** To be called before any attempt at using this instance in a
111 : * multi-threaded context.
112 : */
113 : void InitMutex();
114 :
115 : private:
116 : mutable std::mutex m_oMutex{};
117 : bool m_bUseMutex = false;
118 : std::map<std::string, GDALDataset *> m_oMap{};
119 :
120 : std::unique_ptr<std::lock_guard<std::mutex>> LockGuard() const;
121 :
122 : CPL_DISALLOW_COPY_ASSIGN(VRTMapSharedResources)
123 : };
124 :
125 : /************************************************************************/
126 : /* VRTSource */
127 : /************************************************************************/
128 :
129 : class CPL_DLL VRTSource
130 : {
131 : public:
132 : struct CPL_DLL WorkingState
133 : {
134 : // GByte whose initialization constructor does nothing
135 : #ifdef __GNUC__
136 : #pragma GCC diagnostic push
137 : #pragma GCC diagnostic ignored "-Weffc++"
138 : #endif
139 : struct NoInitByte
140 : {
141 : #ifdef __COVERITY__
142 : GByte value = 0;
143 : #else
144 : GByte value;
145 : #endif
146 :
147 : // cppcheck-suppress uninitMemberVar
148 17654000 : NoInitByte()
149 17654000 : {
150 : // do nothing
151 17654000 : }
152 :
153 27 : inline operator GByte() const
154 : {
155 27 : return value;
156 : }
157 : };
158 : #ifdef __GNUC__
159 : #pragma GCC diagnostic pop
160 : #endif
161 :
162 : std::vector<NoInitByte> m_abyWrkBuffer{};
163 : std::vector<NoInitByte> m_abyWrkBufferMask{};
164 : };
165 :
166 : virtual ~VRTSource();
167 :
168 : virtual CPLErr RasterIO(GDALDataType eVRTBandDataType, int nXOff, int nYOff,
169 : int nXSize, int nYSize, void *pData, int nBufXSize,
170 : int nBufYSize, GDALDataType eBufType,
171 : GSpacing nPixelSpace, GSpacing nLineSpace,
172 : GDALRasterIOExtraArg *psExtraArg,
173 : WorkingState &oWorkingState) = 0;
174 :
175 : virtual double GetMinimum(int nXSize, int nYSize, int *pbSuccess) = 0;
176 : virtual double GetMaximum(int nXSize, int nYSize, int *pbSuccess) = 0;
177 : virtual CPLErr GetHistogram(int nXSize, int nYSize, double dfMin,
178 : double dfMax, int nBuckets,
179 : GUIntBig *panHistogram, int bIncludeOutOfRange,
180 : int bApproxOK, GDALProgressFunc pfnProgress,
181 : void *pProgressData) = 0;
182 :
183 : virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *,
184 : VRTMapSharedResources &) = 0;
185 : virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath) = 0;
186 :
187 : virtual void GetFileList(char ***ppapszFileList, int *pnSize,
188 : int *pnMaxSize, CPLHashSet *hSetFiles);
189 :
190 : /** Returns whether this instance can be cast to a VRTSimpleSource
191 : * (and its subclasses).
192 : */
193 82 : virtual bool IsSimpleSource() const
194 : {
195 82 : return false;
196 : }
197 :
198 611 : const std::string &GetName() const
199 : {
200 611 : return m_osName;
201 : }
202 :
203 1067 : void SetName(const std::string &s)
204 : {
205 1067 : m_osName = s;
206 1067 : }
207 :
208 : /** Returns a string with the VRTSource class type.
209 : * This method must be implemented in all subclasses
210 : */
211 : virtual const char *GetType() const = 0;
212 :
213 34 : virtual CPLErr FlushCache(bool /*bAtClosing*/)
214 : {
215 34 : return CE_None;
216 : }
217 :
218 : protected:
219 : std::string m_osName{};
220 : };
221 :
222 : typedef VRTSource *(*VRTSourceParser)(const CPLXMLNode *, const char *,
223 : VRTMapSharedResources &oMapSharedSources);
224 :
225 : VRTSource *VRTParseCoreSources(const CPLXMLNode *psTree, const char *,
226 : VRTMapSharedResources &oMapSharedSources);
227 : VRTSource *VRTParseFilterSources(const CPLXMLNode *psTree, const char *,
228 : VRTMapSharedResources &oMapSharedSources);
229 : VRTSource *VRTParseArraySource(const CPLXMLNode *psTree, const char *,
230 : VRTMapSharedResources &oMapSharedSources);
231 :
232 : /************************************************************************/
233 : /* VRTDataset */
234 : /************************************************************************/
235 :
236 : class VRTRasterBand;
237 :
238 : template <class T> struct VRTFlushCacheStruct
239 : {
240 : static CPLErr FlushCache(T &obj, bool bAtClosing);
241 : };
242 :
243 : class VRTWarpedDataset;
244 : class VRTPansharpenedDataset;
245 : class VRTProcessedDataset;
246 : class VRTGroup;
247 : class VRTSimpleSource;
248 :
249 : class CPL_DLL VRTDataset CPL_NON_FINAL : public GDALDataset
250 : {
251 : friend class VRTRasterBand;
252 : friend struct VRTFlushCacheStruct<VRTDataset>;
253 : friend struct VRTFlushCacheStruct<VRTWarpedDataset>;
254 : friend struct VRTFlushCacheStruct<VRTPansharpenedDataset>;
255 : friend struct VRTFlushCacheStruct<VRTProcessedDataset>;
256 : friend class VRTSourcedRasterBand;
257 : friend class VRTDerivedRasterBand;
258 : friend class VRTSimpleSource;
259 : friend struct VRTSourcedRasterBandRasterIOJob;
260 : friend VRTDatasetH CPL_STDCALL VRTCreate(int nXSize, int nYSize);
261 :
262 : std::vector<gdal::GCP> m_asGCPs{};
263 : std::unique_ptr<OGRSpatialReference, OGRSpatialReferenceReleaser>
264 : m_poGCP_SRS{};
265 :
266 : bool m_bNeedsFlush = false;
267 : bool m_bWritable = true;
268 : bool m_bCanTakeRef = true;
269 :
270 : char *m_pszVRTPath = nullptr;
271 :
272 : std::unique_ptr<VRTRasterBand> m_poMaskBand{};
273 :
274 : mutable int m_nCompatibleForDatasetIO = -1;
275 : bool CheckCompatibleForDatasetIO() const;
276 :
277 : // Virtual (ie not materialized) overviews, created either implicitly
278 : // when it is cheap to do it, or explicitly.
279 : std::vector<GDALDataset *> m_apoOverviews{};
280 : std::vector<GDALDataset *> m_apoOverviewsBak{};
281 : CPLStringList m_aosOverviewList{}; // only temporarily set during Open()
282 : CPLString m_osOverviewResampling{};
283 : std::vector<int> m_anOverviewFactors{};
284 :
285 : char **m_papszXMLVRTMetadata = nullptr;
286 :
287 : VRTMapSharedResources m_oMapSharedSources{};
288 : std::shared_ptr<VRTGroup> m_poRootGroup{};
289 :
290 : // Used by VRTSourcedRasterBand::IRasterIO() in single-threaded situations
291 : VRTSource::WorkingState m_oWorkingState{};
292 :
293 : // Used by VRTSourcedRasterBand::IRasterIO() when using multi-threading
294 : struct QueueWorkingStates
295 : {
296 : std::mutex oMutex{};
297 : std::vector<std::unique_ptr<VRTSource::WorkingState>> oStates{};
298 : };
299 :
300 : QueueWorkingStates m_oQueueWorkingStates{};
301 :
302 : bool m_bMultiThreadedRasterIOLastUsed = false;
303 :
304 : std::unique_ptr<VRTRasterBand> InitBand(const char *pszSubclass, int nBand,
305 : bool bAllowPansharpenedOrProcessed);
306 : static GDALDataset *OpenVRTProtocol(const char *pszSpec);
307 : bool AddVirtualOverview(int nOvFactor, const char *pszResampling);
308 :
309 : bool GetShiftedDataset(int nXOff, int nYOff, int nXSize, int nYSize,
310 : GDALDataset *&poSrcDataset, int &nSrcXOff,
311 : int &nSrcYOff);
312 :
313 : static bool IsDefaultBlockSize(int nBlockSize, int nDimension);
314 :
315 : CPL_DISALLOW_COPY_ASSIGN(VRTDataset)
316 :
317 : protected:
318 : bool m_bBlockSizeSpecified = false;
319 : int m_nBlockXSize = 0;
320 : int m_nBlockYSize = 0;
321 :
322 : std::unique_ptr<OGRSpatialReference, OGRSpatialReferenceReleaser> m_poSRS{};
323 :
324 : int m_bGeoTransformSet = false;
325 : GDALGeoTransform m_gt{};
326 :
327 : int CloseDependentDatasets() override;
328 :
329 : public:
330 : VRTDataset(int nXSize, int nYSize, int nBlockXSize = 0,
331 : int nBlockYSize = 0);
332 : ~VRTDataset() override;
333 :
334 973859 : void SetNeedsFlush()
335 : {
336 973859 : m_bNeedsFlush = true;
337 973859 : }
338 :
339 : CPLErr FlushCache(bool bAtClosing) override;
340 :
341 195 : void SetWritable(int bWritableIn)
342 : {
343 195 : m_bWritable = CPL_TO_BOOL(bWritableIn);
344 195 : }
345 :
346 : CPLErr CreateMaskBand(int nFlags) override;
347 : void SetMaskBand(std::unique_ptr<VRTRasterBand> poMaskBand);
348 :
349 10147 : const OGRSpatialReference *GetSpatialRef() const override
350 : {
351 10147 : return m_poSRS.get();
352 : }
353 :
354 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
355 :
356 : CPLErr GetGeoTransform(GDALGeoTransform &) const override;
357 : CPLErr SetGeoTransform(const GDALGeoTransform &) override;
358 :
359 : CPLErr SetMetadata(CSLConstList papszMetadata,
360 : const char *pszDomain = "") override;
361 : CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
362 : const char *pszDomain = "") override;
363 :
364 : CSLConstList GetMetadata(const char *pszDomain = "") override;
365 : virtual const char *GetMetadataItem(const char *pszName,
366 : const char *pszDomain = "") override;
367 :
368 : int GetGCPCount() override;
369 :
370 63 : const OGRSpatialReference *GetGCPSpatialRef() const override
371 : {
372 63 : return m_poGCP_SRS.get();
373 : }
374 :
375 : const GDAL_GCP *GetGCPs() override;
376 : using GDALDataset::SetGCPs;
377 : CPLErr SetGCPs(int nGCPCount, const GDAL_GCP *pasGCPList,
378 : const OGRSpatialReference *poSRS) override;
379 :
380 : virtual CPLErr AddBand(GDALDataType eType,
381 : char **papszOptions = nullptr) override;
382 :
383 : char **GetFileList() override;
384 :
385 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
386 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
387 : GDALDataType eBufType, int nBandCount,
388 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
389 : GSpacing nLineSpace, GSpacing nBandSpace,
390 : GDALRasterIOExtraArg *psExtraArg) override;
391 :
392 : virtual CPLStringList
393 : GetCompressionFormats(int nXOff, int nYOff, int nXSize, int nYSize,
394 : int nBandCount, const int *panBandList) override;
395 : virtual CPLErr ReadCompressedData(const char *pszFormat, int nXOff,
396 : int nYOff, int nXSize, int nYSize,
397 : int nBandCount, const int *panBandList,
398 : void **ppBuffer, size_t *pnBufferSize,
399 : char **ppszDetailedFormat) override;
400 :
401 : CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
402 : int nBufXSize, int nBufYSize, GDALDataType eDT,
403 : int nBandCount, int *panBandList,
404 : char **papszOptions) override;
405 :
406 : virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath);
407 : virtual CPLErr XMLInit(const CPLXMLNode *, const char *);
408 :
409 : CPLErr IBuildOverviews(const char *, int, const int *, int, const int *,
410 : GDALProgressFunc, void *,
411 : CSLConstList papszOptions) override;
412 :
413 : std::shared_ptr<GDALGroup> GetRootGroup() const override;
414 :
415 116 : std::shared_ptr<VRTGroup> GetRootVRTGroup() const
416 : {
417 116 : return m_poRootGroup;
418 : }
419 :
420 : void ClearStatistics() override;
421 :
422 : /** To be called when a new source is added, to invalidate cached states. */
423 247089 : void SourceAdded()
424 : {
425 247089 : m_nCompatibleForDatasetIO = -1;
426 247089 : }
427 :
428 : /* Used by PDF driver for example */
429 : GDALDataset *GetSingleSimpleSource();
430 : void BuildVirtualOverviews();
431 :
432 : void UnsetPreservedRelativeFilenames();
433 :
434 65889 : bool IsBlockSizeSpecified() const
435 : {
436 65889 : return m_bBlockSizeSpecified;
437 : }
438 :
439 66372 : int GetBlockXSize() const
440 : {
441 66372 : return m_nBlockXSize;
442 : }
443 :
444 66372 : int GetBlockYSize() const
445 : {
446 66372 : return m_nBlockYSize;
447 : }
448 :
449 : static int Identify(GDALOpenInfo *);
450 : static GDALDataset *Open(GDALOpenInfo *);
451 : static std::unique_ptr<VRTDataset>
452 : OpenXML(const char *, const char * = nullptr,
453 : GDALAccess eAccess = GA_ReadOnly);
454 : static GDALDataset *Create(const char *pszName, int nXSize, int nYSize,
455 : int nBands, GDALDataType eType,
456 : char **papszOptions);
457 : static std::unique_ptr<VRTDataset>
458 : CreateVRTDataset(const char *pszName, int nXSize, int nYSize, int nBands,
459 : GDALDataType eType, CSLConstList papszOptions);
460 : static GDALDataset *
461 : CreateMultiDimensional(const char *pszFilename,
462 : CSLConstList papszRootGroupOptions,
463 : CSLConstList papszOptions);
464 : static std::unique_ptr<VRTDataset>
465 : CreateVRTMultiDimensional(const char *pszFilename,
466 : CSLConstList papszRootGroupOptions,
467 : CSLConstList papszOptions);
468 : static CPLErr Delete(const char *pszFilename);
469 :
470 : static int GetNumThreads(GDALDataset *poDS);
471 :
472 : static bool IsRawRasterBandEnabled();
473 : };
474 :
475 : /************************************************************************/
476 : /* VRTWarpedDataset */
477 : /************************************************************************/
478 :
479 : class GDALWarpOperation;
480 : class VRTWarpedRasterBand;
481 :
482 : class CPL_DLL VRTWarpedDataset final : public VRTDataset
483 : {
484 : GDALWarpOperation *m_poWarper;
485 :
486 : bool m_bIsOverview = false;
487 : std::vector<VRTWarpedDataset *> m_apoOverviews{};
488 : int m_nSrcOvrLevel;
489 :
490 : bool GetOverviewSize(GDALDataset *poSrcDS, int iOvr, int iSrcOvr,
491 : int &nOvrXSize, int &nOvrYSize, double &dfSrcRatioX,
492 : double &dfSrcRatioY) const;
493 : int GetOverviewCount() const;
494 : int GetSrcOverviewLevel(int iOvr, bool &bThisLevelOnlyOut) const;
495 : VRTWarpedDataset *CreateImplicitOverview(int iOvr) const;
496 : void CreateImplicitOverviews();
497 :
498 : friend class VRTWarpedRasterBand;
499 :
500 : CPL_DISALLOW_COPY_ASSIGN(VRTWarpedDataset)
501 :
502 : protected:
503 : int CloseDependentDatasets() override;
504 :
505 : public:
506 : VRTWarpedDataset(int nXSize, int nYSize, int nBlockXSize = 0,
507 : int nBlockYSize = 0);
508 : ~VRTWarpedDataset() override;
509 :
510 : CPLErr FlushCache(bool bAtClosing) override;
511 :
512 : CPLErr Initialize(/* GDALWarpOptions */ void *);
513 :
514 : CPLErr IBuildOverviews(const char *, int, const int *, int, const int *,
515 : GDALProgressFunc, void *,
516 : CSLConstList papszOptions) override;
517 :
518 : CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
519 : const char *pszDomain = "") override;
520 :
521 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
522 : CPLErr XMLInit(const CPLXMLNode *, const char *) override;
523 :
524 : virtual CPLErr AddBand(GDALDataType eType,
525 : char **papszOptions = nullptr) override;
526 :
527 : char **GetFileList() override;
528 :
529 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
530 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
531 : GDALDataType eBufType, int nBandCount,
532 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
533 : GSpacing nLineSpace, GSpacing nBandSpace,
534 : GDALRasterIOExtraArg *psExtraArg) override;
535 :
536 : CPLErr ProcessBlock(int iBlockX, int iBlockY);
537 :
538 : void GetBlockSize(int *, int *) const;
539 : };
540 :
541 : /************************************************************************/
542 : /* VRTPansharpenedDataset */
543 : /************************************************************************/
544 :
545 : class GDALPansharpenOperation;
546 :
547 : typedef enum
548 : {
549 : GTAdjust_Union,
550 : GTAdjust_Intersection,
551 : GTAdjust_None,
552 : GTAdjust_NoneWithoutWarning
553 : } GTAdjustment;
554 :
555 : class VRTPansharpenedDataset final : public VRTDataset
556 : {
557 : friend class VRTPansharpenedRasterBand;
558 :
559 : std::unique_ptr<GDALPansharpenOperation> m_poPansharpener{};
560 : VRTPansharpenedDataset *m_poMainDataset;
561 : std::vector<std::unique_ptr<VRTPansharpenedDataset>>
562 : m_apoOverviewDatasets{};
563 : // Map from absolute to relative.
564 : std::map<CPLString, CPLString> m_oMapToRelativeFilenames{};
565 :
566 : int m_bLoadingOtherBands;
567 :
568 : GByte *m_pabyLastBufferBandRasterIO;
569 : int m_nLastBandRasterIOXOff;
570 : int m_nLastBandRasterIOYOff;
571 : int m_nLastBandRasterIOXSize;
572 : int m_nLastBandRasterIOYSize;
573 : GDALDataType m_eLastBandRasterIODataType;
574 :
575 : GTAdjustment m_eGTAdjustment;
576 : int m_bNoDataDisabled;
577 :
578 : std::vector<std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser>>
579 : m_apoDatasetsToReleaseRef{};
580 :
581 : CPL_DISALLOW_COPY_ASSIGN(VRTPansharpenedDataset)
582 :
583 : protected:
584 : int CloseDependentDatasets() override;
585 :
586 : public:
587 : VRTPansharpenedDataset(int nXSize, int nYSize, int nBlockXSize = 0,
588 : int nBlockYSize = 0);
589 : ~VRTPansharpenedDataset() override;
590 :
591 : CPLErr FlushCache(bool bAtClosing) override;
592 :
593 : CPLErr XMLInit(const CPLXMLNode *, const char *) override;
594 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
595 :
596 : CPLErr XMLInit(const CPLXMLNode *psTree, const char *pszVRTPath,
597 : GDALRasterBandH hPanchroBandIn, int nInputSpectralBandsIn,
598 : GDALRasterBandH *pahInputSpectralBandsIn);
599 :
600 : virtual CPLErr AddBand(GDALDataType eType,
601 : char **papszOptions = nullptr) override;
602 :
603 : char **GetFileList() override;
604 :
605 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
606 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
607 : GDALDataType eBufType, int nBandCount,
608 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
609 : GSpacing nLineSpace, GSpacing nBandSpace,
610 : GDALRasterIOExtraArg *psExtraArg) override;
611 :
612 : void GetBlockSize(int *, int *) const;
613 :
614 : GDALPansharpenOperation *GetPansharpener()
615 : {
616 : return m_poPansharpener.get();
617 : }
618 : };
619 :
620 : /************************************************************************/
621 : /* VRTPansharpenedDataset */
622 : /************************************************************************/
623 :
624 : /** Specialized implementation of VRTDataset that chains several processing
625 : * steps applied on all bands at a time.
626 : *
627 : * @since 3.9
628 : */
629 : class VRTProcessedDataset final : public VRTDataset
630 : {
631 : public:
632 : VRTProcessedDataset(int nXSize, int nYSize);
633 : ~VRTProcessedDataset() override;
634 :
635 : CPLErr FlushCache(bool bAtClosing) override;
636 :
637 : CPLErr XMLInit(const CPLXMLNode *, const char *) override;
638 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
639 :
640 : void GetBlockSize(int *, int *) const;
641 :
642 : // GByte whose initialization constructor does nothing
643 : #ifdef __GNUC__
644 : #pragma GCC diagnostic push
645 : #pragma GCC diagnostic ignored "-Weffc++"
646 : #endif
647 : struct NoInitByte
648 : {
649 : #ifdef __COVERITY__
650 : GByte value = 0;
651 : #else
652 : GByte value;
653 : #endif
654 :
655 : // cppcheck-suppress uninitMemberVar
656 35169300 : NoInitByte()
657 35169300 : {
658 : // do nothing
659 35169300 : }
660 :
661 : inline operator GByte() const
662 : {
663 : return value;
664 : }
665 : };
666 : #ifdef __GNUC__
667 : #pragma GCC diagnostic pop
668 : #endif
669 :
670 : protected:
671 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
672 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
673 : GDALDataType eBufType, int nBandCount,
674 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
675 : GSpacing nLineSpace, GSpacing nBandSpace,
676 : GDALRasterIOExtraArg *psExtraArg) override;
677 :
678 : private:
679 : friend class VRTProcessedRasterBand;
680 :
681 : //! Data for a processing step.
682 : struct Step
683 : {
684 : //! Algorithm name
685 : std::string osAlgorithm{};
686 :
687 : //! Arguments to pass to the processing function.
688 : CPLStringList aosArguments{};
689 :
690 : //! Data type of the input buffer.
691 : GDALDataType eInDT = GDT_Unknown;
692 :
693 : //! Data type of the output buffer.
694 : GDALDataType eOutDT = GDT_Unknown;
695 :
696 : //! Number of input bands.
697 : int nInBands = 0;
698 :
699 : //! Number of output bands.
700 : int nOutBands = 0;
701 :
702 : //! Nodata values (nInBands) of the input bands.
703 : std::vector<double> adfInNoData{};
704 :
705 : //! Nodata values (nOutBands) of the output bands.
706 : std::vector<double> adfOutNoData{};
707 :
708 : //! Working data structure (private data of the implementation of the function)
709 : VRTPDWorkingDataPtr pWorkingData = nullptr;
710 :
711 : // NOTE: if adding a new member, edit the move constructor and
712 : // assignment operators!
713 :
714 89 : Step() = default;
715 : ~Step();
716 : Step(Step &&);
717 : Step &operator=(Step &&);
718 :
719 : private:
720 : Step(const Step &) = delete;
721 : Step &operator=(const Step &) = delete;
722 : void deinit();
723 : };
724 :
725 : //! Directory of the VRT
726 : std::string m_osVRTPath{};
727 :
728 : //! Source of source dataset generated with GDALTranslate
729 : std::unique_ptr<GDALDataset> m_poVRTSrcDS{};
730 :
731 : //! Source dataset
732 : std::unique_ptr<GDALDataset> m_poSrcDS{};
733 :
734 : //! Processing steps.
735 : std::vector<Step> m_aoSteps{};
736 :
737 : //! Backup XML tree passed to XMLInit()
738 : CPLXMLTreeCloser m_oXMLTree{nullptr};
739 :
740 : //! Overview datasets (dynamically generated from the ones of m_poSrcDS)
741 : std::vector<std::unique_ptr<GDALDataset>> m_apoOverviewDatasets{};
742 :
743 : //! Input buffer of a processing step
744 : std::vector<NoInitByte> m_abyInput{};
745 :
746 : //! Output buffer of a processing step
747 : std::vector<NoInitByte> m_abyOutput{};
748 :
749 : //! Provenance of OutputBands.count and OutputBands.dataType
750 : enum class ValueProvenance
751 : {
752 : FROM_VRTRASTERBAND,
753 : FROM_SOURCE,
754 : FROM_LAST_STEP,
755 : USER_PROVIDED,
756 : };
757 :
758 : //! Provenance of OutputBands.count attribute
759 : ValueProvenance m_outputBandCountProvenance = ValueProvenance::FROM_SOURCE;
760 :
761 : //! Value of OutputBands.count attribute if m_outputBandCountProvenance = USER_PROVIDED
762 : int m_outputBandCountValue = 0;
763 :
764 : //! Provenance of OutputBands.dataType attribute
765 : ValueProvenance m_outputBandDataTypeProvenance =
766 : ValueProvenance::FROM_SOURCE;
767 :
768 : //! Value of OutputBands.dataType attribute if m_outputBandDataTypeProvenance = USER_PROVIDED
769 : GDALDataType m_outputBandDataTypeValue = GDT_Unknown;
770 :
771 : //! Number of temporary bytes we need per output pixel.
772 : int m_nWorkingBytesPerPixel = 1;
773 :
774 : //! Value of CPLGetUsablePhysicalRAM() / 10 * 4
775 : GIntBig m_nAllowedRAMUsage = 0;
776 :
777 : CPLErr Init(const CPLXMLNode *, const char *,
778 : const VRTProcessedDataset *poParentDS,
779 : GDALDataset *poParentSrcDS, int iOvrLevel);
780 :
781 : bool ParseStep(const CPLXMLNode *psStep, bool bIsFinalStep,
782 : GDALDataType &eCurrentDT, int &nCurrentBandCount,
783 : std::vector<double> &adfInNoData,
784 : std::vector<double> &adfOutNoData);
785 : bool ProcessRegion(int nXOff, int nYOff, int nBufXSize, int nBufYSize,
786 : GDALProgressFunc pfnProgress, void *pProgressData);
787 : };
788 :
789 : /************************************************************************/
790 : /* VRTRasterBand */
791 : /* */
792 : /* Provides support for all the various kinds of metadata but */
793 : /* no raster access. That is handled by derived classes. */
794 : /************************************************************************/
795 :
796 : constexpr double VRT_DEFAULT_NODATA_VALUE = -10000.0;
797 :
798 142867 : class CPL_DLL VRTRasterBand CPL_NON_FINAL : public GDALRasterBand
799 : {
800 : private:
801 : void ResetNoDataValues();
802 :
803 : protected:
804 : friend class VRTDataset;
805 :
806 : bool m_bIsMaskBand = false;
807 :
808 : bool m_bNoDataValueSet = false;
809 : // If set to true, will not report the existence of nodata.
810 : int m_bHideNoDataValue = false;
811 : double m_dfNoDataValue = VRT_DEFAULT_NODATA_VALUE;
812 :
813 : bool m_bNoDataSetAsInt64 = false;
814 : int64_t m_nNoDataValueInt64 = GDAL_PAM_DEFAULT_NODATA_VALUE_INT64;
815 :
816 : bool m_bNoDataSetAsUInt64 = false;
817 : uint64_t m_nNoDataValueUInt64 = GDAL_PAM_DEFAULT_NODATA_VALUE_UINT64;
818 :
819 : std::unique_ptr<GDALColorTable> m_poColorTable{};
820 :
821 : GDALColorInterp m_eColorInterp = GCI_Undefined;
822 :
823 : std::string m_osUnitType{};
824 : CPLStringList m_aosCategoryNames{};
825 :
826 : double m_dfOffset = 0.0;
827 : double m_dfScale = 1.0;
828 :
829 : CPLXMLTreeCloser m_psSavedHistograms{nullptr};
830 :
831 : void Initialize(int nXSize, int nYSize);
832 :
833 : std::vector<VRTOverviewInfo> m_aoOverviewInfos{};
834 :
835 : std::unique_ptr<VRTRasterBand> m_poMaskBand{};
836 :
837 : std::unique_ptr<GDALRasterAttributeTable> m_poRAT{};
838 :
839 : CPL_DISALLOW_COPY_ASSIGN(VRTRasterBand)
840 :
841 : bool IsNoDataValueInDataTypeRange() const;
842 :
843 : public:
844 : VRTRasterBand();
845 : ~VRTRasterBand() override;
846 :
847 : virtual CPLErr XMLInit(const CPLXMLNode *, const char *,
848 : VRTMapSharedResources &);
849 : virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath,
850 : bool &bHasWarnedAboutRAMUsage,
851 : size_t &nAccRAMUsage);
852 :
853 : CPLErr SetNoDataValue(double) override;
854 : CPLErr SetNoDataValueAsInt64(int64_t nNoData) override;
855 : CPLErr SetNoDataValueAsUInt64(uint64_t nNoData) override;
856 : double GetNoDataValue(int *pbSuccess = nullptr) override;
857 : int64_t GetNoDataValueAsInt64(int *pbSuccess = nullptr) override;
858 : uint64_t GetNoDataValueAsUInt64(int *pbSuccess = nullptr) override;
859 : CPLErr DeleteNoDataValue() override;
860 :
861 : CPLErr SetColorTable(GDALColorTable *) override;
862 : GDALColorTable *GetColorTable() override;
863 :
864 : GDALRasterAttributeTable *GetDefaultRAT() override;
865 : virtual CPLErr
866 : SetDefaultRAT(const GDALRasterAttributeTable *poRAT) override;
867 :
868 : CPLErr SetColorInterpretation(GDALColorInterp) override;
869 : GDALColorInterp GetColorInterpretation() override;
870 :
871 : const char *GetUnitType() override;
872 : CPLErr SetUnitType(const char *) override;
873 :
874 : char **GetCategoryNames() override;
875 : CPLErr SetCategoryNames(char **) override;
876 :
877 : CPLErr SetMetadata(CSLConstList papszMD,
878 : const char *pszDomain = "") override;
879 : CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
880 : const char *pszDomain = "") override;
881 :
882 : double GetOffset(int *pbSuccess = nullptr) override;
883 : CPLErr SetOffset(double) override;
884 : double GetScale(int *pbSuccess = nullptr) override;
885 : CPLErr SetScale(double) override;
886 :
887 : int GetOverviewCount() override;
888 : GDALRasterBand *GetOverview(int) override;
889 :
890 : CPLErr GetHistogram(double dfMin, double dfMax, int nBuckets,
891 : GUIntBig *panHistogram, int bIncludeOutOfRange,
892 : int bApproxOK, GDALProgressFunc,
893 : void *pProgressData) override;
894 :
895 : CPLErr GetDefaultHistogram(double *pdfMin, double *pdfMax, int *pnBuckets,
896 : GUIntBig **ppanHistogram, int bForce,
897 : GDALProgressFunc, void *pProgressData) override;
898 :
899 : virtual CPLErr SetDefaultHistogram(double dfMin, double dfMax, int nBuckets,
900 : GUIntBig *panHistogram) override;
901 :
902 : CPLErr CopyCommonInfoFrom(GDALRasterBand *);
903 :
904 : virtual void GetFileList(char ***ppapszFileList, int *pnSize,
905 : int *pnMaxSize, CPLHashSet *hSetFiles);
906 :
907 : void SetDescription(const char *) override;
908 :
909 : GDALRasterBand *GetMaskBand() override;
910 : int GetMaskFlags() override;
911 :
912 : CPLErr CreateMaskBand(int nFlagsIn) override;
913 :
914 : void SetMaskBand(std::unique_ptr<VRTRasterBand> poMaskBand);
915 :
916 : void SetIsMaskBand();
917 :
918 : bool IsMaskBand() const override;
919 :
920 : CPLErr UnsetNoDataValue();
921 :
922 : virtual int CloseDependentDatasets();
923 :
924 156 : virtual bool IsSourcedRasterBand()
925 : {
926 156 : return false;
927 : }
928 :
929 7 : virtual bool IsPansharpenRasterBand()
930 : {
931 7 : return false;
932 : }
933 : };
934 :
935 : /************************************************************************/
936 : /* VRTSourcedRasterBand */
937 : /************************************************************************/
938 :
939 : class VRTSimpleSource;
940 :
941 : class CPL_DLL VRTSourcedRasterBand CPL_NON_FINAL : public VRTRasterBand
942 : {
943 : private:
944 : CPLString m_osLastLocationInfo{};
945 : CPLStringList m_aosSourceList{};
946 : int m_nSkipBufferInitialization = -1;
947 :
948 : bool CanUseSourcesMinMaxImplementations();
949 :
950 : bool IsMosaicOfNonOverlappingSimpleSourcesOfFullRasterNoResAndTypeChange(
951 : bool bAllowMaxValAdjustment) const;
952 :
953 : CPL_DISALLOW_COPY_ASSIGN(VRTSourcedRasterBand)
954 :
955 : protected:
956 : bool SkipBufferInitialization();
957 :
958 : public:
959 : std::vector<std::unique_ptr<VRTSource>> m_papoSources{};
960 :
961 : VRTSourcedRasterBand(GDALDataset *poDS, int nBand);
962 : VRTSourcedRasterBand(GDALDataType eType, int nXSize, int nYSize);
963 : VRTSourcedRasterBand(GDALDataset *poDS, int nBand, GDALDataType eType,
964 : int nXSize, int nYSize);
965 : VRTSourcedRasterBand(GDALDataset *poDS, int nBand, GDALDataType eType,
966 : int nXSize, int nYSize, int nBlockXSizeIn,
967 : int nBlockYSizeIn);
968 : ~VRTSourcedRasterBand() override;
969 :
970 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
971 : GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace,
972 : GDALRasterIOExtraArg *psExtraArg) override;
973 :
974 : virtual int IGetDataCoverageStatus(int nXOff, int nYOff, int nXSize,
975 : int nYSize, int nMaskFlagStop,
976 : double *pdfDataPct) override;
977 :
978 : char **GetMetadataDomainList() override;
979 : virtual const char *GetMetadataItem(const char *pszName,
980 : const char *pszDomain = "") override;
981 : CSLConstList GetMetadata(const char *pszDomain = "") override;
982 : CPLErr SetMetadata(CSLConstList papszMetadata,
983 : const char *pszDomain = "") override;
984 : CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
985 : const char *pszDomain = "") override;
986 :
987 : virtual CPLErr XMLInit(const CPLXMLNode *, const char *,
988 : VRTMapSharedResources &) override;
989 : virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath,
990 : bool &bHasWarnedAboutRAMUsage,
991 : size_t &nAccRAMUsage) override;
992 :
993 : double GetMinimum(int *pbSuccess = nullptr) override;
994 : double GetMaximum(int *pbSuccess = nullptr) override;
995 : virtual CPLErr ComputeRasterMinMax(int bApproxOK,
996 : double *adfMinMax) override;
997 : virtual CPLErr ComputeStatistics(int bApproxOK, double *pdfMin,
998 : double *pdfMax, double *pdfMean,
999 : double *pdfStdDev,
1000 : GDALProgressFunc pfnProgress,
1001 : void *pProgressData) override;
1002 : CPLErr GetHistogram(double dfMin, double dfMax, int nBuckets,
1003 : GUIntBig *panHistogram, int bIncludeOutOfRange,
1004 : int bApproxOK, GDALProgressFunc pfnProgress,
1005 : void *pProgressData) override;
1006 :
1007 : CPLErr AddSource(std::unique_ptr<VRTSource>);
1008 :
1009 : CPLErr AddSource(VRTSource *);
1010 :
1011 : CPLErr AddSimpleSource(const char *pszFilename, int nBand,
1012 : double dfSrcXOff = -1, double dfSrcYOff = -1,
1013 : double dfSrcXSize = -1, double dfSrcYSize = -1,
1014 : double dfDstXOff = -1, double dfDstYOff = -1,
1015 : double dfDstXSize = -1, double dfDstYSize = -1,
1016 : const char *pszResampling = "near",
1017 : double dfNoDataValue = VRT_NODATA_UNSET);
1018 :
1019 : CPLErr AddSimpleSource(GDALRasterBand *poSrcBand, double dfSrcXOff = -1,
1020 : double dfSrcYOff = -1, double dfSrcXSize = -1,
1021 : double dfSrcYSize = -1, double dfDstXOff = -1,
1022 : double dfDstYOff = -1, double dfDstXSize = -1,
1023 : double dfDstYSize = -1,
1024 : const char *pszResampling = "near",
1025 : double dfNoDataValue = VRT_NODATA_UNSET);
1026 :
1027 : CPLErr AddComplexSource(const char *pszFilename, int nBand,
1028 : double dfSrcXOff = -1, double dfSrcYOff = -1,
1029 : double dfSrcXSize = -1, double dfSrcYSize = -1,
1030 : double dfDstXOff = -1, double dfDstYOff = -1,
1031 : double dfDstXSize = -1, double dfDstYSize = -1,
1032 : double dfScaleOff = 0.0, double dfScaleRatio = 1.0,
1033 : double dfNoDataValue = VRT_NODATA_UNSET,
1034 : int nColorTableComponent = 0);
1035 :
1036 : CPLErr AddComplexSource(GDALRasterBand *poSrcBand, double dfSrcXOff = -1,
1037 : double dfSrcYOff = -1, double dfSrcXSize = -1,
1038 : double dfSrcYSize = -1, double dfDstXOff = -1,
1039 : double dfDstYOff = -1, double dfDstXSize = -1,
1040 : double dfDstYSize = -1, double dfScaleOff = 0.0,
1041 : double dfScaleRatio = 1.0,
1042 : double dfNoDataValue = VRT_NODATA_UNSET,
1043 : int nColorTableComponent = 0);
1044 :
1045 : CPLErr AddMaskBandSource(GDALRasterBand *poSrcBand, double dfSrcXOff = -1,
1046 : double dfSrcYOff = -1, double dfSrcXSize = -1,
1047 : double dfSrcYSize = -1, double dfDstXOff = -1,
1048 : double dfDstYOff = -1, double dfDstXSize = -1,
1049 : double dfDstYSize = -1);
1050 :
1051 : CPLErr AddFuncSource(VRTImageReadFunc pfnReadFunc, void *hCBData,
1052 : double dfNoDataValue = VRT_NODATA_UNSET);
1053 :
1054 : void ConfigureSource(VRTSimpleSource *poSimpleSource,
1055 : GDALRasterBand *poSrcBand, int bAddAsMaskBand,
1056 : double dfSrcXOff, double dfSrcYOff, double dfSrcXSize,
1057 : double dfSrcYSize, double dfDstXOff, double dfDstYOff,
1058 : double dfDstXSize, double dfDstYSize);
1059 :
1060 : void RemoveCoveredSources(CSLConstList papszOptions = nullptr);
1061 :
1062 : bool CanIRasterIOBeForwardedToEachSource(
1063 : GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
1064 : int nBufXSize, int nBufYSize, GDALRasterIOExtraArg *psExtraArg) const;
1065 :
1066 : bool CanMultiThreadRasterIO(double dfXOff, double dfYOff, double dfXSize,
1067 : double dfYSize,
1068 : int &nContributingSources) const;
1069 :
1070 : CPLErr IReadBlock(int, int, void *) override;
1071 :
1072 : virtual void GetFileList(char ***ppapszFileList, int *pnSize,
1073 : int *pnMaxSize, CPLHashSet *hSetFiles) override;
1074 :
1075 : int CloseDependentDatasets() override;
1076 :
1077 10173 : bool IsSourcedRasterBand() override
1078 : {
1079 10173 : return true;
1080 : }
1081 :
1082 : CPLErr FlushCache(bool bAtClosing) override;
1083 : };
1084 :
1085 : /************************************************************************/
1086 : /* VRTWarpedRasterBand */
1087 : /************************************************************************/
1088 :
1089 : class CPL_DLL VRTWarpedRasterBand final : public VRTRasterBand
1090 : {
1091 : public:
1092 : VRTWarpedRasterBand(GDALDataset *poDS, int nBand,
1093 : GDALDataType eType = GDT_Unknown);
1094 : ~VRTWarpedRasterBand() override;
1095 :
1096 : virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath,
1097 : bool &bHasWarnedAboutRAMUsage,
1098 : size_t &nAccRAMUsage) override;
1099 :
1100 : CPLErr IReadBlock(int, int, void *) override;
1101 : CPLErr IWriteBlock(int, int, void *) override;
1102 :
1103 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
1104 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
1105 : GDALDataType eBufType, GSpacing nPixelSpace,
1106 : GSpacing nLineSpace,
1107 : GDALRasterIOExtraArg *psExtraArg) override;
1108 :
1109 : int GetOverviewCount() override;
1110 : GDALRasterBand *GetOverview(int) override;
1111 :
1112 : bool
1113 : EmitErrorMessageIfWriteNotSupported(const char *pszCaller) const override;
1114 :
1115 : private:
1116 : int m_nIRasterIOCounter =
1117 : 0; //! Protects against infinite recursion inside IRasterIO()
1118 :
1119 : int GetBestOverviewLevel(int &nXOff, int &nYOff, int &nXSize, int &nYSize,
1120 : int nBufXSize, int nBufYSize,
1121 : GDALRasterIOExtraArg *psExtraArg) const;
1122 : };
1123 :
1124 : /************************************************************************/
1125 : /* VRTPansharpenedRasterBand */
1126 : /************************************************************************/
1127 :
1128 : class VRTPansharpenedRasterBand final : public VRTRasterBand
1129 : {
1130 : int m_nIndexAsPansharpenedBand;
1131 :
1132 : public:
1133 : VRTPansharpenedRasterBand(GDALDataset *poDS, int nBand,
1134 : GDALDataType eDataType = GDT_Unknown);
1135 : ~VRTPansharpenedRasterBand() override;
1136 :
1137 : virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath,
1138 : bool &bHasWarnedAboutRAMUsage,
1139 : size_t &nAccRAMUsage) override;
1140 :
1141 : CPLErr IReadBlock(int, int, void *) override;
1142 :
1143 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
1144 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
1145 : GDALDataType eBufType, GSpacing nPixelSpace,
1146 : GSpacing nLineSpace,
1147 : GDALRasterIOExtraArg *psExtraArg) override;
1148 :
1149 : int GetOverviewCount() override;
1150 : GDALRasterBand *GetOverview(int) override;
1151 :
1152 370 : bool IsPansharpenRasterBand() override
1153 : {
1154 370 : return true;
1155 : }
1156 :
1157 64 : void SetIndexAsPansharpenedBand(int nIdx)
1158 : {
1159 64 : m_nIndexAsPansharpenedBand = nIdx;
1160 64 : }
1161 :
1162 39 : int GetIndexAsPansharpenedBand() const
1163 : {
1164 39 : return m_nIndexAsPansharpenedBand;
1165 : }
1166 : };
1167 :
1168 : /************************************************************************/
1169 : /* VRTProcessedRasterBand */
1170 : /************************************************************************/
1171 :
1172 : class VRTProcessedRasterBand final : public VRTRasterBand
1173 : {
1174 : public:
1175 : VRTProcessedRasterBand(VRTProcessedDataset *poDS, int nBand,
1176 : GDALDataType eDataType = GDT_Unknown);
1177 :
1178 : CPLErr IReadBlock(int, int, void *) override;
1179 :
1180 : int GetOverviewCount() override;
1181 : GDALRasterBand *GetOverview(int) override;
1182 :
1183 : virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath,
1184 : bool &bHasWarnedAboutRAMUsage,
1185 : size_t &nAccRAMUsage) override;
1186 : };
1187 :
1188 : /************************************************************************/
1189 : /* VRTDerivedRasterBand */
1190 : /************************************************************************/
1191 :
1192 : class VRTDerivedRasterBandPrivateData;
1193 :
1194 : class CPL_DLL VRTDerivedRasterBand CPL_NON_FINAL : public VRTSourcedRasterBand
1195 : {
1196 : VRTDerivedRasterBandPrivateData *m_poPrivate;
1197 : bool InitializePython();
1198 : CPLErr GetPixelFunctionArguments(
1199 : const CPLString &, const std::vector<int> &anMapBufferIdxToSourceIdx,
1200 : int nXOff, int nYOff, std::vector<std::pair<CPLString, CPLString>> &);
1201 :
1202 : CPL_DISALLOW_COPY_ASSIGN(VRTDerivedRasterBand)
1203 :
1204 : public:
1205 : CPLString osFuncName{};
1206 : GDALDataType eSourceTransferType;
1207 :
1208 : using PixelFunc =
1209 : std::function<CPLErr(void **, int, void *, int, int, GDALDataType,
1210 : GDALDataType, int, int, CSLConstList)>;
1211 :
1212 : VRTDerivedRasterBand(GDALDataset *poDS, int nBand);
1213 : VRTDerivedRasterBand(GDALDataset *poDS, int nBand, GDALDataType eType,
1214 : int nXSize, int nYSize, int nBlockXSizeIn = 0,
1215 : int nBlockYSizeIn = 0);
1216 : ~VRTDerivedRasterBand() override;
1217 :
1218 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
1219 : GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace,
1220 : GDALRasterIOExtraArg *psExtraArg) override;
1221 :
1222 : virtual int IGetDataCoverageStatus(int nXOff, int nYOff, int nXSize,
1223 : int nYSize, int nMaskFlagStop,
1224 : double *pdfDataPct) override;
1225 :
1226 : static CPLErr AddPixelFunction(const char *pszFuncNameIn,
1227 : GDALDerivedPixelFunc pfnPixelFunc);
1228 : static CPLErr AddPixelFunction(const char *pszFuncNameIn,
1229 : GDALDerivedPixelFuncWithArgs pfnPixelFunc,
1230 : const char *pszMetadata);
1231 :
1232 : static const std::pair<PixelFunc, std::string> *
1233 : GetPixelFunction(const char *pszFuncNameIn);
1234 :
1235 : static std::vector<std::string> GetPixelFunctionNames();
1236 :
1237 : void SetPixelFunctionName(const char *pszFuncNameIn);
1238 : void AddPixelFunctionArgument(const char *pszArg, const char *pszValue);
1239 : void SetSkipNonContributingSources(bool bSkip);
1240 : void SetSourceTransferType(GDALDataType eDataType);
1241 : void SetPixelFunctionLanguage(const char *pszLanguage);
1242 :
1243 : virtual CPLErr XMLInit(const CPLXMLNode *, const char *,
1244 : VRTMapSharedResources &) override;
1245 : virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath,
1246 : bool &bHasWarnedAboutRAMUsage,
1247 : size_t &nAccRAMUsage) override;
1248 :
1249 : double GetMinimum(int *pbSuccess = nullptr) override;
1250 : double GetMaximum(int *pbSuccess = nullptr) override;
1251 : virtual CPLErr ComputeRasterMinMax(int bApproxOK,
1252 : double *adfMinMax) override;
1253 : virtual CPLErr ComputeStatistics(int bApproxOK, double *pdfMin,
1254 : double *pdfMax, double *pdfMean,
1255 : double *pdfStdDev,
1256 : GDALProgressFunc pfnProgress,
1257 : void *pProgressData) override;
1258 : CPLErr GetHistogram(double dfMin, double dfMax, int nBuckets,
1259 : GUIntBig *panHistogram, int bIncludeOutOfRange,
1260 : int bApproxOK, GDALProgressFunc pfnProgress,
1261 : void *pProgressData) override;
1262 :
1263 : static void Cleanup();
1264 : };
1265 :
1266 : #ifndef GDAL_VRT_DISABLE_RAWRASTERBAND
1267 : /************************************************************************/
1268 : /* VRTRawRasterBand */
1269 : /************************************************************************/
1270 :
1271 : class RawRasterBand;
1272 :
1273 : class CPL_DLL VRTRawRasterBand CPL_NON_FINAL : public VRTRasterBand
1274 : {
1275 : RawRasterBand *m_poRawRaster;
1276 :
1277 : char *m_pszSourceFilename;
1278 : int m_bRelativeToVRT;
1279 :
1280 : CPL_DISALLOW_COPY_ASSIGN(VRTRawRasterBand)
1281 :
1282 : public:
1283 : VRTRawRasterBand(GDALDataset *poDS, int nBand,
1284 : GDALDataType eType = GDT_Unknown);
1285 : ~VRTRawRasterBand() override;
1286 :
1287 : virtual CPLErr XMLInit(const CPLXMLNode *, const char *,
1288 : VRTMapSharedResources &) override;
1289 : virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath,
1290 : bool &bHasWarnedAboutRAMUsage,
1291 : size_t &nAccRAMUsage) override;
1292 :
1293 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
1294 : GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace,
1295 : GDALRasterIOExtraArg *psExtraArg) override;
1296 :
1297 : CPLErr IReadBlock(int, int, void *) override;
1298 : CPLErr IWriteBlock(int, int, void *) override;
1299 :
1300 : CPLErr SetRawLink(const char *pszFilename, const char *pszVRTPath,
1301 : int bRelativeToVRT, vsi_l_offset nImageOffset,
1302 : int nPixelOffset, int nLineOffset,
1303 : const char *pszByteOrder);
1304 :
1305 : void ClearRawLink();
1306 :
1307 : CPLVirtualMem *GetVirtualMemAuto(GDALRWFlag eRWFlag, int *pnPixelSpace,
1308 : GIntBig *pnLineSpace,
1309 : char **papszOptions) override;
1310 :
1311 : virtual void GetFileList(char ***ppapszFileList, int *pnSize,
1312 : int *pnMaxSize, CPLHashSet *hSetFiles) override;
1313 : };
1314 : #endif
1315 :
1316 : /************************************************************************/
1317 : /* VRTDriver */
1318 : /************************************************************************/
1319 :
1320 : class VRTDriver final : public GDALDriver
1321 : {
1322 : CPL_DISALLOW_COPY_ASSIGN(VRTDriver)
1323 :
1324 : std::mutex m_oMutex{};
1325 : std::map<std::string, VRTSourceParser> m_oMapSourceParser{};
1326 :
1327 : public:
1328 : VRTDriver();
1329 : ~VRTDriver() override;
1330 :
1331 : char **papszSourceParsers;
1332 :
1333 : char **GetMetadataDomainList() override;
1334 : CSLConstList GetMetadata(const char *pszDomain = "") override;
1335 : CPLErr SetMetadata(CSLConstList papszMetadata,
1336 : const char *pszDomain = "") override;
1337 :
1338 : VRTSource *ParseSource(const CPLXMLNode *psSrc, const char *pszVRTPath,
1339 : VRTMapSharedResources &oMapSharedSources);
1340 : void AddSourceParser(const char *pszElementName, VRTSourceParser pfnParser);
1341 : };
1342 :
1343 : /************************************************************************/
1344 : /* VRTSimpleSource */
1345 : /************************************************************************/
1346 :
1347 247342 : class CPL_DLL VRTSimpleSource CPL_NON_FINAL : public VRTSource
1348 : {
1349 : CPL_DISALLOW_COPY_ASSIGN(VRTSimpleSource)
1350 :
1351 : private:
1352 : // Owned by the VRTDataset
1353 : VRTMapSharedResources *m_poMapSharedSources = nullptr;
1354 :
1355 : mutable GDALRasterBand *m_poRasterBand = nullptr;
1356 :
1357 : // When poRasterBand is a mask band, poMaskBandMainBand is the band
1358 : // from which the mask band is taken.
1359 : mutable GDALRasterBand *m_poMaskBandMainBand = nullptr;
1360 :
1361 : CPLStringList m_aosOpenOptionsOri{}; // as read in the original source XML
1362 : CPLStringList
1363 : m_aosOpenOptions{}; // same as above, but potentially augmented with ROOT_PATH
1364 : bool m_bSrcDSNameFromVRT =
1365 : false; // whereas content in m_osSrcDSName is a <VRTDataset> XML node
1366 :
1367 : void OpenSource() const;
1368 :
1369 : GDALDataset *GetSourceDataset() const;
1370 :
1371 : protected:
1372 : friend class VRTSourcedRasterBand;
1373 : friend class VRTDerivedRasterBand;
1374 : friend class VRTDataset;
1375 : friend class GDALTileIndexDataset;
1376 : friend class GDALTileIndexBand;
1377 :
1378 : int m_nBand = 0;
1379 : bool m_bGetMaskBand = false;
1380 :
1381 : /* Value for uninitialized source or destination window. It is chosen such
1382 : * that SrcToDst() and DstToSrc() are no-ops if both source and destination
1383 : * windows are unset.
1384 : */
1385 : static constexpr double UNINIT_WINDOW = -1.0;
1386 :
1387 : double m_dfSrcXOff = UNINIT_WINDOW;
1388 : double m_dfSrcYOff = UNINIT_WINDOW;
1389 : double m_dfSrcXSize = UNINIT_WINDOW;
1390 : double m_dfSrcYSize = UNINIT_WINDOW;
1391 :
1392 : double m_dfDstXOff = UNINIT_WINDOW;
1393 : double m_dfDstYOff = UNINIT_WINDOW;
1394 : double m_dfDstXSize = UNINIT_WINDOW;
1395 : double m_dfDstYSize = UNINIT_WINDOW;
1396 :
1397 : CPLString m_osResampling{};
1398 :
1399 : int m_nMaxValue = 0;
1400 :
1401 : int m_bRelativeToVRTOri = -1;
1402 : CPLString m_osSourceFileNameOri{};
1403 : int m_nExplicitSharedStatus = -1; // -1 unknown, 0 = unshared, 1 = shared
1404 : CPLString m_osSrcDSName{};
1405 :
1406 : bool m_bDropRefOnSrcBand = true;
1407 :
1408 : int NeedMaxValAdjustment() const;
1409 :
1410 2 : GDALRasterBand *GetRasterBandNoOpen() const
1411 : {
1412 2 : return m_poRasterBand;
1413 : }
1414 :
1415 485 : void SetRasterBand(GDALRasterBand *poBand, bool bDropRef)
1416 : {
1417 485 : m_poRasterBand = poBand;
1418 485 : m_bDropRefOnSrcBand = bDropRef;
1419 485 : }
1420 :
1421 101988 : virtual bool ValidateOpenedBand(GDALRasterBand * /*poBand*/) const
1422 : {
1423 101988 : return true;
1424 : }
1425 :
1426 : /** Returns whether the source window is set */
1427 384099 : bool IsSrcWinSet() const
1428 : {
1429 103102 : return m_dfSrcXOff != UNINIT_WINDOW || m_dfSrcYOff != UNINIT_WINDOW ||
1430 487201 : m_dfSrcXSize != UNINIT_WINDOW || m_dfSrcYSize != UNINIT_WINDOW;
1431 : }
1432 :
1433 : /** Returns whether the destination window is set */
1434 385197 : bool IsDstWinSet() const
1435 : {
1436 103111 : return m_dfDstXOff != UNINIT_WINDOW || m_dfDstYOff != UNINIT_WINDOW ||
1437 488308 : m_dfDstXSize != UNINIT_WINDOW || m_dfDstYSize != UNINIT_WINDOW;
1438 : }
1439 :
1440 : void AddSourceFilenameNode(const char *pszVRTPath, CPLXMLNode *psSrc);
1441 :
1442 : public:
1443 : VRTSimpleSource();
1444 : VRTSimpleSource(const VRTSimpleSource *poSrcSource, double dfXDstRatio,
1445 : double dfYDstRatio);
1446 : ~VRTSimpleSource() override;
1447 :
1448 : virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *,
1449 : VRTMapSharedResources &) override;
1450 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
1451 :
1452 : CPLErr ParseSrcRectAndDstRect(const CPLXMLNode *psSrc);
1453 :
1454 : void SetSrcBand(const char *pszFilename, int nBand);
1455 : void SetSrcBand(GDALRasterBand *);
1456 : void SetSrcMaskBand(GDALRasterBand *);
1457 : void SetSrcWindow(double, double, double, double);
1458 : void SetDstWindow(double, double, double, double);
1459 : void GetDstWindow(double &, double &, double &, double &) const;
1460 : bool DstWindowIntersects(double dfXOff, double dfYOff, double dfXSize,
1461 : double dfYSize) const;
1462 :
1463 6 : const std::string &GetSourceDatasetName() const
1464 : {
1465 6 : return m_osSrcDSName;
1466 : }
1467 :
1468 : // Must be called after SetSrcBand()
1469 : void SetSourceDatasetName(const char *pszFilename, bool bRelativeToVRT);
1470 :
1471 8423 : const CPLString &GetResampling() const
1472 : {
1473 8423 : return m_osResampling;
1474 : }
1475 :
1476 : void SetResampling(const char *pszResampling);
1477 :
1478 : int GetSrcDstWindow(double, double, double, double, int, int,
1479 : double *pdfReqXOff, double *pdfReqYOff,
1480 : double *pdfReqXSize, double *pdfReqYSize, int *, int *,
1481 : int *, int *, int *, int *, int *, int *,
1482 : bool &bErrorOut);
1483 :
1484 : int GetSrcDstWindow(double, double, double, double, int, int,
1485 : GDALRIOResampleAlg eResampleAlg, double *pdfReqXOff,
1486 : double *pdfReqYOff, double *pdfReqXSize,
1487 : double *pdfReqYSize, int *, int *, int *, int *, int *,
1488 : int *, int *, int *, bool &bErrorOut);
1489 :
1490 : virtual CPLErr RasterIO(GDALDataType eVRTBandDataType, int nXOff, int nYOff,
1491 : int nXSize, int nYSize, void *pData, int nBufXSize,
1492 : int nBufYSize, GDALDataType eBufType,
1493 : GSpacing nPixelSpace, GSpacing nLineSpace,
1494 : GDALRasterIOExtraArg *psExtraArgIn,
1495 : WorkingState &oWorkingState) override;
1496 :
1497 : double GetMinimum(int nXSize, int nYSize, int *pbSuccess) override;
1498 : double GetMaximum(int nXSize, int nYSize, int *pbSuccess) override;
1499 : CPLErr GetHistogram(int nXSize, int nYSize, double dfMin, double dfMax,
1500 : int nBuckets, GUIntBig *panHistogram,
1501 : int bIncludeOutOfRange, int bApproxOK,
1502 : GDALProgressFunc pfnProgress,
1503 : void *pProgressData) override;
1504 :
1505 : void DstToSrc(double dfX, double dfY, double &dfXOut, double &dfYOut) const;
1506 : void SrcToDst(double dfX, double dfY, double &dfXOut, double &dfYOut) const;
1507 :
1508 : virtual void GetFileList(char ***ppapszFileList, int *pnSize,
1509 : int *pnMaxSize, CPLHashSet *hSetFiles) override;
1510 :
1511 470417 : bool IsSimpleSource() const override
1512 : {
1513 470417 : return true;
1514 : }
1515 :
1516 : /** Returns the same value as GetType() called on objects that are exactly
1517 : * instances of VRTSimpleSource.
1518 : */
1519 : static const char *GetTypeStatic();
1520 :
1521 : const char *GetType() const override;
1522 :
1523 : CPLErr FlushCache(bool bAtClosing) override;
1524 :
1525 : GDALRasterBand *GetRasterBand() const;
1526 : GDALRasterBand *GetMaskBandMainBand();
1527 : bool IsSameExceptBandNumber(const VRTSimpleSource *poOtherSource) const;
1528 : CPLErr DatasetRasterIO(GDALDataType eVRTBandDataType, int nXOff, int nYOff,
1529 : int nXSize, int nYSize, void *pData, int nBufXSize,
1530 : int nBufYSize, GDALDataType eBufType, int nBandCount,
1531 : const int *panBandMap, GSpacing nPixelSpace,
1532 : GSpacing nLineSpace, GSpacing nBandSpace,
1533 : GDALRasterIOExtraArg *psExtraArg);
1534 :
1535 : void UnsetPreservedRelativeFilenames();
1536 :
1537 47 : void SetMaxValue(int nVal)
1538 : {
1539 47 : m_nMaxValue = nVal;
1540 47 : }
1541 : };
1542 :
1543 : /************************************************************************/
1544 : /* VRTAveragedSource */
1545 : /************************************************************************/
1546 :
1547 : class VRTAveragedSource final : public VRTSimpleSource
1548 : {
1549 : CPL_DISALLOW_COPY_ASSIGN(VRTAveragedSource)
1550 :
1551 : int m_bNoDataSet = false;
1552 : double m_dfNoDataValue = VRT_NODATA_UNSET;
1553 :
1554 : public:
1555 : VRTAveragedSource();
1556 : virtual CPLErr RasterIO(GDALDataType eVRTBandDataType, int nXOff, int nYOff,
1557 : int nXSize, int nYSize, void *pData, int nBufXSize,
1558 : int nBufYSize, GDALDataType eBufType,
1559 : GSpacing nPixelSpace, GSpacing nLineSpace,
1560 : GDALRasterIOExtraArg *psExtraArgIn,
1561 : WorkingState &oWorkingState) override;
1562 :
1563 : double GetMinimum(int nXSize, int nYSize, int *pbSuccess) override;
1564 : double GetMaximum(int nXSize, int nYSize, int *pbSuccess) override;
1565 : CPLErr GetHistogram(int nXSize, int nYSize, double dfMin, double dfMax,
1566 : int nBuckets, GUIntBig *panHistogram,
1567 : int bIncludeOutOfRange, int bApproxOK,
1568 : GDALProgressFunc pfnProgress,
1569 : void *pProgressData) override;
1570 :
1571 : void SetNoDataValue(double dfNoDataValue);
1572 :
1573 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
1574 :
1575 : /** Returns the same value as GetType() called on objects that are exactly
1576 : * instances of VRTAveragedSource.
1577 : */
1578 : static const char *GetTypeStatic();
1579 :
1580 : const char *GetType() const override;
1581 : };
1582 :
1583 : /************************************************************************/
1584 : /* VRTNoDataFromMaskSource */
1585 : /************************************************************************/
1586 :
1587 : class VRTNoDataFromMaskSource final : public VRTSimpleSource
1588 : {
1589 : CPL_DISALLOW_COPY_ASSIGN(VRTNoDataFromMaskSource)
1590 :
1591 : bool m_bNoDataSet = false;
1592 : double m_dfNoDataValue = 0;
1593 : double m_dfMaskValueThreshold = 0;
1594 : bool m_bHasRemappedValue = false;
1595 : double m_dfRemappedValue = 0;
1596 :
1597 : public:
1598 : VRTNoDataFromMaskSource();
1599 : virtual CPLErr RasterIO(GDALDataType eVRTBandDataType, int nXOff, int nYOff,
1600 : int nXSize, int nYSize, void *pData, int nBufXSize,
1601 : int nBufYSize, GDALDataType eBufType,
1602 : GSpacing nPixelSpace, GSpacing nLineSpace,
1603 : GDALRasterIOExtraArg *psExtraArgIn,
1604 : WorkingState &oWorkingState) override;
1605 :
1606 : double GetMinimum(int nXSize, int nYSize, int *pbSuccess) override;
1607 : double GetMaximum(int nXSize, int nYSize, int *pbSuccess) override;
1608 : CPLErr GetHistogram(int nXSize, int nYSize, double dfMin, double dfMax,
1609 : int nBuckets, GUIntBig *panHistogram,
1610 : int bIncludeOutOfRange, int bApproxOK,
1611 : GDALProgressFunc pfnProgress,
1612 : void *pProgressData) override;
1613 :
1614 : void SetParameters(double dfNoDataValue, double dfMaskValueThreshold);
1615 : void SetParameters(double dfNoDataValue, double dfMaskValueThreshold,
1616 : double dfRemappedValue);
1617 :
1618 : virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *,
1619 : VRTMapSharedResources &) override;
1620 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
1621 :
1622 : /** Returns the same value as GetType() called on objects that are exactly
1623 : * instances of VRTNoDataFromMaskSource.
1624 : */
1625 : static const char *GetTypeStatic();
1626 :
1627 : const char *GetType() const override;
1628 : };
1629 :
1630 : /************************************************************************/
1631 : /* VRTComplexSource */
1632 : /************************************************************************/
1633 :
1634 : class CPL_DLL VRTComplexSource CPL_NON_FINAL : public VRTSimpleSource
1635 : {
1636 : CPL_DISALLOW_COPY_ASSIGN(VRTComplexSource)
1637 :
1638 : protected:
1639 : static constexpr int PROCESSING_FLAG_NODATA = 1 << 0;
1640 : static constexpr int PROCESSING_FLAG_USE_MASK_BAND =
1641 : 1 << 1; // Mutually exclusive with NODATA
1642 : static constexpr int PROCESSING_FLAG_SCALING_LINEAR = 1 << 2;
1643 : static constexpr int PROCESSING_FLAG_SCALING_EXPONENTIAL =
1644 : 1 << 3; // Mutually exclusive with SCALING_LINEAR
1645 : static constexpr int PROCESSING_FLAG_COLOR_TABLE_EXPANSION = 1 << 4;
1646 : static constexpr int PROCESSING_FLAG_LUT = 1 << 5;
1647 :
1648 : int m_nProcessingFlags = 0;
1649 :
1650 : // adjusted value should be read with GetAdjustedNoDataValue()
1651 : double m_dfNoDataValue = VRT_NODATA_UNSET;
1652 : std::string
1653 : m_osNoDataValueOri{}; // string value read in XML deserialization
1654 :
1655 : double m_dfScaleOff = 0; // For linear scaling.
1656 : double m_dfScaleRatio = 1; // For linear scaling.
1657 :
1658 : // For non-linear scaling with a power function.
1659 : bool m_bSrcMinMaxDefined = false;
1660 : double m_dfSrcMin = 0;
1661 : double m_dfSrcMax = 0;
1662 : double m_dfDstMin = 0;
1663 : double m_dfDstMax = 0;
1664 : double m_dfExponent = 1;
1665 : bool m_bClip = true; // Only taken into account for non-linear scaling
1666 :
1667 : int m_nColorTableComponent = 0;
1668 :
1669 : std::vector<double> m_adfLUTInputs{};
1670 : std::vector<double> m_adfLUTOutputs{};
1671 :
1672 : double GetAdjustedNoDataValue() const;
1673 :
1674 : template <class WorkingDT>
1675 : CPLErr
1676 : RasterIOInternal(GDALRasterBand *poSourceBand,
1677 : GDALDataType eVRTBandDataType, int nReqXOff, int nReqYOff,
1678 : int nReqXSize, int nReqYSize, void *pData, int nOutXSize,
1679 : int nOutYSize, GDALDataType eBufType, GSpacing nPixelSpace,
1680 : GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg,
1681 : GDALDataType eWrkDataType, WorkingState &oWorkingState);
1682 :
1683 : template <class SourceDT, GDALDataType eSourceType>
1684 : CPLErr RasterIOProcessNoData(GDALRasterBand *poSourceBand,
1685 : GDALDataType eVRTBandDataType, int nReqXOff,
1686 : int nReqYOff, int nReqXSize, int nReqYSize,
1687 : void *pData, int nOutXSize, int nOutYSize,
1688 : GDALDataType eBufType, GSpacing nPixelSpace,
1689 : GSpacing nLineSpace,
1690 : GDALRasterIOExtraArg *psExtraArg,
1691 : WorkingState &oWorkingState);
1692 :
1693 : public:
1694 670 : VRTComplexSource() = default;
1695 : VRTComplexSource(const VRTComplexSource *poSrcSource, double dfXDstRatio,
1696 : double dfYDstRatio);
1697 :
1698 : virtual CPLErr RasterIO(GDALDataType eVRTBandDataType, int nXOff, int nYOff,
1699 : int nXSize, int nYSize, void *pData, int nBufXSize,
1700 : int nBufYSize, GDALDataType eBufType,
1701 : GSpacing nPixelSpace, GSpacing nLineSpace,
1702 : GDALRasterIOExtraArg *psExtraArgIn,
1703 : WorkingState &oWorkingState) override;
1704 :
1705 : double GetMinimum(int nXSize, int nYSize, int *pbSuccess) override;
1706 : double GetMaximum(int nXSize, int nYSize, int *pbSuccess) override;
1707 : CPLErr GetHistogram(int nXSize, int nYSize, double dfMin, double dfMax,
1708 : int nBuckets, GUIntBig *panHistogram,
1709 : int bIncludeOutOfRange, int bApproxOK,
1710 : GDALProgressFunc pfnProgress,
1711 : void *pProgressData) override;
1712 :
1713 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
1714 : virtual CPLErr XMLInit(const CPLXMLNode *, const char *,
1715 : VRTMapSharedResources &) override;
1716 :
1717 : /** Returns the same value as GetType() called on objects that are exactly
1718 : * instances of VRTComplexSource.
1719 : */
1720 : static const char *GetTypeStatic();
1721 :
1722 : const char *GetType() const override;
1723 :
1724 : bool AreValuesUnchanged() const;
1725 :
1726 : double LookupValue(double dfInput);
1727 :
1728 : void SetNoDataValue(double dfNoDataValue);
1729 :
1730 75 : void SetUseMaskBand(bool bUseMaskBand)
1731 : {
1732 75 : if (bUseMaskBand)
1733 75 : m_nProcessingFlags |= PROCESSING_FLAG_USE_MASK_BAND;
1734 : else
1735 0 : m_nProcessingFlags &= ~PROCESSING_FLAG_USE_MASK_BAND;
1736 75 : }
1737 :
1738 : void SetLinearScaling(double dfOffset, double dfScale);
1739 : void SetPowerScaling(double dfExponent, double dfSrcMin, double dfSrcMax,
1740 : double dfDstMin, double dfDstMax, bool bClip = true);
1741 : void SetColorTableComponent(int nComponent);
1742 : };
1743 :
1744 : /************************************************************************/
1745 : /* VRTFilteredSource */
1746 : /************************************************************************/
1747 :
1748 : class VRTFilteredSource CPL_NON_FINAL : public VRTComplexSource
1749 : {
1750 : private:
1751 : int IsTypeSupported(GDALDataType eTestType) const;
1752 :
1753 : CPL_DISALLOW_COPY_ASSIGN(VRTFilteredSource)
1754 :
1755 : protected:
1756 : int m_nSupportedTypesCount;
1757 : GDALDataType m_aeSupportedTypes[20];
1758 :
1759 : int m_nExtraEdgePixels;
1760 :
1761 : public:
1762 : VRTFilteredSource();
1763 : ~VRTFilteredSource() override;
1764 :
1765 : const char *GetType() const override = 0;
1766 :
1767 : void SetExtraEdgePixels(int);
1768 : void SetFilteringDataTypesSupported(int, GDALDataType *);
1769 :
1770 : virtual CPLErr FilterData(int nXSize, int nYSize, GDALDataType eType,
1771 : GByte *pabySrcData, GByte *pabyDstData) = 0;
1772 :
1773 : virtual CPLErr RasterIO(GDALDataType eVRTBandDataType, int nXOff, int nYOff,
1774 : int nXSize, int nYSize, void *pData, int nBufXSize,
1775 : int nBufYSize, GDALDataType eBufType,
1776 : GSpacing nPixelSpace, GSpacing nLineSpace,
1777 : GDALRasterIOExtraArg *psExtraArg,
1778 : WorkingState &oWorkingState) override;
1779 : };
1780 :
1781 : /************************************************************************/
1782 : /* VRTKernelFilteredSource */
1783 : /************************************************************************/
1784 :
1785 : class VRTKernelFilteredSource CPL_NON_FINAL : public VRTFilteredSource
1786 : {
1787 : CPL_DISALLOW_COPY_ASSIGN(VRTKernelFilteredSource)
1788 :
1789 : protected:
1790 : int m_nKernelSize = 0;
1791 : bool m_bSeparable = false;
1792 : // m_nKernelSize elements if m_bSeparable, m_nKernelSize * m_nKernelSize otherwise
1793 : std::vector<double> m_adfKernelCoefs{};
1794 : bool m_bNormalized = false;
1795 : std::string m_function{};
1796 :
1797 : public:
1798 : VRTKernelFilteredSource();
1799 :
1800 : const char *GetType() const override;
1801 :
1802 : virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *,
1803 : VRTMapSharedResources &) override;
1804 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
1805 :
1806 : virtual CPLErr FilterData(int nXSize, int nYSize, GDALDataType eType,
1807 : GByte *pabySrcData, GByte *pabyDstData) override;
1808 :
1809 : CPLErr SetKernel(int nKernelSize, bool bSeparable,
1810 : const std::vector<double> &adfNewCoefs);
1811 : void SetNormalized(bool);
1812 :
1813 10 : void SetFunction(const std::string &s)
1814 : {
1815 10 : m_function = s;
1816 10 : }
1817 : };
1818 :
1819 : /************************************************************************/
1820 : /* VRTAverageFilteredSource */
1821 : /************************************************************************/
1822 :
1823 : class VRTAverageFilteredSource final : public VRTKernelFilteredSource
1824 : {
1825 : CPL_DISALLOW_COPY_ASSIGN(VRTAverageFilteredSource)
1826 :
1827 : public:
1828 : explicit VRTAverageFilteredSource(int nKernelSize);
1829 : ~VRTAverageFilteredSource() override;
1830 :
1831 : const char *GetType() const override;
1832 :
1833 : virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *,
1834 : VRTMapSharedResources &) override;
1835 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
1836 : };
1837 :
1838 : /************************************************************************/
1839 : /* VRTFuncSource */
1840 : /************************************************************************/
1841 : class VRTFuncSource final : public VRTSource
1842 : {
1843 : CPL_DISALLOW_COPY_ASSIGN(VRTFuncSource)
1844 :
1845 : public:
1846 : VRTFuncSource();
1847 : ~VRTFuncSource() override;
1848 :
1849 0 : virtual CPLErr XMLInit(const CPLXMLNode *, const char *,
1850 : VRTMapSharedResources &) override
1851 : {
1852 0 : return CE_Failure;
1853 : }
1854 :
1855 : CPLXMLNode *SerializeToXML(const char *pszVRTPath) override;
1856 :
1857 : virtual CPLErr RasterIO(GDALDataType eVRTBandDataType, int nXOff, int nYOff,
1858 : int nXSize, int nYSize, void *pData, int nBufXSize,
1859 : int nBufYSize, GDALDataType eBufType,
1860 : GSpacing nPixelSpace, GSpacing nLineSpace,
1861 : GDALRasterIOExtraArg *psExtraArg,
1862 : WorkingState &oWorkingState) override;
1863 :
1864 : double GetMinimum(int nXSize, int nYSize, int *pbSuccess) override;
1865 : double GetMaximum(int nXSize, int nYSize, int *pbSuccess) override;
1866 : CPLErr GetHistogram(int nXSize, int nYSize, double dfMin, double dfMax,
1867 : int nBuckets, GUIntBig *panHistogram,
1868 : int bIncludeOutOfRange, int bApproxOK,
1869 : GDALProgressFunc pfnProgress,
1870 : void *pProgressData) override;
1871 :
1872 : const char *GetType() const override;
1873 :
1874 : VRTImageReadFunc pfnReadFunc;
1875 : void *pCBData;
1876 : GDALDataType eType;
1877 :
1878 : float fNoDataValue;
1879 : };
1880 :
1881 : /************************************************************************/
1882 : /* VRTGroup */
1883 : /************************************************************************/
1884 :
1885 : #ifdef TMPEXPORT
1886 : #define TMP_CPL_DLL CPL_DLL
1887 : #else
1888 : #define TMP_CPL_DLL
1889 : #endif
1890 :
1891 : class VRTMDArray;
1892 : class VRTAttribute;
1893 : class VRTDimension;
1894 :
1895 : class VRTGroup final : public GDALGroup
1896 : {
1897 : public:
1898 : struct Ref
1899 : {
1900 : VRTGroup *m_ptr;
1901 :
1902 1009 : explicit Ref(VRTGroup *ptr) : m_ptr(ptr)
1903 : {
1904 1009 : }
1905 :
1906 : Ref(const Ref &) = delete;
1907 : Ref &operator=(const Ref &) = delete;
1908 : };
1909 :
1910 : private:
1911 : std::shared_ptr<Ref> m_poSharedRefRootGroup{};
1912 : std::weak_ptr<Ref> m_poWeakRefRootGroup{};
1913 : std::shared_ptr<Ref> m_poRefSelf{};
1914 :
1915 : std::string m_osFilename{};
1916 : mutable bool m_bDirty = false;
1917 : std::string m_osVRTPath{};
1918 : std::vector<std::string> m_aosGroupNames{};
1919 : std::map<std::string, std::shared_ptr<VRTGroup>> m_oMapGroups{};
1920 : std::vector<std::string> m_aosMDArrayNames{};
1921 : std::map<std::string, std::shared_ptr<VRTMDArray>> m_oMapMDArrays{};
1922 : std::map<std::string, std::shared_ptr<VRTAttribute>> m_oMapAttributes{};
1923 : std::map<std::string, std::shared_ptr<VRTDimension>> m_oMapDimensions{};
1924 :
1925 : std::shared_ptr<VRTGroup>
1926 : OpenGroupInternal(const std::string &osName) const;
1927 : void SetRootGroupRef(const std::weak_ptr<Ref> &rgRef);
1928 : std::weak_ptr<Ref> GetRootGroupRef() const;
1929 :
1930 : protected:
1931 : friend class VRTMDArray;
1932 : friend std::shared_ptr<GDALMDArray>
1933 : VRTDerivedArrayCreate(const char *pszVRTPath, const CPLXMLNode *psTree);
1934 :
1935 : explicit VRTGroup(const char *pszVRTPath);
1936 : VRTGroup(const std::string &osParentName, const std::string &osName);
1937 :
1938 : public:
1939 629 : static std::shared_ptr<VRTGroup> Create(const std::string &osParentName,
1940 : const std::string &osName)
1941 : {
1942 : auto poGroup =
1943 629 : std::shared_ptr<VRTGroup>(new VRTGroup(osParentName, osName));
1944 629 : poGroup->SetSelf(poGroup);
1945 629 : return poGroup;
1946 : }
1947 :
1948 : ~VRTGroup() override;
1949 :
1950 : bool XMLInit(const std::shared_ptr<VRTGroup> &poRoot,
1951 : const std::shared_ptr<VRTGroup> &poThisGroup,
1952 : const CPLXMLNode *psNode, const char *pszVRTPath);
1953 :
1954 : std::vector<std::string>
1955 : GetMDArrayNames(CSLConstList papszOptions) const override;
1956 : std::shared_ptr<GDALMDArray>
1957 : OpenMDArray(const std::string &osName,
1958 : CSLConstList papszOptions = nullptr) const override;
1959 :
1960 : std::vector<std::string>
1961 : GetGroupNames(CSLConstList papszOptions) const override;
1962 :
1963 43 : std::shared_ptr<GDALGroup> OpenGroup(const std::string &osName,
1964 : CSLConstList) const override
1965 : {
1966 43 : return OpenGroupInternal(osName);
1967 : }
1968 :
1969 : std::vector<std::shared_ptr<GDALDimension>>
1970 : GetDimensions(CSLConstList) const override;
1971 :
1972 : std::vector<std::shared_ptr<GDALAttribute>>
1973 : GetAttributes(CSLConstList) const override;
1974 :
1975 2120 : std::shared_ptr<VRTDimension> GetDimension(const std::string &name) const
1976 : {
1977 2120 : auto oIter = m_oMapDimensions.find(name);
1978 4240 : return oIter == m_oMapDimensions.end() ? nullptr : oIter->second;
1979 : }
1980 :
1981 : std::shared_ptr<VRTDimension>
1982 : GetDimensionFromFullName(const std::string &name, bool bEmitError) const;
1983 :
1984 : std::shared_ptr<GDALGroup>
1985 : CreateGroup(const std::string &osName,
1986 : CSLConstList papszOptions = nullptr) override;
1987 :
1988 : std::shared_ptr<VRTGroup>
1989 : CreateVRTGroup(const std::string &osName,
1990 : CSLConstList papszOptions = nullptr);
1991 :
1992 : std::shared_ptr<GDALDimension>
1993 : CreateDimension(const std::string &osName, const std::string &osType,
1994 : const std::string &osDirection, GUInt64 nSize,
1995 : CSLConstList papszOptions = nullptr) override;
1996 :
1997 : std::shared_ptr<GDALAttribute>
1998 : CreateAttribute(const std::string &osName,
1999 : const std::vector<GUInt64> &anDimensions,
2000 : const GDALExtendedDataType &oDataType,
2001 : CSLConstList papszOptions = nullptr) override;
2002 :
2003 : std::shared_ptr<GDALMDArray> CreateMDArray(
2004 : const std::string &osName,
2005 : const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
2006 : const GDALExtendedDataType &oDataType,
2007 : CSLConstList papszOptions = nullptr) override;
2008 :
2009 : std::shared_ptr<VRTMDArray> CreateVRTMDArray(
2010 : const std::string &osName,
2011 : const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
2012 : const GDALExtendedDataType &oDataType,
2013 : CSLConstList papszOptions = nullptr);
2014 :
2015 : void SetIsRootGroup();
2016 :
2017 2010 : const std::shared_ptr<Ref> &GetRef() const
2018 : {
2019 2010 : return m_poRefSelf;
2020 : }
2021 :
2022 : VRTGroup *GetRootGroup() const;
2023 : std::shared_ptr<VRTGroup> GetRootGroupSharedPtr() const;
2024 :
2025 1234 : const std::string &GetVRTPath() const
2026 : {
2027 1234 : return m_osVRTPath;
2028 : }
2029 :
2030 366 : void SetVRTPath(const std::string &osVRTPath)
2031 : {
2032 366 : m_osVRTPath = osVRTPath;
2033 366 : }
2034 :
2035 : void SetDirty();
2036 :
2037 207 : void SetFilename(const std::string &osFilename)
2038 : {
2039 207 : m_osFilename = osFilename;
2040 207 : }
2041 :
2042 1234 : const std::string &GetFilename() const
2043 : {
2044 1234 : return m_osFilename;
2045 : }
2046 :
2047 : bool Serialize() const;
2048 : CPLXMLNode *SerializeToXML(const char *pszVRTPathIn) const;
2049 : void Serialize(CPLXMLNode *psParent, const char *pszVRTPathIn) const;
2050 : };
2051 :
2052 : /************************************************************************/
2053 : /* VRTDimension */
2054 : /************************************************************************/
2055 :
2056 : class VRTDimension final : public GDALDimension
2057 : {
2058 : std::weak_ptr<VRTGroup::Ref> m_poGroupRef;
2059 : std::string m_osIndexingVariableName;
2060 :
2061 : public:
2062 776 : VRTDimension(const std::shared_ptr<VRTGroup::Ref> &poGroupRef,
2063 : const std::string &osParentName, const std::string &osName,
2064 : const std::string &osType, const std::string &osDirection,
2065 : GUInt64 nSize, const std::string &osIndexingVariableName)
2066 776 : : GDALDimension(osParentName, osName, osType, osDirection, nSize),
2067 : m_poGroupRef(poGroupRef),
2068 776 : m_osIndexingVariableName(osIndexingVariableName)
2069 : {
2070 776 : }
2071 :
2072 : VRTGroup *GetGroup() const;
2073 :
2074 : static std::shared_ptr<VRTDimension>
2075 : Create(const std::shared_ptr<VRTGroup> &poThisGroup,
2076 : const std::string &osParentName, const CPLXMLNode *psNode);
2077 :
2078 : std::shared_ptr<GDALMDArray> GetIndexingVariable() const override;
2079 :
2080 : bool SetIndexingVariable(
2081 : std::shared_ptr<GDALMDArray> poIndexingVariable) override;
2082 :
2083 : void Serialize(CPLXMLNode *psParent) const;
2084 : };
2085 :
2086 : /************************************************************************/
2087 : /* VRTAttribute */
2088 : /************************************************************************/
2089 :
2090 : class VRTAttribute final : public GDALAttribute
2091 : {
2092 : GDALExtendedDataType m_dt;
2093 : std::vector<std::string> m_aosList{};
2094 : std::vector<std::shared_ptr<GDALDimension>> m_dims{};
2095 :
2096 : protected:
2097 : bool IRead(const GUInt64 *arrayStartIdx, const size_t *count,
2098 : const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
2099 : const GDALExtendedDataType &bufferDataType,
2100 : void *pDstBuffer) const override;
2101 :
2102 : bool IWrite(const GUInt64 *arrayStartIdx, const size_t *count,
2103 : const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
2104 : const GDALExtendedDataType &bufferDataType,
2105 : const void *pSrcBuffer) override;
2106 :
2107 : public:
2108 221 : VRTAttribute(const std::string &osParentName, const std::string &osName,
2109 : const GDALExtendedDataType &dt,
2110 : std::vector<std::string> &&aosList)
2111 221 : : GDALAbstractMDArray(osParentName, osName),
2112 : GDALAttribute(osParentName, osName), m_dt(dt),
2113 221 : m_aosList(std::move(aosList))
2114 : {
2115 221 : if (m_aosList.size() > 1)
2116 : {
2117 12 : m_dims.emplace_back(std::make_shared<GDALDimension>(
2118 12 : std::string(), "dim", std::string(), std::string(),
2119 18 : m_aosList.size()));
2120 : }
2121 221 : }
2122 :
2123 117 : VRTAttribute(const std::string &osParentName, const std::string &osName,
2124 : GUInt64 nDim, const GDALExtendedDataType &dt)
2125 117 : : GDALAbstractMDArray(osParentName, osName),
2126 117 : GDALAttribute(osParentName, osName), m_dt(dt)
2127 : {
2128 117 : if (nDim != 0)
2129 : {
2130 14 : m_dims.emplace_back(std::make_shared<GDALDimension>(
2131 21 : std::string(), "dim", std::string(), std::string(), nDim));
2132 : }
2133 117 : }
2134 :
2135 : static bool CreationCommonChecks(
2136 : const std::string &osName, const std::vector<GUInt64> &anDimensions,
2137 : const std::map<std::string, std::shared_ptr<VRTAttribute>>
2138 : &oMapAttributes);
2139 :
2140 : static std::shared_ptr<VRTAttribute> Create(const std::string &osParentName,
2141 : const CPLXMLNode *psNode);
2142 :
2143 : const std::vector<std::shared_ptr<GDALDimension>> &
2144 660 : GetDimensions() const override
2145 : {
2146 660 : return m_dims;
2147 : }
2148 :
2149 448 : const GDALExtendedDataType &GetDataType() const override
2150 : {
2151 448 : return m_dt;
2152 : }
2153 :
2154 : void Serialize(CPLXMLNode *psParent) const;
2155 : };
2156 :
2157 : /************************************************************************/
2158 : /* VRTMDArraySource */
2159 : /************************************************************************/
2160 :
2161 1240 : class VRTMDArraySource
2162 : {
2163 : public:
2164 : virtual ~VRTMDArraySource();
2165 :
2166 : enum class RelationShip
2167 : {
2168 : NO_INTERSECTION,
2169 : PARTIAL_INTERSECTION,
2170 : SOURCE_BLOCK_MATCH,
2171 : };
2172 :
2173 : virtual RelationShip GetRelationship(const uint64_t *arrayStartIdx,
2174 : const size_t *count) const = 0;
2175 :
2176 : virtual bool GetRawBlockInfo(const uint64_t *arrayStartIdx,
2177 : const size_t *count,
2178 : GDALMDArrayRawBlockInfo &info) const = 0;
2179 :
2180 : virtual bool Read(const GUInt64 *arrayStartIdx, const size_t *count,
2181 : const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
2182 : const GDALExtendedDataType &bufferDataType,
2183 : void *pDstBuffer) const = 0;
2184 :
2185 : virtual void Serialize(CPLXMLNode *psParent,
2186 : const char *pszVRTPath) const = 0;
2187 : };
2188 :
2189 : /************************************************************************/
2190 : /* VRTMDArray */
2191 : /************************************************************************/
2192 :
2193 : class VRTMDArray final : public GDALMDArray
2194 : {
2195 : protected:
2196 : friend class VRTGroup; // for access to SetSelf()
2197 :
2198 : std::weak_ptr<VRTGroup::Ref> m_poGroupRef;
2199 : std::string m_osVRTPath{};
2200 : std::shared_ptr<VRTGroup> m_poDummyOwningGroup{};
2201 :
2202 : GDALExtendedDataType m_dt;
2203 : std::vector<std::shared_ptr<GDALDimension>> m_dims;
2204 : std::map<std::string, std::shared_ptr<VRTAttribute>> m_oMapAttributes{};
2205 : std::vector<std::unique_ptr<VRTMDArraySource>> m_sources{};
2206 : std::shared_ptr<OGRSpatialReference> m_poSRS{};
2207 : std::vector<GByte> m_abyNoData{};
2208 : std::string m_osUnit{};
2209 : double m_dfScale = 1.0;
2210 : double m_dfOffset = 0.0;
2211 : bool m_bHasScale = false;
2212 : bool m_bHasOffset = false;
2213 : std::string m_osFilename{};
2214 : std::vector<GUInt64> m_anBlockSize{};
2215 :
2216 : bool IRead(const GUInt64 *arrayStartIdx, const size_t *count,
2217 : const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
2218 : const GDALExtendedDataType &bufferDataType,
2219 : void *pDstBuffer) const override;
2220 :
2221 : void SetDirty();
2222 :
2223 : public:
2224 1060 : VRTMDArray(
2225 : const std::shared_ptr<VRTGroup::Ref> &poGroupRef,
2226 : const std::string &osParentName, const std::string &osName,
2227 : const GDALExtendedDataType &dt,
2228 : std::vector<std::shared_ptr<GDALDimension>> &&dims,
2229 : std::map<std::string, std::shared_ptr<VRTAttribute>> &&oMapAttributes,
2230 : std::vector<GUInt64> &&anBlockSize)
2231 1060 : : GDALAbstractMDArray(osParentName, osName),
2232 : GDALMDArray(osParentName, osName), m_poGroupRef(poGroupRef),
2233 1060 : m_osVRTPath(poGroupRef->m_ptr->GetVRTPath()), m_dt(dt),
2234 2120 : m_dims(std::move(dims)), m_oMapAttributes(std::move(oMapAttributes)),
2235 1060 : m_osFilename(poGroupRef->m_ptr->GetFilename()),
2236 3180 : m_anBlockSize(std::move(anBlockSize))
2237 : {
2238 1060 : }
2239 :
2240 174 : VRTMDArray(const std::shared_ptr<VRTGroup::Ref> &poGroupRef,
2241 : const std::string &osParentName, const std::string &osName,
2242 : const std::vector<std::shared_ptr<GDALDimension>> &dims,
2243 : const GDALExtendedDataType &dt,
2244 : const std::vector<GUInt64> &anBlockSize)
2245 174 : : GDALAbstractMDArray(osParentName, osName),
2246 : GDALMDArray(osParentName, osName), m_poGroupRef(poGroupRef),
2247 174 : m_osVRTPath(poGroupRef->m_ptr->GetVRTPath()), m_dt(dt), m_dims(dims),
2248 174 : m_osFilename(poGroupRef->m_ptr->GetFilename()),
2249 522 : m_anBlockSize(anBlockSize)
2250 : {
2251 174 : }
2252 :
2253 15 : bool IsWritable() const override
2254 : {
2255 15 : return false;
2256 : }
2257 :
2258 228 : const std::string &GetFilename() const override
2259 : {
2260 228 : return m_osFilename;
2261 : }
2262 :
2263 : static std::shared_ptr<VRTMDArray> Create(const char *pszVRTPath,
2264 : const CPLXMLNode *psNode);
2265 :
2266 : static std::shared_ptr<VRTMDArray>
2267 : Create(const std::shared_ptr<VRTGroup> &poThisGroup,
2268 : const std::string &osParentName, const CPLXMLNode *psNode);
2269 :
2270 : const std::vector<std::shared_ptr<GDALDimension>> &
2271 5209 : GetDimensions() const override
2272 : {
2273 5209 : return m_dims;
2274 : }
2275 :
2276 : std::vector<std::shared_ptr<GDALAttribute>>
2277 : GetAttributes(CSLConstList) const override;
2278 :
2279 2789 : const GDALExtendedDataType &GetDataType() const override
2280 : {
2281 2789 : return m_dt;
2282 : }
2283 :
2284 : bool SetSpatialRef(const OGRSpatialReference *poSRS) override;
2285 :
2286 253 : std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override
2287 : {
2288 253 : return m_poSRS;
2289 : }
2290 :
2291 : const void *GetRawNoDataValue() const override;
2292 :
2293 : bool SetRawNoDataValue(const void *pRawNoData) override;
2294 :
2295 273 : const std::string &GetUnit() const override
2296 : {
2297 273 : return m_osUnit;
2298 : }
2299 :
2300 13 : bool SetUnit(const std::string &osUnit) override
2301 : {
2302 13 : m_osUnit = osUnit;
2303 13 : return true;
2304 : }
2305 :
2306 255 : double GetOffset(bool *pbHasOffset,
2307 : GDALDataType *peStorageType) const override
2308 : {
2309 255 : if (pbHasOffset)
2310 255 : *pbHasOffset = m_bHasOffset;
2311 255 : if (peStorageType)
2312 157 : *peStorageType = GDT_Unknown;
2313 255 : return m_dfOffset;
2314 : }
2315 :
2316 255 : double GetScale(bool *pbHasScale,
2317 : GDALDataType *peStorageType) const override
2318 : {
2319 255 : if (pbHasScale)
2320 255 : *pbHasScale = m_bHasScale;
2321 255 : if (peStorageType)
2322 157 : *peStorageType = GDT_Unknown;
2323 255 : return m_dfScale;
2324 : }
2325 :
2326 3 : bool SetOffset(double dfOffset,
2327 : GDALDataType /* eStorageType */ = GDT_Unknown) override
2328 : {
2329 3 : SetDirty();
2330 3 : m_bHasOffset = true;
2331 3 : m_dfOffset = dfOffset;
2332 3 : return true;
2333 : }
2334 :
2335 3 : bool SetScale(double dfScale,
2336 : GDALDataType /* eStorageType */ = GDT_Unknown) override
2337 : {
2338 3 : SetDirty();
2339 3 : m_bHasScale = true;
2340 3 : m_dfScale = dfScale;
2341 3 : return true;
2342 : }
2343 :
2344 : void AddSource(std::unique_ptr<VRTMDArraySource> &&poSource);
2345 :
2346 : std::shared_ptr<GDALAttribute>
2347 : CreateAttribute(const std::string &osName,
2348 : const std::vector<GUInt64> &anDimensions,
2349 : const GDALExtendedDataType &oDataType,
2350 : CSLConstList papszOptions = nullptr) override;
2351 :
2352 : bool CopyFrom(GDALDataset *poSrcDS, const GDALMDArray *poSrcArray,
2353 : bool bStrict, GUInt64 &nCurCost, const GUInt64 nTotalCost,
2354 : GDALProgressFunc pfnProgress, void *pProgressData) override;
2355 :
2356 : void Serialize(CPLXMLNode *psParent, const char *pszVRTPathIn) const;
2357 :
2358 : VRTGroup *GetGroup() const;
2359 :
2360 52 : const std::string &GetVRTPath() const
2361 : {
2362 52 : return m_osVRTPath;
2363 : }
2364 :
2365 0 : std::shared_ptr<VRTGroup> GetRootVRTGroup() const
2366 : {
2367 0 : auto poGroup = m_poGroupRef.lock();
2368 0 : if (poGroup)
2369 0 : return poGroup->m_ptr->GetRootGroupSharedPtr();
2370 0 : return nullptr;
2371 : }
2372 :
2373 0 : std::shared_ptr<GDALGroup> GetRootGroup() const override
2374 : {
2375 0 : return GetRootVRTGroup();
2376 : }
2377 :
2378 185 : std::vector<GUInt64> GetBlockSize() const override
2379 : {
2380 185 : return m_anBlockSize;
2381 : }
2382 :
2383 : bool GetRawBlockInfo(const uint64_t *panBlockCoordinates,
2384 : GDALMDArrayRawBlockInfo &info) const override;
2385 : };
2386 :
2387 : /************************************************************************/
2388 : /* VRTMDArraySourceInlinedValues */
2389 : /************************************************************************/
2390 :
2391 : class VRTMDArraySourceInlinedValues final : public VRTMDArraySource
2392 : {
2393 : const VRTMDArray *m_poDstArray = nullptr;
2394 : bool m_bIsConstantValue;
2395 : std::vector<GUInt64> m_anOffset{};
2396 : std::vector<size_t> m_anCount{};
2397 : std::vector<GByte> m_abyValues{};
2398 : std::vector<size_t> m_anInlinedArrayStrideInBytes{};
2399 : GDALExtendedDataType m_dt;
2400 :
2401 : VRTMDArraySourceInlinedValues(const VRTMDArraySourceInlinedValues &) =
2402 : delete;
2403 : VRTMDArraySourceInlinedValues &
2404 : operator=(const VRTMDArraySourceInlinedValues &) = delete;
2405 :
2406 : public:
2407 642 : VRTMDArraySourceInlinedValues(const VRTMDArray *poDstArray,
2408 : bool bIsConstantValue,
2409 : std::vector<GUInt64> &&anOffset,
2410 : std::vector<size_t> &&anCount,
2411 : std::vector<GByte> &&abyValues)
2412 642 : : m_poDstArray(poDstArray), m_bIsConstantValue(bIsConstantValue),
2413 1284 : m_anOffset(std::move(anOffset)), m_anCount(std::move(anCount)),
2414 642 : m_abyValues(std::move(abyValues)), m_dt(poDstArray->GetDataType())
2415 : {
2416 642 : const auto nDims(poDstArray->GetDimensionCount());
2417 642 : m_anInlinedArrayStrideInBytes.resize(nDims);
2418 642 : if (!bIsConstantValue && nDims > 0)
2419 : {
2420 271 : m_anInlinedArrayStrideInBytes.back() =
2421 271 : poDstArray->GetDataType().GetSize();
2422 276 : for (size_t i = nDims - 1; i > 0;)
2423 : {
2424 5 : --i;
2425 5 : m_anInlinedArrayStrideInBytes[i] =
2426 5 : m_anInlinedArrayStrideInBytes[i + 1] * m_anCount[i + 1];
2427 : }
2428 : }
2429 642 : }
2430 :
2431 : ~VRTMDArraySourceInlinedValues() override;
2432 :
2433 : static std::unique_ptr<VRTMDArraySourceInlinedValues>
2434 : Create(const VRTMDArray *poDstArray, const CPLXMLNode *psNode);
2435 :
2436 0 : RelationShip GetRelationship(const uint64_t * /*arrayStartIdx*/,
2437 : const size_t * /*count*/) const override
2438 : {
2439 0 : return RelationShip::PARTIAL_INTERSECTION;
2440 : }
2441 :
2442 0 : bool GetRawBlockInfo(const uint64_t * /*arrayStartIdx*/,
2443 : const size_t * /*count*/,
2444 : GDALMDArrayRawBlockInfo & /*info*/) const override
2445 : {
2446 0 : return false;
2447 : }
2448 :
2449 : bool Read(const GUInt64 *arrayStartIdx, const size_t *count,
2450 : const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
2451 : const GDALExtendedDataType &bufferDataType,
2452 : void *pDstBuffer) const override;
2453 :
2454 : void Serialize(CPLXMLNode *psParent, const char *pszVRTPath) const override;
2455 : };
2456 :
2457 : /************************************************************************/
2458 : /* VRTMDArraySourceRegularlySpaced */
2459 : /************************************************************************/
2460 :
2461 : class VRTMDArraySourceRegularlySpaced final : public VRTMDArraySource
2462 : {
2463 : double m_dfStart;
2464 : double m_dfIncrement;
2465 :
2466 : public:
2467 279 : VRTMDArraySourceRegularlySpaced(double dfStart, double dfIncrement)
2468 279 : : m_dfStart(dfStart), m_dfIncrement(dfIncrement)
2469 : {
2470 279 : }
2471 :
2472 0 : RelationShip GetRelationship(const uint64_t * /*arrayStartIdx*/,
2473 : const size_t * /*count*/) const override
2474 : {
2475 0 : return RelationShip::PARTIAL_INTERSECTION;
2476 : }
2477 :
2478 0 : bool GetRawBlockInfo(const uint64_t * /*arrayStartIdx*/,
2479 : const size_t * /*count*/,
2480 : GDALMDArrayRawBlockInfo & /*info*/) const override
2481 : {
2482 0 : return false;
2483 : }
2484 :
2485 : bool Read(const GUInt64 *arrayStartIdx, const size_t *count,
2486 : const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
2487 : const GDALExtendedDataType &bufferDataType,
2488 : void *pDstBuffer) const override;
2489 :
2490 : void Serialize(CPLXMLNode *psParent, const char *pszVRTPath) const override;
2491 : };
2492 :
2493 : /************************************************************************/
2494 : /* VRTMDArraySourceFromArray */
2495 : /************************************************************************/
2496 :
2497 : struct VRTArrayDatasetWrapper;
2498 :
2499 : class VRTMDArraySourceFromArray final : public VRTMDArraySource
2500 : {
2501 : const VRTMDArray *m_poDstArray = nullptr;
2502 : bool m_bRelativeToVRTSet = false;
2503 : bool m_bRelativeToVRT = false;
2504 : std::string m_osFilename{};
2505 : std::string m_osArray{};
2506 : std::string m_osBand{};
2507 : std::vector<int> m_anTransposedAxis{};
2508 : std::string m_osViewExpr{};
2509 : std::vector<GUInt64> m_anSrcOffset{};
2510 : mutable std::vector<GUInt64> m_anCount{};
2511 : std::vector<GUInt64> m_anStep{};
2512 : std::vector<GUInt64> m_anDstOffset{};
2513 :
2514 : std::pair<std::shared_ptr<VRTArrayDatasetWrapper>,
2515 : std::shared_ptr<GDALMDArray>>
2516 : GetSourceArray() const;
2517 :
2518 : VRTMDArraySourceFromArray(const VRTMDArraySourceFromArray &) = delete;
2519 : VRTMDArraySourceFromArray &
2520 : operator=(const VRTMDArraySourceFromArray &) = delete;
2521 :
2522 : public:
2523 319 : VRTMDArraySourceFromArray(
2524 : const VRTMDArray *poDstArray, bool bRelativeToVRTSet,
2525 : bool bRelativeToVRT, const std::string &osFilename,
2526 : const std::string &osArray, const std::string &osBand,
2527 : std::vector<int> &&anTransposedAxis, const std::string &osViewExpr,
2528 : std::vector<GUInt64> &&anSrcOffset, std::vector<GUInt64> &&anCount,
2529 : std::vector<GUInt64> &&anStep, std::vector<GUInt64> &&anDstOffset)
2530 319 : : m_poDstArray(poDstArray), m_bRelativeToVRTSet(bRelativeToVRTSet),
2531 : m_bRelativeToVRT(bRelativeToVRT), m_osFilename(osFilename),
2532 : m_osArray(osArray), m_osBand(osBand),
2533 319 : m_anTransposedAxis(std::move(anTransposedAxis)),
2534 319 : m_osViewExpr(osViewExpr), m_anSrcOffset(std::move(anSrcOffset)),
2535 638 : m_anCount(std::move(anCount)), m_anStep(std::move(anStep)),
2536 319 : m_anDstOffset(std::move(anDstOffset))
2537 : {
2538 319 : }
2539 :
2540 : ~VRTMDArraySourceFromArray() override;
2541 :
2542 : static std::unique_ptr<VRTMDArraySourceFromArray>
2543 : Create(const VRTMDArray *poDstArray, const CPLXMLNode *psNode);
2544 :
2545 : RelationShip GetRelationship(const uint64_t *arrayStartIdx,
2546 : const size_t *count) const override;
2547 :
2548 : bool GetRawBlockInfo(const uint64_t *arrayStartIdx, const size_t *count,
2549 : GDALMDArrayRawBlockInfo &info) const override;
2550 :
2551 : bool Read(const GUInt64 *arrayStartIdx, const size_t *count,
2552 : const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
2553 : const GDALExtendedDataType &bufferDataType,
2554 : void *pDstBuffer) const override;
2555 :
2556 : void Serialize(CPLXMLNode *psParent, const char *pszVRTPath) const override;
2557 : };
2558 :
2559 : #endif /* #ifndef DOXYGEN_SKIP */
2560 :
2561 : #endif /* ndef VIRTUALDATASET_H_INCLUDED */
|