forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhfa_write.py
executable file
·466 lines (361 loc) · 14.7 KB
/
hfa_write.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# $Id$
#
# Project: GDAL/OGR Test Suite
# Purpose: Test read/write functionality for Erdas Imagine (.img) HFA driver.
# Author: Frank Warmerdam <[email protected]>
#
###############################################################################
# Copyright (c) 2003, Frank Warmerdam <[email protected]>
# Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################
import sys
from osgeo import gdal
import shutil
sys.path.append( '../pymod' )
import gdaltest
###############################################################################
# test that we can write a small file with a custom layer name.
def hfa_write_desc():
src_ds = gdal.Open( 'data/byte.tif' )
new_ds = gdal.GetDriverByName('HFA').CreateCopy( 'tmp/test_desc.img',
src_ds )
bnd = new_ds.GetRasterBand(1)
bnd.SetDescription( 'CustomBandName' )
bnd = None
src_ds = None
new_ds = None
new_ds = gdal.Open( 'tmp/test_desc.img' )
bnd = new_ds.GetRasterBand(1)
if bnd.GetDescription() != 'CustomBandName':
gdaltest.post_reason( 'Didnt get custom band name.' )
return 'fail'
bnd = None
new_ds = None
gdal.GetDriverByName('HFA').Delete( 'tmp/test_desc.img' )
return 'success'
###############################################################################
# test writing 4 bit files.
def hfa_write_4bit():
drv = gdal.GetDriverByName('HFA')
src_ds = gdal.Open('data/byte.tif')
ds = drv.CreateCopy('tmp/4bit.img', src_ds, options = ['NBITS=1'] )
ds = None
src_ds = None
ds = gdal.Open('tmp/4bit.img')
cs = ds.GetRasterBand(1).Checksum()
if cs != 252:
gdaltest.post_reason( 'Got wrong checksum on 4bit image.' )
print(cs)
return 'fail'
ds = None
drv.Delete( 'tmp/4bit.img' )
return 'success'
###############################################################################
# test writing 4 bit files compressed.
def hfa_write_4bit_compressed():
drv = gdal.GetDriverByName('HFA')
src_ds = gdal.Open('data/byte.tif')
ds = drv.CreateCopy('tmp/4bitc.img', src_ds,
options = ['NBITS=1', 'COMPRESSED=YES'] )
ds = None
src_ds = None
ds = gdal.Open('tmp/4bitc.img')
cs = ds.GetRasterBand(1).Checksum()
if cs != 252:
gdaltest.post_reason( 'Got wrong checksum on 4bit image.' )
print(cs)
return 'fail'
ds = None
drv.Delete( 'tmp/4bitc.img' )
return 'success'
###############################################################################
# Test creating a file with a nodata value, and fetching otherwise unread
# blocks and verifying they are the nodata value. (#2427)
def hfa_write_nd_invalid():
drv = gdal.GetDriverByName('HFA')
ds = drv.Create('tmp/ndinvalid.img', 512, 512, 1, gdal.GDT_Byte, [] )
ds.GetRasterBand(1).SetNoDataValue( 200 )
ds = None
ds = gdal.Open('tmp/ndinvalid.img')
cs = ds.GetRasterBand(1).Checksum()
if cs != 29754:
gdaltest.post_reason( 'Got wrong checksum on invalid image.' )
print(cs)
return 'fail'
ds = None
drv.Delete( 'tmp/ndinvalid.img' )
return 'success'
###############################################################################
# Test updating .rrd overviews in place (#2524).
def hfa_update_overviews():
shutil.copyfile( 'data/small_ov.img', 'tmp/small.img' )
shutil.copyfile( 'data/small_ov.rrd', 'tmp/small.rrd' )
ds = gdal.Open( 'tmp/small.img', gdal.GA_Update )
result = ds.BuildOverviews( overviewlist = [2] )
if result != 0:
print(result)
gdaltest.post_reason( 'BuildOverviews() failed.' )
return 'fail'
ds = None
return 'success'
###############################################################################
# Test cleaning external overviews.
def hfa_clean_external_overviews():
ds = gdal.Open( 'tmp/small.img', gdal.GA_Update )
result = ds.BuildOverviews( overviewlist = [] )
if result != 0:
gdaltest.post_reason( 'BuildOverviews() failed.' )
return 'fail'
if ds.GetRasterBand(1).GetOverviewCount() != 0:
gdaltest.post_reason( 'Overviews still exist.' )
return 'fail'
ds = None
ds = gdal.Open( 'tmp/small.img' )
if ds.GetRasterBand(1).GetOverviewCount() != 0:
gdaltest.post_reason( 'Overviews still exist.' )
return 'fail'
ds = None
try:
fd = open('tmp/small.rrd')
except:
fd = None
if fd is not None:
gdaltest.post_reason( 'small.rrd still present.' )
return 'fail'
fd = None
gdal.GetDriverByName('HFA').Delete( 'tmp/small.img' )
return 'success'
###############################################################################
# Test writing high frequency data (#2525).
def hfa_bug_2525():
drv = gdal.GetDriverByName('HFA')
ds = drv.Create('tmp/test_hfa.img', 64, 64, 1, gdal.GDT_UInt16, options = [ 'COMPRESSED=YES'] )
import struct
data = struct.pack('H' * 64, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535)
for i in range(64):
ds.GetRasterBand(1).WriteRaster( 0, i, 64, 1, data)
ds = None
drv.Delete( 'tmp/test_hfa.img' )
return 'success'
###############################################################################
# Test building external overviews with HFA_USE_RRD=YES
def hfa_use_rrd():
shutil.copyfile( 'data/small_ov.img', 'tmp/small.img' )
old_value = gdal.GetConfigOption('HFA_USE_RRD', 'NO')
gdal.SetConfigOption('HFA_USE_RRD', 'YES')
ds = gdal.Open( 'tmp/small.img', gdal.GA_Update )
result = ds.BuildOverviews( overviewlist = [2] )
gdal.SetConfigOption('HFA_USE_RRD', old_value)
if result != 0:
print(result)
gdaltest.post_reason( 'BuildOverviews() failed.' )
return 'fail'
ds = None
try:
fd = open('tmp/small.rrd')
except:
fd = None
if fd is None:
gdaltest.post_reason( 'small.rrd not present.' )
return 'fail'
fd.close()
ds = gdal.Open('tmp/small.img' )
if ds.GetRasterBand(1).GetOverview(0).Checksum() != 26148:
print(ds.GetRasterBand(1).GetOverview(0).Checksum())
gdaltest.post_reason( 'Unexpected checksum.' )
return 'fail'
ds = None
gdal.GetDriverByName('HFA').Delete( 'tmp/small.img' )
return 'success'
###############################################################################
# Test fix for #4831
def hfa_update_existing_aux_overviews():
gdal.SetConfigOption('USE_RRD', 'YES')
ds = gdal.GetDriverByName('BMP').Create('tmp/hfa_update_existing_aux_overviews.bmp', 100, 100, 1)
ds.GetRasterBand(1).Fill(255)
ds = None
# Create overviews
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
ds.BuildOverviews( 'NEAR', overviewlist = [2, 4])
ds = None
# Save overviews checksum
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
# and regenerate them
ds.BuildOverviews( 'NEAR', overviewlist = [2, 4])
ds = None
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
# Check overviews checksum
new_cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
new_cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
if cs_ovr0 != new_cs_ovr0:
gdaltest.post_reason('fail')
gdal.SetConfigOption('USE_RRD', None)
return 'fail'
if cs_ovr1 != new_cs_ovr1:
gdaltest.post_reason('fail')
gdal.SetConfigOption('USE_RRD', None)
return 'fail'
# and regenerate them twice in a row
ds.BuildOverviews( 'NEAR', overviewlist = [2, 4])
ds.BuildOverviews( 'NEAR', overviewlist = [2, 4])
ds = None
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
# Check overviews checksum
new_cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
new_cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
if cs_ovr0 != new_cs_ovr0:
gdaltest.post_reason('fail')
gdal.SetConfigOption('USE_RRD', None)
return 'fail'
if cs_ovr1 != new_cs_ovr1:
gdaltest.post_reason('fail')
gdal.SetConfigOption('USE_RRD', None)
return 'fail'
# and regenerate them with an extra overview level
ds.BuildOverviews( 'NEAR', overviewlist = [8])
ds = None
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
# Check overviews checksum
new_cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
new_cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
if cs_ovr0 != new_cs_ovr0:
gdaltest.post_reason('fail')
gdal.SetConfigOption('USE_RRD', None)
return 'fail'
if cs_ovr1 != new_cs_ovr1:
gdaltest.post_reason('fail')
gdal.SetConfigOption('USE_RRD', None)
return 'fail'
ds = None
gdal.GetDriverByName('BMP').Delete('tmp/hfa_update_existing_aux_overviews.bmp')
gdal.SetConfigOption('USE_RRD', None)
return 'success'
###############################################################################
# Test writing invalid WKT (#5258)
def hfa_write_invalid_wkt():
# No GEOGCS
ds = gdal.GetDriverByName('HFA').Create('/vsimem/hfa_write_invalid_wkt.img', 1,1)
ds.SetProjection("""PROJCS["NAD27 / UTM zone 11N",
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",-117],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AUTHORITY["EPSG","26711"]]""")
ds = None
# No DATUM in GEOGCS
ds = gdal.GetDriverByName('HFA').Create('/vsimem/hfa_write_invalid_wkt.img', 1,1)
ds.SetProjection("""PROJCS["NAD27 / UTM zone 11N",
GEOGCS["NAD27",
AUTHORITY["EPSG","4267"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",-117],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AUTHORITY["EPSG","26711"]]""")
ds = None
# No SPHEROID in DATUM
ds = gdal.GetDriverByName('HFA').Create('/vsimem/hfa_write_invalid_wkt.img', 1,1)
ds.SetProjection("""PROJCS["NAD27 / UTM zone 11N",
GEOGCS["NAD27",
DATUM["North_American_Datum_1927",
AUTHORITY["EPSG","6267"]],
PRIMEM["Greenwich",0],
UNIT["degree",0.0174532925199433],
AUTHORITY["EPSG","4267"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",-117],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AUTHORITY["EPSG","26711"]]""")
ds = None
gdal.GetDriverByName('HFA').Delete('/vsimem/hfa_write_invalid_wkt.img')
return 'success'
###############################################################################
# Get the driver, and verify a few things about it.
init_list = [ \
('byte.tif', 1, 4672, None),
('int16.tif', 1, 4672, None),
('uint16.tif', 1, 4672, None),
('int32.tif', 1, 4672, None),
('uint32.tif', 1, 4672, None),
('float32.tif', 1, 4672, None),
('float64.tif', 1, 4672, None),
('cfloat32.tif', 1, 5028, None),
('cfloat64.tif', 1, 5028, None),
('utmsmall.tif', 1, 50054, None) ]
gdaltest_list = [ hfa_write_desc,
hfa_write_4bit,
hfa_write_4bit_compressed,
hfa_write_nd_invalid,
hfa_update_overviews,
hfa_clean_external_overviews,
hfa_bug_2525,
hfa_use_rrd,
hfa_update_existing_aux_overviews,
hfa_write_invalid_wkt ]
# full set of tests for normal mode.
for item in init_list:
ut1 = gdaltest.GDALTest( 'HFA', item[0], item[1], item[2] )
if ut1 is None:
print( 'HFA tests skipped' )
gdaltest_list.append( (ut1.testCreateCopy, item[0]) )
gdaltest_list.append( (ut1.testCreate, item[0]) )
gdaltest_list.append( (ut1.testSetGeoTransform, item[0]) )
gdaltest_list.append( (ut1.testSetMetadata, item[0]) )
# Just a few for spill file, and compressed support.
short_list = [ \
('byte.tif', 1, 4672, None),
('uint16.tif', 1, 4672, None),
('float64.tif', 1, 4672, None) ]
for item in short_list:
ut2 = gdaltest.GDALTest( 'HFA', item[0], item[1], item[2],
options = [ 'USE_SPILL=YES' ] )
if ut2 is None:
print( 'HFA tests skipped' )
gdaltest_list.append( (ut2.testCreateCopy, item[0] + ' (spill)') )
gdaltest_list.append( (ut2.testCreate, item[0] + ' (spill)') )
ut2 = gdaltest.GDALTest( 'HFA', item[0], item[1], item[2],
options = [ 'COMPRESS=YES' ] )
if ut2 is None:
print( 'HFA tests skipped' )
# gdaltest_list.append( (ut2.testCreateCopy, item[0] + ' (compressed)') )
gdaltest_list.append( (ut2.testCreate, item[0] + ' (compressed)') )
if __name__ == '__main__':
gdaltest.setup_run( 'hfa_write' )
gdaltest.run_tests( gdaltest_list )
gdaltest.summarize()