LCOV - code coverage report
Current view: top level - frmts/vrt - vrtdriver.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 212 251 84.5 %
Date: 2026-12-03 10:33:05 Functions: 9 11 81.8 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  Virtual GDAL Datasets
       4             :  * Purpose:  Implementation of VRTDriver
       5             :  * Author:   Frank Warmerdam <warmerdam@pobox.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
       9             :  * Copyright (c) 2009-2013, Even Rouault <even dot rouault at spatialys.com>
      10             :  *
      11             :  * SPDX-License-Identifier: MIT
      12             :  ****************************************************************************/
      13             : 
      14             : #include "vrtdataset.h"
      15             : 
      16             : #include "cpl_minixml.h"
      17             : #include "cpl_string.h"
      18             : #include "gdal_alg_priv.h"
      19             : #include "gdal_frmts.h"
      20             : #include "gdal_priv.h"
      21             : #include "vrtexpression.h"
      22             : 
      23             : #include <mutex>
      24             : 
      25             : /*! @cond Doxygen_Suppress */
      26             : 
      27             : /************************************************************************/
      28             : /*                             VRTDriver()                              */
      29             : /************************************************************************/
      30             : 
      31        1800 : VRTDriver::VRTDriver() : papszSourceParsers(nullptr)
      32             : {
      33             : #if 0
      34             :     pDeserializerData = GDALRegisterTransformDeserializer(
      35             :         "WarpedOverviewTransformer",
      36             :         VRTWarpedOverviewTransform,
      37             :         VRTDeserializeWarpedOverviewTransformer );
      38             : #endif
      39        1800 : }
      40             : 
      41             : /************************************************************************/
      42             : /*                             ~VRTDriver()                             */
      43             : /************************************************************************/
      44             : 
      45        2454 : VRTDriver::~VRTDriver()
      46             : 
      47             : {
      48        1227 :     CSLDestroy(papszSourceParsers);
      49        1227 :     VRTDerivedRasterBand::Cleanup();
      50             : #if 0
      51             :     if(  pDeserializerData )
      52             :     {
      53             :         GDALUnregisterTransformDeserializer( pDeserializerData );
      54             :     }
      55             : #endif
      56        2454 : }
      57             : 
      58             : /************************************************************************/
      59             : /*                       GetMetadataDomainList()                        */
      60             : /************************************************************************/
      61             : 
      62           0 : char **VRTDriver::GetMetadataDomainList()
      63             : {
      64           0 :     return BuildMetadataDomainList(GDALDriver::GetMetadataDomainList(), TRUE,
      65           0 :                                    "SourceParsers", nullptr);
      66             : }
      67             : 
      68             : /************************************************************************/
      69             : /*                            GetMetadata()                             */
      70             : /************************************************************************/
      71             : 
      72        1268 : CSLConstList VRTDriver::GetMetadata(const char *pszDomain)
      73             : 
      74             : {
      75        2536 :     std::lock_guard oLock(m_oMutex);
      76        1268 :     if (pszDomain && EQUAL(pszDomain, "SourceParsers"))
      77           0 :         return papszSourceParsers;
      78             : 
      79        1268 :     return GDALDriver::GetMetadata(pszDomain);
      80             : }
      81             : 
      82             : /************************************************************************/
      83             : /*                            SetMetadata()                             */
      84             : /************************************************************************/
      85             : 
      86           0 : CPLErr VRTDriver::SetMetadata(CSLConstList papszMetadata, const char *pszDomain)
      87             : 
      88             : {
      89           0 :     std::lock_guard oLock(m_oMutex);
      90           0 :     if (pszDomain && EQUAL(pszDomain, "SourceParsers"))
      91             :     {
      92           0 :         m_oMapSourceParser.clear();
      93           0 :         CSLDestroy(papszSourceParsers);
      94           0 :         papszSourceParsers = CSLDuplicate(papszMetadata);
      95           0 :         return CE_None;
      96             :     }
      97             : 
      98           0 :     return GDALDriver::SetMetadata(papszMetadata, pszDomain);
      99             : }
     100             : 
     101             : /************************************************************************/
     102             : /*                          AddSourceParser()                           */
     103             : /************************************************************************/
     104             : 
     105       10800 : void VRTDriver::AddSourceParser(const char *pszElementName,
     106             :                                 VRTSourceParser pfnParser)
     107             : 
     108             : {
     109       10800 :     m_oMapSourceParser[pszElementName] = pfnParser;
     110             : 
     111             :     // Below won't work on architectures with "capability pointers"
     112             : 
     113       10800 :     char szPtrValue[128] = {'\0'};
     114             :     void *ptr;
     115       10800 :     CPL_STATIC_ASSERT(sizeof(pfnParser) == sizeof(void *));
     116       10800 :     memcpy(&ptr, &pfnParser, sizeof(void *));
     117       10800 :     int nRet = CPLPrintPointer(szPtrValue, ptr, sizeof(szPtrValue));
     118       10800 :     szPtrValue[nRet] = 0;
     119             : 
     120       10800 :     papszSourceParsers =
     121       10800 :         CSLSetNameValue(papszSourceParsers, pszElementName, szPtrValue);
     122       10800 : }
     123             : 
     124             : /************************************************************************/
     125             : /*                            ParseSource()                             */
     126             : /************************************************************************/
     127             : 
     128      108557 : VRTSource *VRTDriver::ParseSource(const CPLXMLNode *psSrc,
     129             :                                   const char *pszVRTPath,
     130             :                                   VRTMapSharedResources &oMapSharedSources)
     131             : 
     132             : {
     133             : 
     134      108557 :     if (psSrc == nullptr || psSrc->eType != CXT_Element)
     135             :     {
     136           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     137             :                  "Corrupt or empty VRT source XML document.");
     138           0 :         return nullptr;
     139             :     }
     140             : 
     141      108557 :     if (!m_oMapSourceParser.empty())
     142             :     {
     143      108557 :         auto oIter = m_oMapSourceParser.find(psSrc->pszValue);
     144      108557 :         if (oIter != m_oMapSourceParser.end())
     145             :         {
     146      103901 :             return oIter->second(psSrc, pszVRTPath, oMapSharedSources);
     147             :         }
     148        4656 :         return nullptr;
     149             :     }
     150             : 
     151             :     // Below won't work on architectures with "capability pointers"
     152             : 
     153             :     const char *pszParserFunc =
     154           0 :         CSLFetchNameValue(papszSourceParsers, psSrc->pszValue);
     155           0 :     if (pszParserFunc == nullptr)
     156           0 :         return nullptr;
     157             : 
     158             :     VRTSourceParser pfnParser;
     159           0 :     CPL_STATIC_ASSERT(sizeof(pfnParser) == sizeof(void *));
     160             :     void *ptr =
     161           0 :         CPLScanPointer(pszParserFunc, static_cast<int>(strlen(pszParserFunc)));
     162           0 :     memcpy(&pfnParser, &ptr, sizeof(void *));
     163             : 
     164           0 :     if (pfnParser == nullptr)
     165           0 :         return nullptr;
     166             : 
     167           0 :     return pfnParser(psSrc, pszVRTPath, oMapSharedSources);
     168             : }
     169             : 
     170             : /************************************************************************/
     171             : /*                           VRTCreateCopy()                            */
     172             : /************************************************************************/
     173             : 
     174         308 : static GDALDataset *VRTCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
     175             :                                   int /* bStrict */, CSLConstList papszOptions,
     176             :                                   GDALProgressFunc pfnProgress,
     177             :                                   void *pProgressData)
     178             : {
     179         308 :     CPLAssert(nullptr != poSrcDS);
     180             : 
     181         308 :     VRTDataset *poSrcVRTDS = nullptr;
     182             : 
     183         308 :     void *pHandle = poSrcDS->GetInternalHandle("VRT_DATASET");
     184         308 :     if (pHandle && poSrcDS->GetInternalHandle(nullptr) == nullptr)
     185             :     {
     186           2 :         poSrcVRTDS = static_cast<VRTDataset *>(pHandle);
     187             :     }
     188             :     else
     189             :     {
     190         306 :         poSrcVRTDS = dynamic_cast<VRTDataset *>(poSrcDS);
     191             :     }
     192             : 
     193             :     /* -------------------------------------------------------------------- */
     194             :     /*      If the source dataset is a virtual dataset then just write      */
     195             :     /*      it to disk as a special case to avoid extra layers of           */
     196             :     /*      indirection.                                                    */
     197             :     /* -------------------------------------------------------------------- */
     198         308 :     if (poSrcVRTDS)
     199             :     {
     200             : 
     201             :         /* --------------------------------------------------------------------
     202             :          */
     203             :         /*      Convert tree to a single block of XML text. */
     204             :         /* --------------------------------------------------------------------
     205             :          */
     206         132 :         char *pszVRTPath = CPLStrdup(CPLGetPathSafe(pszFilename).c_str());
     207         132 :         poSrcVRTDS->UnsetPreservedRelativeFilenames();
     208         132 :         CPLXMLNode *psDSTree = poSrcVRTDS->SerializeToXML(pszVRTPath);
     209             : 
     210         132 :         char *pszXML = CPLSerializeXMLTree(psDSTree);
     211             : 
     212         132 :         CPLDestroyXMLNode(psDSTree);
     213             : 
     214         132 :         CPLFree(pszVRTPath);
     215             : 
     216             :         /* --------------------------------------------------------------------
     217             :          */
     218             :         /*      Write to disk. */
     219             :         /* --------------------------------------------------------------------
     220             :          */
     221         132 :         GDALDataset *pCopyDS = nullptr;
     222             : 
     223         132 :         if (0 != strlen(pszFilename))
     224             :         {
     225         118 :             VSILFILE *fpVRT = VSIFOpenL(pszFilename, "wb");
     226         118 :             if (fpVRT == nullptr)
     227             :             {
     228           0 :                 CPLError(CE_Failure, CPLE_AppDefined, "Cannot create %s",
     229             :                          pszFilename);
     230           0 :                 CPLFree(pszXML);
     231           0 :                 return nullptr;
     232             :             }
     233             : 
     234         118 :             bool bRet = VSIFWriteL(pszXML, strlen(pszXML), 1, fpVRT) > 0;
     235         118 :             if (VSIFCloseL(fpVRT) != 0)
     236           0 :                 bRet = false;
     237             : 
     238         118 :             if (bRet)
     239         118 :                 pCopyDS = GDALDataset::Open(
     240             :                     pszFilename,
     241             :                     GDAL_OF_RASTER | GDAL_OF_MULTIDIM_RASTER | GDAL_OF_UPDATE);
     242             :         }
     243             :         else
     244             :         {
     245             :             /* No destination file is given, so pass serialized XML directly. */
     246          14 :             CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
     247          14 :             pCopyDS = GDALDataset::Open(pszXML, GDAL_OF_RASTER |
     248             :                                                     GDAL_OF_MULTIDIM_RASTER |
     249             :                                                     GDAL_OF_UPDATE);
     250             :         }
     251             : 
     252         132 :         CPLFree(pszXML);
     253             : 
     254         132 :         if (pCopyDS || pszFilename[0] != '\0')
     255         130 :             return pCopyDS;
     256             :     }
     257             : 
     258             :     /* -------------------------------------------------------------------- */
     259             :     /*      Multidimensional raster ?                                       */
     260             :     /* -------------------------------------------------------------------- */
     261         356 :     auto poSrcGroup = poSrcDS->GetRootGroup();
     262         178 :     if (poSrcGroup != nullptr)
     263             :     {
     264             :         auto poDstDS = VRTDataset::CreateVRTMultiDimensional(pszFilename,
     265           8 :                                                              nullptr, nullptr);
     266           4 :         if (!poDstDS)
     267           0 :             return nullptr;
     268           8 :         auto poDstGroup = poDstDS->GetRootVRTGroup();
     269           4 :         if (!poDstGroup)
     270           0 :             return nullptr;
     271           4 :         poDstGroup->SetGuessRegularlySpacedArrays(
     272           4 :             CPLTestBool(CSLFetchNameValueDef(
     273             :                 papszOptions, "GUESS_REGULARLY_SPACED_ARRAYS", "YES")));
     274           4 :         if (GDALDriver::DefaultCreateCopyMultiDimensional(
     275           8 :                 poSrcDS, poDstDS.get(), false, nullptr, nullptr, nullptr) !=
     276             :             CE_None)
     277           0 :             return nullptr;
     278             : 
     279           4 :         if (strcmp(pszFilename, "") != 0)
     280             :         {
     281           4 :             if (poDstDS->FlushCache(true) != CE_None)
     282             :             {
     283           1 :                 poDstDS.reset();
     284             :             }
     285             :         }
     286             : 
     287           4 :         if (pfnProgress)
     288           4 :             pfnProgress(1.0, "", pProgressData);
     289           4 :         return poDstDS.release();
     290             :     }
     291             : 
     292             :     /* -------------------------------------------------------------------- */
     293             :     /*      Create the virtual dataset.                                     */
     294             :     /* -------------------------------------------------------------------- */
     295             :     auto poVRTDS = VRTDataset::CreateVRTDataset(
     296             :         pszFilename, poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize(), 0,
     297         348 :         GDT_UInt8, papszOptions);
     298         174 :     if (poVRTDS == nullptr)
     299           0 :         return nullptr;
     300             : 
     301             :     /* -------------------------------------------------------------------- */
     302             :     /*      Do we have a geotransform?                                      */
     303             :     /* -------------------------------------------------------------------- */
     304         174 :     GDALGeoTransform gt;
     305         174 :     if (poSrcDS->GetGeoTransform(gt) == CE_None)
     306             :     {
     307         139 :         poVRTDS->SetGeoTransform(gt);
     308             :     }
     309             : 
     310             :     /* -------------------------------------------------------------------- */
     311             :     /*      Copy projection                                                 */
     312             :     /* -------------------------------------------------------------------- */
     313         174 :     poVRTDS->SetSpatialRef(poSrcDS->GetSpatialRef());
     314             : 
     315             :     /* -------------------------------------------------------------------- */
     316             :     /*      Emit dataset level metadata.                                    */
     317             :     /* -------------------------------------------------------------------- */
     318             :     const char *pszCopySrcMDD =
     319         174 :         CSLFetchNameValueDef(papszOptions, "COPY_SRC_MDD", "AUTO");
     320         174 :     char **papszSrcMDD = CSLFetchNameValueMultiple(papszOptions, "SRC_MDD");
     321         174 :     if (EQUAL(pszCopySrcMDD, "AUTO") || CPLTestBool(pszCopySrcMDD) ||
     322             :         papszSrcMDD)
     323             :     {
     324         175 :         if (!papszSrcMDD || CSLFindString(papszSrcMDD, "") >= 0 ||
     325           2 :             CSLFindString(papszSrcMDD, "_DEFAULT_") >= 0)
     326             :         {
     327         171 :             poVRTDS->SetMetadata(poSrcDS->GetMetadata());
     328             :         }
     329             : 
     330             :         /* -------------------------------------------------------------------- */
     331             :         /*      Copy any special domains that should be transportable.          */
     332             :         /* -------------------------------------------------------------------- */
     333         173 :         constexpr const char *apszDefaultDomains[] = {
     334             :             GDAL_MDD_RPC, GDAL_MDD_IMD, GDAL_MDD_GEOLOCATION};
     335         692 :         for (const char *pszDomain : apszDefaultDomains)
     336             :         {
     337         519 :             if (!papszSrcMDD || CSLFindString(papszSrcMDD, pszDomain) >= 0)
     338             :             {
     339         510 :                 CSLConstList papszMD = poSrcDS->GetMetadata(pszDomain);
     340         510 :                 if (papszMD)
     341           0 :                     poVRTDS->SetMetadata(papszMD, pszDomain);
     342             :             }
     343             :         }
     344             : 
     345         173 :         if ((!EQUAL(pszCopySrcMDD, "AUTO") && CPLTestBool(pszCopySrcMDD)) ||
     346             :             papszSrcMDD)
     347             :         {
     348           4 :             char **papszDomainList = poSrcDS->GetMetadataDomainList();
     349           4 :             constexpr const char *apszReservedDomains[] = {
     350             :                 GDAL_MDD_IMAGE_STRUCTURE, "DERIVED_SUBDATASETS"};
     351          18 :             for (char **papszIter = papszDomainList; papszIter && *papszIter;
     352             :                  ++papszIter)
     353             :             {
     354          14 :                 const char *pszDomain = *papszIter;
     355          22 :                 if (pszDomain[0] != 0 &&
     356           8 :                     (!papszSrcMDD ||
     357           8 :                      CSLFindString(papszSrcMDD, pszDomain) >= 0))
     358             :                 {
     359           6 :                     bool bCanCopy = true;
     360          24 :                     for (const char *pszOtherDomain : apszDefaultDomains)
     361             :                     {
     362          18 :                         if (EQUAL(pszDomain, pszOtherDomain))
     363             :                         {
     364           0 :                             bCanCopy = false;
     365           0 :                             break;
     366             :                         }
     367             :                     }
     368           6 :                     if (!papszSrcMDD)
     369             :                     {
     370           6 :                         for (const char *pszOtherDomain : apszReservedDomains)
     371             :                         {
     372           5 :                             if (EQUAL(pszDomain, pszOtherDomain))
     373             :                             {
     374           2 :                                 bCanCopy = false;
     375           2 :                                 break;
     376             :                             }
     377             :                         }
     378             :                     }
     379           6 :                     if (bCanCopy)
     380             :                     {
     381           8 :                         poVRTDS->SetMetadata(poSrcDS->GetMetadata(pszDomain),
     382           4 :                                              pszDomain);
     383             :                     }
     384             :                 }
     385             :             }
     386           4 :             CSLDestroy(papszDomainList);
     387             :         }
     388             :     }
     389         174 :     CSLDestroy(papszSrcMDD);
     390             : 
     391             :     {
     392         348 :         const char *pszInterleave = poSrcDS->GetMetadataItem(
     393         174 :             GDALMD_INTERLEAVE, GDAL_MDD_IMAGE_STRUCTURE);
     394         174 :         if (pszInterleave)
     395             :         {
     396         140 :             poVRTDS->SetMetadataItem(GDALMD_INTERLEAVE, pszInterleave,
     397         140 :                                      GDAL_MDD_IMAGE_STRUCTURE);
     398             :         }
     399             :     }
     400             :     {
     401         348 :         const char *pszCompression = poSrcDS->GetMetadataItem(
     402         174 :             GDALMD_COMPRESSION, GDAL_MDD_IMAGE_STRUCTURE);
     403         174 :         if (pszCompression)
     404             :         {
     405           6 :             poVRTDS->SetMetadataItem(GDALMD_COMPRESSION, pszCompression,
     406           6 :                                      GDAL_MDD_IMAGE_STRUCTURE);
     407             :         }
     408             :     }
     409             : 
     410             :     /* -------------------------------------------------------------------- */
     411             :     /*      GCPs                                                            */
     412             :     /* -------------------------------------------------------------------- */
     413         174 :     if (poSrcDS->GetGCPCount() > 0)
     414             :     {
     415           2 :         poVRTDS->SetGCPs(poSrcDS->GetGCPCount(), poSrcDS->GetGCPs(),
     416           1 :                          poSrcDS->GetGCPSpatialRef());
     417             :     }
     418             : 
     419             :     /* -------------------------------------------------------------------- */
     420             :     /*      Loop over all the bands.                                        */
     421             :     /* -------------------------------------------------------------------- */
     422       66023 :     for (int iBand = 0; iBand < poSrcDS->GetRasterCount(); iBand++)
     423             :     {
     424       65849 :         GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(iBand + 1);
     425             : 
     426             :         /* --------------------------------------------------------------------
     427             :          */
     428             :         /*      Create the band with the appropriate band type. */
     429             :         /* --------------------------------------------------------------------
     430             :          */
     431      131698 :         CPLStringList aosAddBandOptions;
     432       65849 :         int nBlockXSize = poVRTDS->GetBlockXSize();
     433       65849 :         int nBlockYSize = poVRTDS->GetBlockYSize();
     434       65849 :         if (!poVRTDS->IsBlockSizeSpecified())
     435             :         {
     436       65847 :             poSrcBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
     437             :         }
     438             :         aosAddBandOptions.SetNameValue("BLOCKXSIZE",
     439       65849 :                                        CPLSPrintf("%d", nBlockXSize));
     440             :         aosAddBandOptions.SetNameValue("BLOCKYSIZE",
     441       65849 :                                        CPLSPrintf("%d", nBlockYSize));
     442       65849 :         poVRTDS->AddBand(poSrcBand->GetRasterDataType(), aosAddBandOptions);
     443             : 
     444             :         VRTSourcedRasterBand *poVRTBand = static_cast<VRTSourcedRasterBand *>(
     445       65849 :             poVRTDS->GetRasterBand(iBand + 1));
     446             : 
     447             :         /* --------------------------------------------------------------------
     448             :          */
     449             :         /*      Setup source mapping. */
     450             :         /* --------------------------------------------------------------------
     451             :          */
     452       65849 :         poVRTBand->AddSimpleSource(poSrcBand);
     453             : 
     454             :         /* --------------------------------------------------------------------
     455             :          */
     456             :         /*      Emit various band level metadata. */
     457             :         /* --------------------------------------------------------------------
     458             :          */
     459       65849 :         poVRTBand->CopyCommonInfoFrom(poSrcBand);
     460             : 
     461      131698 :         const char *pszCompression = poSrcBand->GetMetadataItem(
     462       65849 :             GDALMD_COMPRESSION, GDAL_MDD_IMAGE_STRUCTURE);
     463       65849 :         if (pszCompression)
     464             :         {
     465           3 :             poVRTBand->SetMetadataItem(GDALMD_COMPRESSION, pszCompression,
     466           3 :                                        GDAL_MDD_IMAGE_STRUCTURE);
     467             :         }
     468             : 
     469             :         /* --------------------------------------------------------------------
     470             :          */
     471             :         /*      Add specific mask band. */
     472             :         /* --------------------------------------------------------------------
     473             :          */
     474       65849 :         if ((poSrcBand->GetMaskFlags() &
     475       65849 :              (GMF_PER_DATASET | GMF_ALL_VALID | GMF_NODATA)) == 0)
     476             :         {
     477             :             auto poVRTMaskBand = std::make_unique<VRTSourcedRasterBand>(
     478           0 :                 poVRTDS.get(), 0, poSrcBand->GetMaskBand()->GetRasterDataType(),
     479           0 :                 poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize());
     480           0 :             poVRTMaskBand->AddMaskBandSource(poSrcBand);
     481           0 :             poVRTBand->SetMaskBand(std::move(poVRTMaskBand));
     482             :         }
     483             :     }
     484             : 
     485             :     /* -------------------------------------------------------------------- */
     486             :     /*      Add dataset mask band                                           */
     487             :     /* -------------------------------------------------------------------- */
     488         174 :     if (poSrcDS->GetRasterCount() != 0 &&
     489         347 :         poSrcDS->GetRasterBand(1) != nullptr &&
     490         173 :         poSrcDS->GetRasterBand(1)->GetMaskFlags() == GMF_PER_DATASET)
     491             :     {
     492           2 :         GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(1);
     493             :         auto poVRTMaskBand = std::make_unique<VRTSourcedRasterBand>(
     494           2 :             poVRTDS.get(), 0, poSrcBand->GetMaskBand()->GetRasterDataType(),
     495           4 :             poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize());
     496           2 :         poVRTMaskBand->AddMaskBandSource(poSrcBand);
     497           2 :         poVRTDS->SetMaskBand(std::move(poVRTMaskBand));
     498             :     }
     499             : 
     500         174 :     if (strcmp(pszFilename, "") != 0)
     501             :     {
     502          86 :         if (poVRTDS->FlushCache(true) != CE_None)
     503             :         {
     504          11 :             poVRTDS.reset();
     505             :         }
     506             :     }
     507             : 
     508         174 :     if (pfnProgress)
     509         174 :         pfnProgress(1.0, "", pProgressData);
     510             : 
     511         174 :     return poVRTDS.release();
     512             : }
     513             : 
     514             : /************************************************************************/
     515             : /*                          GDALRegister_VRT()                          */
     516             : /************************************************************************/
     517             : 
     518        8890 : void GDALRegister_VRT()
     519             : 
     520             : {
     521        8890 :     auto poDM = GetGDALDriverManager();
     522        8890 :     if (poDM->GetDriverByName("VRT") != nullptr)
     523        7090 :         return;
     524             : 
     525             :     static std::once_flag flag;
     526        1800 :     std::call_once(flag,
     527        1600 :                    []()
     528             :                    {
     529             :                        // First register the pixel functions
     530        1600 :                        GDALRegisterDefaultPixelFunc();
     531             : 
     532             :                        // Register functions for VRTProcessedDataset
     533        1600 :                        GDALVRTRegisterDefaultProcessedDatasetFuncs();
     534        1600 :                    });
     535             : 
     536        1800 :     VRTDriver *poDriver = new VRTDriver();
     537             : 
     538        1800 :     poDriver->SetDescription("VRT");
     539        1800 :     poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
     540        1800 :     poDriver->SetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER, "YES");
     541        1800 :     poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "Virtual Raster");
     542        1800 :     poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "vrt");
     543        1800 :     poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/vrt.html");
     544        1800 :     poDriver->SetMetadataItem(
     545             :         GDAL_DMD_CREATIONDATATYPES,
     546             :         "Byte Int8 Int16 UInt16 Int32 UInt32 Int64 UInt64 "
     547             :         "Float16 Float32 Float64 "
     548        1800 :         "CInt16 CInt32 CFloat16 CFloat32 CFloat64");
     549        1800 :     poDriver->SetMetadataItem(
     550             :         GDAL_DMD_CREATIONOPTIONLIST,
     551             :         "<CreationOptionList>\n"
     552             :         "   <Option name='SUBCLASS' type='string-select' "
     553             :         "default='VRTDataset'>\n"
     554             :         "       <Value>VRTDataset</Value>\n"
     555             :         "       <Value>VRTWarpedDataset</Value>\n"
     556             :         "   </Option>\n"
     557             :         "   <Option name='BLOCKXSIZE' type='int' description='Block width'/>\n"
     558             :         "   <Option name='BLOCKYSIZE' type='int' description='Block height'/>\n"
     559        1800 :         "</CreationOptionList>\n");
     560             : 
     561        1800 :     auto poGTiffDrv = poDM->GetDriverByName("GTiff");
     562        1800 :     if (poGTiffDrv)
     563             :     {
     564             :         const char *pszGTiffOvrCO =
     565        1800 :             poGTiffDrv->GetMetadataItem(GDAL_DMD_OVERVIEW_CREATIONOPTIONLIST);
     566        1800 :         if (pszGTiffOvrCO &&
     567        1800 :             STARTS_WITH(pszGTiffOvrCO, "<OverviewCreationOptionList>"))
     568             :         {
     569             :             std::string ocoList =
     570             :                 "<OverviewCreationOptionList>"
     571             :                 "   <Option name='VIRTUAL' type='boolean' "
     572             :                 "default='NO' "
     573             :                 "description='Whether virtual overviews rather than "
     574        3600 :                 "materialized external GeoTIFF .ovr should be created'/>";
     575        1800 :             ocoList += (pszGTiffOvrCO + strlen("<OverviewCreationOptionList>"));
     576        1800 :             poDriver->SetMetadataItem(GDAL_DMD_OVERVIEW_CREATIONOPTIONLIST,
     577        1800 :                                       ocoList.c_str());
     578             :         }
     579             :     }
     580             : 
     581        1800 :     poDriver->SetMetadataItem(
     582             :         GDAL_DMD_MULTIDIM_DATASET_CREATIONOPTIONLIST,
     583             :         "<MultiDimDatasetCreationOptionList>"
     584             :         "   <Option name='GUESS_REGULARLY_SPACED_ARRAYS' type='boolean' "
     585             :         "description='Whether content of 1D-arrays should be read to deduce "
     586             :         "if they are regularly spaced. Can be slow on huge remote datasets' "
     587             :         "default='YES'/>"
     588        1800 :         "</MultiDimDatasetCreationOptionList>");
     589             : 
     590        1800 :     poDriver->SetMetadataItem(
     591             :         GDAL_DMD_MULTIDIM_ARRAY_CREATIONOPTIONLIST,
     592             :         "<MultiDimArrayCreationOptionList>"
     593             :         "   <Option name='BLOCKSIZE' type='int' description='Block size in "
     594             :         "pixels'/>"
     595        1800 :         "</MultiDimArrayCreationOptionList>");
     596             : 
     597        1800 :     poDriver->pfnCreateCopy = VRTCreateCopy;
     598        1800 :     poDriver->pfnCreate = VRTDataset::Create;
     599        1800 :     poDriver->pfnCreateMultiDimensional = VRTDataset::CreateMultiDimensional;
     600             : 
     601             : #ifndef NO_OPEN
     602        1800 :     poDriver->pfnOpen = VRTDataset::Open;
     603        1800 :     poDriver->pfnIdentify = VRTDataset::Identify;
     604        1800 :     poDriver->pfnDelete = VRTDataset::Delete;
     605             : 
     606        1800 :     poDriver->SetMetadataItem(
     607             :         GDAL_DMD_OPENOPTIONLIST,
     608             :         "<OpenOptionList>"
     609             :         "  <Option name='ROOT_PATH' type='string' description='Root path to "
     610             :         "evaluate "
     611             :         "relative paths inside the VRT. Mainly useful for inlined VRT, or "
     612             :         "in-memory "
     613             :         "VRT, where their own directory does not make sense'/>"
     614             :         "<Option name='NUM_THREADS' type='string' description="
     615             :         "'Number of worker threads for reading. Can be set to ALL_CPUS' "
     616             :         "default='ALL_CPUS'/>"
     617        1800 :         "</OpenOptionList>");
     618             : #endif
     619             : 
     620        1800 :     poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
     621        1800 :     poDriver->SetMetadataItem(GDAL_DCAP_COORDINATE_EPOCH, "YES");
     622             : 
     623        1800 :     poDriver->SetMetadataItem(GDAL_DCAP_UPDATE, "YES");
     624        1800 :     poDriver->SetMetadataItem(GDAL_DMD_UPDATE_ITEMS,
     625             :                               "GeoTransform SRS GCPs NoData "
     626             :                               "ColorInterpretation "
     627        1800 :                               "DatasetMetadata BandMetadata");
     628             : 
     629        1800 :     const char *pszExpressionDialects = "ExpressionDialects";
     630             : #if defined(GDAL_VRT_ENABLE_MUPARSER) && defined(GDAL_VRT_ENABLE_EXPRTK)
     631        1800 :     poDriver->SetMetadataItem(pszExpressionDialects, "muparser,exprtk");
     632             : #elif defined(GDAL_VRT_ENABLE_MUPARSER)
     633             :     poDriver->SetMetadataItem(pszExpressionDialects, "muparser");
     634             : #elif defined(GDAL_VRT_ENABLE_EXPRTK)
     635             :     poDriver->SetMetadataItem(pszExpressionDialects, "exprtk");
     636             : #else
     637             :     poDriver->SetMetadataItem(pszExpressionDialects, "none");
     638             : #endif
     639             : 
     640             : #ifdef GDAL_VRT_ENABLE_MUPARSER
     641        1800 :     if (gdal::MuParserHasDefineFunUserData())
     642             :     {
     643           0 :         poDriver->SetMetadataItem("MUPARSER_HAS_DEFINE_FUN_USER_DATA", "YES");
     644             :     }
     645             : #endif
     646             : 
     647             : #ifdef GDAL_VRT_ENABLE_RAWRASTERBAND
     648        1800 :     poDriver->SetMetadataItem("GDAL_VRT_ENABLE_RAWRASTERBAND", "YES");
     649             : #endif
     650             : 
     651        1800 :     poDriver->AddSourceParser("SimpleSource", VRTParseCoreSources);
     652        1800 :     poDriver->AddSourceParser("ComplexSource", VRTParseCoreSources);
     653        1800 :     poDriver->AddSourceParser("AveragedSource", VRTParseCoreSources);
     654        1800 :     poDriver->AddSourceParser("NoDataFromMaskSource", VRTParseCoreSources);
     655        1800 :     poDriver->AddSourceParser("KernelFilteredSource", VRTParseFilterSources);
     656        1800 :     poDriver->AddSourceParser("ArraySource", VRTParseArraySource);
     657             : 
     658        1800 :     poDM->RegisterDriver(poDriver);
     659             : }
     660             : 
     661             : /*! @endcond */

Generated by: LCOV version 1.14