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