File: gdalalg_vector_info.cpp

package info (click to toggle)
gdal 3.12.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92,292 kB
  • sloc: cpp: 1,223,498; ansic: 206,434; python: 26,278; java: 6,001; xml: 4,769; sh: 3,855; cs: 2,513; yacc: 1,306; makefile: 214
file content (193 lines) | stat: -rw-r--r-- 6,324 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/******************************************************************************
 *
 * Project:  GDAL
 * Purpose:  gdal "vector info" subcommand
 * Author:   Even Rouault <even dot rouault at spatialys.com>
 *
 ******************************************************************************
 * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
 *
 * SPDX-License-Identifier: MIT
 ****************************************************************************/

#include "gdalalg_vector_info.h"

#include "cpl_conv.h"
#include "gdal_priv.h"
#include "gdal_utils.h"

//! @cond Doxygen_Suppress

#ifndef _
#define _(x) (x)
#endif

/************************************************************************/
/*            GDALVectorInfoAlgorithm::GDALVectorInfoAlgorithm()        */
/************************************************************************/

GDALVectorInfoAlgorithm::GDALVectorInfoAlgorithm(bool standaloneStep)
    : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
                                      ConstructorOptions()
                                          .SetStandaloneStep(standaloneStep)
                                          .SetInputDatasetMaxCount(1)
                                          .SetAddDefaultArguments(false))
{
    AddOutputFormatArg(&m_format).SetChoices("json", "text");
    AddOpenOptionsArg(&m_openOptions).SetHiddenForCLI(!standaloneStep);
    AddInputFormatsArg(&m_inputFormats)
        .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR})
        .SetHiddenForCLI(!standaloneStep);
    auto &datasetArg =
        AddInputDatasetArg(&m_inputDataset, GDAL_OF_VECTOR,
                           /* positionalAndRequired = */ standaloneStep)
            .AddAlias("dataset")
            .SetHiddenForCLI(!standaloneStep);
    auto &layerArg = AddLayerNameArg(&m_layerNames)
                         .SetMutualExclusionGroup("layer-sql")
                         .AddAlias("layer");
    SetAutoCompleteFunctionForLayerName(layerArg, datasetArg);
    auto &argFeature =
        AddArg(
            "features", 0,
            _("List all features (beware of RAM consumption on large layers)"),
            &m_listFeatures)
            .SetMutualExclusionGroup("summary-features");
    AddArg("summary", 0, _("List the layer names and the geometry type"),
           &m_summaryOnly)
        .SetMutualExclusionGroup("summary-features");
    AddArg("limit", 0,
           _("Limit the number of features per layer (implies --features)"),
           &m_limit)
        .SetMinValueIncluded(0)
        .SetMetaVar("FEATURE-COUNT")
        .AddAction([&argFeature]() { argFeature.Set(true); });
    AddArg("sql", 0,
           _("Execute the indicated SQL statement and return the result"),
           &m_sql)
        .SetReadFromFileAtSyntaxAllowed()
        .SetMetaVar("<statement>|@<filename>")
        .SetRemoveSQLCommentsEnabled()
        .SetMutualExclusionGroup("layer-sql");
    AddArg("where", 0,
           _("Attribute query in a restricted form of the queries used in the "
             "SQL WHERE statement"),
           &m_where)
        .SetReadFromFileAtSyntaxAllowed()
        .SetMetaVar("<WHERE>|@<filename>")
        .SetRemoveSQLCommentsEnabled();
    AddArg("dialect", 0, _("SQL dialect"), &m_dialect);
    AddArg(GDAL_ARG_NAME_UPDATE, 0, _("Open the dataset in update mode"),
           &m_update)
        .SetHiddenForCLI(!standaloneStep)
        .AddAction(
            [this]()
            {
                if (m_update)
                {
                    ReportError(CE_Warning, CPLE_AppDefined,
                                "Option 'update' is deprecated since GDAL 3.12 "
                                "and will be removed in GDAL 3.13. Use 'gdal "
                                "vector sql --update' instead.");
                }
            });
    AddOutputStringArg(&m_output);
    AddStdoutArg(&m_stdout);

    AddValidationAction(
        [this]()
        {
            if (!m_sql.empty() && !m_where.empty())
            {
                ReportError(CE_Failure, CPLE_NotSupported,
                            "Option 'sql' and 'where' are mutually exclusive");
                return false;
            }
            return true;
        });
}

/************************************************************************/
/*                  GDALVectorInfoAlgorithm::RunStep()                  */
/************************************************************************/

bool GDALVectorInfoAlgorithm::RunStep(GDALPipelineStepRunContext &)
{
    CPLAssert(m_inputDataset.size() == 1);
    auto poSrcDS = m_inputDataset[0].GetDatasetRef();
    CPLAssert(poSrcDS);

    if (m_format.empty())
        m_format = IsCalledFromCommandLine() ? "text" : "json";

    CPLStringList aosOptions;

    aosOptions.AddString("--cli");

    if (m_format == "json")
    {
        aosOptions.AddString("-json");
    }

    if (m_summaryOnly)
    {
        aosOptions.AddString("-summary");
    }
    else if (m_listFeatures)
    {
        aosOptions.AddString("-features");
    }

    if (!m_sql.empty())
    {
        aosOptions.AddString("-sql");
        aosOptions.AddString(m_sql.c_str());
    }
    if (!m_where.empty())
    {
        aosOptions.AddString("-where");
        aosOptions.AddString(m_where.c_str());
    }
    if (!m_dialect.empty())
    {
        aosOptions.AddString("-dialect");
        aosOptions.AddString(m_dialect.c_str());
    }
    if (m_stdout)
    {
        aosOptions.AddString("-stdout");
    }
    if (m_limit > 0)
    {
        aosOptions.AddString("-limit");
        aosOptions.AddString(std::to_string(m_limit));
    }

    // Must be last, as positional
    aosOptions.AddString("dummy");
    for (const std::string &name : m_layerNames)
        aosOptions.AddString(name.c_str());

    if (m_layerNames.empty())
    {
        aosOptions.AddString("-al");
    }

    GDALVectorInfoOptions *psInfo =
        GDALVectorInfoOptionsNew(aosOptions.List(), nullptr);

    char *ret = GDALVectorInfo(GDALDataset::ToHandle(poSrcDS), psInfo);
    GDALVectorInfoOptionsFree(psInfo);
    if (!ret)
        return false;

    m_output = ret;
    CPLFree(ret);

    return true;
}

GDALVectorInfoAlgorithmStandalone::~GDALVectorInfoAlgorithmStandalone() =
    default;

//! @endcond