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 8 : GDALMdimInfoAlgorithm::GDALMdimInfoAlgorithm() 30 8 : : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL) 31 : { 32 8 : AddOutputFormatArg(&m_format).SetHidden().SetDefault("json").SetChoices( 33 8 : "json", "text"); 34 8 : AddOpenOptionsArg(&m_openOptions); 35 8 : AddInputFormatsArg(&m_inputFormats) 36 : .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, 37 16 : {GDAL_DCAP_MULTIDIM_RASTER}); 38 8 : AddInputDatasetArg(&m_dataset, GDAL_OF_MULTIDIM_RASTER).AddAlias("dataset"); 39 8 : AddOutputStringArg(&m_output); 40 : AddArg( 41 : "detailed", 0, 42 : _("Most verbose output. Report attribute data types and array values."), 43 8 : &m_detailed); 44 : AddArrayNameArg(&m_array, _("Name of the array, used to restrict the " 45 8 : "output to the specified array.")); 46 : 47 : AddArg("limit", 0, 48 : _("Number of values in each dimension that is used to limit the " 49 : "display of array values."), 50 8 : &m_limit); 51 : { 52 : auto &arg = AddArg("array-option", 0, 53 : _("Option passed to GDALGroup::GetMDArrayNames() to " 54 : "filter reported arrays."), 55 16 : &m_arrayOptions) 56 16 : .SetMetaVar("<KEY>=<VALUE>") 57 8 : .SetPackedValuesAllowed(false); 58 3 : arg.AddValidationAction([this, &arg]() 59 11 : { return ParseAndValidateKeyValue(arg); }); 60 : 61 : arg.SetAutoCompleteFunction( 62 4 : [this](const std::string ¤tValue) 63 : { 64 2 : std::vector<std::string> ret; 65 : 66 2 : if (auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open( 67 2 : m_dataset.GetName().c_str(), GDAL_OF_MULTIDIM_RASTER, 68 4 : nullptr, nullptr, nullptr))) 69 : { 70 1 : if (auto poDriver = poDS->GetDriver()) 71 : { 72 1 : if (const char *pszXML = poDriver->GetMetadataItem( 73 1 : GDAL_DMD_MULTIDIM_ARRAY_OPENOPTIONLIST)) 74 : { 75 1 : AddOptionsSuggestions(pszXML, 0, currentValue, ret); 76 : } 77 : } 78 : } 79 : 80 2 : return ret; 81 8 : }); 82 : } 83 8 : AddArg("stats", 0, _("Read and display image statistics."), &m_stats); 84 : 85 8 : AddStdoutArg(&m_stdout); 86 8 : } 87 : 88 : /************************************************************************/ 89 : /* GDALMdimInfoAlgorithm::RunImpl() */ 90 : /************************************************************************/ 91 : 92 3 : bool GDALMdimInfoAlgorithm::RunImpl(GDALProgressFunc, void *) 93 : { 94 3 : CPLAssert(m_dataset.GetDatasetRef()); 95 : 96 3 : CPLStringList aosOptions; 97 : 98 3 : if (m_stdout) 99 1 : aosOptions.AddString("-stdout"); 100 3 : if (m_detailed) 101 1 : aosOptions.AddString("-detailed"); 102 3 : if (m_stats) 103 1 : aosOptions.AddString("-stats"); 104 3 : if (m_limit > 0) 105 : { 106 1 : aosOptions.AddString("-limit"); 107 1 : aosOptions.AddString(CPLSPrintf("%d", m_limit)); 108 : } 109 3 : if (!m_array.empty()) 110 : { 111 1 : aosOptions.AddString("-array"); 112 1 : aosOptions.AddString(m_array.c_str()); 113 : } 114 4 : for (const std::string &opt : m_arrayOptions) 115 : { 116 1 : aosOptions.AddString("-arrayoption"); 117 1 : aosOptions.AddString(opt.c_str()); 118 : } 119 : 120 3 : GDALDatasetH hDS = GDALDataset::ToHandle(m_dataset.GetDatasetRef()); 121 : GDALMultiDimInfoOptions *psOptions = 122 3 : GDALMultiDimInfoOptionsNew(aosOptions.List(), nullptr); 123 3 : char *ret = GDALMultiDimInfo(hDS, psOptions); 124 3 : GDALMultiDimInfoOptionsFree(psOptions); 125 3 : const bool bOK = ret != nullptr; 126 3 : if (ret && !m_stdout) 127 : { 128 2 : m_output = ret; 129 : } 130 3 : CPLFree(ret); 131 : 132 6 : return bOK; 133 : } 134 : 135 : //! @endcond