/src/gdal/apps/gdalalg_vector_info.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: gdal "vector info" subcommand |
5 | | * Author: Even Rouault <even dot rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "gdalalg_vector_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 | | /* GDALVectorInfoAlgorithm::GDALVectorInfoAlgorithm() */ |
27 | | /************************************************************************/ |
28 | | |
29 | | GDALVectorInfoAlgorithm::GDALVectorInfoAlgorithm(bool standaloneStep) |
30 | 0 | : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, |
31 | 0 | ConstructorOptions() |
32 | 0 | .SetStandaloneStep(standaloneStep) |
33 | 0 | .SetInputDatasetMaxCount(1) |
34 | 0 | .SetAddDefaultArguments(false)) |
35 | 0 | { |
36 | 0 | AddOutputFormatArg(&m_format).SetChoices("json", "text"); |
37 | 0 | AddOpenOptionsArg(&m_openOptions).SetHiddenForCLI(!standaloneStep); |
38 | 0 | AddInputFormatsArg(&m_inputFormats) |
39 | 0 | .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR}) |
40 | 0 | .SetHiddenForCLI(!standaloneStep); |
41 | |
|
42 | 0 | auto &datasetArg = |
43 | 0 | AddInputDatasetArg(&m_inputDataset, GDAL_OF_VECTOR).AddAlias("dataset"); |
44 | 0 | if (!standaloneStep) |
45 | 0 | datasetArg.SetHidden(); |
46 | 0 | auto &layerArg = AddLayerNameArg(&m_layerNames) |
47 | 0 | .SetMutualExclusionGroup("layer-sql") |
48 | 0 | .AddAlias("layer"); |
49 | 0 | SetAutoCompleteFunctionForLayerName(layerArg, datasetArg); |
50 | 0 | auto &argFeature = |
51 | 0 | AddArg( |
52 | 0 | "features", 0, |
53 | 0 | _("List all features (beware of RAM consumption on large layers)"), |
54 | 0 | &m_listFeatures) |
55 | 0 | .SetMutualExclusionGroup("summary-features"); |
56 | 0 | AddArg("summary", 0, _("List the layer names and the geometry type"), |
57 | 0 | &m_summaryOnly) |
58 | 0 | .SetMutualExclusionGroup("summary-features"); |
59 | 0 | AddArg("limit", 0, |
60 | 0 | _("Limit the number of features per layer (implies --features)"), |
61 | 0 | &m_limit) |
62 | 0 | .SetMinValueIncluded(0) |
63 | 0 | .SetMetaVar("FEATURE-COUNT") |
64 | 0 | .AddAction([&argFeature]() { argFeature.Set(true); }); |
65 | 0 | AddArg("sql", 0, |
66 | 0 | _("Execute the indicated SQL statement and return the result"), |
67 | 0 | &m_sql) |
68 | 0 | .SetReadFromFileAtSyntaxAllowed() |
69 | 0 | .SetMetaVar("<statement>|@<filename>") |
70 | 0 | .SetRemoveSQLCommentsEnabled() |
71 | 0 | .SetMutualExclusionGroup("layer-sql"); |
72 | 0 | AddArg("where", 0, |
73 | 0 | _("Attribute query in a restricted form of the queries used in the " |
74 | 0 | "SQL WHERE statement"), |
75 | 0 | &m_where) |
76 | 0 | .SetReadFromFileAtSyntaxAllowed() |
77 | 0 | .SetMetaVar("<WHERE>|@<filename>") |
78 | 0 | .SetRemoveSQLCommentsEnabled(); |
79 | 0 | AddArg("dialect", 0, _("SQL dialect"), &m_dialect); |
80 | 0 | AddOutputStringArg(&m_output); |
81 | 0 | AddStdoutArg(&m_stdout); |
82 | |
|
83 | 0 | AddValidationAction( |
84 | 0 | [this]() |
85 | 0 | { |
86 | 0 | if (!m_sql.empty() && !m_where.empty()) |
87 | 0 | { |
88 | 0 | ReportError(CE_Failure, CPLE_NotSupported, |
89 | 0 | "Option 'sql' and 'where' are mutually exclusive"); |
90 | 0 | return false; |
91 | 0 | } |
92 | 0 | return true; |
93 | 0 | }); |
94 | 0 | } |
95 | | |
96 | | /************************************************************************/ |
97 | | /* GDALVectorInfoAlgorithm::RunStep() */ |
98 | | /************************************************************************/ |
99 | | |
100 | | bool GDALVectorInfoAlgorithm::RunStep(GDALPipelineStepRunContext &) |
101 | 0 | { |
102 | 0 | CPLAssert(m_inputDataset.size() == 1); |
103 | 0 | auto poSrcDS = m_inputDataset[0].GetDatasetRef(); |
104 | 0 | CPLAssert(poSrcDS); |
105 | | |
106 | 0 | if (m_format.empty()) |
107 | 0 | m_format = IsCalledFromCommandLine() ? "text" : "json"; |
108 | |
|
109 | 0 | CPLStringList aosOptions; |
110 | |
|
111 | 0 | aosOptions.AddString("--cli"); |
112 | |
|
113 | 0 | if (m_format == "json") |
114 | 0 | { |
115 | 0 | aosOptions.AddString("-json"); |
116 | 0 | } |
117 | |
|
118 | 0 | if (m_summaryOnly) |
119 | 0 | { |
120 | 0 | aosOptions.AddString("-summary"); |
121 | 0 | } |
122 | 0 | else if (m_listFeatures) |
123 | 0 | { |
124 | 0 | aosOptions.AddString("-features"); |
125 | 0 | } |
126 | |
|
127 | 0 | if (!m_sql.empty()) |
128 | 0 | { |
129 | 0 | aosOptions.AddString("-sql"); |
130 | 0 | aosOptions.AddString(m_sql.c_str()); |
131 | 0 | } |
132 | 0 | if (!m_where.empty()) |
133 | 0 | { |
134 | 0 | aosOptions.AddString("-where"); |
135 | 0 | aosOptions.AddString(m_where.c_str()); |
136 | 0 | } |
137 | 0 | if (!m_dialect.empty()) |
138 | 0 | { |
139 | 0 | aosOptions.AddString("-dialect"); |
140 | 0 | aosOptions.AddString(m_dialect.c_str()); |
141 | 0 | } |
142 | 0 | if (m_stdout) |
143 | 0 | { |
144 | 0 | aosOptions.AddString("-stdout"); |
145 | 0 | } |
146 | 0 | if (m_limit > 0) |
147 | 0 | { |
148 | 0 | aosOptions.AddString("-limit"); |
149 | 0 | aosOptions.AddString(std::to_string(m_limit)); |
150 | 0 | } |
151 | | |
152 | | // Must be last, as positional |
153 | 0 | aosOptions.AddString("dummy"); |
154 | 0 | for (const std::string &name : m_layerNames) |
155 | 0 | aosOptions.AddString(name.c_str()); |
156 | |
|
157 | 0 | if (m_layerNames.empty()) |
158 | 0 | { |
159 | 0 | aosOptions.AddString("-al"); |
160 | 0 | } |
161 | |
|
162 | 0 | GDALVectorInfoOptions *psInfo = |
163 | 0 | GDALVectorInfoOptionsNew(aosOptions.List(), nullptr); |
164 | |
|
165 | 0 | char *ret = GDALVectorInfo(GDALDataset::ToHandle(poSrcDS), psInfo); |
166 | 0 | GDALVectorInfoOptionsFree(psInfo); |
167 | 0 | if (!ret) |
168 | 0 | return false; |
169 | | |
170 | 0 | m_output = ret; |
171 | 0 | CPLFree(ret); |
172 | |
|
173 | 0 | return true; |
174 | 0 | } |
175 | | |
176 | 0 | GDALVectorInfoAlgorithmStandalone::~GDALVectorInfoAlgorithmStandalone() = |
177 | | default; |
178 | | |
179 | | //! @endcond |