-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
downsample_integer_factor.py
executable file
·239 lines (201 loc) · 6.18 KB
/
downsample_integer_factor.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
# SPDX-License-Identifier: MIT
# Copyright 2020 Even Rouault
import timeit
from osgeo import gdal
ds = gdal.GetDriverByName("MEM").Create("", 1024 * 10, 1024 * 10)
ds.GetRasterBand(1).Fill(127)
ds_nodata = gdal.GetDriverByName("MEM").Create("", 1024 * 10, 1024 * 10)
ds_nodata.GetRasterBand(1).SetNoDataValue(0)
ds_nodata.GetRasterBand(1).Fill(127)
ds_uint16 = gdal.GetDriverByName("MEM").Create(
"", 1024 * 10, 1024 * 10, 1, gdal.GDT_UInt16
)
ds_uint16.GetRasterBand(1).Fill(32767)
ds_uint16_16383 = gdal.GetDriverByName("MEM").Create(
"", 1024 * 10, 1024 * 10, 1, gdal.GDT_UInt16
)
ds_uint16_16383.GetRasterBand(1).Fill(16383)
ds_float32 = gdal.GetDriverByName("MEM").Create(
"", 1024 * 10, 1024 * 10, 1, gdal.GDT_Float32
)
ds_float32.GetRasterBand(1).Fill(32767)
NITERS = 50
def testNear(downsampling_factor):
ds.ReadRaster(
buf_xsize=ds.RasterXSize // downsampling_factor,
buf_ysize=ds.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_NearestNeighbour,
)
def testNearUInt16(downsampling_factor):
ds_uint16.ReadRaster(
buf_xsize=ds_uint16.RasterXSize // downsampling_factor,
buf_ysize=ds_uint16.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_NearestNeighbour,
)
def testNearFloat32(downsampling_factor):
ds_float32.ReadRaster(
buf_xsize=ds_float32.RasterXSize // downsampling_factor,
buf_ysize=ds_float32.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_NearestNeighbour,
)
def testAverage(downsampling_factor):
ds.ReadRaster(
buf_xsize=ds.RasterXSize // downsampling_factor,
buf_ysize=ds.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_Average,
)
def testAverageUInt16(downsampling_factor):
ds_uint16.ReadRaster(
buf_xsize=ds_uint16.RasterXSize // downsampling_factor,
buf_ysize=ds_uint16.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_Average,
)
def testAverageFloat32(downsampling_factor):
ds_float32.ReadRaster(
buf_xsize=ds_float32.RasterXSize // downsampling_factor,
buf_ysize=ds_float32.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_Average,
)
def testAverageNoData(downsampling_factor):
ds_nodata.ReadRaster(
buf_xsize=ds_nodata.RasterXSize // downsampling_factor,
buf_ysize=ds_nodata.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_Average,
)
def testRMS(downsampling_factor):
ds.ReadRaster(
buf_xsize=ds.RasterXSize // downsampling_factor,
buf_ysize=ds.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_RMS,
)
def testRMSUInt16(downsampling_factor):
ds_uint16.ReadRaster(
buf_xsize=ds_uint16.RasterXSize // downsampling_factor,
buf_ysize=ds_uint16.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_RMS,
)
# Test special case where all values are < 2^14.
def testRMSUInt16_16383(downsampling_factor):
ds_uint16_16383.ReadRaster(
buf_xsize=ds_uint16_16383.RasterXSize // downsampling_factor,
buf_ysize=ds_uint16_16383.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_RMS,
)
def testRMSFloat32(downsampling_factor):
ds_float32.ReadRaster(
buf_xsize=ds_float32.RasterXSize // downsampling_factor,
buf_ysize=ds_float32.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_RMS,
)
def testCubic(downsampling_factor):
ds.ReadRaster(
buf_xsize=ds.RasterXSize // downsampling_factor,
buf_ysize=ds.RasterYSize // downsampling_factor,
resample_alg=gdal.GRIORA_Cubic,
)
print(
"testNearUInt16(2): %.3f"
% timeit.timeit(
"testNearUInt16(2)", setup="from __main__ import testNearUInt16", number=NITERS
)
)
print(
"testAverageUInt16(2): %.3f"
% timeit.timeit(
"testAverageUInt16(2)",
setup="from __main__ import testAverageUInt16",
number=NITERS,
)
)
print(
"testRMSUInt16(2): %.3f"
% timeit.timeit(
"testRMSUInt16(2)", setup="from __main__ import testRMSUInt16", number=NITERS
)
)
print(
"testRMSUInt16_16383(2): %.3f"
% timeit.timeit(
"testRMSUInt16_16383(2)",
setup="from __main__ import testRMSUInt16_16383",
number=NITERS,
)
)
print(
"testNearFloat32(2): %.3f"
% timeit.timeit(
"testNearFloat32(2)",
setup="from __main__ import testNearFloat32",
number=NITERS,
)
)
print(
"testAverageFloat32(2): %.3f"
% timeit.timeit(
"testAverageFloat32(2)",
setup="from __main__ import testAverageFloat32",
number=NITERS,
)
)
print(
"testRMSFloat32(2): %.3f"
% timeit.timeit(
"testRMSFloat32(2)", setup="from __main__ import testRMSFloat32", number=NITERS
)
)
print(
"testNear(2): %.3f"
% timeit.timeit("testNear(2)", setup="from __main__ import testNear", number=NITERS)
)
print(
"testNear(4): %.3f"
% timeit.timeit("testNear(4)", setup="from __main__ import testNear", number=NITERS)
)
print(
"testAverage(2): %.3f"
% timeit.timeit(
"testAverage(2)", setup="from __main__ import testAverage", number=NITERS
)
)
print(
"testAverageNoData(2): %.3f"
% timeit.timeit(
"testAverageNoData(2)",
setup="from __main__ import testAverageNoData",
number=NITERS,
)
)
print(
"testAverage(4): %.3f"
% timeit.timeit(
"testAverage(4)", setup="from __main__ import testAverage", number=NITERS
)
)
print(
"testAverageNoData(4): %.3f"
% timeit.timeit(
"testAverageNoData(4)",
setup="from __main__ import testAverageNoData",
number=NITERS,
)
)
print(
"testRMS(2): %.3f"
% timeit.timeit("testRMS(2)", setup="from __main__ import testRMS", number=NITERS)
)
print(
"testRMS(4): %.3f"
% timeit.timeit("testRMS(4)", setup="from __main__ import testRMS", number=NITERS)
)
print(
"testCubic(2): %.3f"
% timeit.timeit(
"testCubic(2)", setup="from __main__ import testCubic", number=NITERS
)
)
print(
"testCubic(4): %.3f"
% timeit.timeit(
"testCubic(4)", setup="from __main__ import testCubic", number=NITERS
)
)