LCOV - code coverage report
Current view: top level - apps - gdalalg_mdim_info.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 63 63 100.0 %
Date: 2025-03-31 11:17:27 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  gdal "mdim info" 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_mdim_info.h"
      14             : 
      15             : #include "cpl_conv.h"
      16             : #include "gdal_priv.h"
      17             : #include "gdal_utils.h"
      18             : 
      19             : //! @cond Doxygen_Suppress
      20             : 
      21             : #ifndef _
      22             : #define _(x) (x)
      23             : #endif
      24             : 
      25             : /************************************************************************/
      26             : /*              GDALMdimInfoAlgorithm::GDALMdimInfoAlgorithm()          */
      27             : /************************************************************************/
      28             : 
      29           7 : GDALMdimInfoAlgorithm::GDALMdimInfoAlgorithm()
      30           7 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      31             : {
      32           7 :     AddOutputFormatArg(&m_format).SetHidden().SetDefault("json").SetChoices(
      33           7 :         "json", "text");
      34           7 :     AddOpenOptionsArg(&m_openOptions);
      35           7 :     AddInputFormatsArg(&m_inputFormats)
      36             :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES,
      37          14 :                          {GDAL_DCAP_MULTIDIM_RASTER});
      38           7 :     AddInputDatasetArg(&m_dataset, GDAL_OF_MULTIDIM_RASTER);
      39           7 :     AddOutputStringArg(&m_output);
      40             :     AddArg(
      41             :         "detailed", 0,
      42             :         _("Most verbose output. Report attribute data types and array values."),
      43           7 :         &m_detailed);
      44             :     {
      45             :         auto &arg = AddArg("array", 0,
      46             :                            _("Name of the array, used to restrict the output "
      47             :                              "to the specified array."),
      48           7 :                            &m_array);
      49             : 
      50             :         arg.SetAutoCompleteFunction(
      51           4 :             [this](const std::string &)
      52             :             {
      53           2 :                 std::vector<std::string> ret;
      54             : 
      55           2 :                 if (auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
      56           2 :                         m_dataset.GetName().c_str(), GDAL_OF_MULTIDIM_RASTER,
      57           4 :                         nullptr, nullptr, nullptr)))
      58             :                 {
      59           2 :                     if (auto poRG = poDS->GetRootGroup())
      60             :                     {
      61           1 :                         ret = poRG->GetMDArrayFullNamesRecursive();
      62             :                     }
      63             :                 }
      64             : 
      65           2 :                 return ret;
      66           7 :             });
      67             :     }
      68             : 
      69             :     AddArg("limit", 0,
      70             :            _("Number of values in each dimension that is used to limit the "
      71             :              "display of array values."),
      72           7 :            &m_limit);
      73             :     {
      74             :         auto &arg = AddArg("array-option", 0,
      75             :                            _("Option passed to GDALGroup::GetMDArrayNames() to "
      76             :                              "filter reported arrays."),
      77          14 :                            &m_arrayOptions)
      78           7 :                         .SetMetaVar("<KEY>=<VALUE>");
      79           1 :         arg.AddValidationAction([this, &arg]()
      80           8 :                                 { return ValidateKeyValue(arg); });
      81             : 
      82             :         arg.SetAutoCompleteFunction(
      83           4 :             [this](const std::string &currentValue)
      84             :             {
      85           2 :                 std::vector<std::string> ret;
      86             : 
      87           2 :                 if (auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
      88           2 :                         m_dataset.GetName().c_str(), GDAL_OF_MULTIDIM_RASTER,
      89           4 :                         nullptr, nullptr, nullptr)))
      90             :                 {
      91           1 :                     if (auto poDriver = poDS->GetDriver())
      92             :                     {
      93           1 :                         if (const char *pszXML = poDriver->GetMetadataItem(
      94           1 :                                 GDAL_DMD_MULTIDIM_ARRAY_OPENOPTIONLIST))
      95             :                         {
      96           1 :                             AddOptionsSuggestions(pszXML, 0, currentValue, ret);
      97             :                         }
      98             :                     }
      99             :                 }
     100             : 
     101           2 :                 return ret;
     102           7 :             });
     103             :     }
     104           7 :     AddArg("stats", 0, _("Read and display image statistics."), &m_stats);
     105           7 : }
     106             : 
     107             : /************************************************************************/
     108             : /*                   GDALMdimInfoAlgorithm::RunImpl()                   */
     109             : /************************************************************************/
     110             : 
     111           2 : bool GDALMdimInfoAlgorithm::RunImpl(GDALProgressFunc, void *)
     112             : {
     113           2 :     CPLAssert(m_dataset.GetDatasetRef());
     114             : 
     115           2 :     CPLStringList aosOptions;
     116             : 
     117           2 :     if (m_detailed)
     118           1 :         aosOptions.AddString("-detailed");
     119           2 :     if (m_stats)
     120           1 :         aosOptions.AddString("-stats");
     121           2 :     if (m_limit > 0)
     122             :     {
     123           1 :         aosOptions.AddString("-limit");
     124           1 :         aosOptions.AddString(CPLSPrintf("%d", m_limit));
     125             :     }
     126           2 :     if (!m_array.empty())
     127             :     {
     128           1 :         aosOptions.AddString("-array");
     129           1 :         aosOptions.AddString(m_array.c_str());
     130             :     }
     131           3 :     for (const std::string &opt : m_arrayOptions)
     132             :     {
     133           1 :         aosOptions.AddString("-arrayoption");
     134           1 :         aosOptions.AddString(opt.c_str());
     135             :     }
     136             : 
     137           2 :     GDALDatasetH hDS = GDALDataset::ToHandle(m_dataset.GetDatasetRef());
     138             :     GDALMultiDimInfoOptions *psOptions =
     139           2 :         GDALMultiDimInfoOptionsNew(aosOptions.List(), nullptr);
     140           2 :     char *ret = GDALMultiDimInfo(hDS, psOptions);
     141           2 :     GDALMultiDimInfoOptionsFree(psOptions);
     142           2 :     const bool bOK = ret != nullptr;
     143           2 :     if (ret)
     144             :     {
     145           2 :         m_output = ret;
     146             :     }
     147           2 :     CPLFree(ret);
     148             : 
     149           4 :     return bOK;
     150             : }
     151             : 
     152             : //! @endcond

Generated by: LCOV version 1.14