@@ -65,129 +65,99 @@ GDALVectorClipAlgorithm::GDALVectorClipAlgorithm(bool standaloneStep)
65
65
66
66
namespace
67
67
{
68
- class GDALVectorClipAlgorithmLayer final
69
- : public OGRLayer,
70
- public OGRGetNextFeatureThroughRaw<GDALVectorClipAlgorithmLayer>
68
+ class GDALVectorClipAlgorithmLayer final : public GDALVectorPipelineOutputLayer
71
69
{
72
-
73
- DEFINE_GET_NEXT_FEATURE_THROUGH_RAW (GDALVectorClipAlgorithmLayer)
74
-
75
70
public:
76
- GDALVectorClipAlgorithmLayer (OGRLayer *poSrcLayer ,
71
+ GDALVectorClipAlgorithmLayer (OGRLayer &oSrcLayer ,
77
72
std::unique_ptr<OGRGeometry> poClipGeom)
78
- : m_poSrcLayer(poSrcLayer), m_poClipGeom(std::move(poClipGeom)),
79
- m_eSrcLayerGeomType (m_poSrcLayer->GetGeomType ()),
73
+ : GDALVectorPipelineOutputLayer(oSrcLayer),
74
+ m_poClipGeom (std::move(poClipGeom)),
75
+ m_eSrcLayerGeomType(oSrcLayer.GetGeomType()),
80
76
m_eFlattenSrcLayerGeomType(wkbFlatten(m_eSrcLayerGeomType)),
81
77
m_bSrcLayerGeomTypeIsCollection(OGR_GT_IsSubClassOf(
82
78
m_eFlattenSrcLayerGeomType, wkbGeometryCollection))
83
79
{
84
- SetDescription (poSrcLayer-> GetDescription ());
85
- poSrcLayer-> SetSpatialFilter (m_poClipGeom.get ());
80
+ SetDescription (oSrcLayer. GetDescription ());
81
+ oSrcLayer. SetSpatialFilter (m_poClipGeom.get ());
86
82
}
87
83
88
84
OGRFeatureDefn *GetLayerDefn () override
89
85
{
90
- return m_poSrcLayer->GetLayerDefn ();
91
- }
92
-
93
- void ResetReading () override
94
- {
95
- m_poSrcLayer->ResetReading ();
96
- m_poSrcFeature.reset ();
97
- m_poCurGeomColl.reset ();
98
- m_idxInCurGeomColl = 0 ;
86
+ return m_srcLayer.GetLayerDefn ();
99
87
}
100
88
101
- OGRFeature *GetNextRawFeature ()
89
+ void TranslateFeature (
90
+ std::unique_ptr<OGRFeature> poSrcFeature,
91
+ std::vector<std::unique_ptr<OGRFeature>> &apoOutFeatures) override
102
92
{
103
- if (m_poSrcFeature && m_poCurGeomColl)
93
+ auto poGeom = poSrcFeature->GetGeometryRef ();
94
+ if (!poGeom)
95
+ return ;
96
+
97
+ auto poIntersection = std::unique_ptr<OGRGeometry>(
98
+ poGeom->Intersection (m_poClipGeom.get ()));
99
+ if (!poIntersection)
100
+ return ;
101
+
102
+ const auto eFeatGeomType =
103
+ wkbFlatten (poIntersection->getGeometryType ());
104
+ if (m_eFlattenSrcLayerGeomType != wkbUnknown &&
105
+ m_eFlattenSrcLayerGeomType != eFeatGeomType)
104
106
{
105
- while (m_idxInCurGeomColl < m_poCurGeomColl->getNumGeometries ())
107
+ // If the intersection is a collection of geometry and the
108
+ // layer geometry type is of non-collection type, create
109
+ // one feature per element of the collection.
110
+ if (!m_bSrcLayerGeomTypeIsCollection &&
111
+ OGR_GT_IsSubClassOf (eFeatGeomType, wkbGeometryCollection))
106
112
{
107
- const auto poGeom =
108
- m_poCurGeomColl->getGeometryRef (m_idxInCurGeomColl);
109
- ++m_idxInCurGeomColl;
110
- if (m_eFlattenSrcLayerGeomType == wkbUnknown ||
111
- m_eFlattenSrcLayerGeomType ==
112
- wkbFlatten (poGeom->getGeometryType ()))
113
+ auto poGeomColl = std::unique_ptr<OGRGeometryCollection>(
114
+ poIntersection.release ()->toGeometryCollection ());
115
+ for (const auto *poSubGeom : poGeomColl.get ())
113
116
{
114
117
auto poDstFeature =
115
- std::unique_ptr<OGRFeature>(m_poSrcFeature ->Clone ());
116
- poDstFeature->SetGeometry (poGeom );
117
- return poDstFeature. release ( );
118
+ std::unique_ptr<OGRFeature>(poSrcFeature ->Clone ());
119
+ poDstFeature->SetGeometry (poSubGeom );
120
+ apoOutFeatures. push_back ( std::move (poDstFeature) );
118
121
}
119
122
}
120
- m_poSrcFeature.reset ();
121
- m_poCurGeomColl.reset ();
122
- m_idxInCurGeomColl = 0 ;
123
- }
124
-
125
- while (auto poFeature =
126
- std::unique_ptr<OGRFeature>(m_poSrcLayer->GetNextFeature ()))
127
- {
128
- auto poGeom = poFeature->GetGeometryRef ();
129
- if (!poGeom)
130
- continue ;
131
-
132
- auto poIntersection = std::unique_ptr<OGRGeometry>(
133
- poGeom->Intersection (m_poClipGeom.get ()));
134
- if (!poIntersection)
135
- continue ;
136
-
137
- const auto eFeatGeomType =
138
- wkbFlatten (poIntersection->getGeometryType ());
139
- if (m_eFlattenSrcLayerGeomType != wkbUnknown &&
140
- m_eFlattenSrcLayerGeomType != eFeatGeomType)
123
+ else if (OGR_GT_GetCollection (eFeatGeomType) ==
124
+ m_eFlattenSrcLayerGeomType)
141
125
{
142
- // If the intersection is a collection of geometry and the
143
- // layer geometry type is of non-collection type, create
144
- // one feature per element of the collection.
145
- if (!m_bSrcLayerGeomTypeIsCollection &&
146
- OGR_GT_IsSubClassOf (eFeatGeomType, wkbGeometryCollection))
147
- {
148
- m_poSrcFeature = std::move (poFeature);
149
- m_poCurGeomColl.reset (
150
- poIntersection.release ()->toGeometryCollection ());
151
- m_idxInCurGeomColl = 0 ;
152
- return GetNextFeature ();
153
- }
154
- else if (OGR_GT_GetCollection (eFeatGeomType) ==
155
- m_eFlattenSrcLayerGeomType)
156
- {
157
- poIntersection.reset (OGRGeometryFactory::forceTo (
158
- poIntersection.release (), m_eSrcLayerGeomType));
159
- poFeature->SetGeometryDirectly (poIntersection.release ());
160
- return poFeature.release ();
161
- }
162
- // else discard geometries of incompatible type with the
163
- // layer geometry type
126
+ poIntersection.reset (OGRGeometryFactory::forceTo (
127
+ poIntersection.release (), m_eSrcLayerGeomType));
128
+ poSrcFeature->SetGeometryDirectly (poIntersection.release ());
129
+ apoOutFeatures.push_back (std::move (poSrcFeature));
164
130
}
165
- else
131
+ else if (m_eFlattenSrcLayerGeomType == wkbGeometryCollection)
166
132
{
167
- poFeature->SetGeometryDirectly (poIntersection.release ());
168
- return poFeature.release ();
133
+ auto poGeomColl = std::make_unique<OGRGeometryCollection>();
134
+ poGeomColl->addGeometry (std::move (poIntersection));
135
+ poSrcFeature->SetGeometryDirectly (poGeomColl.release ());
136
+ apoOutFeatures.push_back (std::move (poSrcFeature));
169
137
}
138
+ // else discard geometries of incompatible type with the
139
+ // layer geometry type
140
+ }
141
+ else
142
+ {
143
+ poSrcFeature->SetGeometryDirectly (poIntersection.release ());
144
+ apoOutFeatures.push_back (std::move (poSrcFeature));
170
145
}
171
- return nullptr ;
172
146
}
173
147
174
148
int TestCapability (const char *pszCap) override
175
149
{
176
150
if (EQUAL (pszCap, OLCStringsAsUTF8) ||
177
151
EQUAL (pszCap, OLCCurveGeometries) || EQUAL (pszCap, OLCZGeometries))
178
- return m_poSrcLayer-> TestCapability (pszCap);
152
+ return m_srcLayer. TestCapability (pszCap);
179
153
return false ;
180
154
}
181
155
182
156
private:
183
- OGRLayer *m_poSrcLayer = nullptr ;
184
157
std::unique_ptr<OGRGeometry> m_poClipGeom{};
185
158
const OGRwkbGeometryType m_eSrcLayerGeomType;
186
159
const OGRwkbGeometryType m_eFlattenSrcLayerGeomType;
187
160
const bool m_bSrcLayerGeomTypeIsCollection;
188
- std::unique_ptr<OGRFeature> m_poSrcFeature{};
189
- std::unique_ptr<OGRGeometryCollection> m_poCurGeomColl{};
190
- int m_idxInCurGeomColl = 0 ;
191
161
192
162
CPL_DISALLOW_COPY_ASSIGN (GDALVectorClipAlgorithmLayer)
193
163
};
@@ -428,8 +398,7 @@ bool GDALVectorClipAlgorithm::RunStep(GDALProgressFunc, void *)
428
398
return false ;
429
399
}
430
400
431
- auto outDS = std::make_unique<GDALVectorPipelineOutputDataset>();
432
- outDS->SetDescription (poSrcDS->GetDescription ());
401
+ auto outDS = std::make_unique<GDALVectorPipelineOutputDataset>(*poSrcDS);
433
402
434
403
bool ret = true ;
435
404
for (int i = 0 ; ret && i < nLayerCount; ++i)
@@ -448,8 +417,10 @@ bool GDALVectorClipAlgorithm::RunStep(GDALProgressFunc, void *)
448
417
}
449
418
if (ret)
450
419
{
451
- outDS->AddLayer (std::make_unique<GDALVectorClipAlgorithmLayer>(
452
- poSrcLayer, std::move (poClipGeomForLayer)));
420
+ outDS->AddLayer (
421
+ *poSrcLayer,
422
+ std::make_unique<GDALVectorClipAlgorithmLayer>(
423
+ *poSrcLayer, std::move (poClipGeomForLayer)));
453
424
}
454
425
}
455
426
}
0 commit comments