28
28
# test that we can write a small file with a custom layer name.
29
29
30
30
31
- def test_hfa_write_desc ():
31
+ def test_hfa_write_desc (tmp_path ):
32
+
33
+ img_path = tmp_path / "test_desc.img"
32
34
33
35
src_ds = gdal .Open ("data/byte.tif" )
34
36
35
- new_ds = gdal .GetDriverByName ("HFA" ).CreateCopy ("tmp/test_desc.img" , src_ds )
37
+ new_ds = gdal .GetDriverByName ("HFA" ).CreateCopy (img_path , src_ds )
36
38
37
39
bnd = new_ds .GetRasterBand (1 )
38
40
bnd .SetDescription ("CustomBandName" )
@@ -41,88 +43,69 @@ def test_hfa_write_desc():
41
43
src_ds = None
42
44
new_ds = None
43
45
44
- new_ds = gdal .Open ("tmp/test_desc.img" )
46
+ new_ds = gdal .Open (img_path )
45
47
bnd = new_ds .GetRasterBand (1 )
46
48
assert bnd .GetDescription () == "CustomBandName" , "Didn't get custom band name."
47
49
48
- bnd = None
49
- new_ds = None
50
-
51
- gdal .GetDriverByName ("HFA" ).Delete ("tmp/test_desc.img" )
52
-
53
50
54
51
###############################################################################
55
- # test writing 4 bit files.
56
-
57
-
58
- def test_hfa_write_4bit ():
59
- drv = gdal .GetDriverByName ("HFA" )
60
- src_ds = gdal .Open ("data/byte.tif" )
61
- ds = drv .CreateCopy ("tmp/4bit.img" , src_ds , options = ["NBITS=1" ])
62
- ds = None
63
- src_ds = None
64
-
65
- ds = gdal .Open ("tmp/4bit.img" )
66
- cs = ds .GetRasterBand (1 ).Checksum ()
52
+ # test writing n-bit files.
67
53
68
- assert cs == 252 , "Got wrong checksum on 4bit image."
69
54
70
- ds = None
71
-
72
- drv .Delete ("tmp/4bit.img" )
73
-
74
-
75
- ###############################################################################
76
- # test writing 4 bit files compressed.
55
+ @pytest .mark .parametrize (
56
+ "options,expected_cs" ,
57
+ [
58
+ (["NBITS=1" ], 252 ),
59
+ (["NBITS=1" , "COMPRESSED=YES" ], 252 ),
60
+ (["NBITS=2" ], 718 ),
61
+ (["NBITS=2" , "COMPRESSED=YES" ], 718 ),
62
+ (["NBITS=4" ], 2578 ),
63
+ (["NBITS=4" , "COMPRESSED=YES" ], 2578 ),
64
+ ],
65
+ )
66
+ def test_hfa_write_nbits (tmp_path , options , expected_cs ):
77
67
68
+ img_path = tmp_path / "nbits.img"
78
69
79
- def test_hfa_write_4bit_compressed ():
80
70
drv = gdal .GetDriverByName ("HFA" )
81
71
src_ds = gdal .Open ("data/byte.tif" )
82
- ds = drv .CreateCopy ("tmp/4bitc.img" , src_ds , options = [ "NBITS=1" , "COMPRESSED=YES" ] )
72
+ ds = drv .CreateCopy (img_path , src_ds , options = options )
83
73
ds = None
84
74
src_ds = None
85
75
86
- ds = gdal .Open ("tmp/4bitc.img" )
76
+ ds = gdal .Open (img_path )
87
77
cs = ds .GetRasterBand (1 ).Checksum ()
88
-
89
- assert cs == 252 , "Got wrong checksum on 4bit image."
90
-
91
- ds = None
92
-
93
- drv .Delete ("tmp/4bitc.img" )
78
+ assert cs == expected_cs
94
79
95
80
96
81
###############################################################################
97
82
# Test creating a file with a nodata value, and fetching otherwise unread
98
83
# blocks and verifying they are the nodata value. (#2427)
99
84
100
85
101
- def test_hfa_write_nd_invalid ():
86
+ def test_hfa_write_nd_invalid (tmp_path ):
87
+
88
+ img_path = tmp_path / "ndinvalid.img"
102
89
103
90
drv = gdal .GetDriverByName ("HFA" )
104
- ds = drv .Create ("tmp/ndinvalid.img" , 512 , 512 , 1 , gdal .GDT_Byte , [])
91
+ ds = drv .Create (img_path , 512 , 512 , 1 , gdal .GDT_Byte , [])
105
92
ds .GetRasterBand (1 ).SetNoDataValue (200 )
106
93
ds = None
107
94
108
- ds = gdal .Open ("tmp/ndinvalid.img" )
95
+ ds = gdal .Open (img_path )
109
96
cs = ds .GetRasterBand (1 ).Checksum ()
110
97
111
98
assert cs == 29754 , "Got wrong checksum on invalid image."
112
99
113
- ds = None
114
-
115
- drv .Delete ("tmp/ndinvalid.img" )
116
-
117
100
118
101
###############################################################################
119
102
# Test updating .rrd overviews in place (#2524).
120
103
121
104
122
105
def test_hfa_update_overviews (tmp_path ):
123
106
124
- img_path = str ( tmp_path / "small.img" )
125
- rrd_path = str ( tmp_path / "small.rrd" )
107
+ img_path = tmp_path / "small.img"
108
+ rrd_path = tmp_path / "small.rrd"
126
109
127
110
shutil .copyfile ("data/small_ov.img" , img_path )
128
111
shutil .copyfile ("data/small_ov.rrd" , rrd_path )
@@ -131,7 +114,6 @@ def test_hfa_update_overviews(tmp_path):
131
114
result = ds .BuildOverviews (overviewlist = [2 ])
132
115
133
116
assert result == 0 , "BuildOverviews() failed."
134
- ds = None
135
117
136
118
137
119
###############################################################################
@@ -140,8 +122,8 @@ def test_hfa_update_overviews(tmp_path):
140
122
141
123
def test_hfa_clean_external_overviews (tmp_path ):
142
124
143
- img_path = str ( tmp_path / "small.img" )
144
- rrd_path = str ( tmp_path / "small.rrd" )
125
+ img_path = tmp_path / "small.img"
126
+ rrd_path = tmp_path / "small.rrd"
145
127
146
128
shutil .copyfile ("data/small_ov.img" , img_path )
147
129
shutil .copyfile ("data/small_ov.rrd" , rrd_path )
@@ -160,17 +142,18 @@ def test_hfa_clean_external_overviews(tmp_path):
160
142
161
143
assert not os .path .exists (rrd_path )
162
144
163
- gdal .GetDriverByName ("HFA" ).Delete (img_path )
164
-
165
145
166
146
###############################################################################
167
147
# Test writing high frequency data (#2525).
168
148
169
149
170
- def test_hfa_bug_2525 ():
150
+ def test_hfa_bug_2525 (tmp_path ):
151
+
152
+ tmp_filename = tmp_path / "test_hfa"
153
+
171
154
drv = gdal .GetDriverByName ("HFA" )
172
155
ds = drv .Create (
173
- "tmp/test_hfa.img" , 64 , 64 , 1 , gdal .GDT_UInt16 , options = ["COMPRESSED=YES" ]
156
+ tmp_filename , 64 , 64 , 1 , gdal .GDT_UInt16 , options = ["COMPRESSED=YES" ]
174
157
)
175
158
import struct
176
159
@@ -243,40 +226,33 @@ def test_hfa_bug_2525():
243
226
)
244
227
for i in range (64 ):
245
228
ds .GetRasterBand (1 ).WriteRaster (0 , i , 64 , 1 , data )
246
- ds = None
247
-
248
- drv .Delete ("tmp/test_hfa.img" )
229
+ ds .Close ()
249
230
250
231
251
232
###############################################################################
252
233
# Test building external overviews with HFA_USE_RRD=YES
253
234
254
235
255
- def test_hfa_use_rrd ():
236
+ def test_hfa_use_rrd (tmp_path ):
237
+
238
+ tmp_filename = tmp_path / "small.img"
256
239
257
- shutil .copyfile ("data/small_ov.img" , "tmp/small.img" )
240
+ shutil .copyfile ("data/small_ov.img" , tmp_filename )
258
241
259
242
with gdal .config_option ("HFA_USE_RRD" , "YES" ):
260
- ds = gdal .Open ("tmp/small.img" , gdal .GA_Update )
243
+ ds = gdal .Open (tmp_filename , gdal .GA_Update )
261
244
result = ds .BuildOverviews (overviewlist = [2 ])
262
245
263
246
assert result == 0 , "BuildOverviews() failed."
264
247
ds = None
265
248
266
- try :
267
- os .stat ("tmp/small.rrd" )
268
- except OSError :
269
- pytest .fail ("small.rrd not present." )
249
+ assert os .path .exists (tmp_path / "small.rrd" )
270
250
271
- ds = gdal .Open ("tmp/small.img" )
251
+ ds = gdal .Open (tmp_filename )
272
252
assert (
273
253
ds .GetRasterBand (1 ).GetOverview (0 ).Checksum () == 26148
274
254
), "Unexpected checksum."
275
255
276
- ds = None
277
-
278
- gdal .GetDriverByName ("HFA" ).Delete ("tmp/small.img" )
279
-
280
256
281
257
###############################################################################
282
258
# Test fix for #4831
@@ -285,7 +261,7 @@ def test_hfa_use_rrd():
285
261
@pytest .mark .require_driver ("BMP" )
286
262
def test_hfa_update_existing_aux_overviews (tmp_path ):
287
263
288
- tmp_filename = str ( tmp_path / "hfa_update_existing_aux_overviews.bmp" )
264
+ tmp_filename = tmp_path / "hfa_update_existing_aux_overviews.bmp"
289
265
290
266
with gdal .config_option ("USE_RRD" , "YES" ):
291
267
@@ -351,8 +327,6 @@ def test_hfa_update_existing_aux_overviews(tmp_path):
351
327
pytest .fail ()
352
328
ds = None
353
329
354
- gdal .GetDriverByName ("BMP" ).Delete (tmp_filename )
355
-
356
330
357
331
###############################################################################
358
332
# Get the driver, and verify a few things about it.
@@ -436,25 +410,22 @@ def test_hfa_create_compress(filename, checksum, testfunction):
436
410
getattr (ut , testfunction )()
437
411
438
412
439
- def test_hfa_create_compress_big_block ():
440
- src_ds = gdal .GetDriverByName ("MEM" ).Create (
441
- "/vsimem/big_block.img" , 128 , 128 , 1 , gdal .GDT_UInt32
442
- )
413
+ def test_hfa_create_compress_big_block (tmp_vsimem ):
414
+ filename = tmp_vsimem / "test.img"
415
+ src_ds = gdal .GetDriverByName ("MEM" ).Create ("" , 128 , 128 , 1 , gdal .GDT_UInt32 )
443
416
src_ds .GetRasterBand (1 ).Fill (4 * 1000 * 1000 * 1000 )
444
417
src_ds .GetRasterBand (1 ).WriteRaster (0 , 0 , 1 , 1 , struct .pack ("I" , 0 ))
445
418
gdal .GetDriverByName ("HFA" ).CreateCopy (
446
- "/vsimem/big_block.img" , src_ds , options = ["COMPRESS=YES" , "BLOCKSIZE=128" ]
419
+ filename , src_ds , options = ["COMPRESS=YES" , "BLOCKSIZE=128" ]
447
420
)
448
- ds = gdal .Open ("/vsimem/big_block.img" )
421
+ ds = gdal .Open (filename )
449
422
got_data = ds .GetRasterBand (1 ).ReadRaster ()
450
- ds = None
451
- gdal .Unlink ("/vsimem/big_block.img" )
452
423
assert got_data == src_ds .GetRasterBand (1 ).ReadRaster ()
453
424
454
425
455
426
# GCPs go to PAM currently
456
- def test_hfa_create_gcp ():
457
- filename = "/vsimem/ test.img"
427
+ def test_hfa_create_gcp (tmp_vsimem ):
428
+ filename = tmp_vsimem / " test.img"
458
429
ds = gdal .GetDriverByName ("HFA" ).Create (filename , 1 , 1 )
459
430
gcp1 = gdal .GCP ()
460
431
gcp1 .GCPPixel = 0
@@ -465,9 +436,15 @@ def test_hfa_create_gcp():
465
436
sr .ImportFromEPSG (4326 )
466
437
assert ds .SetGCPs ((gcp1 ,), sr .ExportToWkt ()) == gdal .CE_None
467
438
ds = None
468
- ds = gdal .Open (filename )
469
- assert ds .GetGCPCount () == 1
470
- assert ds .GetGCPSpatialRef () is not None
471
- assert len (ds .GetGCPs ()) == 1
472
- ds = None
473
- gdal .GetDriverByName ("HFA" ).Delete (filename )
439
+
440
+ with gdal .Open (filename ) as ds :
441
+ assert ds .GetGCPCount () == 1
442
+ assert ds .GetGCPSpatialRef () is not None
443
+ assert len (ds .GetGCPs ()) == 1
444
+
445
+
446
+ @pytest .mark .require_driver ("L1B" )
447
+ def test_hfa_create_copy_from_ysize_0 (tmp_vsimem ):
448
+ src_ds = gdal .Open ("../gdrivers/data/l1b/n12gac8bit_truncated_ysize_0_1band.l1b" )
449
+ with pytest .raises (Exception , match = "nXSize == 0 || nYSize == 0 not supported" ):
450
+ gdal .GetDriverByName ("HFA" ).CreateCopy (tmp_vsimem / "out.img" , src_ds )
0 commit comments