Coverage Report

Created: 2026-04-10 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/gdalalg_vector_clip.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL
4
 * Purpose:  "clip" step of "vector pipeline", or "gdal vector clip" standalone
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_vector_clip.h"
14
15
#include "gdal_priv.h"
16
#include "gdal_utils.h"
17
#include "ogrsf_frmts.h"
18
19
#include <algorithm>
20
21
//! @cond Doxygen_Suppress
22
23
#ifndef _
24
0
#define _(x) (x)
25
#endif
26
27
/************************************************************************/
28
/*          GDALVectorClipAlgorithm::GDALVectorClipAlgorithm()          */
29
/************************************************************************/
30
31
GDALVectorClipAlgorithm::GDALVectorClipAlgorithm(bool standaloneStep)
32
0
    : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
33
0
                                      standaloneStep)
34
0
{
35
0
    AddActiveLayerArg(&m_activeLayer);
36
0
    AddBBOXArg(&m_bbox, _("Clipping bounding box as xmin,ymin,xmax,ymax"))
37
0
        .SetMutualExclusionGroup("bbox-geometry-like");
38
0
    AddArg("bbox-crs", 0, _("CRS of clipping bounding box"), &m_bboxCrs)
39
0
        .SetIsCRSArg()
40
0
        .AddHiddenAlias("bbox_srs");
41
0
    AddArg("geometry", 0, _("Clipping geometry (WKT or GeoJSON)"), &m_geometry)
42
0
        .SetMutualExclusionGroup("bbox-geometry-like");
43
0
    AddArg("geometry-crs", 0, _("CRS of clipping geometry"), &m_geometryCrs)
44
0
        .SetIsCRSArg()
45
0
        .AddHiddenAlias("geometry_srs");
46
0
    AddArg("like", 0, _("Dataset to use as a template for bounds"),
47
0
           &m_likeDataset, GDAL_OF_RASTER | GDAL_OF_VECTOR)
48
0
        .SetMetaVar("DATASET")
49
0
        .SetMutualExclusionGroup("bbox-geometry-like");
50
0
    AddArg("like-sql", 0, ("SELECT statement to run on the 'like' dataset"),
51
0
           &m_likeSQL)
52
0
        .SetMetaVar("SELECT-STATEMENT")
53
0
        .SetMutualExclusionGroup("sql-where");
54
0
    AddArg("like-layer", 0, ("Name of the layer of the 'like' dataset"),
55
0
           &m_likeLayer)
56
0
        .SetMetaVar("LAYER-NAME");
57
0
    AddArg("like-where", 0, ("WHERE SQL clause to run on the 'like' dataset"),
58
0
           &m_likeWhere)
59
0
        .SetMetaVar("WHERE-EXPRESSION")
60
0
        .SetMutualExclusionGroup("sql-where");
61
0
}
62
63
/************************************************************************/
64
/*                     GDALVectorClipAlgorithmLayer                     */
65
/************************************************************************/
66
67
namespace
68
{
69
class GDALVectorClipAlgorithmLayer final : public GDALVectorPipelineOutputLayer
70
{
71
  public:
72
    GDALVectorClipAlgorithmLayer(OGRLayer &oSrcLayer,
73
                                 std::unique_ptr<OGRGeometry> poClipGeom)
74
0
        : GDALVectorPipelineOutputLayer(oSrcLayer),
75
0
          m_poClipGeom(std::move(poClipGeom)),
76
0
          m_eSrcLayerGeomType(oSrcLayer.GetGeomType()),
77
0
          m_eFlattenSrcLayerGeomType(wkbFlatten(m_eSrcLayerGeomType)),
78
0
          m_bSrcLayerGeomTypeIsCollection(OGR_GT_IsSubClassOf(
79
0
              m_eFlattenSrcLayerGeomType, wkbGeometryCollection)),
80
0
          m_poFeatureDefn(oSrcLayer.GetLayerDefn()->Clone())
81
0
    {
82
0
        SetDescription(oSrcLayer.GetDescription());
83
0
        SetMetadata(oSrcLayer.GetMetadata());
84
0
        oSrcLayer.SetSpatialFilter(m_poClipGeom.get());
85
0
    }
86
87
    const OGRFeatureDefn *GetLayerDefn() const override
88
0
    {
89
0
        return m_poFeatureDefn.get();
90
0
    }
91
92
    void TranslateFeature(
93
        std::unique_ptr<OGRFeature> poSrcFeature,
94
        std::vector<std::unique_ptr<OGRFeature>> &apoOutFeatures) override
95
0
    {
96
0
        std::unique_ptr<OGRGeometry> poIntersection;
97
0
        auto poGeom = poSrcFeature->GetGeometryRef();
98
99
0
        if (poGeom == nullptr)
100
0
            return;
101
102
0
        poIntersection.reset(poGeom->Intersection(m_poClipGeom.get()));
103
0
        if (!poIntersection)
104
0
            return;
105
0
        poIntersection->assignSpatialReference(
106
0
            m_poFeatureDefn->GetGeomFieldDefn(0)->GetSpatialRef());
107
108
0
        poSrcFeature->SetFDefnUnsafe(m_poFeatureDefn.get());
109
110
0
        const auto eSrcGeomType = wkbFlatten(poGeom->getGeometryType());
111
0
        const auto eFeatGeomType =
112
0
            wkbFlatten(poIntersection->getGeometryType());
113
0
        if (eFeatGeomType != eSrcGeomType &&
114
0
            m_eFlattenSrcLayerGeomType != wkbUnknown &&
115
0
            m_eFlattenSrcLayerGeomType != eFeatGeomType)
116
0
        {
117
            // If the intersection is a collection of geometry and the
118
            // layer geometry type is of non-collection type, create
119
            // one feature per element of the collection.
120
0
            if (!m_bSrcLayerGeomTypeIsCollection &&
121
0
                OGR_GT_IsSubClassOf(eFeatGeomType, wkbGeometryCollection))
122
0
            {
123
0
                auto poGeomColl = std::unique_ptr<OGRGeometryCollection>(
124
0
                    poIntersection.release()->toGeometryCollection());
125
0
                for (const auto *poSubGeom : poGeomColl.get())
126
0
                {
127
0
                    auto poDstFeature =
128
0
                        std::unique_ptr<OGRFeature>(poSrcFeature->Clone());
129
0
                    poDstFeature->SetGeometry(poSubGeom);
130
0
                    apoOutFeatures.push_back(std::move(poDstFeature));
131
0
                }
132
0
            }
133
0
            else if (OGR_GT_GetCollection(eFeatGeomType) ==
134
0
                     m_eFlattenSrcLayerGeomType)
135
0
            {
136
0
                poIntersection = OGRGeometryFactory::forceTo(
137
0
                    std::move(poIntersection), m_eSrcLayerGeomType);
138
0
                poSrcFeature->SetGeometry(std::move(poIntersection));
139
0
                apoOutFeatures.push_back(std::move(poSrcFeature));
140
0
            }
141
0
            else if (m_eFlattenSrcLayerGeomType == wkbGeometryCollection)
142
0
            {
143
0
                auto poGeomColl = std::make_unique<OGRGeometryCollection>();
144
0
                poGeomColl->addGeometry(std::move(poIntersection));
145
0
                poSrcFeature->SetGeometry(std::move(poGeomColl));
146
0
                apoOutFeatures.push_back(std::move(poSrcFeature));
147
0
            }
148
            // else discard geometries of incompatible type with the
149
            // layer geometry type
150
0
        }
151
0
        else
152
0
        {
153
0
            poSrcFeature->SetGeometry(std::move(poIntersection));
154
0
            apoOutFeatures.push_back(std::move(poSrcFeature));
155
0
        }
156
0
    }
157
158
    int TestCapability(const char *pszCap) const override
159
0
    {
160
0
        if (EQUAL(pszCap, OLCStringsAsUTF8) ||
161
0
            EQUAL(pszCap, OLCCurveGeometries) || EQUAL(pszCap, OLCZGeometries))
162
0
            return m_srcLayer.TestCapability(pszCap);
163
0
        return false;
164
0
    }
165
166
  private:
167
    std::unique_ptr<OGRGeometry> const m_poClipGeom{};
168
    const OGRwkbGeometryType m_eSrcLayerGeomType;
169
    const OGRwkbGeometryType m_eFlattenSrcLayerGeomType;
170
    const bool m_bSrcLayerGeomTypeIsCollection;
171
    const OGRFeatureDefnRefCountedPtr m_poFeatureDefn;
172
173
    CPL_DISALLOW_COPY_ASSIGN(GDALVectorClipAlgorithmLayer)
174
};
175
176
}  // namespace
177
178
/************************************************************************/
179
/*                  GDALVectorClipAlgorithm::RunStep()                  */
180
/************************************************************************/
181
182
bool GDALVectorClipAlgorithm::RunStep(GDALPipelineStepRunContext &)
183
0
{
184
0
    auto poSrcDS = m_inputDataset[0].GetDatasetRef();
185
0
    CPLAssert(poSrcDS);
186
187
0
    CPLAssert(m_outputDataset.GetName().empty());
188
0
    CPLAssert(!m_outputDataset.GetDatasetRef());
189
190
0
    const int nLayerCount = poSrcDS->GetLayerCount();
191
0
    bool bSrcLayerHasSRS = false;
192
0
    for (int i = 0; i < nLayerCount; ++i)
193
0
    {
194
0
        auto poSrcLayer = poSrcDS->GetLayer(i);
195
0
        if (poSrcLayer &&
196
0
            (m_activeLayer.empty() ||
197
0
             m_activeLayer == poSrcLayer->GetDescription()) &&
198
0
            poSrcLayer->GetSpatialRef())
199
0
        {
200
0
            bSrcLayerHasSRS = true;
201
0
            break;
202
0
        }
203
0
    }
204
205
0
    auto [poClipGeom, errMsg] = GetClipGeometry();
206
0
    if (!poClipGeom)
207
0
    {
208
0
        ReportError(CE_Failure, CPLE_AppDefined, "%s", errMsg.c_str());
209
0
        return false;
210
0
    }
211
212
0
    auto poLikeDS = m_likeDataset.GetDatasetRef();
213
0
    if (bSrcLayerHasSRS && !poClipGeom->getSpatialReference() && poLikeDS &&
214
0
        poLikeDS->GetLayerCount() == 0)
215
0
    {
216
0
        ReportError(CE_Warning, CPLE_AppDefined,
217
0
                    "Dataset '%s' has no CRS. Assuming its CRS is the "
218
0
                    "same as the input vector.",
219
0
                    poLikeDS->GetDescription());
220
0
    }
221
222
0
    auto outDS = std::make_unique<GDALVectorPipelineOutputDataset>(*poSrcDS);
223
224
0
    bool ret = true;
225
0
    for (int i = 0; ret && i < nLayerCount; ++i)
226
0
    {
227
0
        auto poSrcLayer = poSrcDS->GetLayer(i);
228
0
        ret = (poSrcLayer != nullptr);
229
0
        if (ret)
230
0
        {
231
0
            if (m_activeLayer.empty() ||
232
0
                m_activeLayer == poSrcLayer->GetDescription())
233
0
            {
234
0
                auto poClipGeomForLayer =
235
0
                    std::unique_ptr<OGRGeometry>(poClipGeom->clone());
236
0
                if (poClipGeomForLayer->getSpatialReference() &&
237
0
                    poSrcLayer->GetSpatialRef())
238
0
                {
239
0
                    ret = poClipGeomForLayer->transformTo(
240
0
                              poSrcLayer->GetSpatialRef()) == OGRERR_NONE;
241
0
                }
242
0
                if (ret)
243
0
                {
244
0
                    outDS->AddLayer(
245
0
                        *poSrcLayer,
246
0
                        std::make_unique<GDALVectorClipAlgorithmLayer>(
247
0
                            *poSrcLayer, std::move(poClipGeomForLayer)));
248
0
                }
249
0
            }
250
0
            else
251
0
            {
252
0
                outDS->AddLayer(
253
0
                    *poSrcLayer,
254
0
                    std::make_unique<GDALVectorPipelinePassthroughLayer>(
255
0
                        *poSrcLayer));
256
0
            }
257
0
        }
258
0
    }
259
260
0
    if (ret)
261
0
        m_outputDataset.Set(std::move(outDS));
262
263
0
    return ret;
264
0
}
265
266
0
GDALVectorClipAlgorithmStandalone::~GDALVectorClipAlgorithmStandalone() =
267
    default;
268
269
//! @endcond