Skip to content

Commit 808a093

Browse files
committed
ComputeRasterMinMaxLocation(): fix on all inf/-inf rasters
1 parent abbb048 commit 808a093

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

autotest/gcore/basic_test.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,12 +1001,31 @@ def test_ComputeMinMaxLocation():
10011001
and ret.maxY == 18
10021002
)
10031003

1004-
ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1, gdal.GDT_Float64)
1004+
1005+
@pytest.mark.parametrize(
1006+
"datatype", [gdal.GDT_Float16, gdal.GDT_Float32, gdal.GDT_Float64]
1007+
)
1008+
def test_ComputeMinMaxLocation_nan(datatype):
1009+
1010+
ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1, datatype)
10051011
ds.GetRasterBand(1).Fill(float("nan"))
10061012
ret = ds.GetRasterBand(1).ComputeMinMaxLocation()
10071013
assert ret is None
10081014

10091015

1016+
@pytest.mark.parametrize("value", [float("inf"), float("-inf")])
1017+
@pytest.mark.parametrize(
1018+
"datatype", [gdal.GDT_Float16, gdal.GDT_Float32, gdal.GDT_Float64]
1019+
)
1020+
def test_ComputeMinMaxLocation_inf(value, datatype):
1021+
1022+
ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1, datatype)
1023+
ds.GetRasterBand(1).Fill(value)
1024+
ret = ds.GetRasterBand(1).ComputeMinMaxLocation()
1025+
assert ret.min == value and ret.max == value
1026+
assert ret.minX == 0 and ret.minY == 0 and ret.maxX == 0 and ret.maxY == 0
1027+
1028+
10101029
def test_create_numpy_types():
10111030
np = pytest.importorskip("numpy")
10121031
gdaltest.importorskip_gdal_array()

gcore/gdalrasterband.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8745,7 +8745,7 @@ CPLErr GDALRasterBand::ComputeRasterMinMaxLocation(double *pdfMin,
87458745
const double dfMinValueBlock =
87468746
GetPixelValue(eDataType, bSignedByte, pData, pos_min,
87478747
sNoDataValues, bValid);
8748-
if (bValid && dfMinValueBlock < dfMin)
8748+
if (bValid && (dfMinValueBlock < dfMin || nMinX < 0))
87498749
{
87508750
dfMin = dfMinValueBlock;
87518751
nMinX = iXBlock * nBlockXSize + nMinXBlock;
@@ -8761,7 +8761,7 @@ CPLErr GDALRasterBand::ComputeRasterMinMaxLocation(double *pdfMin,
87618761
const double dfMaxValueBlock =
87628762
GetPixelValue(eDataType, bSignedByte, pData, pos_max,
87638763
sNoDataValues, bValid);
8764-
if (bValid && dfMaxValueBlock > dfMax)
8764+
if (bValid && (dfMaxValueBlock > dfMax || nMaxX < 0))
87658765
{
87668766
dfMax = dfMaxValueBlock;
87678767
nMaxX = iXBlock * nBlockXSize + nMaxXBlock;

0 commit comments

Comments
 (0)