Skip to content

Commit 4786d20

Browse files
authored
autotest: add tests for gdal_translate error cases (#12130)
1 parent 4061ad3 commit 4786d20

File tree

3 files changed

+146
-18
lines changed

3 files changed

+146
-18
lines changed

apps/gdal_translate_lib.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
975975
}
976976
else
977977
{
978-
CPLError(CE_None, CPLE_None,
978+
CPLError(CE_Warning, CPLE_None,
979979
"-projwin_srs ignored since the dataset has no "
980980
"projection.");
981981
}
@@ -1095,6 +1095,8 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
10951095
const std::string osFormat = GetOutputDriverForRaster(pszDest);
10961096
if (osFormat.empty())
10971097
{
1098+
CPLError(CE_Failure, CPLE_AppDefined,
1099+
"Could not identify an output driver for %s", pszDest);
10981100
GDALTranslateOptionsFree(psOptions);
10991101
return nullptr;
11001102
}
@@ -1313,7 +1315,7 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
13131315
fabs(adfSrcGeoTransform[5]) +
13141316
0.5;
13151317
if (dfOXSize < 1 || !GDALIsValueInRange<int>(dfOXSize) ||
1316-
dfOYSize < 1 || !GDALIsValueInRange<int>(dfOXSize))
1318+
dfOYSize < 1 || !GDALIsValueInRange<int>(dfOYSize))
13171319
{
13181320
CPLError(CE_Failure, CPLE_IllegalArg,
13191321
"Invalid output size: %g x %g", dfOXSize, dfOYSize);
@@ -1328,7 +1330,7 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
13281330
double dfOXSize = ceil(psOptions->adfSrcWin[2] - 0.001);
13291331
double dfOYSize = ceil(psOptions->adfSrcWin[3] - 0.001);
13301332
if (dfOXSize < 1 || !GDALIsValueInRange<int>(dfOXSize) ||
1331-
dfOYSize < 1 || !GDALIsValueInRange<int>(dfOXSize))
1333+
dfOYSize < 1 || !GDALIsValueInRange<int>(dfOYSize))
13321334
{
13331335
CPLError(CE_Failure, CPLE_IllegalArg,
13341336
"Invalid output size: %g x %g", dfOXSize, dfOYSize);

autotest/pymod/gdaltest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,7 @@ def handler(lvl, no, msg):
21312131

21322132
assert any(
21332133
[err["level"] == type and match in err["message"] for err in errors]
2134-
), f'Did not receive an error of type {err_levels[type]} matching "{match}"'
2134+
), f'Did not receive an error of type {err_levels[type]} matching "{match}". Received: {[(err["level"], err["message"]) for err in errors]}'
21352135

21362136

21372137
###############################################################################

autotest/utilities/test_gdal_translate_lib.py

+140-14
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,31 @@ def test_gdal_translate_lib_6(tmp_path):
185185
# Test oXSizePct and oYSizePct option
186186

187187

188-
def test_gdal_translate_lib_7(tmp_path):
188+
def test_gdal_translate_lib_7(tmp_vsimem):
189189

190-
dst_tif = str(tmp_path / "test7.tif")
190+
dst_tif = tmp_vsimem / "test7.tif"
191191

192192
ds = gdal.Open("../gcore/data/byte.tif")
193193
ds = gdal.Translate(dst_tif, ds, widthPct=200.0, heightPct=200.0)
194194
assert ds is not None
195195

196196
assert ds.GetRasterBand(1).Checksum() == 18784, "Bad checksum"
197197

198-
ds = None
198+
199+
def test_gdal_translate_lib_7_error(tmp_vsimem):
200+
201+
with pytest.raises(Exception, match="Invalid output width"):
202+
gdal.Translate(
203+
tmp_vsimem / "out.tif",
204+
"../gcore/data/byte.tif",
205+
widthPct=1,
206+
heightPct=200.0,
207+
)
208+
209+
with pytest.raises(Exception, match="Invalid output height"):
210+
gdal.Translate(
211+
tmp_vsimem / "out.tif", "../gcore/data/byte.tif", widthPct=200, heightPct=1
212+
)
199213

200214

201215
###############################################################################
@@ -249,14 +263,32 @@ def test_gdal_translate_lib_9(tmp_path):
249263

250264
def test_gdal_translate_lib_nodata_uint64():
251265

252-
ds = gdal.Open("../gcore/data/byte.tif")
253266
noData = (1 << 64) - 1
254-
ds = gdal.Translate("", ds, format="MEM", outputType=gdal.GDT_UInt64, noData=noData)
267+
ds = gdal.Translate(
268+
"",
269+
"../gcore/data/byte.tif",
270+
format="MEM",
271+
outputType=gdal.GDT_UInt64,
272+
noData=noData,
273+
)
255274
assert ds is not None
256275

257276
assert ds.GetRasterBand(1).GetNoDataValue() == noData, "Bad nodata value"
258277

259-
ds = None
278+
279+
@pytest.mark.parametrize("nodata", (1 << 65, 3.2))
280+
def test_gdal_translate_lib_nodata_uint64_invalid(nodata):
281+
282+
with gdaltest.error_raised(gdal.CE_Warning, "Cannot set nodata value"):
283+
ds = gdal.Translate(
284+
"",
285+
"../gcore/data/byte.tif",
286+
format="MEM",
287+
outputType=gdal.GDT_Int64,
288+
noData=nodata,
289+
)
290+
assert ds is not None
291+
assert ds.GetRasterBand(1).GetNoDataValue() is None
260292

261293

262294
###############################################################################
@@ -275,6 +307,21 @@ def test_gdal_translate_lib_nodata_int64():
275307
ds = None
276308

277309

310+
@pytest.mark.parametrize("nodata", (1 << 65, 3.2))
311+
def test_gdal_translate_lib_nodata_int64_invalid(nodata):
312+
313+
with gdaltest.error_raised(gdal.CE_Warning, "Cannot set nodata value"):
314+
ds = gdal.Translate(
315+
"",
316+
"../gcore/data/byte.tif",
317+
format="MEM",
318+
outputType=gdal.GDT_Int64,
319+
noData=nodata,
320+
)
321+
assert ds is not None
322+
assert ds.GetRasterBand(1).GetNoDataValue() is None
323+
324+
278325
###############################################################################
279326
# Test nodata=-inf
280327

@@ -288,20 +335,29 @@ def test_gdal_translate_lib_nodata_minus_inf():
288335

289336

290337
###############################################################################
291-
# Test srcWin option
338+
# Test -srcwin option
292339

293340

294-
def test_gdal_translate_lib_10(tmp_path):
341+
def test_gdal_translate_lib_10(tmp_vsimem):
295342

296-
dst_tif = str(tmp_path / "test10.tif")
297-
298-
ds = gdal.Open("../gcore/data/byte.tif")
299-
ds = gdal.Translate(dst_tif, ds, srcWin=[0, 0, 1, 1])
300-
assert ds is not None
343+
ds = gdal.Translate(
344+
tmp_vsimem / "out.tif", "../gcore/data/byte.tif", srcWin=(0, 0, 1, 1)
345+
)
301346

302347
assert ds.GetRasterBand(1).Checksum() == 2, "Bad checksum"
303348

304-
ds = None
349+
350+
def test_gdal_translate_lib_srcwin_invalid(tmp_vsimem):
351+
352+
with pytest.raises(Exception, match="Invalid output size"):
353+
gdal.Translate(
354+
tmp_vsimem / "out.tif", "../gcore/data/byte.tif", srcWin=(0, 0, 2 << 33, 1)
355+
)
356+
357+
with pytest.raises(Exception, match="Invalid output size"):
358+
gdal.Translate(
359+
tmp_vsimem / "out.tif", "../gcore/data/byte.tif", srcWin=(0, 0, 1, 2 << 33)
360+
)
305361

306362

307363
###############################################################################
@@ -540,6 +596,76 @@ def test_gdal_translate_lib_scale_0_255_input_range():
540596
assert ds.ReadRaster() == expected_data
541597

542598

599+
###############################################################################
600+
# Test error cases of -projwin
601+
602+
603+
def test_gdal_translate_lib_projwin_rotated():
604+
605+
with pytest.raises(Exception, match="not supported"):
606+
607+
gdal.Translate(
608+
"",
609+
"../gcore/data/geomatrix.tif",
610+
format="VRT",
611+
projWin=[1840936, 1143965, 1840999, 1143922],
612+
)
613+
614+
615+
def test_gdal_translate_lib_projwin_srs_no_source_srs():
616+
617+
src_ds = gdal.GetDriverByName("MEM").Create("", 10, 10)
618+
src_ds.SetGeoTransform((0, 1, 0, 10, 0, -1))
619+
620+
with gdaltest.error_raised(gdal.CE_Warning, "projwin_srs ignored"):
621+
gdal.Translate(
622+
"", src_ds, format="VRT", projWin=[2, 4, 4, 2], projWinSRS="EPSG:4326"
623+
)
624+
625+
626+
def test_gdal_translate_lib_srcwin_negative():
627+
628+
with pytest.raises(Exception, match="negative width and/or height"):
629+
630+
gdal.Translate(
631+
"",
632+
"../gcore/data/byte.tif",
633+
format="VRT",
634+
projWin=[440720.000, 3751320.000, 441920.000, 3751320.000],
635+
)
636+
637+
638+
def test_gdal_translate_lib_cannot_identify_format(tmp_vsimem):
639+
640+
with pytest.raises(Exception, match="Could not identify an output driver"):
641+
642+
gdal.Translate(tmp_vsimem / "out.txt", "../gcore/data/byte.tif")
643+
644+
645+
def test_gdal_translate_lib_invalid_format(tmp_vsimem):
646+
647+
with pytest.raises(Exception, match="Output driver .* not recognised"):
648+
649+
gdal.Translate(
650+
tmp_vsimem / "out.txt", "../gcore/data/byte.tif", format="JPEG3000"
651+
)
652+
653+
654+
def test_gdal_translate_lib_no_raster_capabilites(tmp_vsimem):
655+
656+
with pytest.raises(Exception, match="no raster capabilities"):
657+
gdal.Translate(
658+
tmp_vsimem / "byte.shp", "../gcore/data/byte.tif", format="ESRI Shapefile"
659+
)
660+
661+
662+
@pytest.mark.require_driver("DOQ1")
663+
def test_gdal_translate_lib_no_creation_capabilites(tmp_vsimem):
664+
665+
with pytest.raises(Exception, match="no creation capabilities"):
666+
gdal.Translate(tmp_vsimem / "byte.doq", "../gcore/data/byte.tif", format="DOQ1")
667+
668+
543669
###############################################################################
544670
# Test that -projwin with nearest neighbor resampling uses integer source
545671
# pixel boundaries (#6610)

0 commit comments

Comments
 (0)