Skip to content

Commit 9fe5fe3

Browse files
committed
gdal raster clip: Fix bbox check with clip geometry
1 parent b7ee802 commit 9fe5fe3

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

apps/gdalalg_raster_clip.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,9 @@ bool GDALRasterClipAlgorithm::RunStep(GDALPipelineStepRunContext &)
252252
poClipGeomInSrcSRS->getEnvelope(&env);
253253
}
254254

255-
if (!m_allowExtentOutsideSource &&
256-
!(env.MinX >= gt[0] &&
257-
env.MaxX <= gt[0] + gt[1] * poSrcDS->GetRasterXSize() &&
258-
env.MaxY >= gt[3] &&
259-
env.MinY <= gt[3] + gt[5] * poSrcDS->GetRasterYSize()))
255+
OGREnvelope rasterEnv;
256+
poSrcDS->GetExtent(&rasterEnv, nullptr);
257+
if (!m_allowExtentOutsideSource && !rasterEnv.Contains(env))
260258
{
261259
ReportError(CE_Failure, CPLE_AppDefined,
262260
"Clipping geometry is partially or totally outside the "

autotest/utilities/test_gdalalg_raster_clip.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ def test_gdalalg_raster_clip_bbox_crs():
169169
)
170170

171171

172+
def test_gdalgalg_raster_clip_geometry(tmp_vsimem):
173+
174+
alg = get_alg()
175+
176+
alg["input"] = "../gcore/data/byte.tif"
177+
alg["output"] = ""
178+
alg["output-format"] = "MEM"
179+
alg[
180+
"geometry"
181+
] = "POLYGON ((440885 3750741, 441344 3750294, 441612 3750501, 441773 3751203, 441545 3751254, 441576 3750847, 441576 3750847, 440885 3750741))"
182+
183+
assert alg.Run()
184+
185+
ds = alg["output"].GetDataset()
186+
assert ds.RasterXSize == 16
187+
assert ds.RasterYSize == 17
188+
assert ds.GetSpatialRef().GetAuthorityCode(None) == "26711"
189+
assert ds.GetGeoTransform() == pytest.approx(
190+
(440840, 60.0, 0.0, 3751260, 0.0, -60.0), rel=1e-8
191+
)
192+
193+
172194
def test_gdalalg_raster_clip_geometry_add_alpha():
173195

174196
src_ds = gdal.Open("../gcore/data/byte.tif")

0 commit comments

Comments
 (0)