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