-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_imageutils_stdimgblockiter.py
100 lines (85 loc) · 3.3 KB
/
test_imageutils_stdimgblockiter.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
import os
DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
def test_stdimgblockiter_ndvi(tmp_path):
import rsgislib.imageutils
import rsgislib.imageutils.stdimgblockiter
import numpy
import tqdm
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
output_img = os.path.join(tmp_path, "sen2_20210527_aber_ndvi.kea")
imgBandInfo = []
imgBandInfo.append(
rsgislib.imageutils.ImageBandInfo(
file_name=input_img, name="sen2", bands=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)
)
imgOutInfo = []
imgOutInfo.append(
rsgislib.imageutils.OutImageInfo(
file_name=output_img,
name="sen2_ndvi",
nbands=1,
no_data_val=-999,
gdalformat="KEA",
datatype=rsgislib.TYPE_32FLOAT,
)
)
img_iter = rsgislib.imageutils.stdimgblockiter.StdImgBlockIter(imgBandInfo, 64)
img_iter.create_output_imgs(imgOutInfo)
for x_block, y_block, out_img_data in tqdm.tqdm(img_iter):
ndvi = (
out_img_data["sen2"]["np_arr"][8] - out_img_data["sen2"]["np_arr"][3]
) / (out_img_data["sen2"]["np_arr"][8] + out_img_data["sen2"]["np_arr"][3])
out_data = {"sen2_ndvi": numpy.expand_dims(ndvi, axis=0)}
img_iter.write_block_to_image(x_block, y_block, out_data)
assert os.path.exists(output_img)
def test_stdimgblockiter_ndvi_ndwi(tmp_path):
import rsgislib.imageutils
import rsgislib.imageutils.stdimgblockiter
import numpy
import tqdm
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
out_ndvi_img = os.path.join(tmp_path, "sen2_20210527_aber_ndvi.kea")
out_ndwi_img = os.path.join(tmp_path, "sen2_20210527_aber_ndwi.kea")
imgBandInfo = []
imgBandInfo.append(
rsgislib.imageutils.ImageBandInfo(
file_name=input_img, name="sen2", bands=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)
)
imgOutInfo = []
imgOutInfo.append(
rsgislib.imageutils.OutImageInfo(
file_name=out_ndvi_img,
name="sen2_ndvi",
nbands=1,
no_data_val=-999,
gdalformat="KEA",
datatype=rsgislib.TYPE_32FLOAT,
)
)
imgOutInfo.append(
rsgislib.imageutils.OutImageInfo(
file_name=out_ndwi_img,
name="sen2_ndwi",
nbands=1,
no_data_val=-999,
gdalformat="KEA",
datatype=rsgislib.TYPE_32FLOAT,
)
)
img_iter = rsgislib.imageutils.stdimgblockiter.StdImgBlockIter(imgBandInfo, 64)
img_iter.create_output_imgs(imgOutInfo)
for x_block, y_block, out_img_data in tqdm.tqdm(img_iter):
ndvi = (
out_img_data["sen2"]["np_arr"][8] - out_img_data["sen2"]["np_arr"][3]
) / (out_img_data["sen2"]["np_arr"][8] + out_img_data["sen2"]["np_arr"][3])
ndwi = (
out_img_data["sen2"]["np_arr"][8] - out_img_data["sen2"]["np_arr"][9]
) / (out_img_data["sen2"]["np_arr"][8] + out_img_data["sen2"]["np_arr"][9])
out_data = {
"sen2_ndvi": numpy.expand_dims(ndvi, axis=0),
"sen2_ndwi": numpy.expand_dims(ndwi, axis=0),
}
img_iter.write_block_to_image(x_block, y_block, out_data)
assert os.path.exists(out_ndvi_img) and os.path.exists(out_ndwi_img)