LCOV - code coverage report
Current view: top level - apps - gdalalg_vector_geom_simplify.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 31 31 100.0 %
Date: 2025-04-16 00:42:22 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  "gdal vector geom simplify"
       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_geom_simplify.h"
      14             : 
      15             : #include "gdal_priv.h"
      16             : #include "ogrsf_frmts.h"
      17             : 
      18             : //! @cond Doxygen_Suppress
      19             : 
      20             : #ifndef _
      21             : #define _(x) (x)
      22             : #endif
      23             : 
      24             : /************************************************************************/
      25             : /*                 GDALVectorGeomSimplifyAlgorithm()                    */
      26             : /************************************************************************/
      27             : 
      28           7 : GDALVectorGeomSimplifyAlgorithm::GDALVectorGeomSimplifyAlgorithm(
      29           7 :     bool standaloneStep)
      30             :     : GDALVectorGeomAbstractAlgorithm(NAME, DESCRIPTION, HELP_URL,
      31           7 :                                       standaloneStep, m_opts)
      32             : {
      33             :     AddArg("tolerance", 0, _("Distance tolerance for simplification."),
      34          14 :            &m_opts.m_tolerance)
      35           7 :         .SetPositional()
      36           7 :         .SetRequired()
      37           7 :         .SetMinValueIncluded(0);
      38           7 : }
      39             : 
      40             : #ifdef HAVE_GEOS
      41             : 
      42             : namespace
      43             : {
      44             : 
      45             : /************************************************************************/
      46             : /*                  GDALVectorGeomSimplifyAlgorithmLayer                */
      47             : /************************************************************************/
      48             : 
      49             : class GDALVectorGeomSimplifyAlgorithmLayer final
      50             :     : public GDALVectorGeomOneToOneAlgorithmLayer<
      51             :           GDALVectorGeomSimplifyAlgorithm>
      52             : {
      53             :   protected:
      54             :     using GDALVectorGeomOneToOneAlgorithmLayer::TranslateFeature;
      55             : 
      56             :     std::unique_ptr<OGRFeature>
      57             :     TranslateFeature(std::unique_ptr<OGRFeature> poSrcFeature) const override;
      58             : 
      59             :   public:
      60           1 :     GDALVectorGeomSimplifyAlgorithmLayer(
      61             :         OGRLayer &oSrcLayer,
      62             :         const GDALVectorGeomSimplifyAlgorithm::Options &opts)
      63           1 :         : GDALVectorGeomOneToOneAlgorithmLayer<GDALVectorGeomSimplifyAlgorithm>(
      64           1 :               oSrcLayer, opts)
      65             :     {
      66           1 :     }
      67             : };
      68             : 
      69             : /************************************************************************/
      70             : /*                          TranslateFeature()                          */
      71             : /************************************************************************/
      72             : 
      73             : std::unique_ptr<OGRFeature>
      74           5 : GDALVectorGeomSimplifyAlgorithmLayer::TranslateFeature(
      75             :     std::unique_ptr<OGRFeature> poSrcFeature) const
      76             : {
      77           5 :     const int nGeomFieldCount = poSrcFeature->GetGeomFieldCount();
      78          10 :     for (int i = 0; i < nGeomFieldCount; ++i)
      79             :     {
      80           5 :         if (IsSelectedGeomField(i))
      81             :         {
      82           5 :             if (auto poGeom = std::unique_ptr<OGRGeometry>(
      83          10 :                     poSrcFeature->StealGeometry(i)))
      84             :             {
      85           6 :                 poGeom.reset(
      86           3 :                     poGeom->SimplifyPreserveTopology(m_opts.m_tolerance));
      87           3 :                 if (poGeom)
      88             :                 {
      89           6 :                     poGeom->assignSpatialReference(m_srcLayer.GetLayerDefn()
      90           3 :                                                        ->GetGeomFieldDefn(i)
      91           6 :                                                        ->GetSpatialRef());
      92           3 :                     poSrcFeature->SetGeomField(i, std::move(poGeom));
      93             :                 }
      94             :             }
      95             :         }
      96             :     }
      97             : 
      98           5 :     return poSrcFeature;
      99             : }
     100             : 
     101             : }  // namespace
     102             : 
     103             : #endif  // HAVE_GEOS
     104             : 
     105             : /************************************************************************/
     106             : /*           GDALVectorGeomSimplifyAlgorithm::CreateAlgLayer()          */
     107             : /************************************************************************/
     108             : 
     109             : std::unique_ptr<OGRLayerWithTranslateFeature>
     110           1 : GDALVectorGeomSimplifyAlgorithm::CreateAlgLayer(
     111             :     [[maybe_unused]] OGRLayer &srcLayer)
     112             : {
     113             : #ifdef HAVE_GEOS
     114           1 :     return std::make_unique<GDALVectorGeomSimplifyAlgorithmLayer>(srcLayer,
     115           1 :                                                                   m_opts);
     116             : #else
     117             :     CPLAssert(false);
     118             :     return nullptr;
     119             : #endif
     120             : }
     121             : 
     122             : /************************************************************************/
     123             : /*                GDALVectorGeomSimplifyAlgorithm::RunStep()            */
     124             : /************************************************************************/
     125             : 
     126           1 : bool GDALVectorGeomSimplifyAlgorithm::RunStep(GDALProgressFunc, void *)
     127             : {
     128             : #ifdef HAVE_GEOS
     129           1 :     return GDALVectorGeomAbstractAlgorithm::RunStep(nullptr, nullptr);
     130             : #else
     131             :     ReportError(CE_Failure, CPLE_NotSupported,
     132             :                 "This algorithm is only supported for builds against GEOS");
     133             :     return false;
     134             : #endif
     135             : }
     136             : 
     137             : //! @endcond

Generated by: LCOV version 1.14