LCOV - code coverage report
Current view: top level - apps - gdalalg_raster_index.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 93 95 97.9 %
Date: 2025-06-19 12:30:01 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  gdal "raster index" subcommand
       5             :  * Author:   Even Rouault <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "gdalalg_raster_index.h"
      14             : 
      15             : #include "cpl_conv.h"
      16             : #include "gdal_priv.h"
      17             : #include "gdal_utils_priv.h"
      18             : #include "ogrsf_frmts.h"
      19             : 
      20             : //! @cond Doxygen_Suppress
      21             : 
      22             : #ifndef _
      23             : #define _(x) (x)
      24             : #endif
      25             : 
      26             : /************************************************************************/
      27             : /*          GDALRasterIndexAlgorithm::GDALRasterIndexAlgorithm()        */
      28             : /************************************************************************/
      29             : 
      30          18 : GDALRasterIndexAlgorithm::GDALRasterIndexAlgorithm()
      31          18 :     : GDALVectorOutputAbstractAlgorithm(NAME, DESCRIPTION, HELP_URL)
      32             : {
      33          18 :     AddProgressArg();
      34          18 :     AddInputDatasetArg(&m_inputDatasets, GDAL_OF_RASTER)
      35          18 :         .SetAutoOpenDataset(false)
      36          18 :         .SetDatasetInputFlags(GADV_NAME);
      37          18 :     GDALVectorOutputAbstractAlgorithm::AddAllOutputArgs();
      38             : 
      39          18 :     AddCommonOptions();
      40             : 
      41             :     AddArg("source-crs-field-name", 0,
      42             :            _("Name of the field to store the CRS of each dataset"),
      43          36 :            &m_sourceCrsName)
      44          18 :         .SetMinCharCount(1);
      45             :     AddArg("source-crs-format", 0,
      46             :            _("Format in which the CRS of each dataset must be written"),
      47          36 :            &m_sourceCrsFormat)
      48          18 :         .SetMinCharCount(1)
      49          18 :         .SetDefault(m_sourceCrsFormat)
      50          18 :         .SetChoices("auto", "WKT", "EPSG", "PROJ");
      51          18 : }
      52             : 
      53             : /************************************************************************/
      54             : /*          GDALRasterIndexAlgorithm::GDALRasterIndexAlgorithm()        */
      55             : /************************************************************************/
      56             : 
      57          19 : GDALRasterIndexAlgorithm::GDALRasterIndexAlgorithm(
      58             :     const std::string &name, const std::string &description,
      59          19 :     const std::string &helpURL)
      60          19 :     : GDALVectorOutputAbstractAlgorithm(name, description, helpURL)
      61             : {
      62          19 : }
      63             : 
      64             : /************************************************************************/
      65             : /*              GDALRasterIndexAlgorithm::AddCommonOptions()            */
      66             : /************************************************************************/
      67             : 
      68          37 : void GDALRasterIndexAlgorithm::AddCommonOptions()
      69             : {
      70             :     AddArg("recursive", 0,
      71             :            _("Whether input directories should be explored recursively."),
      72          37 :            &m_recursive);
      73             :     AddArg("filename-filter", 0,
      74             :            _("Pattern that the filenames in input directories should follow "
      75             :              "('*' and '?' wildcard)"),
      76          37 :            &m_filenameFilter);
      77             :     AddArg("min-pixel-size", 0,
      78             :            _("Minimum pixel size in term of geospatial extent per pixel "
      79             :              "(resolution) that a raster should have to be selected."),
      80          74 :            &m_minPixelSize)
      81          37 :         .SetMinValueExcluded(0);
      82             :     AddArg("max-pixel-size", 0,
      83             :            _("Maximum pixel size in term of geospatial extent per pixel "
      84             :              "(resolution) that a raster should have to be selected."),
      85          74 :            &m_maxPixelSize)
      86          37 :         .SetMinValueExcluded(0);
      87             :     AddArg("location-name", 0, _("Name of the field with the raster path"),
      88          74 :            &m_locationName)
      89          37 :         .SetDefault(m_locationName)
      90          37 :         .SetMinCharCount(1);
      91             :     AddAbsolutePathArg(
      92             :         &m_writeAbsolutePaths,
      93             :         _("Whether the path to the input datasets should be stored as an "
      94          37 :           "absolute path"));
      95          74 :     AddArg("dst-crs", 0, _("Destination CRS"), &m_crs)
      96          74 :         .SetIsCRSArg()
      97          37 :         .AddHiddenAlias("t_srs");
      98             : 
      99             :     {
     100             :         auto &arg =
     101          74 :             AddArg("metadata", 0, _("Add dataset metadata item"), &m_metadata)
     102          74 :                 .SetMetaVar("<KEY>=<VALUE>")
     103          37 :                 .SetPackedValuesAllowed(false);
     104           1 :         arg.AddValidationAction([this, &arg]()
     105          38 :                                 { return ParseAndValidateKeyValue(arg); });
     106          37 :         arg.AddHiddenAlias("mo");
     107             :     }
     108          37 : }
     109             : 
     110             : /************************************************************************/
     111             : /*                   GDALRasterIndexAlgorithm::RunImpl()                */
     112             : /************************************************************************/
     113             : 
     114          18 : bool GDALRasterIndexAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
     115             :                                        void *pProgressData)
     116             : {
     117          36 :     CPLStringList aosSources;
     118          35 :     for (auto &srcDS : m_inputDatasets)
     119             :     {
     120          18 :         if (srcDS.GetDatasetRef())
     121             :         {
     122           1 :             ReportError(
     123             :                 CE_Failure, CPLE_IllegalArg,
     124             :                 "Input datasets must be provided by name, not as object");
     125           1 :             return false;
     126             :         }
     127          17 :         aosSources.push_back(srcDS.GetName());
     128             :     }
     129             : 
     130          34 :     auto setupRet = SetupOutputDataset();
     131          17 :     if (!setupRet.outDS)
     132           1 :         return false;
     133             : 
     134          16 :     if (!SetDefaultOutputLayerNameIfNeeded(setupRet.outDS))
     135           1 :         return false;
     136             : 
     137          30 :     CPLStringList aosOptions;
     138          15 :     if (m_recursive)
     139             :     {
     140           1 :         aosOptions.push_back("-recursive");
     141             :     }
     142          17 :     for (const std::string &s : m_filenameFilter)
     143             :     {
     144           2 :         aosOptions.push_back("-filename_filter");
     145           2 :         aosOptions.push_back(s);
     146             :     }
     147          15 :     if (m_minPixelSize > 0)
     148             :     {
     149           2 :         aosOptions.push_back("-min_pixel_size");
     150           2 :         aosOptions.push_back(CPLSPrintf("%.17g", m_minPixelSize));
     151             :     }
     152          15 :     if (m_maxPixelSize > 0)
     153             :     {
     154           0 :         aosOptions.push_back("-max_pixel_size");
     155           0 :         aosOptions.push_back(CPLSPrintf("%.17g", m_maxPixelSize));
     156             :     }
     157             : 
     158          15 :     if (!m_outputLayerName.empty())
     159             :     {
     160          15 :         aosOptions.push_back("-lyr_name");
     161          15 :         aosOptions.push_back(m_outputLayerName);
     162             :     }
     163             : 
     164          15 :     aosOptions.push_back("-tileindex");
     165          15 :     aosOptions.push_back(m_locationName);
     166             : 
     167          15 :     if (m_writeAbsolutePaths)
     168             :     {
     169           1 :         aosOptions.push_back("-write_absolute_path");
     170             :     }
     171          15 :     if (m_crs.empty())
     172             :     {
     173          14 :         if (m_sourceCrsName.empty())
     174          14 :             aosOptions.push_back("-skip_different_projection");
     175             :     }
     176             :     else
     177             :     {
     178           1 :         aosOptions.push_back("-t_srs");
     179           1 :         aosOptions.push_back(m_crs);
     180             :     }
     181          15 :     if (!m_sourceCrsName.empty())
     182             :     {
     183           1 :         aosOptions.push_back("-src_srs_name");
     184           1 :         aosOptions.push_back(m_sourceCrsName);
     185             : 
     186           1 :         aosOptions.push_back("-src_srs_format");
     187           1 :         aosOptions.push_back(CPLString(m_sourceCrsFormat).toupper());
     188             :     }
     189             : 
     190          16 :     for (const std::string &s : m_metadata)
     191             :     {
     192           1 :         aosOptions.push_back("-mo");
     193           1 :         aosOptions.push_back(s);
     194             :     }
     195             : 
     196          15 :     if (!AddExtraOptions(aosOptions))
     197           2 :         return false;
     198             : 
     199             :     std::unique_ptr<GDALTileIndexOptions, decltype(&GDALTileIndexOptionsFree)>
     200             :         options(GDALTileIndexOptionsNew(aosOptions.List(), nullptr),
     201          13 :                 GDALTileIndexOptionsFree);
     202             : 
     203          13 :     if (options)
     204             :     {
     205          13 :         GDALTileIndexOptionsSetProgress(options.get(), pfnProgress,
     206             :                                         pProgressData);
     207             :     }
     208             : 
     209             :     const bool ret =
     210          26 :         options && GDALTileIndexInternal(m_outputDataset.GetName().c_str(),
     211             :                                          GDALDataset::ToHandle(setupRet.outDS),
     212             :                                          OGRLayer::ToHandle(setupRet.layer),
     213          13 :                                          aosSources.size(), aosSources.List(),
     214          26 :                                          options.get(), nullptr) != nullptr;
     215             : 
     216          13 :     if (ret && setupRet.newDS)
     217             :     {
     218          10 :         m_outputDataset.Set(std::move(setupRet.newDS));
     219             :     }
     220             : 
     221          13 :     return ret;
     222             : }
     223             : 
     224             : //! @endcond

Generated by: LCOV version 1.14