File: gdalalg_vector_layer_algebra.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 (419 lines) | stat: -rw-r--r-- 14,984 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/******************************************************************************
 *
 * Project:  GDAL
 * Purpose:  gdal "vector layer-algebra" subcommand
 * Author:   Even Rouault <even dot rouault at spatialys.com>
 *
 ******************************************************************************
 * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
 *
 * SPDX-License-Identifier: MIT
 ****************************************************************************/

#include "gdalalg_vector_layer_algebra.h"

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

#include <algorithm>

//! @cond Doxygen_Suppress

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

/************************************************************************/
/*                 GDALVectorLayerAlgebraAlgorithm()                    */
/************************************************************************/

GDALVectorLayerAlgebraAlgorithm::GDALVectorLayerAlgebraAlgorithm()
    : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
{
    AddProgressArg();

    AddArg("operation", 0, _("Operation to perform"), &m_operation)
        .SetChoices("union", "intersection", "sym-difference", "identity",
                    "update", "clip", "erase")
        .SetRequired()
        .SetPositional();

    AddOutputFormatArg(&m_format).AddMetadataItem(
        GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR, GDAL_DCAP_CREATE});
    AddOpenOptionsArg(&m_openOptions);
    AddInputFormatsArg(&m_inputFormats)
        .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR});
    AddInputDatasetArg(&m_inputDataset, GDAL_OF_VECTOR);

    {
        auto &arg = AddArg("method", 0, _("Method vector dataset"),
                           &m_methodDataset, GDAL_OF_VECTOR)
                        .SetPositional()
                        .SetRequired();

        SetAutoCompleteFunctionForFilename(arg, GDAL_OF_VECTOR);
    }
    AddOutputDatasetArg(&m_outputDataset, GDAL_OF_VECTOR)
        .SetDatasetInputFlags(GADV_NAME | GADV_OBJECT);
    AddCreationOptionsArg(&m_creationOptions);
    AddLayerCreationOptionsArg(&m_layerCreationOptions);
    AddOverwriteArg(&m_overwrite);
    AddUpdateArg(&m_update);
    AddOverwriteLayerArg(&m_overwriteLayer);
    AddAppendLayerArg(&m_appendLayer);

    AddArg(GDAL_ARG_NAME_INPUT_LAYER, 0, _("Input layer name"),
           &m_inputLayerName);
    AddArg("method-layer", 0, _("Method layer name"), &m_methodLayerName);
    AddOutputLayerNameArg(&m_outputLayerName)
        .AddHiddenAlias("nln");  // For ogr2ogr nostalgic people

    AddGeometryTypeArg(&m_geometryType);

    AddArg("input-prefix", 0,
           _("Prefix for fields corresponding to input layer"), &m_inputPrefix)
        .SetCategory(GAAC_ADVANCED);
    AddArg("input-field", 0, _("Input field(s) to add to output layer"),
           &m_inputFields)
        .SetCategory(GAAC_ADVANCED)
        .SetMutualExclusionGroup("input-field");
    AddArg("no-input-field", 0, _("Do not add any input field to output layer"),
           &m_noInputFields)
        .SetCategory(GAAC_ADVANCED)
        .SetMutualExclusionGroup("input-field");
    AddArg("all-input-field", 0, _("Add all input fields to output layer"),
           &m_allInputFields)
        .SetCategory(GAAC_ADVANCED)
        .SetMutualExclusionGroup("input-field");

    AddArg("method-prefix", 0,
           _("Prefix for fields corresponding to method layer"),
           &m_methodPrefix)
        .SetCategory(GAAC_ADVANCED);
    AddArg("method-field", 0, _("Method field(s) to add to output layer"),
           &m_methodFields)
        .SetCategory(GAAC_ADVANCED)
        .SetMutualExclusionGroup("method-field");
    AddArg("no-method-field", 0,
           _("Do not add any method field to output layer"), &m_noMethodFields)
        .SetCategory(GAAC_ADVANCED)
        .SetMutualExclusionGroup("method-field");
    AddArg("all-method-field", 0, _("Add all method fields to output layer"),
           &m_allMethodFields)
        .SetCategory(GAAC_ADVANCED)
        .SetMutualExclusionGroup("method-field");
}

/************************************************************************/
/*               GDALVectorLayerAlgebraAlgorithm::RunImpl()             */
/************************************************************************/

bool GDALVectorLayerAlgebraAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
                                              void *pProgressData)
{
#ifdef HAVE_GEOS
    auto poSrcDS = m_inputDataset.GetDatasetRef();
    CPLAssert(poSrcDS);
    auto poMethodDS = m_methodDataset.GetDatasetRef();
    CPLAssert(poMethodDS);

    if (poSrcDS == poMethodDS)
    {
        ReportError(CE_Failure, CPLE_NotSupported,
                    "Input and method datasets must be different");
        return false;
    }

    auto poDstDS = m_outputDataset.GetDatasetRef();
    std::unique_ptr<GDALDataset> poDstDSUniquePtr;
    const bool bNewDataset = poDstDS == nullptr;
    if (poDstDS == nullptr)
    {
        if (m_format.empty())
        {
            const CPLStringList aosFormats(GDALGetOutputDriversForDatasetName(
                m_outputDataset.GetName().c_str(), GDAL_OF_VECTOR,
                /* bSingleMatch = */ true,
                /* bEmitWarning = */ true));
            if (aosFormats.size() != 1)
            {
                ReportError(CE_Failure, CPLE_AppDefined,
                            "Cannot guess driver for %s",
                            m_outputDataset.GetName().c_str());
                return false;
            }
            m_format = aosFormats[0];
        }

        auto poOutDrv =
            GetGDALDriverManager()->GetDriverByName(m_format.c_str());
        if (!poOutDrv)
        {
            // shouldn't happen given checks done in GDALAlgorithm unless
            // someone deregister the driver between ParseCommandLineArgs() and
            // Run()
            ReportError(CE_Failure, CPLE_AppDefined, "Driver %s does not exist",
                        m_format.c_str());
            return false;
        }

        const CPLStringList aosCreationOptions(m_creationOptions);
        poDstDSUniquePtr.reset(
            poOutDrv->Create(m_outputDataset.GetName().c_str(), 0, 0, 0,
                             GDT_Unknown, aosCreationOptions.List()));
        poDstDS = poDstDSUniquePtr.get();
        if (!poDstDS)
            return false;
    }

    OGRLayer *poDstLayer = nullptr;

    if (m_outputLayerName.empty())
    {
        if (bNewDataset)
        {
            auto poOutDrv = poDstDS->GetDriver();
            if (poOutDrv && EQUAL(poOutDrv->GetDescription(), "ESRI Shapefile"))
                m_outputLayerName =
                    CPLGetBasenameSafe(m_outputDataset.GetName().c_str());
            else
                m_outputLayerName = "output";
        }
        else if (m_appendLayer)
        {
            if (poDstDS->GetLayerCount() == 1)
                poDstLayer = poDstDS->GetLayer(0);
            else
            {
                ReportError(CE_Failure, CPLE_AppDefined,
                            "--output-layer should be specified");
                return false;
            }
        }
        else if (m_overwriteLayer)
        {
            if (poDstDS->GetLayerCount() == 1)
            {
                if (poDstDS->DeleteLayer(0) != OGRERR_NONE)
                {
                    return false;
                }
            }
            else
            {
                ReportError(CE_Failure, CPLE_AppDefined,
                            "--output-layer should be specified");
                return false;
            }
        }
        else
        {
            ReportError(CE_Failure, CPLE_AppDefined,
                        "--output-layer should be specified");
            return false;
        }
    }
    else if (m_overwriteLayer)
    {
        const int nLayerIdx = poDstDS->GetLayerIndex(m_outputLayerName.c_str());
        if (nLayerIdx < 0)
        {
            ReportError(CE_Failure, CPLE_AppDefined,
                        "Layer '%s' does not exist", m_outputLayerName.c_str());
            return false;
        }
        if (poDstDS->DeleteLayer(nLayerIdx) != OGRERR_NONE)
        {
            return false;
        }
    }
    else if (m_appendLayer)
    {
        poDstLayer = poDstDS->GetLayerByName(m_outputLayerName.c_str());
        if (!poDstLayer)
        {
            ReportError(CE_Failure, CPLE_AppDefined,
                        "Layer '%s' does not exist", m_outputLayerName.c_str());
            return false;
        }
    }

    if (!bNewDataset && m_update && !m_appendLayer && !m_overwriteLayer)
    {
        poDstLayer = poDstDS->GetLayerByName(m_outputLayerName.c_str());
        if (poDstLayer)
        {
            ReportError(CE_Failure, CPLE_AppDefined,
                        "Output layer '%s' already exists. Specify "
                        "--%s, --%s, --%s or "
                        "--%s + --output-layer with a different name",
                        m_outputLayerName.c_str(), GDAL_ARG_NAME_OVERWRITE,
                        GDAL_ARG_NAME_OVERWRITE_LAYER, GDAL_ARG_NAME_APPEND,
                        GDAL_ARG_NAME_UPDATE);
            return false;
        }
    }

    OGRLayer *poInputLayer;
    if (m_inputLayerName.empty() && poSrcDS->GetLayerCount() == 1)
        poInputLayer = poSrcDS->GetLayer(0);
    else
        poInputLayer = poSrcDS->GetLayerByName(m_inputLayerName.c_str());
    if (!poInputLayer)
    {
        ReportError(CE_Failure, CPLE_AppDefined, "Cannot get input layer '%s'",
                    m_inputLayerName.c_str());
        return false;
    }

    OGRLayer *poMethodLayer;
    if (m_methodLayerName.empty() && poMethodDS->GetLayerCount() == 1)
        poMethodLayer = poMethodDS->GetLayer(0);
    else
        poMethodLayer = poMethodDS->GetLayerByName(m_methodLayerName.c_str());
    if (!poMethodLayer)
    {
        ReportError(CE_Failure, CPLE_AppDefined, "Cannot get method layer '%s'",
                    m_methodLayerName.c_str());
        return false;
    }

    if (bNewDataset || !m_appendLayer)
    {
        const CPLStringList aosLayerCreationOptions(m_layerCreationOptions);

        const OGRwkbGeometryType eType =
            !m_geometryType.empty() ? OGRFromOGCGeomType(m_geometryType.c_str())
                                    : poInputLayer->GetGeomType();
        poDstLayer = poDstDS->CreateLayer(m_outputLayerName.c_str(),
                                          poInputLayer->GetSpatialRef(), eType,
                                          aosLayerCreationOptions.List());
    }
    if (!poDstLayer)
        return false;

    CPLStringList aosOptions;

    if (m_inputFields.empty() && !m_noInputFields)
        m_allInputFields = true;

    if (m_methodFields.empty() && !m_noMethodFields && !m_allMethodFields)
    {
        if (m_operation == "update" || m_operation == "clip" ||
            m_operation == "erase")
            m_noMethodFields = true;
        else
            m_allMethodFields = true;
    }

    if (m_noInputFields && m_noMethodFields)
    {
        aosOptions.SetNameValue("ADD_INPUT_FIELDS", "NO");
        aosOptions.SetNameValue("ADD_METHOD_FIELDS", "NO");
    }
    else
    {
        // Copy fields from input or method layer to output layer
        const auto CopyFields =
            [poDstLayer](OGRLayer *poSrcLayer, const std::string &prefix,
                         const std::vector<std::string> &srcFields)
        {
            const auto contains =
                [](const std::vector<std::string> &v, const std::string &s)
            { return std::find(v.begin(), v.end(), s) != v.end(); };

            const auto poOutFDefn = poDstLayer->GetLayerDefn();
            const auto poFDefn = poSrcLayer->GetLayerDefn();
            const int nCount = poFDefn->GetFieldCount();
            for (int i = 0; i < nCount; ++i)
            {
                const auto poSrcFieldDefn = poFDefn->GetFieldDefn(i);
                const char *pszName = poSrcFieldDefn->GetNameRef();
                if (srcFields.empty() || contains(srcFields, pszName))
                {
                    OGRFieldDefn oField(*poSrcFieldDefn);
                    const std::string outName = prefix + pszName;
                    whileUnsealing(&oField)->SetName(outName.c_str());
                    if (poOutFDefn->GetFieldIndex(outName.c_str()) < 0 &&
                        poDstLayer->CreateField(&oField) != OGRERR_NONE)
                    {
                        return false;
                    }
                }
            }
            return true;
        };

        if (!m_noInputFields)
        {
            if (!GetArg("input-prefix")->IsExplicitlySet() &&
                m_inputPrefix.empty() && !m_noMethodFields)
            {
                m_inputPrefix = "input_";
            }
            if (!m_inputPrefix.empty())
            {
                aosOptions.SetNameValue("INPUT_PREFIX", m_inputPrefix.c_str());
            }
            if (!CopyFields(poInputLayer, m_inputPrefix, m_inputFields))
                return false;
        }

        if (!m_noMethodFields)
        {
            if (!GetArg("method-prefix")->IsExplicitlySet() &&
                m_methodPrefix.empty() && !m_noInputFields)
            {
                m_methodPrefix = "method_";
            }
            if (!m_methodPrefix.empty())
            {
                aosOptions.SetNameValue("METHOD_PREFIX",
                                        m_methodPrefix.c_str());
            }
            if (!CopyFields(poMethodLayer, m_methodPrefix, m_methodFields))
                return false;
        }
    }

    if (OGR_GT_IsSubClassOf(poDstLayer->GetGeomType(), wkbGeometryCollection))
    {
        aosOptions.SetNameValue("PROMOTE_TO_MULTI", "YES");
    }

    const std::map<std::string, decltype(&OGRLayer::Union)>
        mapOperationToMethod = {
            {"union", &OGRLayer::Union},
            {"intersection", &OGRLayer::Intersection},
            {"sym-difference", &OGRLayer::SymDifference},
            {"identity", &OGRLayer::Identity},
            {"update", &OGRLayer::Update},
            {"clip", &OGRLayer::Clip},
            {"erase", &OGRLayer::Erase},
        };

    const auto oIter = mapOperationToMethod.find(m_operation);
    CPLAssert(oIter != mapOperationToMethod.end());
    const auto pFunc = oIter->second;
    const bool bOK =
        (poInputLayer->*pFunc)(poMethodLayer, poDstLayer, aosOptions.List(),
                               pfnProgress, pProgressData) == OGRERR_NONE;
    if (bOK && !m_outputDataset.GetDatasetRef())
    {
        m_outputDataset.Set(std::move(poDstDSUniquePtr));
    }

    return bOK;
#else
    (void)pfnProgress;
    (void)pProgressData;
    ReportError(CE_Failure, CPLE_NotSupported,
                "This algorithm is only supported for builds against GEOS");
    return false;
#endif
}

//! @endcond