/src/gdal/apps/gdalalg_vector_clip.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: "clip" step of "vector pipeline", or "gdal vector clip" standalone |
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_clip.h" |
14 | | |
15 | | #include "gdal_priv.h" |
16 | | #include "gdal_utils.h" |
17 | | #include "ogrsf_frmts.h" |
18 | | |
19 | | #include <algorithm> |
20 | | |
21 | | //! @cond Doxygen_Suppress |
22 | | |
23 | | #ifndef _ |
24 | 0 | #define _(x) (x) |
25 | | #endif |
26 | | |
27 | | /************************************************************************/ |
28 | | /* GDALVectorClipAlgorithm::GDALVectorClipAlgorithm() */ |
29 | | /************************************************************************/ |
30 | | |
31 | | GDALVectorClipAlgorithm::GDALVectorClipAlgorithm(bool standaloneStep) |
32 | 0 | : GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, |
33 | 0 | standaloneStep) |
34 | 0 | { |
35 | 0 | AddActiveLayerArg(&m_activeLayer); |
36 | 0 | AddBBOXArg(&m_bbox, _("Clipping bounding box as xmin,ymin,xmax,ymax")) |
37 | 0 | .SetMutualExclusionGroup("bbox-geometry-like"); |
38 | 0 | AddArg("bbox-crs", 0, _("CRS of clipping bounding box"), &m_bboxCrs) |
39 | 0 | .SetIsCRSArg() |
40 | 0 | .AddHiddenAlias("bbox_srs"); |
41 | 0 | AddArg("geometry", 0, _("Clipping geometry (WKT or GeoJSON)"), &m_geometry) |
42 | 0 | .SetMutualExclusionGroup("bbox-geometry-like"); |
43 | 0 | AddArg("geometry-crs", 0, _("CRS of clipping geometry"), &m_geometryCrs) |
44 | 0 | .SetIsCRSArg() |
45 | 0 | .AddHiddenAlias("geometry_srs"); |
46 | 0 | AddArg("like", 0, _("Dataset to use as a template for bounds"), |
47 | 0 | &m_likeDataset, GDAL_OF_RASTER | GDAL_OF_VECTOR) |
48 | 0 | .SetMetaVar("DATASET") |
49 | 0 | .SetMutualExclusionGroup("bbox-geometry-like"); |
50 | 0 | AddArg("like-sql", 0, ("SELECT statement to run on the 'like' dataset"), |
51 | 0 | &m_likeSQL) |
52 | 0 | .SetMetaVar("SELECT-STATEMENT") |
53 | 0 | .SetMutualExclusionGroup("sql-where"); |
54 | 0 | AddArg("like-layer", 0, ("Name of the layer of the 'like' dataset"), |
55 | 0 | &m_likeLayer) |
56 | 0 | .SetMetaVar("LAYER-NAME"); |
57 | 0 | AddArg("like-where", 0, ("WHERE SQL clause to run on the 'like' dataset"), |
58 | 0 | &m_likeWhere) |
59 | 0 | .SetMetaVar("WHERE-EXPRESSION") |
60 | 0 | .SetMutualExclusionGroup("sql-where"); |
61 | 0 | } |
62 | | |
63 | | /************************************************************************/ |
64 | | /* GDALVectorClipAlgorithmLayer */ |
65 | | /************************************************************************/ |
66 | | |
67 | | namespace |
68 | | { |
69 | | class GDALVectorClipAlgorithmLayer final : public GDALVectorPipelineOutputLayer |
70 | | { |
71 | | public: |
72 | | GDALVectorClipAlgorithmLayer(OGRLayer &oSrcLayer, |
73 | | std::unique_ptr<OGRGeometry> poClipGeom) |
74 | 0 | : GDALVectorPipelineOutputLayer(oSrcLayer), |
75 | 0 | m_poClipGeom(std::move(poClipGeom)), |
76 | 0 | m_eSrcLayerGeomType(oSrcLayer.GetGeomType()), |
77 | 0 | m_eFlattenSrcLayerGeomType(wkbFlatten(m_eSrcLayerGeomType)), |
78 | 0 | m_bSrcLayerGeomTypeIsCollection(OGR_GT_IsSubClassOf( |
79 | 0 | m_eFlattenSrcLayerGeomType, wkbGeometryCollection)), |
80 | 0 | m_poFeatureDefn(oSrcLayer.GetLayerDefn()->Clone()) |
81 | 0 | { |
82 | 0 | SetDescription(oSrcLayer.GetDescription()); |
83 | 0 | SetMetadata(oSrcLayer.GetMetadata()); |
84 | 0 | oSrcLayer.SetSpatialFilter(m_poClipGeom.get()); |
85 | 0 | m_poFeatureDefn->Reference(); |
86 | 0 | } |
87 | | |
88 | | ~GDALVectorClipAlgorithmLayer() override |
89 | 0 | { |
90 | 0 | m_poFeatureDefn->Release(); |
91 | 0 | } |
92 | | |
93 | | const OGRFeatureDefn *GetLayerDefn() const override |
94 | 0 | { |
95 | 0 | return m_poFeatureDefn; |
96 | 0 | } |
97 | | |
98 | | void TranslateFeature( |
99 | | std::unique_ptr<OGRFeature> poSrcFeature, |
100 | | std::vector<std::unique_ptr<OGRFeature>> &apoOutFeatures) override |
101 | 0 | { |
102 | 0 | std::unique_ptr<OGRGeometry> poIntersection; |
103 | 0 | auto poGeom = poSrcFeature->GetGeometryRef(); |
104 | |
|
105 | 0 | if (poGeom == nullptr) |
106 | 0 | return; |
107 | | |
108 | 0 | poIntersection.reset(poGeom->Intersection(m_poClipGeom.get())); |
109 | 0 | if (!poIntersection) |
110 | 0 | return; |
111 | 0 | poIntersection->assignSpatialReference( |
112 | 0 | m_poFeatureDefn->GetGeomFieldDefn(0)->GetSpatialRef()); |
113 | |
|
114 | 0 | poSrcFeature->SetFDefnUnsafe(m_poFeatureDefn); |
115 | |
|
116 | 0 | const auto eSrcGeomType = wkbFlatten(poGeom->getGeometryType()); |
117 | 0 | const auto eFeatGeomType = |
118 | 0 | wkbFlatten(poIntersection->getGeometryType()); |
119 | 0 | if (eFeatGeomType != eSrcGeomType && |
120 | 0 | m_eFlattenSrcLayerGeomType != wkbUnknown && |
121 | 0 | m_eFlattenSrcLayerGeomType != eFeatGeomType) |
122 | 0 | { |
123 | | // If the intersection is a collection of geometry and the |
124 | | // layer geometry type is of non-collection type, create |
125 | | // one feature per element of the collection. |
126 | 0 | if (!m_bSrcLayerGeomTypeIsCollection && |
127 | 0 | OGR_GT_IsSubClassOf(eFeatGeomType, wkbGeometryCollection)) |
128 | 0 | { |
129 | 0 | auto poGeomColl = std::unique_ptr<OGRGeometryCollection>( |
130 | 0 | poIntersection.release()->toGeometryCollection()); |
131 | 0 | for (const auto *poSubGeom : poGeomColl.get()) |
132 | 0 | { |
133 | 0 | auto poDstFeature = |
134 | 0 | std::unique_ptr<OGRFeature>(poSrcFeature->Clone()); |
135 | 0 | poDstFeature->SetGeometry(poSubGeom); |
136 | 0 | apoOutFeatures.push_back(std::move(poDstFeature)); |
137 | 0 | } |
138 | 0 | } |
139 | 0 | else if (OGR_GT_GetCollection(eFeatGeomType) == |
140 | 0 | m_eFlattenSrcLayerGeomType) |
141 | 0 | { |
142 | 0 | poIntersection = OGRGeometryFactory::forceTo( |
143 | 0 | std::move(poIntersection), m_eSrcLayerGeomType); |
144 | 0 | poSrcFeature->SetGeometry(std::move(poIntersection)); |
145 | 0 | apoOutFeatures.push_back(std::move(poSrcFeature)); |
146 | 0 | } |
147 | 0 | else if (m_eFlattenSrcLayerGeomType == wkbGeometryCollection) |
148 | 0 | { |
149 | 0 | auto poGeomColl = std::make_unique<OGRGeometryCollection>(); |
150 | 0 | poGeomColl->addGeometry(std::move(poIntersection)); |
151 | 0 | poSrcFeature->SetGeometry(std::move(poGeomColl)); |
152 | 0 | apoOutFeatures.push_back(std::move(poSrcFeature)); |
153 | 0 | } |
154 | | // else discard geometries of incompatible type with the |
155 | | // layer geometry type |
156 | 0 | } |
157 | 0 | else |
158 | 0 | { |
159 | 0 | poSrcFeature->SetGeometry(std::move(poIntersection)); |
160 | 0 | apoOutFeatures.push_back(std::move(poSrcFeature)); |
161 | 0 | } |
162 | 0 | } |
163 | | |
164 | | int TestCapability(const char *pszCap) const override |
165 | 0 | { |
166 | 0 | if (EQUAL(pszCap, OLCStringsAsUTF8) || |
167 | 0 | EQUAL(pszCap, OLCCurveGeometries) || EQUAL(pszCap, OLCZGeometries)) |
168 | 0 | return m_srcLayer.TestCapability(pszCap); |
169 | 0 | return false; |
170 | 0 | } |
171 | | |
172 | | private: |
173 | | std::unique_ptr<OGRGeometry> const m_poClipGeom{}; |
174 | | const OGRwkbGeometryType m_eSrcLayerGeomType; |
175 | | const OGRwkbGeometryType m_eFlattenSrcLayerGeomType; |
176 | | const bool m_bSrcLayerGeomTypeIsCollection; |
177 | | OGRFeatureDefn *const m_poFeatureDefn; |
178 | | |
179 | | CPL_DISALLOW_COPY_ASSIGN(GDALVectorClipAlgorithmLayer) |
180 | | }; |
181 | | |
182 | | } // namespace |
183 | | |
184 | | /************************************************************************/ |
185 | | /* GDALVectorClipAlgorithm::RunStep() */ |
186 | | /************************************************************************/ |
187 | | |
188 | | bool GDALVectorClipAlgorithm::RunStep(GDALPipelineStepRunContext &) |
189 | 0 | { |
190 | 0 | auto poSrcDS = m_inputDataset[0].GetDatasetRef(); |
191 | 0 | CPLAssert(poSrcDS); |
192 | | |
193 | 0 | CPLAssert(m_outputDataset.GetName().empty()); |
194 | 0 | CPLAssert(!m_outputDataset.GetDatasetRef()); |
195 | | |
196 | 0 | const int nLayerCount = poSrcDS->GetLayerCount(); |
197 | 0 | bool bSrcLayerHasSRS = false; |
198 | 0 | for (int i = 0; i < nLayerCount; ++i) |
199 | 0 | { |
200 | 0 | auto poSrcLayer = poSrcDS->GetLayer(i); |
201 | 0 | if (poSrcLayer && |
202 | 0 | (m_activeLayer.empty() || |
203 | 0 | m_activeLayer == poSrcLayer->GetDescription()) && |
204 | 0 | poSrcLayer->GetSpatialRef()) |
205 | 0 | { |
206 | 0 | bSrcLayerHasSRS = true; |
207 | 0 | break; |
208 | 0 | } |
209 | 0 | } |
210 | |
|
211 | 0 | auto [poClipGeom, errMsg] = GetClipGeometry(); |
212 | 0 | if (!poClipGeom) |
213 | 0 | { |
214 | 0 | ReportError(CE_Failure, CPLE_AppDefined, "%s", errMsg.c_str()); |
215 | 0 | return false; |
216 | 0 | } |
217 | | |
218 | 0 | auto poLikeDS = m_likeDataset.GetDatasetRef(); |
219 | 0 | if (bSrcLayerHasSRS && !poClipGeom->getSpatialReference() && poLikeDS && |
220 | 0 | poLikeDS->GetLayerCount() == 0) |
221 | 0 | { |
222 | 0 | ReportError(CE_Warning, CPLE_AppDefined, |
223 | 0 | "Dataset '%s' has no CRS. Assuming its CRS is the " |
224 | 0 | "same as the input vector.", |
225 | 0 | poLikeDS->GetDescription()); |
226 | 0 | } |
227 | |
|
228 | 0 | auto outDS = std::make_unique<GDALVectorPipelineOutputDataset>(*poSrcDS); |
229 | |
|
230 | 0 | bool ret = true; |
231 | 0 | for (int i = 0; ret && i < nLayerCount; ++i) |
232 | 0 | { |
233 | 0 | auto poSrcLayer = poSrcDS->GetLayer(i); |
234 | 0 | ret = (poSrcLayer != nullptr); |
235 | 0 | if (ret) |
236 | 0 | { |
237 | 0 | if (m_activeLayer.empty() || |
238 | 0 | m_activeLayer == poSrcLayer->GetDescription()) |
239 | 0 | { |
240 | 0 | auto poClipGeomForLayer = |
241 | 0 | std::unique_ptr<OGRGeometry>(poClipGeom->clone()); |
242 | 0 | if (poClipGeomForLayer->getSpatialReference() && |
243 | 0 | poSrcLayer->GetSpatialRef()) |
244 | 0 | { |
245 | 0 | ret = poClipGeomForLayer->transformTo( |
246 | 0 | poSrcLayer->GetSpatialRef()) == OGRERR_NONE; |
247 | 0 | } |
248 | 0 | if (ret) |
249 | 0 | { |
250 | 0 | outDS->AddLayer( |
251 | 0 | *poSrcLayer, |
252 | 0 | std::make_unique<GDALVectorClipAlgorithmLayer>( |
253 | 0 | *poSrcLayer, std::move(poClipGeomForLayer))); |
254 | 0 | } |
255 | 0 | } |
256 | 0 | else |
257 | 0 | { |
258 | 0 | outDS->AddLayer( |
259 | 0 | *poSrcLayer, |
260 | 0 | std::make_unique<GDALVectorPipelinePassthroughLayer>( |
261 | 0 | *poSrcLayer)); |
262 | 0 | } |
263 | 0 | } |
264 | 0 | } |
265 | |
|
266 | 0 | if (ret) |
267 | 0 | m_outputDataset.Set(std::move(outDS)); |
268 | |
|
269 | 0 | return ret; |
270 | 0 | } |
271 | | |
272 | 0 | GDALVectorClipAlgorithmStandalone::~GDALVectorClipAlgorithmStandalone() = |
273 | | default; |
274 | | |
275 | | //! @endcond |