31 #ifndef VIRTUALDATASET_H_INCLUDED
32 #define VIRTUALDATASET_H_INCLUDED
51 CPLErr GDALRegisterDefaultPixelFunc();
55 int VRTWarpedOverviewTransform(
void *pTransformArg,
int bDstToSrc,
57 double *padfX,
double *padfY,
double *padfZ,
59 void* VRTDeserializeWarpedOverviewTransformer(
CPLXMLNode *psTree );
73 int bTriedToOpen = FALSE;
75 VRTOverviewInfo() =
default;
76 VRTOverviewInfo(VRTOverviewInfo &&oOther) noexcept
77 : osFilename(std::move(oOther.osFilename)), nBand(oOther.nBand),
78 poBand(oOther.poBand), bTriedToOpen(oOther.bTriedToOpen)
80 oOther.poBand =
nullptr;
90 if (poBand ==
nullptr)
109 class CPL_DLL VRTSource
112 virtual ~VRTSource();
115 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
120 virtual double GetMinimum(
int nXSize,
int nYSize,
int *pbSuccess) = 0;
121 virtual double GetMaximum(
int nXSize,
int nYSize,
int *pbSuccess) = 0;
122 virtual CPLErr GetHistogram(
int nXSize,
int nYSize,
double dfMin,
123 double dfMax,
int nBuckets,
124 GUIntBig *panHistogram,
int bIncludeOutOfRange,
125 int bApproxOK, GDALProgressFunc pfnProgress,
126 void *pProgressData) = 0;
129 std::map<CPLString, GDALDataset *> &) = 0;
130 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath) = 0;
132 virtual void GetFileList(
char ***ppapszFileList,
int *pnSize,
135 virtual int IsSimpleSource()
139 virtual CPLErr FlushCache(
bool )
145 typedef VRTSource *(*VRTSourceParser)(
147 std::map<CPLString, GDALDataset *> &oMapSharedSources);
150 VRTParseCoreSources(
CPLXMLNode *psTree,
const char *,
151 std::map<CPLString, GDALDataset *> &oMapSharedSources);
153 VRTParseFilterSources(
CPLXMLNode *psTree,
const char *,
154 std::map<CPLString, GDALDataset *> &oMapSharedSources);
156 VRTParseArraySource(
CPLXMLNode *psTree,
const char *,
157 std::map<CPLString, GDALDataset *> &oMapSharedSources);
165 template <
class T>
struct VRTFlushCacheStruct
167 static CPLErr FlushCache(T &obj,
bool bAtClosing);
170 class VRTWarpedDataset;
171 class VRTPansharpenedDataset;
176 friend class VRTRasterBand;
177 friend struct VRTFlushCacheStruct<VRTDataset>;
178 friend struct VRTFlushCacheStruct<VRTWarpedDataset>;
179 friend struct VRTFlushCacheStruct<VRTPansharpenedDataset>;
180 friend class VRTSourcedRasterBand;
185 int m_bGeoTransformSet =
false;
186 double m_adfGeoTransform[6];
192 bool m_bNeedsFlush =
false;
193 bool m_bWritable =
true;
194 bool m_bCanTakeRef =
true;
196 char *m_pszVRTPath =
nullptr;
198 VRTRasterBand *m_poMaskBand =
nullptr;
200 int m_bCompatibleForDatasetIO = -1;
201 int CheckCompatibleForDatasetIO();
205 std::vector<GDALDataset *> m_apoOverviews{};
206 std::vector<GDALDataset *> m_apoOverviewsBak{};
209 std::vector<int> m_anOverviewFactors{};
211 char **m_papszXMLVRTMetadata =
nullptr;
213 std::map<CPLString, GDALDataset *> m_oMapSharedSources{};
214 std::shared_ptr<VRTGroup> m_poRootGroup{};
216 VRTRasterBand *InitBand(
const char *pszSubclass,
int nBand,
217 bool bAllowPansharpened);
218 static GDALDataset *OpenVRTProtocol(
const char *pszSpec);
219 bool AddVirtualOverview(
int nOvFactor,
const char *pszResampling);
221 bool GetShiftedDataset(
int nXOff,
int nYOff,
int nXSize,
int nYSize,
228 bool m_bBlockSizeSpecified =
false;
229 int m_nBlockXSize = 0;
230 int m_nBlockYSize = 0;
235 VRTDataset(
int nXSize,
int nYSize,
int nBlockXSize = 0,
236 int nBlockYSize = 0);
237 virtual ~VRTDataset();
241 m_bNeedsFlush =
true;
245 void SetWritable(
int bWritableIn)
247 m_bWritable = CPL_TO_BOOL(bWritableIn);
251 void SetMaskBand(VRTRasterBand *poMaskBand);
263 const char *pszDomain =
"")
override;
265 const char *pszDomain =
"")
override;
267 virtual char **
GetMetadata(
const char *pszDomain =
"")
override;
280 char **papszOptions =
nullptr)
override;
285 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
287 int nBandCount,
int *panBandMap,
294 int nBandCount,
const int *panBandList)
override;
296 int nYOff,
int nXSize,
int nYSize,
297 int nBandCount,
const int *panBandList,
298 void **ppBuffer,
size_t *pnBufferSize,
299 char **ppszDetailedFormat)
override;
303 int nBandCount,
int *panBandList,
304 char **papszOptions)
override;
306 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath);
309 virtual CPLErr IBuildOverviews(
const char *,
int,
const int *,
int,
310 const int *, GDALProgressFunc,
void *,
313 std::shared_ptr<GDALGroup>
GetRootGroup()
const override;
319 void BuildVirtualOverviews();
321 void UnsetPreservedRelativeFilenames();
323 bool IsBlockSizeSpecified()
const
325 return m_bBlockSizeSpecified;
327 int GetBlockXSize()
const
329 return m_nBlockXSize;
331 int GetBlockYSize()
const
333 return m_nBlockYSize;
338 static GDALDataset *OpenXML(
const char *,
const char * =
nullptr,
340 static GDALDataset *Create(
const char *pszName,
int nXSize,
int nYSize,
342 char **papszOptions);
344 CreateMultiDimensional(
const char *pszFilename,
347 static CPLErr Delete(
const char *pszFilename);
355 class VRTWarpedRasterBand;
357 class CPL_DLL VRTWarpedDataset final :
public VRTDataset
361 int m_nOverviewCount;
362 VRTWarpedDataset **m_papoOverviews;
365 void CreateImplicitOverviews();
367 friend class VRTWarpedRasterBand;
372 virtual int CloseDependentDatasets()
override;
375 VRTWarpedDataset(
int nXSize,
int nYSize,
int nBlockXSize = 0,
376 int nBlockYSize = 0);
377 virtual ~VRTWarpedDataset();
379 virtual CPLErr FlushCache(
bool bAtClosing)
override;
381 CPLErr Initialize(
void *);
383 virtual CPLErr IBuildOverviews(
const char *,
int,
const int *,
int,
384 const int *, GDALProgressFunc,
void *,
387 virtual CPLErr SetMetadataItem(
const char *pszName,
const char *pszValue,
388 const char *pszDomain =
"")
override;
390 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
394 char **papszOptions =
nullptr)
override;
396 virtual char **GetFileList()
override;
398 CPLErr ProcessBlock(
int iBlockX,
int iBlockY);
400 void GetBlockSize(
int *,
int *)
const;
412 GTAdjust_Intersection,
414 GTAdjust_NoneWithoutWarning
417 class VRTPansharpenedDataset final :
public VRTDataset
419 friend class VRTPansharpenedRasterBand;
422 VRTPansharpenedDataset *m_poMainDataset;
423 std::vector<VRTPansharpenedDataset *> m_apoOverviewDatasets{};
425 std::map<CPLString, CPLString> m_oMapToRelativeFilenames{};
427 int m_bLoadingOtherBands;
429 GByte *m_pabyLastBufferBandRasterIO;
430 int m_nLastBandRasterIOXOff;
431 int m_nLastBandRasterIOYOff;
432 int m_nLastBandRasterIOXSize;
433 int m_nLastBandRasterIOYSize;
436 GTAdjustment m_eGTAdjustment;
437 int m_bNoDataDisabled;
439 std::vector<GDALDataset *> m_apoDatasetsToClose{};
444 virtual int CloseDependentDatasets()
override;
447 VRTPansharpenedDataset(
int nXSize,
int nYSize,
int nBlockXSize = 0,
448 int nBlockYSize = 0);
449 virtual ~VRTPansharpenedDataset();
451 virtual CPLErr FlushCache(
bool bAtClosing)
override;
454 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
461 char **papszOptions =
nullptr)
override;
463 virtual char **GetFileList()
override;
466 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
468 int nBandCount,
int *panBandMap,
473 void GetBlockSize(
int *,
int *)
const;
477 return m_poPansharpener;
488 constexpr
double VRT_DEFAULT_NODATA_VALUE = -10000.0;
493 void ResetNoDataValues();
496 friend class VRTDataset;
498 int m_bIsMaskBand = FALSE;
500 int m_bNoDataValueSet = FALSE;
502 int m_bHideNoDataValue = FALSE;
503 double m_dfNoDataValue = VRT_DEFAULT_NODATA_VALUE;
505 bool m_bNoDataSetAsInt64 =
false;
506 int64_t m_nNoDataValueInt64 = GDAL_PAM_DEFAULT_NODATA_VALUE_INT64;
508 bool m_bNoDataSetAsUInt64 =
false;
509 uint64_t m_nNoDataValueUInt64 = GDAL_PAM_DEFAULT_NODATA_VALUE_UINT64;
511 std::unique_ptr<GDALColorTable> m_poColorTable{};
515 char *m_pszUnitType =
nullptr;
516 char **m_papszCategoryNames =
nullptr;
518 double m_dfOffset = 0.0;
519 double m_dfScale = 1.0;
523 void Initialize(
int nXSize,
int nYSize);
525 std::vector<VRTOverviewInfo> m_aoOverviewInfos{};
527 VRTRasterBand *m_poMaskBand =
nullptr;
529 std::unique_ptr<GDALRasterAttributeTable> m_poRAT{};
533 bool IsNoDataValueInDataTypeRange()
const;
537 virtual ~VRTRasterBand();
540 std::map<CPLString, GDALDataset *> &);
541 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath);
568 const char *pszDomain =
"")
override;
570 const char *pszDomain =
"")
override;
572 virtual double GetOffset(
int *pbSuccess =
nullptr)
override;
574 virtual double GetScale(
int *pbSuccess =
nullptr)
override;
581 GUIntBig *panHistogram,
int bIncludeOutOfRange,
582 int bApproxOK, GDALProgressFunc,
583 void *pProgressData)
override;
586 int *pnBuckets,
GUIntBig **ppanHistogram,
587 int bForce, GDALProgressFunc,
588 void *pProgressData)
override;
595 virtual void GetFileList(
char ***ppapszFileList,
int *pnSize,
605 void SetMaskBand(VRTRasterBand *poMaskBand);
607 void SetIsMaskBand();
611 CPLErr UnsetNoDataValue();
613 virtual int CloseDependentDatasets();
615 virtual int IsSourcedRasterBand()
619 virtual int IsPansharpenRasterBand()
629 class VRTSimpleSource;
631 class CPL_DLL VRTSourcedRasterBand
CPL_NON_FINAL :
public VRTRasterBand
635 char **m_papszSourceList =
nullptr;
636 int m_nSkipBufferInitialization = -1;
638 bool CanUseSourcesMinMaxImplementations();
640 bool IsMosaicOfNonOverlappingSimpleSourcesOfFullRasterNoResAndTypeChange(
641 bool bAllowMaxValAdjustment)
const;
646 bool SkipBufferInitialization();
650 VRTSource **papoSources =
nullptr;
652 VRTSourcedRasterBand(
GDALDataset *poDS,
int nBand);
653 VRTSourcedRasterBand(
GDALDataType eType,
int nXSize,
int nYSize);
655 int nXSize,
int nYSize);
657 int nXSize,
int nYSize,
int nBlockXSizeIn,
659 virtual ~VRTSourcedRasterBand();
666 virtual int IGetDataCoverageStatus(
int nXOff,
int nYOff,
int nXSize,
667 int nYSize,
int nMaskFlagStop,
668 double *pdfDataPct)
override;
670 virtual char **GetMetadataDomainList()
override;
671 virtual const char *GetMetadataItem(
const char *pszName,
672 const char *pszDomain =
"")
override;
673 virtual char **GetMetadata(
const char *pszDomain =
"")
override;
674 virtual CPLErr SetMetadata(
char **papszMetadata,
675 const char *pszDomain =
"")
override;
676 virtual CPLErr SetMetadataItem(
const char *pszName,
const char *pszValue,
677 const char *pszDomain =
"")
override;
680 std::map<CPLString, GDALDataset *> &)
override;
681 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
683 virtual double GetMinimum(
int *pbSuccess =
nullptr)
override;
684 virtual double GetMaximum(
int *pbSuccess =
nullptr)
override;
685 virtual CPLErr ComputeRasterMinMax(
int bApproxOK,
686 double *adfMinMax)
override;
687 virtual CPLErr ComputeStatistics(
int bApproxOK,
double *pdfMin,
688 double *pdfMax,
double *pdfMean,
690 GDALProgressFunc pfnProgress,
691 void *pProgressData)
override;
692 virtual CPLErr GetHistogram(
double dfMin,
double dfMax,
int nBuckets,
693 GUIntBig *panHistogram,
int bIncludeOutOfRange,
694 int bApproxOK, GDALProgressFunc pfnProgress,
695 void *pProgressData)
override;
697 CPLErr AddSource(VRTSource *);
699 CPLErr AddSimpleSource(
const char *pszFilename,
int nBand,
700 double dfSrcXOff = -1,
double dfSrcYOff = -1,
701 double dfSrcXSize = -1,
double dfSrcYSize = -1,
702 double dfDstXOff = -1,
double dfDstYOff = -1,
703 double dfDstXSize = -1,
double dfDstYSize = -1,
704 const char *pszResampling =
"near",
708 double dfSrcYOff = -1,
double dfSrcXSize = -1,
709 double dfSrcYSize = -1,
double dfDstXOff = -1,
710 double dfDstYOff = -1,
double dfDstXSize = -1,
711 double dfDstYSize = -1,
712 const char *pszResampling =
"near",
715 CPLErr AddComplexSource(
const char *pszFilename,
int nBand,
716 double dfSrcXOff = -1,
double dfSrcYOff = -1,
717 double dfSrcXSize = -1,
double dfSrcYSize = -1,
718 double dfDstXOff = -1,
double dfDstYOff = -1,
719 double dfDstXSize = -1,
double dfDstYSize = -1,
720 double dfScaleOff = 0.0,
double dfScaleRatio = 1.0,
722 int nColorTableComponent = 0);
725 double dfSrcYOff = -1,
double dfSrcXSize = -1,
726 double dfSrcYSize = -1,
double dfDstXOff = -1,
727 double dfDstYOff = -1,
double dfDstXSize = -1,
728 double dfDstYSize = -1,
double dfScaleOff = 0.0,
729 double dfScaleRatio = 1.0,
731 int nColorTableComponent = 0);
734 double dfSrcYOff = -1,
double dfSrcXSize = -1,
735 double dfSrcYSize = -1,
double dfDstXOff = -1,
736 double dfDstYOff = -1,
double dfDstXSize = -1,
737 double dfDstYSize = -1);
742 void ConfigureSource(VRTSimpleSource *poSimpleSource,
744 double dfSrcXOff,
double dfSrcYOff,
double dfSrcXSize,
745 double dfSrcYSize,
double dfDstXOff,
double dfDstYOff,
746 double dfDstXSize,
double dfDstYSize);
748 void RemoveCoveredSources(
CSLConstList papszOptions =
nullptr);
750 bool CanIRasterIOBeForwardedToEachSource(
751 GDALRWFlag eRWFlag,
int nXOff,
int nYOff,
int nXSize,
int nYSize,
754 virtual CPLErr IReadBlock(
int,
int,
void *)
override;
756 virtual void GetFileList(
char ***ppapszFileList,
int *pnSize,
757 int *pnMaxSize,
CPLHashSet *hSetFiles)
override;
759 virtual int CloseDependentDatasets()
override;
761 virtual int IsSourcedRasterBand()
override
766 virtual CPLErr FlushCache(
bool bAtClosing)
override;
773 class CPL_DLL VRTWarpedRasterBand final :
public VRTRasterBand
778 virtual ~VRTWarpedRasterBand();
780 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
782 virtual CPLErr IReadBlock(
int,
int,
void *)
override;
783 virtual CPLErr IWriteBlock(
int,
int,
void *)
override;
785 virtual int GetOverviewCount()
override;
792 class VRTPansharpenedRasterBand final :
public VRTRasterBand
794 int m_nIndexAsPansharpenedBand;
797 VRTPansharpenedRasterBand(
GDALDataset *poDS,
int nBand,
799 virtual ~VRTPansharpenedRasterBand();
801 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
803 virtual CPLErr IReadBlock(
int,
int,
void *)
override;
806 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
811 virtual int GetOverviewCount()
override;
814 virtual int IsPansharpenRasterBand()
override
819 void SetIndexAsPansharpenedBand(
int nIdx)
821 m_nIndexAsPansharpenedBand = nIdx;
823 int GetIndexAsPansharpenedBand()
const
825 return m_nIndexAsPansharpenedBand;
833 class VRTDerivedRasterBandPrivateData;
835 class CPL_DLL VRTDerivedRasterBand
CPL_NON_FINAL :
public VRTSourcedRasterBand
837 VRTDerivedRasterBandPrivateData *m_poPrivate;
838 bool InitializePython();
840 GetPixelFunctionArguments(
const CPLString &,
841 std::vector<std::pair<CPLString, CPLString>> &);
853 VRTDerivedRasterBand(
GDALDataset *poDS,
int nBand);
855 int nXSize,
int nYSize);
856 virtual ~VRTDerivedRasterBand();
863 virtual int IGetDataCoverageStatus(
int nXOff,
int nYOff,
int nXSize,
864 int nYSize,
int nMaskFlagStop,
865 double *pdfDataPct)
override;
867 static CPLErr AddPixelFunction(
const char *pszFuncNameIn,
869 static CPLErr AddPixelFunction(
const char *pszFuncNameIn,
871 const char *pszMetadata);
873 static std::pair<PixelFunc, CPLString> *
874 GetPixelFunction(
const char *pszFuncNameIn);
876 void SetPixelFunctionName(
const char *pszFuncNameIn);
878 void SetPixelFunctionLanguage(
const char *pszLanguage);
881 std::map<CPLString, GDALDataset *> &)
override;
882 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
884 virtual double GetMinimum(
int *pbSuccess =
nullptr)
override;
885 virtual double GetMaximum(
int *pbSuccess =
nullptr)
override;
886 virtual CPLErr ComputeRasterMinMax(
int bApproxOK,
887 double *adfMinMax)
override;
888 virtual CPLErr ComputeStatistics(
int bApproxOK,
double *pdfMin,
889 double *pdfMax,
double *pdfMean,
891 GDALProgressFunc pfnProgress,
892 void *pProgressData)
override;
893 virtual CPLErr GetHistogram(
double dfMin,
double dfMax,
int nBuckets,
894 GUIntBig *panHistogram,
int bIncludeOutOfRange,
895 int bApproxOK, GDALProgressFunc pfnProgress,
896 void *pProgressData)
override;
898 static void Cleanup();
907 class CPL_DLL VRTRawRasterBand
CPL_NON_FINAL :
public VRTRasterBand
909 RawRasterBand *m_poRawRaster;
911 char *m_pszSourceFilename;
912 int m_bRelativeToVRT;
919 virtual ~VRTRawRasterBand();
922 std::map<CPLString, GDALDataset *> &)
override;
923 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
930 virtual CPLErr IReadBlock(
int,
int,
void *)
override;
931 virtual CPLErr IWriteBlock(
int,
int,
void *)
override;
933 CPLErr SetRawLink(
const char *pszFilename,
const char *pszVRTPath,
935 int nPixelOffset,
int nLineOffset,
936 const char *pszByteOrder);
942 char **papszOptions)
override;
944 virtual void GetFileList(
char ***ppapszFileList,
int *pnSize,
945 int *pnMaxSize,
CPLHashSet *hSetFiles)
override;
956 std::map<std::string, VRTSourceParser> m_oMapSourceParser{};
960 virtual ~VRTDriver();
962 char **papszSourceParsers;
965 virtual char **
GetMetadata(
const char *pszDomain =
"")
override;
967 const char *pszDomain =
"")
override;
970 ParseSource(
CPLXMLNode *psSrc,
const char *pszVRTPath,
971 std::map<CPLString, GDALDataset *> &oMapSharedSources);
972 void AddSourceParser(
const char *pszElementName, VRTSourceParser pfnParser);
979 class CPL_DLL VRTSimpleSource
CPL_NON_FINAL :
public VRTSource
985 std::map<CPLString, GDALDataset *> *m_poMapSharedSources =
nullptr;
995 void OpenSource()
const;
998 friend class VRTSourcedRasterBand;
999 friend class VRTDataset;
1002 bool m_bGetMaskBand =
false;
1004 double m_dfSrcXOff = 0;
1005 double m_dfSrcYOff = 0;
1006 double m_dfSrcXSize = 0;
1007 double m_dfSrcYSize = 0;
1009 double m_dfDstXOff = 0;
1010 double m_dfDstYOff = 0;
1011 double m_dfDstXSize = 0;
1012 double m_dfDstYSize = 0;
1016 int m_nMaxValue = 0;
1018 int m_bRelativeToVRTOri = -1;
1020 int m_nExplicitSharedStatus = -1;
1023 bool m_bDropRefOnSrcBand =
true;
1025 int NeedMaxValAdjustment()
const;
1029 return m_poRasterBand;
1039 VRTSimpleSource(
const VRTSimpleSource *poSrcSource,
double dfXDstRatio,
1040 double dfYDstRatio);
1041 virtual ~VRTSimpleSource();
1044 std::map<CPLString, GDALDataset *> &)
override;
1045 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
1049 void SetSrcBand(
const char *pszFilename,
int nBand);
1052 void SetSrcWindow(
double,
double,
double,
double);
1053 void SetDstWindow(
double,
double,
double,
double);
1054 void GetDstWindow(
double &,
double &,
double &,
double &);
1055 const std::string &GetSourceDatasetName()
const
1057 return m_osSrcDSName;
1061 return m_osResampling;
1063 void SetResampling(
const char *pszResampling);
1065 int GetSrcDstWindow(
double,
double,
double,
double,
int,
int,
1066 double *pdfReqXOff,
double *pdfReqYOff,
1067 double *pdfReqXSize,
double *pdfReqYSize,
int *,
int *,
1068 int *,
int *,
int *,
int *,
int *,
int *,
1072 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
1077 virtual double GetMinimum(
int nXSize,
int nYSize,
int *pbSuccess)
override;
1078 virtual double GetMaximum(
int nXSize,
int nYSize,
int *pbSuccess)
override;
1079 virtual CPLErr GetHistogram(
int nXSize,
int nYSize,
double dfMin,
1080 double dfMax,
int nBuckets,
1081 GUIntBig *panHistogram,
int bIncludeOutOfRange,
1082 int bApproxOK, GDALProgressFunc pfnProgress,
1083 void *pProgressData)
override;
1085 void DstToSrc(
double dfX,
double dfY,
double &dfXOut,
double &dfYOut)
const;
1086 void SrcToDst(
double dfX,
double dfY,
double &dfXOut,
double &dfYOut)
const;
1088 virtual void GetFileList(
char ***ppapszFileList,
int *pnSize,
1089 int *pnMaxSize,
CPLHashSet *hSetFiles)
override;
1091 virtual int IsSimpleSource()
override
1095 virtual const char *GetType()
1097 return "SimpleSource";
1099 virtual CPLErr FlushCache(
bool bAtClosing)
override;
1103 int IsSameExceptBandNumber(VRTSimpleSource *poOtherSource);
1105 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
1107 int *panBandMap,
GSpacing nPixelSpace,
1111 void UnsetPreservedRelativeFilenames();
1113 void SetMaxValue(
int nVal)
1123 class VRTAveragedSource final :
public VRTSimpleSource
1127 int m_bNoDataSet =
false;
1131 VRTAveragedSource();
1133 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
1138 virtual double GetMinimum(
int nXSize,
int nYSize,
int *pbSuccess)
override;
1139 virtual double GetMaximum(
int nXSize,
int nYSize,
int *pbSuccess)
override;
1140 virtual CPLErr GetHistogram(
int nXSize,
int nYSize,
double dfMin,
1141 double dfMax,
int nBuckets,
1142 GUIntBig *panHistogram,
int bIncludeOutOfRange,
1143 int bApproxOK, GDALProgressFunc pfnProgress,
1144 void *pProgressData)
override;
1146 void SetNoDataValue(
double dfNoDataValue);
1148 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
1149 virtual const char *GetType()
override
1151 return "AveragedSource";
1159 class CPL_DLL VRTComplexSource
CPL_NON_FINAL :
public VRTSimpleSource
1164 static constexpr
int PROCESSING_FLAG_NODATA = 1 << 0;
1165 static constexpr
int PROCESSING_FLAG_USE_MASK_BAND =
1167 static constexpr
int PROCESSING_FLAG_SCALING_LINEAR = 1 << 2;
1168 static constexpr
int PROCESSING_FLAG_SCALING_EXPONENTIAL =
1170 static constexpr
int PROCESSING_FLAG_COLOR_TABLE_EXPANSION = 1 << 4;
1171 static constexpr
int PROCESSING_FLAG_LUT = 1 << 5;
1173 int m_nProcessingFlags = 0;
1177 #pragma GCC diagnostic push
1178 #pragma GCC diagnostic ignored "-Weffc++"
1191 #pragma GCC diagnostic pop
1194 std::vector<NoInitByte> m_abyWrkBuffer{};
1195 std::vector<NoInitByte> m_abyWrkBufferMask{};
1200 m_osNoDataValueOri{};
1202 double m_dfScaleOff = 0;
1203 double m_dfScaleRatio = 1;
1206 bool m_bSrcMinMaxDefined =
false;
1207 double m_dfSrcMin = 0;
1208 double m_dfSrcMax = 0;
1209 double m_dfDstMin = 0;
1210 double m_dfDstMax = 0;
1211 double m_dfExponent = 1;
1213 int m_nColorTableComponent = 0;
1215 std::vector<double> m_adfLUTInputs{};
1216 std::vector<double> m_adfLUTOutputs{};
1218 double GetAdjustedNoDataValue()
const;
1220 template <
class WorkingDT>
1223 GDALDataType eVRTBandDataType,
int nReqXOff,
int nReqYOff,
1224 int nReqXSize,
int nReqYSize,
void *pData,
int nOutXSize,
1229 template <
class SourceDT, GDALDataType eSourceType>
1232 int nReqYOff,
int nReqXSize,
int nReqYSize,
1233 void *pData,
int nOutXSize,
int nOutYSize,
1239 VRTComplexSource() =
default;
1240 VRTComplexSource(
const VRTComplexSource *poSrcSource,
double dfXDstRatio,
1241 double dfYDstRatio);
1244 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
1249 virtual double GetMinimum(
int nXSize,
int nYSize,
int *pbSuccess)
override;
1250 virtual double GetMaximum(
int nXSize,
int nYSize,
int *pbSuccess)
override;
1251 virtual CPLErr GetHistogram(
int nXSize,
int nYSize,
double dfMin,
1252 double dfMax,
int nBuckets,
1253 GUIntBig *panHistogram,
int bIncludeOutOfRange,
1254 int bApproxOK, GDALProgressFunc pfnProgress,
1255 void *pProgressData)
override;
1257 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
1259 std::map<CPLString, GDALDataset *> &)
override;
1260 virtual const char *GetType()
override
1262 return "ComplexSource";
1265 bool AreValuesUnchanged()
const;
1267 double LookupValue(
double dfInput);
1269 void SetNoDataValue(
double dfNoDataValue);
1271 void SetUseMaskBand(
bool bUseMaskBand)
1274 m_nProcessingFlags |= PROCESSING_FLAG_USE_MASK_BAND;
1276 m_nProcessingFlags &= ~PROCESSING_FLAG_USE_MASK_BAND;
1279 void SetLinearScaling(
double dfOffset,
double dfScale);
1280 void SetPowerScaling(
double dfExponent,
double dfSrcMin,
double dfSrcMax,
1281 double dfDstMin,
double dfDstMax);
1282 void SetColorTableComponent(
int nComponent);
1289 class VRTFilteredSource
CPL_NON_FINAL :
public VRTComplexSource
1297 int m_nSupportedTypesCount;
1300 int m_nExtraEdgePixels;
1303 VRTFilteredSource();
1304 virtual ~VRTFilteredSource();
1306 void SetExtraEdgePixels(
int);
1307 void SetFilteringDataTypesSupported(
int,
GDALDataType *);
1313 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
1323 class VRTKernelFilteredSource
CPL_NON_FINAL :
public VRTFilteredSource
1332 double *m_padfKernelCoefs;
1337 VRTKernelFilteredSource();
1338 virtual ~VRTKernelFilteredSource();
1341 std::map<CPLString, GDALDataset *> &)
override;
1342 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
1345 GByte *pabySrcData,
GByte *pabyDstData)
override;
1347 CPLErr SetKernel(
int nKernelSize,
bool bSeparable,
double *padfCoefs);
1348 void SetNormalized(
int);
1355 class VRTAverageFilteredSource final :
public VRTKernelFilteredSource
1360 explicit VRTAverageFilteredSource(
int nKernelSize);
1361 virtual ~VRTAverageFilteredSource();
1364 std::map<CPLString, GDALDataset *> &)
override;
1365 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
1371 class VRTFuncSource final :
public VRTSource
1377 virtual ~VRTFuncSource();
1380 std::map<CPLString, GDALDataset *> &)
override
1384 virtual CPLXMLNode *SerializeToXML(
const char *pszVRTPath)
override;
1387 int nXSize,
int nYSize,
void *pData,
int nBufXSize,
1392 virtual double GetMinimum(
int nXSize,
int nYSize,
int *pbSuccess)
override;
1393 virtual double GetMaximum(
int nXSize,
int nYSize,
int *pbSuccess)
override;
1394 virtual CPLErr GetHistogram(
int nXSize,
int nYSize,
double dfMin,
1395 double dfMax,
int nBuckets,
1396 GUIntBig *panHistogram,
int bIncludeOutOfRange,
1397 int bApproxOK, GDALProgressFunc pfnProgress,
1398 void *pProgressData)
override;
1412 #define TMP_CPL_DLL CPL_DLL
1427 explicit Ref(VRTGroup *ptr) : m_ptr(ptr)
1430 Ref(
const Ref &) =
delete;
1431 Ref &operator=(
const Ref &) =
delete;
1435 std::shared_ptr<Ref> m_poSharedRefRootGroup{};
1436 std::weak_ptr<Ref> m_poWeakRefRootGroup{};
1437 std::shared_ptr<Ref> m_poRefSelf{};
1439 std::string m_osFilename{};
1440 mutable bool m_bDirty =
false;
1441 std::string m_osVRTPath{};
1442 std::map<std::string, std::shared_ptr<VRTGroup>> m_oMapGroups{};
1443 std::map<std::string, std::shared_ptr<VRTMDArray>> m_oMapMDArrays{};
1444 std::map<std::string, std::shared_ptr<VRTAttribute>> m_oMapAttributes{};
1445 std::map<std::string, std::shared_ptr<VRTDimension>> m_oMapDimensions{};
1447 std::shared_ptr<VRTGroup>
1448 OpenGroupInternal(
const std::string &osName)
const;
1449 void SetRootGroupRef(
const std::weak_ptr<Ref> &rgRef);
1450 std::weak_ptr<Ref> GetRootGroupRef()
const;
1453 friend class VRTMDArray;
1454 friend std::shared_ptr<GDALMDArray>
1455 VRTDerivedArrayCreate(
const char *pszVRTPath,
const CPLXMLNode *psTree);
1457 explicit VRTGroup(
const char *pszVRTPath);
1458 VRTGroup(
const std::string &osParentName,
const std::string &osName);
1461 static std::shared_ptr<VRTGroup> Create(
const std::string &osParentName,
1462 const std::string &osName)
1465 std::shared_ptr<VRTGroup>(
new VRTGroup(osParentName, osName));
1466 poGroup->SetSelf(poGroup);
1472 bool XMLInit(
const std::shared_ptr<VRTGroup> &poRoot,
1473 const std::shared_ptr<VRTGroup> &poThisGroup,
1474 const CPLXMLNode *psNode,
const char *pszVRTPath);
1476 std::vector<std::string>
1478 std::shared_ptr<GDALMDArray>
1482 std::vector<std::string>
1484 std::shared_ptr<GDALGroup>
OpenGroup(
const std::string &osName,
1487 return OpenGroupInternal(osName);
1490 std::vector<std::shared_ptr<GDALDimension>>
1493 std::vector<std::shared_ptr<GDALAttribute>>
1496 std::shared_ptr<VRTDimension> GetDimension(
const std::string &name)
const
1498 auto oIter = m_oMapDimensions.find(name);
1499 return oIter == m_oMapDimensions.end() ? nullptr : oIter->second;
1501 std::shared_ptr<VRTDimension>
1502 GetDimensionFromFullName(
const std::string &name,
bool bEmitError)
const;
1504 std::shared_ptr<GDALGroup>
1508 std::shared_ptr<GDALDimension>
1510 const std::string &osDirection,
GUInt64 nSize,
1513 std::shared_ptr<GDALAttribute>
1515 const std::vector<GUInt64> &anDimensions,
1520 const std::string &osName,
1521 const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
1525 void SetIsRootGroup();
1527 const std::shared_ptr<Ref> &GetRef()
const
1531 VRTGroup *GetRootGroup()
const;
1532 std::shared_ptr<GDALGroup> GetRootGroupSharedPtr()
const;
1534 const std::string &GetVRTPath()
const
1539 void SetFilename(
const std::string &osFilename)
1541 m_osFilename = osFilename;
1543 const std::string &GetFilename()
const
1545 return m_osFilename;
1547 bool Serialize()
const;
1548 CPLXMLNode *SerializeToXML(
const char *pszVRTPathIn)
const;
1549 void Serialize(
CPLXMLNode *psParent,
const char *pszVRTPathIn)
const;
1558 std::weak_ptr<VRTGroup::Ref> m_poGroupRef;
1559 std::string m_osIndexingVariableName;
1562 VRTDimension(
const std::shared_ptr<VRTGroup::Ref> &poGroupRef,
1563 const std::string &osParentName,
const std::string &osName,
1564 const std::string &osType,
const std::string &osDirection,
1565 GUInt64 nSize,
const std::string &osIndexingVariableName)
1566 :
GDALDimension(osParentName, osName, osType, osDirection, nSize),
1567 m_poGroupRef(poGroupRef),
1568 m_osIndexingVariableName(osIndexingVariableName)
1572 VRTGroup *GetGroup()
const;
1574 static std::shared_ptr<VRTDimension>
1575 Create(
const std::shared_ptr<VRTGroup> &poThisGroup,
1576 const std::string &osParentName,
const CPLXMLNode *psNode);
1581 std::shared_ptr<GDALMDArray> poIndexingVariable)
override;
1593 std::vector<std::string> m_aosList{};
1594 std::vector<std::shared_ptr<GDALDimension>> m_dims{};
1597 bool IRead(
const GUInt64 *arrayStartIdx,
const size_t *count,
1600 void *pDstBuffer)
const override;
1602 bool IWrite(
const GUInt64 *arrayStartIdx,
const size_t *count,
1605 const void *pSrcBuffer)
override;
1608 VRTAttribute(
const std::string &osParentName,
const std::string &osName,
1610 std::vector<std::string> &&aosList)
1613 m_aosList(std::move(aosList))
1615 if (m_aosList.size() > 1)
1617 m_dims.emplace_back(std::make_shared<GDALDimension>(
1618 std::string(),
"dim", std::string(), std::string(),
1623 VRTAttribute(
const std::string &osParentName,
const std::string &osName,
1630 m_dims.emplace_back(std::make_shared<GDALDimension>(
1631 std::string(),
"dim", std::string(), std::string(), nDim));
1635 static bool CreationCommonChecks(
1636 const std::string &osName,
const std::vector<GUInt64> &anDimensions,
1637 const std::map<std::string, std::shared_ptr<VRTAttribute>>
1640 static std::shared_ptr<VRTAttribute> Create(
const std::string &osParentName,
1643 const std::vector<std::shared_ptr<GDALDimension>> &
1661 class VRTMDArraySource
1664 virtual ~VRTMDArraySource() =
default;
1666 virtual bool Read(
const GUInt64 *arrayStartIdx,
const size_t *count,
1669 void *pDstBuffer)
const = 0;
1672 const char *pszVRTPath)
const = 0;
1682 friend class VRTGroup;
1684 std::weak_ptr<VRTGroup::Ref> m_poGroupRef;
1685 std::string m_osVRTPath{};
1686 std::shared_ptr<VRTGroup> m_poDummyOwningGroup{};
1689 std::vector<std::shared_ptr<GDALDimension>> m_dims;
1690 std::map<std::string, std::shared_ptr<VRTAttribute>> m_oMapAttributes{};
1691 std::vector<std::unique_ptr<VRTMDArraySource>> m_sources{};
1692 std::shared_ptr<OGRSpatialReference> m_poSRS{};
1693 std::vector<GByte> m_abyNoData{};
1694 std::string m_osUnit{};
1695 double m_dfScale = 1.0;
1696 double m_dfOffset = 0.0;
1697 bool m_bHasScale =
false;
1698 bool m_bHasOffset =
false;
1699 std::string m_osFilename{};
1701 bool IRead(
const GUInt64 *arrayStartIdx,
const size_t *count,
1704 void *pDstBuffer)
const override;
1710 const std::shared_ptr<VRTGroup::Ref> &poGroupRef,
1711 const std::string &osParentName,
const std::string &osName,
1713 std::vector<std::shared_ptr<GDALDimension>> &&dims,
1714 std::map<std::string, std::shared_ptr<VRTAttribute>> &&oMapAttributes)
1716 GDALMDArray(osParentName, osName), m_poGroupRef(poGroupRef),
1717 m_osVRTPath(poGroupRef->m_ptr->GetVRTPath()), m_dt(dt),
1718 m_dims(std::move(dims)), m_oMapAttributes(std::move(oMapAttributes)),
1719 m_osFilename(poGroupRef->m_ptr->GetFilename())
1723 VRTMDArray(
const std::shared_ptr<VRTGroup::Ref> &poGroupRef,
1724 const std::string &osParentName,
const std::string &osName,
1725 const std::vector<std::shared_ptr<GDALDimension>> &dims,
1728 GDALMDArray(osParentName, osName), m_poGroupRef(poGroupRef),
1729 m_osVRTPath(poGroupRef->m_ptr->GetVRTPath()), m_dt(dt), m_dims(dims),
1730 m_osFilename(poGroupRef->m_ptr->GetFilename())
1741 return m_osFilename;
1744 static std::shared_ptr<VRTMDArray> Create(
const char *pszVRTPath,
1747 static std::shared_ptr<VRTMDArray>
1748 Create(
const std::shared_ptr<VRTGroup> &poThisGroup,
1749 const std::string &osParentName,
const CPLXMLNode *psNode);
1751 const std::vector<std::shared_ptr<GDALDimension>> &
1757 std::vector<std::shared_ptr<GDALAttribute>>
1767 std::shared_ptr<OGRSpatialReference>
GetSpatialRef()
const override
1776 const std::string &
GetUnit()
const override
1781 bool SetUnit(
const std::string &osUnit)
override
1791 *pbHasOffset = m_bHasOffset;
1801 *pbHasScale = m_bHasScale;
1811 m_bHasOffset =
true;
1812 m_dfOffset = dfOffset;
1821 m_dfScale = dfScale;
1825 void AddSource(std::unique_ptr<VRTMDArraySource> &&poSource);
1827 std::shared_ptr<GDALAttribute>
1829 const std::vector<GUInt64> &anDimensions,
1835 GDALProgressFunc pfnProgress,
void *pProgressData)
override;
1837 void Serialize(
CPLXMLNode *psParent,
const char *pszVRTPathIn)
const;
1839 VRTGroup *GetGroup()
const;
1841 const std::string &GetVRTPath()
const
1846 std::shared_ptr<GDALGroup>
GetRootGroup()
const override
1848 auto poGroup = m_poGroupRef.lock();
1850 return poGroup->m_ptr->GetRootGroupSharedPtr();
1859 class VRTMDArraySourceInlinedValues final :
public VRTMDArraySource
1861 const VRTMDArray *m_poDstArray =
nullptr;
1862 bool m_bIsConstantValue;
1863 std::vector<GUInt64> m_anOffset{};
1864 std::vector<size_t> m_anCount{};
1865 std::vector<GByte> m_abyValues{};
1866 std::vector<size_t> m_anInlinedArrayStrideInBytes{};
1869 VRTMDArraySourceInlinedValues(
const VRTMDArraySourceInlinedValues &) =
1871 VRTMDArraySourceInlinedValues &
1872 operator=(
const VRTMDArraySourceInlinedValues &) =
delete;
1875 VRTMDArraySourceInlinedValues(
const VRTMDArray *poDstArray,
1876 bool bIsConstantValue,
1877 std::vector<GUInt64> &&anOffset,
1878 std::vector<size_t> &&anCount,
1879 std::vector<GByte> &&abyValues)
1880 : m_poDstArray(poDstArray), m_bIsConstantValue(bIsConstantValue),
1881 m_anOffset(std::move(anOffset)), m_anCount(std::move(anCount)),
1882 m_abyValues(std::move(abyValues)), m_dt(poDstArray->GetDataType())
1884 const auto nDims(poDstArray->GetDimensionCount());
1885 m_anInlinedArrayStrideInBytes.resize(nDims);
1886 if (!bIsConstantValue && nDims > 0)
1888 m_anInlinedArrayStrideInBytes.back() =
1889 poDstArray->GetDataType().GetSize();
1890 for (
size_t i = nDims - 1; i > 0;)
1893 m_anInlinedArrayStrideInBytes[i] =
1894 m_anInlinedArrayStrideInBytes[i + 1] * m_anCount[i + 1];
1899 ~VRTMDArraySourceInlinedValues();
1901 static std::unique_ptr<VRTMDArraySourceInlinedValues>
1902 Create(
const VRTMDArray *poDstArray,
const CPLXMLNode *psNode);
1904 bool Read(
const GUInt64 *arrayStartIdx,
const size_t *count,
1907 void *pDstBuffer)
const override;
1909 void Serialize(
CPLXMLNode *psParent,
const char *pszVRTPath)
const override;
1916 class VRTMDArraySourceRegularlySpaced final :
public VRTMDArraySource
1919 double m_dfIncrement;
1922 VRTMDArraySourceRegularlySpaced(
double dfStart,
double dfIncrement)
1923 : m_dfStart(dfStart), m_dfIncrement(dfIncrement)
1927 bool Read(
const GUInt64 *arrayStartIdx,
const size_t *count,
1930 void *pDstBuffer)
const override;
1932 void Serialize(
CPLXMLNode *psParent,
const char *pszVRTPath)
const override;
1939 class VRTMDArraySourceFromArray final :
public VRTMDArraySource
1941 const VRTMDArray *m_poDstArray =
nullptr;
1942 bool m_bRelativeToVRTSet =
false;
1943 bool m_bRelativeToVRT =
false;
1944 std::string m_osFilename{};
1945 std::string m_osArray{};
1946 std::string m_osBand{};
1947 std::vector<int> m_anTransposedAxis{};
1948 std::string m_osViewExpr{};
1949 std::vector<GUInt64> m_anSrcOffset{};
1950 mutable std::vector<GUInt64> m_anCount{};
1951 std::vector<GUInt64> m_anStep{};
1952 std::vector<GUInt64> m_anDstOffset{};
1954 VRTMDArraySourceFromArray(
const VRTMDArraySourceFromArray &) =
delete;
1955 VRTMDArraySourceFromArray &
1956 operator=(
const VRTMDArraySourceFromArray &) =
delete;
1959 VRTMDArraySourceFromArray(
1960 const VRTMDArray *poDstArray,
bool bRelativeToVRTSet,
1961 bool bRelativeToVRT,
const std::string &osFilename,
1962 const std::string &osArray,
const std::string &osBand,
1963 std::vector<int> &&anTransposedAxis,
const std::string &osViewExpr,
1964 std::vector<GUInt64> &&anSrcOffset, std::vector<GUInt64> &&anCount,
1965 std::vector<GUInt64> &&anStep, std::vector<GUInt64> &&anDstOffset)
1966 : m_poDstArray(poDstArray), m_bRelativeToVRTSet(bRelativeToVRTSet),
1967 m_bRelativeToVRT(bRelativeToVRT), m_osFilename(osFilename),
1968 m_osArray(osArray), m_osBand(osBand),
1969 m_anTransposedAxis(std::move(anTransposedAxis)),
1970 m_osViewExpr(osViewExpr), m_anSrcOffset(std::move(anSrcOffset)),
1971 m_anCount(std::move(anCount)), m_anStep(std::move(anStep)),
1972 m_anDstOffset(std::move(anDstOffset))
1976 ~VRTMDArraySourceFromArray()
override;
1978 static std::unique_ptr<VRTMDArraySourceFromArray>
1979 Create(
const VRTMDArray *poDstArray,
const CPLXMLNode *psNode);
1981 bool Read(
const GUInt64 *arrayStartIdx,
const size_t *count,
1984 void *pDstBuffer)
const override;
1986 void Serialize(
CPLXMLNode *psParent,
const char *pszVRTPath)
const override;