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