Skip to content

Commit ce35780

Browse files
authored
Merge pull request OSGeo#12004 from dbaston/gdal-raster-calc-extent-tol
gdal raster calc: use tolerance for extent check
2 parents 1aa852a + f56ccd3 commit ce35780

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

apps/gdalalg_raster_calc.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,11 @@ UpdateSourceProperties(SourceProperties &out, const std::string &dsn,
162162
double ymin =
163163
source.gt[3] + source.nX * source.gt[4] + source.nY * source.gt[5];
164164

165-
// TODO use a tolerance here
166-
if (xmax != xmaxOut || ymin != yminOut)
165+
// Max allowable extent misalignment, expressed as fraction of a pixel
166+
constexpr double EXTENT_RTOL = 1e-3;
167+
168+
if (std::abs(xmax - xmaxOut) > EXTENT_RTOL * std::abs(source.gt[1]) ||
169+
std::abs(ymin - yminOut) > EXTENT_RTOL * std::abs(source.gt[5]))
167170
{
168171
extentMismatch = true;
169172
}

autotest/utilities/test_gdalalg_raster_calc.py

+40
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,46 @@ def test_gdalalg_raster_calc_error_extent_mismatch(calc, tmp_vsimem):
338338
assert src.GetGeoTransform() == dst.GetGeoTransform()
339339

340340

341+
def test_gdalalg_raster_calc_error_extent_within_tolerance(calc, tmp_vsimem):
342+
343+
input_1 = tmp_vsimem / "in1.tif"
344+
input_2 = tmp_vsimem / "in2.tif"
345+
outfile = tmp_vsimem / "out.tif"
346+
347+
with gdal.GetDriverByName("GTIff").Create(input_2, 3600, 3600) as ds:
348+
ds.SetGeoTransform(
349+
(
350+
2.4999861111111112e01,
351+
2.7777777777777778e-04,
352+
0.0000000000000000e00,
353+
8.0000138888888884e01,
354+
0.0000000000000000e00,
355+
-2.7777777777777778e-04,
356+
)
357+
)
358+
with gdal.GetDriverByName("GTiff").Create(input_1, 3600, 3600) as ds:
359+
# this geotransform represents error introduced by writing a dataset with the above
360+
# geotransform to netCDF
361+
ds.SetGeoTransform(
362+
(
363+
2.4999861111111112e01,
364+
2.7777777777777778e-04,
365+
0.0000000000000000e00,
366+
8.0000138888888884e01,
367+
0.0000000000000000e00,
368+
-2.7777777777778173e-04,
369+
)
370+
)
371+
372+
calc["input"] = [f"A={input_1}", f"B={input_2}"]
373+
calc["output"] = outfile
374+
calc["calc"] = ["A+B"]
375+
assert calc.Run()
376+
377+
with gdal.Open(input_1) as src, gdal.Open(outfile) as dst:
378+
assert src.GetGeoTransform() == dst.GetGeoTransform()
379+
380+
341381
def test_gdalalg_raster_calc_error_crs_mismatch(calc, tmp_vsimem):
342382

343383
input_1 = tmp_vsimem / "in1.tif"

0 commit comments

Comments
 (0)