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