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