LCOV - code coverage report
Current view: top level - apps - gdalalg_vector_check_coverage.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 59 62 95.2 %
Date: 2025-08-19 18:03:11 Functions: 8 8 100.0 %

          Line data    Source code
       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             : #define _(x) (x)
      25             : #endif
      26             : 
      27             : //! @cond Doxygen_Suppress
      28             : 
      29          27 : GDALVectorCheckCoverageAlgorithm::GDALVectorCheckCoverageAlgorithm(
      30          27 :     bool standaloneStep)
      31             :     : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
      32          27 :                                       standaloneStep)
      33             : {
      34             :     AddArg("include-valid", 0,
      35             :            _("Include valid inputs in output, with empty geometry"),
      36          27 :            &m_includeValid);
      37             : 
      38             :     AddArg("geometry-field", 0, _("Name of geometry field to check"),
      39          27 :            &m_geomField);
      40             : 
      41             :     AddArg("maximum-gap-width", 0, _("Maximum width of a gap to be flagged"),
      42          54 :            &m_maximumGapWidth)
      43          27 :         .SetMinValueIncluded(0);
      44          27 : }
      45             : 
      46             : #if defined HAVE_GEOS &&                                                       \
      47             :     (GEOS_VERSION_MAJOR > 3 ||                                                 \
      48             :      (GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR >= 12))
      49             : 
      50          16 : class GDALVectorCheckCoverageOutputDataset final
      51             :     : public GDALGeosNonStreamingAlgorithmDataset
      52             : {
      53             :   public:
      54           8 :     explicit GDALVectorCheckCoverageOutputDataset(double maximumGapWidth,
      55             :                                                   bool includeValid)
      56           8 :         : m_maximumGapWidth(maximumGapWidth), m_includeValid(includeValid)
      57             :     {
      58           8 :     }
      59             : 
      60             :     ~GDALVectorCheckCoverageOutputDataset() override;
      61             : 
      62          19 :     bool PolygonsOnly() const override
      63             :     {
      64          19 :         return true;
      65             :     }
      66             : 
      67          19 :     bool SkipEmpty() const override
      68             :     {
      69          19 :         return !m_includeValid;
      70             :     }
      71             : 
      72           6 :     bool ProcessGeos() override
      73             :     {
      74             :         // Perform coverage checking
      75           6 :         GEOSGeometry *coll = GEOSGeom_createCollection_r(
      76             :             m_poGeosContext, GEOS_GEOMETRYCOLLECTION, m_apoGeosInputs.data(),
      77           6 :             static_cast<unsigned int>(m_apoGeosInputs.size()));
      78             : 
      79           6 :         if (coll == nullptr)
      80             :         {
      81           0 :             return false;
      82             :         }
      83             : 
      84           6 :         m_apoGeosInputs.clear();
      85             : 
      86             :         int geos_result =
      87           6 :             GEOSCoverageIsValid_r(m_poGeosContext, coll, m_maximumGapWidth,
      88             :                                   &m_poGeosResultAsCollection);
      89           6 :         GEOSGeom_destroy_r(m_poGeosContext, coll);
      90             : 
      91           6 :         CPLDebug("CoverageIsValid", "%d", geos_result);
      92             : 
      93           6 :         return geos_result != 2;
      94             :     }
      95             : 
      96             :   private:
      97             :     const double m_maximumGapWidth;
      98             :     const bool m_includeValid;
      99             : };
     100             : 
     101             : GDALVectorCheckCoverageOutputDataset::~GDALVectorCheckCoverageOutputDataset() =
     102             :     default;
     103             : 
     104           8 : bool GDALVectorCheckCoverageAlgorithm::RunStep(GDALPipelineStepRunContext &)
     105             : {
     106           8 :     auto poSrcDS = m_inputDataset[0].GetDatasetRef();
     107             :     auto poDstDS = std::make_unique<GDALVectorCheckCoverageOutputDataset>(
     108          16 :         m_maximumGapWidth, m_includeValid);
     109             : 
     110           8 :     const bool bSingleLayerOutput = m_inputLayerNames.empty()
     111          10 :                                         ? poSrcDS->GetLayerCount() == 1
     112           2 :                                         : m_inputLayerNames.size() == 1;
     113             : 
     114          15 :     for (auto &&poSrcLayer : poSrcDS->GetLayers())
     115             :     {
     116          11 :         if (m_inputLayerNames.empty() ||
     117           0 :             std::find(m_inputLayerNames.begin(), m_inputLayerNames.end(),
     118          11 :                       poSrcLayer->GetDescription()) != m_inputLayerNames.end())
     119             :         {
     120           9 :             const auto poSrcLayerDefn = poSrcLayer->GetLayerDefn();
     121           9 :             if (poSrcLayerDefn->GetGeomFieldCount() == 0)
     122             :             {
     123           2 :                 if (m_inputLayerNames.empty())
     124           1 :                     continue;
     125           1 :                 ReportError(CE_Failure, CPLE_AppDefined,
     126             :                             "Specified layer '%s' has no geometry field",
     127           1 :                             poSrcLayer->GetDescription());
     128           2 :                 return false;
     129             :             }
     130             : 
     131             :             const int geomFieldIndex =
     132           7 :                 m_geomField.empty()
     133           7 :                     ? 0
     134           2 :                     : poSrcLayerDefn->GetGeomFieldIndex(m_geomField.c_str());
     135             : 
     136           7 :             if (geomFieldIndex == -1)
     137             :             {
     138           1 :                 ReportError(CE_Failure, CPLE_AppDefined,
     139             :                             "Specified geometry field '%s' does not exist in "
     140             :                             "layer '%s'",
     141           1 :                             m_geomField.c_str(), poSrcLayer->GetDescription());
     142           1 :                 return false;
     143             :             }
     144             : 
     145             :             OGRFeatureDefn defn(bSingleLayerOutput
     146             :                                     ? "invalid_edge"
     147          10 :                                     : std::string("invalid_edge_")
     148           2 :                                           .append(poSrcLayer->GetDescription())
     149          10 :                                           .c_str());
     150           6 :             defn.SetGeomType(wkbMultiLineString);
     151          12 :             defn.GetGeomFieldDefn(0)->SetSpatialRef(
     152           6 :                 poSrcLayerDefn->GetGeomFieldDefn(geomFieldIndex)
     153           6 :                     ->GetSpatialRef());
     154             : 
     155           6 :             poDstDS->SetSourceGeometryField(geomFieldIndex);
     156             : 
     157           6 :             if (!poDstDS->AddProcessedLayer(*poSrcLayer, defn))
     158             :             {
     159           0 :                 return false;
     160             :             }
     161             :         }
     162             :     }
     163             : 
     164           6 :     m_outputDataset.Set(std::move(poDstDS));
     165             : 
     166           6 :     return true;
     167             : }
     168             : 
     169             : #else
     170             : 
     171             : bool GDALVectorCheckCoverageAlgorithm::RunStep(GDALPipelineStepRunContext &)
     172             : {
     173             :     ReportError(CE_Failure, CPLE_AppDefined,
     174             :                 "%s requires GDAL to be built against version 3.12 or later of "
     175             :                 "the GEOS library.",
     176             :                 NAME);
     177             :     return false;
     178             : }
     179             : #endif  // HAVE_GEOS
     180             : 
     181             : GDALVectorCheckCoverageAlgorithmStandalone::
     182             :     ~GDALVectorCheckCoverageAlgorithmStandalone() = default;
     183             : 
     184             : //! @endcond

Generated by: LCOV version 1.14