@@ -185,17 +185,31 @@ def test_gdal_translate_lib_6(tmp_path):
185
185
# Test oXSizePct and oYSizePct option
186
186
187
187
188
- def test_gdal_translate_lib_7 (tmp_path ):
188
+ def test_gdal_translate_lib_7 (tmp_vsimem ):
189
189
190
- dst_tif = str ( tmp_path / "test7.tif" )
190
+ dst_tif = tmp_vsimem / "test7.tif"
191
191
192
192
ds = gdal .Open ("../gcore/data/byte.tif" )
193
193
ds = gdal .Translate (dst_tif , ds , widthPct = 200.0 , heightPct = 200.0 )
194
194
assert ds is not None
195
195
196
196
assert ds .GetRasterBand (1 ).Checksum () == 18784 , "Bad checksum"
197
197
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
+ )
199
213
200
214
201
215
###############################################################################
@@ -249,14 +263,32 @@ def test_gdal_translate_lib_9(tmp_path):
249
263
250
264
def test_gdal_translate_lib_nodata_uint64 ():
251
265
252
- ds = gdal .Open ("../gcore/data/byte.tif" )
253
266
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
+ )
255
274
assert ds is not None
256
275
257
276
assert ds .GetRasterBand (1 ).GetNoDataValue () == noData , "Bad nodata value"
258
277
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
260
292
261
293
262
294
###############################################################################
@@ -275,6 +307,21 @@ def test_gdal_translate_lib_nodata_int64():
275
307
ds = None
276
308
277
309
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
+
278
325
###############################################################################
279
326
# Test nodata=-inf
280
327
@@ -288,20 +335,29 @@ def test_gdal_translate_lib_nodata_minus_inf():
288
335
289
336
290
337
###############################################################################
291
- # Test srcWin option
338
+ # Test -srcwin option
292
339
293
340
294
- def test_gdal_translate_lib_10 (tmp_path ):
341
+ def test_gdal_translate_lib_10 (tmp_vsimem ):
295
342
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
+ )
301
346
302
347
assert ds .GetRasterBand (1 ).Checksum () == 2 , "Bad checksum"
303
348
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
+ )
305
361
306
362
307
363
###############################################################################
@@ -540,6 +596,76 @@ def test_gdal_translate_lib_scale_0_255_input_range():
540
596
assert ds .ReadRaster () == expected_data
541
597
542
598
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
+
543
669
###############################################################################
544
670
# Test that -projwin with nearest neighbor resampling uses integer source
545
671
# pixel boundaries (#6610)
0 commit comments