Skip to content

Commit b99ab1e

Browse files
committed
gdal vector clip: fix 'SELECT COUNT(*) FROM' on it
1 parent b729e61 commit b99ab1e

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

apps/gdalalg_vector_clip.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,41 @@ class GDALVectorClipAlgorithmLayer final : public GDALVectorPipelineOutputLayer
7676
m_eSrcLayerGeomType(oSrcLayer.GetGeomType()),
7777
m_eFlattenSrcLayerGeomType(wkbFlatten(m_eSrcLayerGeomType)),
7878
m_bSrcLayerGeomTypeIsCollection(OGR_GT_IsSubClassOf(
79-
m_eFlattenSrcLayerGeomType, wkbGeometryCollection))
79+
m_eFlattenSrcLayerGeomType, wkbGeometryCollection)),
80+
m_poFeatureDefn(oSrcLayer.GetLayerDefn()->Clone())
8081
{
8182
SetDescription(oSrcLayer.GetDescription());
8283
SetMetadata(oSrcLayer.GetMetadata());
8384
oSrcLayer.SetSpatialFilter(m_poClipGeom.get());
85+
m_poFeatureDefn->Reference();
86+
}
87+
88+
~GDALVectorClipAlgorithmLayer()
89+
{
90+
m_poFeatureDefn->Release();
8491
}
8592

8693
OGRFeatureDefn *GetLayerDefn() override
8794
{
88-
return m_srcLayer.GetLayerDefn();
95+
return m_poFeatureDefn;
8996
}
9097

9198
void TranslateFeature(
9299
std::unique_ptr<OGRFeature> poSrcFeature,
93100
std::vector<std::unique_ptr<OGRFeature>> &apoOutFeatures) override
94101
{
102+
std::unique_ptr<OGRGeometry> poIntersection;
95103
auto poGeom = poSrcFeature->GetGeometryRef();
96-
if (!poGeom)
97-
return;
98-
99-
auto poIntersection = std::unique_ptr<OGRGeometry>(
100-
poGeom->Intersection(m_poClipGeom.get()));
104+
if (poGeom)
105+
{
106+
poIntersection.reset(poGeom->Intersection(m_poClipGeom.get()));
107+
}
101108
if (!poIntersection)
102109
return;
110+
poIntersection->assignSpatialReference(
111+
m_poFeatureDefn->GetGeomFieldDefn(0)->GetSpatialRef());
112+
113+
poSrcFeature->SetFDefnUnsafe(m_poFeatureDefn);
103114

104115
const auto eFeatGeomType =
105116
wkbFlatten(poIntersection->getGeometryType());
@@ -156,10 +167,11 @@ class GDALVectorClipAlgorithmLayer final : public GDALVectorPipelineOutputLayer
156167
}
157168

158169
private:
159-
std::unique_ptr<OGRGeometry> m_poClipGeom{};
170+
std::unique_ptr<OGRGeometry> const m_poClipGeom{};
160171
const OGRwkbGeometryType m_eSrcLayerGeomType;
161172
const OGRwkbGeometryType m_eFlattenSrcLayerGeomType;
162173
const bool m_bSrcLayerGeomTypeIsCollection;
174+
OGRFeatureDefn *const m_poFeatureDefn;
163175

164176
CPL_DISALLOW_COPY_ASSIGN(GDALVectorClipAlgorithmLayer)
165177
};

autotest/utilities/test_gdalalg_vector_clip.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# SPDX-License-Identifier: MIT
1212
###############################################################################
1313

14+
import gdaltest
1415
import ogrtest
1516
import pytest
1617

@@ -997,3 +998,25 @@ def test_gdalalg_vector_clip_bbox_active_layer():
997998
out_lyr = out_ds.GetLayer(1)
998999
out_f = out_lyr.GetNextFeature()
9991000
ogrtest.check_feature_geometry(out_f, "POLYGON ((10 10,10 11,11 11,10 10))")
1001+
1002+
1003+
@pytest.mark.require_driver("GDALG")
1004+
def test_gdalalg_vector_clip_test_ogrsf(tmp_path):
1005+
1006+
import test_cli_utilities
1007+
1008+
if test_cli_utilities.get_test_ogrsf_path() is None:
1009+
pytest.skip()
1010+
1011+
gdalg_filename = tmp_path / "tmp.gdalg.json"
1012+
open(gdalg_filename, "wb").write(
1013+
b'{"type": "gdal_streamed_alg","command_line": "gdal vector clip ../ogr/data/poly.shp --like ../ogr/data/poly.shp --output-format=stream foo","relative_paths_relative_to_this_file":false}'
1014+
)
1015+
1016+
ret = gdaltest.runexternal(
1017+
test_cli_utilities.get_test_ogrsf_path() + f" -ro {gdalg_filename}"
1018+
)
1019+
1020+
assert "INFO" in ret
1021+
assert "ERROR" not in ret
1022+
assert "FAILURE" not in ret

0 commit comments

Comments
 (0)