Skip to content

Commit

Permalink
OGR VRT: fix SrcRegion.clip at OGRVRTLayer level
Browse files Browse the repository at this point in the history
Fixes #11519
  • Loading branch information
rouault committed Dec 24, 2024
1 parent f314d4b commit 47fd333
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
29 changes: 29 additions & 0 deletions autotest/ogr/ogr_vrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,35 @@ def test_ogr_vrt_16(tmp_path):
vrt_ds = None


###############################################################################
# Test SrcRegion.clip


@pytest.mark.require_driver("CSV")
@pytest.mark.require_geos
def test_ogr_vrt_SrcRegion_clip(tmp_path):

f = open(tmp_path / "test.csv", "wb")
f.write("wkt_geom,val1,val2\n".encode("ascii"))
f.write('"LINESTRING (-1 0.5,1.5 0.5)",,\n'.encode("ascii"))
f.close()

vrt_xml = f"""
<OGRVRTDataSource>
<OGRVRTLayer name="test">
<SrcDataSource relativeToVRT="0">{tmp_path}/test.csv</SrcDataSource>
<SrcLayer>test</SrcLayer>
<GeometryField encoding="WKT" field="wkt_geom"/>
<SrcRegion clip="true">POLYGON((0 0,0 1,1 1,1 0,0 0))</SrcRegion>
</OGRVRTLayer>
</OGRVRTDataSource>"""
vrt_ds = ogr.Open(vrt_xml)
vrt_lyr = vrt_ds.GetLayerByName("test")
feat = vrt_lyr.GetNextFeature()
geom = feat.GetGeometryRef()
assert geom.ExportToWkt() == "LINESTRING (0.0 0.5,1.0 0.5)"


###############################################################################
# Test explicit field definitions.

Expand Down
9 changes: 5 additions & 4 deletions ogr/ogrsf_frmts/vrt/ogrvrtlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,10 @@ bool OGRVRTLayer::ParseGeometryField(CPLXMLNode *psNode,
}

// Do we have a SrcRegion?
const char *pszSrcRegion = CPLGetXMLValue(psNode, "SrcRegion", nullptr);
if (pszSrcRegion == nullptr && poProps == apoGeomFieldProps[0])
pszSrcRegion = CPLGetXMLValue(psNodeParent, "SrcRegion", nullptr);
const CPLXMLNode *psSrcRegionNode = CPLGetXMLNode(psNode, "SrcRegion");
if (psSrcRegionNode == nullptr && poProps == apoGeomFieldProps[0])
psSrcRegionNode = CPLGetXMLNode(psNodeParent, "SrcRegion");
const char *pszSrcRegion = CPLGetXMLValue(psSrcRegionNode, "", nullptr);
if (pszSrcRegion != nullptr)
{
OGRGeometryFactory::createFromWkt(pszSrcRegion, nullptr,
Expand All @@ -478,7 +479,7 @@ bool OGRVRTLayer::ParseGeometryField(CPLXMLNode *psNode,
}

poProps->bSrcClip =
CPLTestBool(CPLGetXMLValue(psNode, "SrcRegion.clip", "FALSE"));
CPLTestBool(CPLGetXMLValue(psSrcRegionNode, "clip", "FALSE"));
}

// Set Extent if provided.
Expand Down

0 comments on commit 47fd333

Please sign in to comment.