forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gdal_retile.py
executable file
·338 lines (270 loc) · 12.1 KB
/
test_gdal_retile.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
#!/usr/bin/env pytest
###############################################################################
# $Id$
#
# Project: GDAL/OGR Test Suite
# Purpose: gdal_retile.py testing
# Author: Even Rouault <even dot rouault @ spatialys.com>
#
###############################################################################
# Copyright (c) 2010, Even Rouault <even dot rouault at spatialys.com>
#
# 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 shutil
import os
from osgeo import gdal
from osgeo import osr
import test_py_scripts
import pytest
###############################################################################
# Test gdal_retile.py
def test_gdal_retile_1():
script_path = test_py_scripts.get_py_script('gdal_retile')
if script_path is None:
pytest.skip()
try:
os.mkdir('tmp/outretile')
except OSError:
pass
test_py_scripts.run_py_script(script_path, 'gdal_retile', '-v -levels 2 -r bilinear -targetDir tmp/outretile ' + test_py_scripts.get_data_path('gcore') + 'byte.tif')
ds = gdal.Open('tmp/outretile/byte_1_1.tif')
assert ds.GetRasterBand(1).Checksum() == 4672
ds = None
ds = gdal.Open('tmp/outretile/1/byte_1_1.tif')
assert ds.RasterXSize == 10
# if ds.GetRasterBand(1).Checksum() != 1152:
# print(ds.GetRasterBand(1).Checksum())
# return 'fail'
ds = None
ds = gdal.Open('tmp/outretile/2/byte_1_1.tif')
assert ds.RasterXSize == 5
# if ds.GetRasterBand(1).Checksum() != 215:
# print(ds.GetRasterBand(1).Checksum())
# return 'fail'
ds = None
###############################################################################
# Test gdal_retile.py with RGBA dataset
def test_gdal_retile_2():
script_path = test_py_scripts.get_py_script('gdal_retile')
if script_path is None:
pytest.skip()
try:
os.mkdir('tmp/outretile2')
except OSError:
pass
test_py_scripts.run_py_script(script_path, 'gdal_retile', '-v -levels 2 -r bilinear -targetDir tmp/outretile2 ' + test_py_scripts.get_data_path('gcore') + 'rgba.tif')
ds = gdal.Open('tmp/outretile2/2/rgba_1_1.tif')
assert ds.GetRasterBand(1).Checksum() == 35, 'wrong checksum for band 1'
assert ds.GetRasterBand(4).Checksum() == 35, 'wrong checksum for band 4'
ds = None
###############################################################################
# Test gdal_retile.py with input images of different pixel sizes
def test_gdal_retile_3():
script_path = test_py_scripts.get_py_script('gdal_retile')
if script_path is None:
pytest.skip()
drv = gdal.GetDriverByName('GTiff')
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS('WGS84')
wkt = srs.ExportToWkt()
# Create two images to tile together. The images will cover the geographic
# range 0E-30E and 0-60N, split horizontally at 30N. The pixel size in the
# second image will be twice that of the first time. If the make the first
# image black and the second gray, then the result of tiling these two
# together should be gray square stacked on top of a black square.
#
# 60 N ---------------
# | | \
# | 50x50 | \ Image 2
# | | /
# | | /
# 30 N ---------------
# | | \
# | 100x100 | \ Image 1
# | | /
# | | /
# 0 N ---------------
# 0 E 30 E
ds = drv.Create('tmp/in1.tif', 100, 100, 1)
px1_x = 30.0 / ds.RasterXSize
px1_y = 30.0 / ds.RasterYSize
ds.SetProjection(wkt)
ds.SetGeoTransform([0, px1_x, 0, 30, 0, -px1_y])
ds.GetRasterBand(1).Fill(0)
ds = None
ds = drv.Create('tmp/in2.tif', 50, 50, 1)
px2_x = 30.0 / ds.RasterXSize
px2_y = 30.0 / ds.RasterYSize
ds.SetProjection(wkt)
ds.SetGeoTransform([0, px2_x, 0, 60, 0, -px2_y])
ds.GetRasterBand(1).Fill(42)
ds = None
try:
os.mkdir('tmp/outretile3')
except OSError:
pass
test_py_scripts.run_py_script(script_path, 'gdal_retile', '-v -levels 2 -r bilinear -targetDir tmp/outretile3 tmp/in1.tif tmp/in2.tif')
ds = gdal.Open('tmp/outretile3/in1_1_1.tif')
assert ds.GetProjectionRef().find('WGS 84') != -1, \
('Expected WGS 84\nGot : %s' % (ds.GetProjectionRef()))
gt = ds.GetGeoTransform()
expected_gt = [0, px1_x, 0, 60, 0, -px1_y]
for i in range(6):
assert not abs(gt[i] - expected_gt[i] > 1e-5), \
('Expected : %s\nGot : %s' % (expected_gt, gt))
assert ds.RasterXSize == 100 and ds.RasterYSize == 200, \
('Wrong raster dimensions : %d x %d' % (ds.RasterXSize, ds.RasterYSize))
assert ds.RasterCount == 1, ('Wrong raster count : %d ' % (ds.RasterCount))
assert ds.GetRasterBand(1).Checksum() == 38999, 'Wrong checksum'
###############################################################################
# Test gdal_retile.py -overlap
def test_gdal_retile_4():
script_path = test_py_scripts.get_py_script('gdal_retile')
if script_path is None:
pytest.skip()
try:
os.mkdir('tmp/outretile4')
except OSError:
pass
test_py_scripts.run_py_script(script_path, 'gdal_retile', '-v -ps 8 7 -overlap 3 -targetDir tmp/outretile4 ' + test_py_scripts.get_data_path('gcore') + 'byte.tif')
expected_results = [['tmp/outretile4/byte_1_1.tif', 8, 7],
['tmp/outretile4/byte_1_2.tif', 8, 7],
['tmp/outretile4/byte_1_3.tif', 8, 7],
['tmp/outretile4/byte_1_4.tif', 5, 7],
['tmp/outretile4/byte_2_1.tif', 8, 7],
['tmp/outretile4/byte_2_2.tif', 8, 7],
['tmp/outretile4/byte_2_3.tif', 8, 7],
['tmp/outretile4/byte_2_4.tif', 5, 7],
['tmp/outretile4/byte_3_1.tif', 8, 7],
['tmp/outretile4/byte_3_2.tif', 8, 7],
['tmp/outretile4/byte_3_3.tif', 8, 7],
['tmp/outretile4/byte_3_4.tif', 5, 7],
['tmp/outretile4/byte_4_1.tif', 8, 7],
['tmp/outretile4/byte_4_2.tif', 8, 7],
['tmp/outretile4/byte_4_3.tif', 8, 7],
['tmp/outretile4/byte_4_4.tif', 5, 7],
['tmp/outretile4/byte_5_1.tif', 8, 4],
['tmp/outretile4/byte_5_2.tif', 8, 4],
['tmp/outretile4/byte_5_3.tif', 8, 4],
['tmp/outretile4/byte_5_4.tif', 5, 4]]
for (filename, width, height) in expected_results:
ds = gdal.Open(filename)
assert ds.RasterXSize == width, filename
assert ds.RasterYSize == height, filename
ds = None
test_py_scripts.run_py_script(script_path, 'gdal_retile', '-v -levels 1 -ps 8 8 -overlap 4 -targetDir tmp/outretile4 ' + test_py_scripts.get_data_path('gcore') + 'byte.tif')
expected_results = [['tmp/outretile4/byte_1_1.tif', 8, 8],
['tmp/outretile4/byte_1_2.tif', 8, 8],
['tmp/outretile4/byte_1_3.tif', 8, 8],
['tmp/outretile4/byte_1_4.tif', 8, 8],
['tmp/outretile4/byte_2_1.tif', 8, 8],
['tmp/outretile4/byte_2_2.tif', 8, 8],
['tmp/outretile4/byte_2_3.tif', 8, 8],
['tmp/outretile4/byte_2_4.tif', 8, 8],
['tmp/outretile4/byte_3_1.tif', 8, 8],
['tmp/outretile4/byte_3_2.tif', 8, 8],
['tmp/outretile4/byte_3_3.tif', 8, 8],
['tmp/outretile4/byte_3_4.tif', 8, 8],
['tmp/outretile4/byte_4_1.tif', 8, 8],
['tmp/outretile4/byte_4_2.tif', 8, 8],
['tmp/outretile4/byte_4_3.tif', 8, 8],
['tmp/outretile4/byte_4_4.tif', 8, 8],
['tmp/outretile4/1/byte_1_1.tif', 8, 8],
['tmp/outretile4/1/byte_1_2.tif', 6, 8],
['tmp/outretile4/1/byte_2_1.tif', 8, 6],
['tmp/outretile4/1/byte_2_2.tif', 6, 6]]
for (filename, width, height) in expected_results:
ds = gdal.Open(filename)
assert ds.RasterXSize == width, filename
assert ds.RasterYSize == height, filename
ds = None
###############################################################################
# Test gdal_retile.py with input having a NoData value
def test_gdal_retile_5():
np = pytest.importorskip('numpy')
nodata_value = -3.4028234663852886e+38
raster_array = np.array(([0.0, 2.0], [-1.0, nodata_value]))
script_path = test_py_scripts.get_py_script('gdal_retile')
if script_path is None:
pytest.skip()
drv = gdal.GetDriverByName('GTiff')
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS('WGS84')
wkt = srs.ExportToWkt()
ds = drv.Create('tmp/in5.tif', 2, 2, 1, gdal.GDT_Float32)
px1_x = 0.1 / ds.RasterXSize
px1_y = 0.1 / ds.RasterYSize
ds.SetProjection(wkt)
ds.SetGeoTransform([0, px1_x, 0, 30, 0, -px1_y])
raster_band = ds.GetRasterBand(1)
raster_band.SetNoDataValue(nodata_value)
raster_band.WriteArray(raster_array)
raster_band = None
ds = None
try:
os.mkdir('tmp/outretile5')
except OSError:
pass
test_py_scripts.run_py_script(script_path, 'gdal_retile', '-v -targetDir tmp/outretile5 tmp/in5.tif')
ds = gdal.Open('tmp/outretile5/in5_1_1.tif')
raster_band = ds.GetRasterBand(1)
assert raster_band.GetNoDataValue() == nodata_value, \
('Wrong nodata value.\nExpected %f, Got: %f' % (nodata_value, raster_band.GetNoDataValue()))
min_val, max_val = raster_band.ComputeRasterMinMax()
assert max_val, \
('Wrong maximum value.\nExpected 2.0, Got: %f' % max_val)
assert min_val == -1.0, \
('Wrong minimum value.\nExpected -1.0, Got: %f' % min_val)
ds = None
###############################################################################
# Cleanup
def test_gdal_retile_cleanup():
lst = ['tmp/outretile/1/byte_1_1.tif',
'tmp/outretile/2/byte_1_1.tif',
'tmp/outretile/byte_1_1.tif',
'tmp/outretile/1',
'tmp/outretile/2',
'tmp/outretile',
'tmp/outretile2/1/rgba_1_1.tif',
'tmp/outretile2/2/rgba_1_1.tif',
'tmp/outretile2/1',
'tmp/outretile2/2',
'tmp/outretile2/rgba_1_1.tif',
'tmp/outretile2',
'tmp/in1.tif',
'tmp/in2.tif',
'tmp/outretile3/1/in1_1_1.tif',
'tmp/outretile3/2/in1_1_1.tif',
'tmp/outretile3/1',
'tmp/outretile3/2',
'tmp/outretile3/in1_1_1.tif',
'tmp/outretile3',
'tmp/in5.tif']
for filename in lst:
try:
os.remove(filename)
except OSError:
try:
os.rmdir(filename)
except OSError:
pass
shutil.rmtree('tmp/outretile4')
if os.path.exists('tmp/outretile5'):
shutil.rmtree('tmp/outretile5')