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_check_coverage.cpp
Line
Count
Source
1
/******************************************************************************
2
*
3
 * Project:  GDAL
4
 * Purpose:  "gdal vector check-coverage" subcommand
5
 * Author:   Daniel Baston
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2025, ISciences LLC
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12
13
#include "gdalalg_vector_check_coverage.h"
14
15
#include "cpl_error.h"
16
#include "gdal_priv.h"
17
#include "gdalalg_vector_geom.h"
18
#include "ogr_geometry.h"
19
#include "ogr_geos.h"
20
21
#include <cinttypes>
22
23
#ifndef _
24
0
#define _(x) (x)
25
#endif
26
27
//! @cond Doxygen_Suppress
28
29
GDALVectorCheckCoverageAlgorithm::GDALVectorCheckCoverageAlgorithm(
30
    bool standaloneStep)
31
0
    : GDALVectorPipelineStepAlgorithm(
32
0
          NAME, DESCRIPTION, HELP_URL,
33
0
          ConstructorOptions()
34
0
              .SetStandaloneStep(standaloneStep)
35
0
              .SetNoCreateEmptyLayersArgument(standaloneStep))
36
0
{
37
0
    AddArg("include-valid", 0,
38
0
           _("Include valid inputs in output, with empty geometry"),
39
0
           &m_includeValid);
40
41
0
    AddArg("geometry-field", 0, _("Name of geometry field to check"),
42
0
           &m_geomField);
43
44
0
    AddArg("maximum-gap-width", 0, _("Maximum width of a gap to be flagged"),
45
0
           &m_maximumGapWidth)
46
0
        .SetMinValueIncluded(0);
47
0
}
48
49
#if defined HAVE_GEOS &&                                                       \
50
    (GEOS_VERSION_MAJOR > 3 ||                                                 \
51
     (GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR >= 12))
52
53
class GDALVectorCheckCoverageOutputLayer final
54
    : public GDALGeosNonStreamingAlgorithmLayer
55
{
56
  public:
57
    explicit GDALVectorCheckCoverageOutputLayer(OGRLayer &srcLayer,
58
                                                int geomFieldIndex,
59
                                                const std::string &name,
60
                                                double maximumGapWidth,
61
                                                bool includeValid)
62
        : GDALGeosNonStreamingAlgorithmLayer(srcLayer, geomFieldIndex),
63
          m_defn(OGRFeatureDefnRefCountedPtr::makeInstance(name.c_str())),
64
          m_maximumGapWidth(maximumGapWidth), m_includeValid(includeValid)
65
    {
66
        const OGRFeatureDefn *poSrcLayerDefn = srcLayer.GetLayerDefn();
67
        m_defn->SetGeomType(wkbMultiLineString);
68
        m_defn->GetGeomFieldDefn(0)->SetSpatialRef(
69
            poSrcLayerDefn->GetGeomFieldDefn(geomFieldIndex)->GetSpatialRef());
70
    }
71
72
    const OGRFeatureDefn *GetLayerDefn() const override;
73
74
    int TestCapability(const char *) const override
75
    {
76
        return false;
77
    }
78
79
    bool PolygonsOnly() const override
80
    {
81
        return true;
82
    }
83
84
    bool SkipEmpty() const override
85
    {
86
        return !m_includeValid;
87
    }
88
89
    bool ProcessGeos() override
90
    {
91
        // Perform coverage checking
92
        GEOSGeometry *coll = GEOSGeom_createCollection_r(
93
            m_poGeosContext, GEOS_GEOMETRYCOLLECTION, m_apoGeosInputs.data(),
94
            static_cast<unsigned int>(m_apoGeosInputs.size()));
95
96
        if (coll == nullptr)
97
        {
98
            return false;
99
        }
100
101
        m_apoGeosInputs.clear();
102
103
        int geos_result =
104
            GEOSCoverageIsValid_r(m_poGeosContext, coll, m_maximumGapWidth,
105
                                  &m_poGeosResultAsCollection);
106
        GEOSGeom_destroy_r(m_poGeosContext, coll);
107
108
        CPLDebug("CoverageIsValid", "%d", geos_result);
109
110
        return geos_result != 2;
111
    }
112
113
  private:
114
    const OGRFeatureDefnRefCountedPtr m_defn;
115
    const double m_maximumGapWidth;
116
    const bool m_includeValid;
117
118
    CPL_DISALLOW_COPY_ASSIGN(GDALVectorCheckCoverageOutputLayer)
119
};
120
121
const OGRFeatureDefn *GDALVectorCheckCoverageOutputLayer::GetLayerDefn() const
122
{
123
    return m_defn.get();
124
}
125
126
bool GDALVectorCheckCoverageAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
127
{
128
    auto poSrcDS = m_inputDataset[0].GetDatasetRef();
129
    auto poDstDS =
130
        std::make_unique<GDALVectorNonStreamingAlgorithmDataset>(*poSrcDS);
131
132
    const bool bSingleLayerOutput = m_inputLayerNames.empty()
133
                                        ? poSrcDS->GetLayerCount() == 1
134
                                        : m_inputLayerNames.size() == 1;
135
136
    GDALVectorAlgorithmLayerProgressHelper progressHelper(ctxt);
137
138
    for (auto &&poSrcLayer : poSrcDS->GetLayers())
139
    {
140
        if (m_inputLayerNames.empty() ||
141
            std::find(m_inputLayerNames.begin(), m_inputLayerNames.end(),
142
                      poSrcLayer->GetDescription()) != m_inputLayerNames.end())
143
        {
144
            const auto poSrcLayerDefn = poSrcLayer->GetLayerDefn();
145
            if (poSrcLayerDefn->GetGeomFieldCount() == 0)
146
            {
147
                if (m_inputLayerNames.empty())
148
                    continue;
149
                ReportError(CE_Failure, CPLE_AppDefined,
150
                            "Specified layer '%s' has no geometry field",
151
                            poSrcLayer->GetDescription());
152
                return false;
153
            }
154
155
            progressHelper.AddProcessedLayer(*poSrcLayer);
156
        }
157
    }
158
159
    for ([[maybe_unused]] auto [poSrcLayer, bProcessed, layerProgressFunc,
160
                                layerProgressData] : progressHelper)
161
    {
162
        const auto poSrcLayerDefn = poSrcLayer->GetLayerDefn();
163
        const int geomFieldIndex =
164
            m_geomField.empty()
165
                ? 0
166
                : poSrcLayerDefn->GetGeomFieldIndex(m_geomField.c_str());
167
168
        if (geomFieldIndex == -1)
169
        {
170
            ReportError(CE_Failure, CPLE_AppDefined,
171
                        "Specified geometry field '%s' does not exist in "
172
                        "layer '%s'",
173
                        m_geomField.c_str(), poSrcLayer->GetDescription());
174
            return false;
175
        }
176
177
        std::string layerName = bSingleLayerOutput
178
                                    ? "invalid_edge"
179
                                    : std::string("invalid_edge_")
180
                                          .append(poSrcLayer->GetDescription());
181
182
        auto poLayer = std::make_unique<GDALVectorCheckCoverageOutputLayer>(
183
            *poSrcLayer, geomFieldIndex, layerName, m_maximumGapWidth,
184
            m_includeValid);
185
186
        if (!poDstDS->AddProcessedLayer(std::move(poLayer), layerProgressFunc,
187
                                        layerProgressData.get()))
188
        {
189
            return false;
190
        }
191
    }
192
193
    m_outputDataset.Set(std::move(poDstDS));
194
195
    return true;
196
}
197
198
#else
199
200
bool GDALVectorCheckCoverageAlgorithm::RunStep(GDALPipelineStepRunContext &)
201
0
{
202
0
    ReportError(CE_Failure, CPLE_AppDefined,
203
0
                "%s requires GDAL to be built against version 3.12 or later of "
204
0
                "the GEOS library.",
205
0
                NAME);
206
0
    return false;
207
0
}
208
#endif  // HAVE_GEOS
209
210
GDALVectorCheckCoverageAlgorithmStandalone::
211
0
    ~GDALVectorCheckCoverageAlgorithmStandalone() = default;
212
213
//! @endcond