-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_rastergis.py
583 lines (436 loc) · 17.7 KB
/
test_rastergis.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import os
from shutil import copy2
import pytest
DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
RASTERGIS_DATA_DIR = os.path.join(DATA_DIR, "rastergis")
CLASSIFICATION_DATA_DIR = os.path.join(DATA_DIR, "classification")
IMGCALC_DATA_DIR = os.path.join(DATA_DIR, "imagecalc")
def test_get_rat_length():
import rsgislib.rastergis
ref_clumps_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_attref.kea"
)
n_clumps = rsgislib.rastergis.get_rat_length(ref_clumps_img)
assert n_clumps == 11949
def test_get_rat_columns():
import rsgislib.rastergis
ref_clumps_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_attref.kea"
)
rat_columns = rsgislib.rastergis.get_rat_columns(ref_clumps_img)
ref_columns = ["Histogram", "Red", "Green", "Blue", "Alpha"]
for i in [1, 2, 3, 4]:
ref_columns.append("b{}Min".format(i))
ref_columns.append("b{}Max".format(i))
ref_columns.append("b{}Mean".format(i))
ref_columns.append("b{}Sum".format(i))
ref_columns.append("b{}StdDev".format(i))
cols_match = True
if len(rat_columns) != len(ref_columns):
cols_match = False
print(
"Number of columns is different: {} != {}".format(
len(rat_columns), len(ref_columns)
)
)
if cols_match:
for col in rat_columns:
if col not in ref_columns:
print("Col '{}' is not within the reference list".format(col))
cols_match = False
break
assert cols_match
def test_get_rat_columns_info():
import rsgislib.rastergis
from osgeo import gdal
ref_clumps_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_attref.kea"
)
rat_columns_info = rsgislib.rastergis.get_rat_columns_info(ref_clumps_img)
correct_info = True
if rat_columns_info["Histogram"]["type"] != gdal.GFT_Real:
correct_info = False
if rat_columns_info["Histogram"]["usage"] != gdal.GFU_PixelCount:
correct_info = False
if rat_columns_info["Red"]["type"] != gdal.GFT_Integer:
correct_info = False
if rat_columns_info["Red"]["usage"] != gdal.GFU_Red:
correct_info = False
if rat_columns_info["b1Mean"]["type"] != gdal.GFT_Real:
correct_info = False
if rat_columns_info["b1Mean"]["usage"] != gdal.GFU_Generic:
correct_info = False
assert correct_info
def test_populate_rat_with_stats(tmp_path):
import rsgislib.rastergis
import numpy
base_clumps_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(base_clumps_img, clumps_img)
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
band_stats = list()
band_stats.append(
rsgislib.rastergis.BandAttStats(
band=1,
min_field="b1Min",
max_field="b1Max",
sum_field="b1Sum",
std_dev_field="b1StdDev",
mean_field="b1Mean",
)
)
band_stats.append(
rsgislib.rastergis.BandAttStats(
band=2,
min_field="b2Min",
max_field="b2Max",
sum_field="b2Sum",
std_dev_field="b2StdDev",
mean_field="b2Mean",
)
)
band_stats.append(
rsgislib.rastergis.BandAttStats(
band=3,
min_field="b3Min",
max_field="b3Max",
sum_field="b3Sum",
std_dev_field="b3StdDev",
mean_field="b3Mean",
)
)
band_stats.append(
rsgislib.rastergis.BandAttStats(
band=4,
min_field="b4Min",
max_field="b4Max",
sum_field="b4Sum",
std_dev_field="b4StdDev",
mean_field="b4Mean",
)
)
rsgislib.rastergis.populate_rat_with_stats(input_img, clumps_img, band_stats)
ref_clumps_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_attref.kea"
)
vars_to_test = [
"Histogram",
"b1Min",
"b2Max",
"b3Sum",
"b4StdDev",
"b1Mean",
"b2Mean",
"b3Mean",
"b4Mean",
]
vars_eq_vals = True
for var in vars_to_test:
print("Testing: {}".format(var))
ref_vals = rsgislib.rastergis.get_column_data(ref_clumps_img, var)
calcd_vals = rsgislib.rastergis.get_column_data(clumps_img, var)
if calcd_vals.shape[0] != ref_vals.shape[0]:
vars_eq_vals = False
break
if numpy.sum(calcd_vals) != numpy.sum(ref_vals):
vars_eq_vals = False
break
assert vars_eq_vals
def test_pop_rat_img_stats(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_nostats.kea"
)
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps_nostats.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.pop_rat_img_stats(
clumps_img, add_clr_tab=True, calc_pyramids=True, ignore_zero=True
)
def test_collapse_rat(tmp_path):
import rsgislib.rastergis
clumps_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_cls_out.kea"
)
output_img = os.path.join(tmp_path, "out_img.kea")
rsgislib.rastergis.collapse_rat(clumps_img, "OutClassName", output_img, "KEA")
assert os.path.exists(output_img)
def test_calc_border_length(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.calc_border_length(clumps_img, "out_col", True)
def test_calc_rel_border(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_cls_out.kea"
)
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps_cls_out.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.calc_rel_border(
clumps_img, "rel_border_forest", "OutClassName", "Forest", True
)
@pytest.mark.skipif(
True,
reason="Sometimes a seg fault with calc_rel_diff_neigh_stats which haven't found fix for yet.",
)
def test_calc_rel_diff_neigh_stats(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_cls_out.kea"
)
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps_cls_out.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.find_neighbours(clumps_img, 1)
fieldInfo = rsgislib.rastergis.FieldAttStats(
field="b8_mean", min_field="MinNIRMeanDiff", max_field="MaxNIRMeanDiff"
)
rsgislib.rastergis.calc_rel_diff_neigh_stats(clumps_img, fieldInfo, False, 1)
@pytest.mark.skipif(
True,
reason="Sometimes a seg fault with calc_rel_diff_neigh_stats which haven't found fix for yet.",
)
def test_calc_rel_diff_neigh_stats_abs(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_cls_out.kea"
)
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps_cls_out.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.find_neighbours(clumps_img, 1)
fieldInfo = rsgislib.rastergis.FieldAttStats(
field="b8_mean", min_field="MinNIRMeanDiff", max_field="MaxNIRMeanDiff"
)
rsgislib.rastergis.calc_rel_diff_neigh_stats(clumps_img, fieldInfo, True, 1)
def test_define_border_clumps(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.define_border_clumps(clumps_img, "out_col")
# TODO rsgislib.rastergis.define_clump_tile_positions
def test_find_boundary_pixels(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
output_img = os.path.join(tmp_path, "out_img.kea")
rsgislib.rastergis.find_boundary_pixels(clumps_img, output_img, "KEA", 1)
assert os.path.exists(output_img)
def test_find_neighbours(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.find_neighbours(clumps_img, 1)
# TODO rsgislib.rastergis.populate_rat_with_cat_proportions
def test_populate_rat_with_percentiles(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
band_percents = []
band_percents.append(
rsgislib.rastergis.BandAttPercentiles(percentile=25.0, field_name="B1Per25")
)
band_percents.append(
rsgislib.rastergis.BandAttPercentiles(percentile=50.0, field_name="B1Per50")
)
band_percents.append(
rsgislib.rastergis.BandAttPercentiles(percentile=75.0, field_name="B1Per75")
)
rsgislib.rastergis.populate_rat_with_percentiles(
input_img, clumps_img, 1, band_percents
)
def test_populate_rat_with_meanlit_stats(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_cls_out.kea"
)
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps_cls_out.kea")
copy2(input_ref_img, clumps_img)
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
input_ndvi_img = os.path.join(IMGCALC_DATA_DIR, "sen2_20210527_aber_ndvi.kea")
band_stats = list()
band_stats.append(rsgislib.rastergis.BandAttStats(band=1, mean_field="b1_meanlit"))
band_stats.append(rsgislib.rastergis.BandAttStats(band=2, mean_field="b2_meanlit"))
band_stats.append(rsgislib.rastergis.BandAttStats(band=3, mean_field="b3_meanlit"))
band_stats.append(rsgislib.rastergis.BandAttStats(band=4, mean_field="b4_meanlit"))
band_stats.append(rsgislib.rastergis.BandAttStats(band=5, mean_field="b5_meanlit"))
band_stats.append(rsgislib.rastergis.BandAttStats(band=6, mean_field="b6_meanlit"))
band_stats.append(rsgislib.rastergis.BandAttStats(band=7, mean_field="b7_meanlit"))
band_stats.append(rsgislib.rastergis.BandAttStats(band=8, mean_field="b8_meanlit"))
band_stats.append(rsgislib.rastergis.BandAttStats(band=9, mean_field="b9_meanlit"))
band_stats.append(
rsgislib.rastergis.BandAttStats(band=10, mean_field="b10_meanlit")
)
rsgislib.rastergis.populate_rat_with_meanlit_stats(
input_img,
clumps_img,
input_ndvi_img,
1,
"ndvi_mean",
"meanlit_pxl_ct",
band_stats,
)
# TODO rsgislib.rastergis.select_clumps_on_grid
def test_clumps_spatial_location(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.clumps_spatial_location(
clumps_img, eastings="eastings", northings="northings"
)
def test_clumps_spatial_extent(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.clumps_spatial_extent(
clumps_img,
min_xx="min_xx",
min_xy="min_xy",
max_xx="max_xx",
max_xy="max_xy",
min_yx="min_yx",
min_yy="min_yy",
max_yx="max_yx",
max_yy="max_yy",
rat_band=1,
)
def test_populate_rat_with_mode(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
in_cls_img = os.path.join(DATA_DIR, "sen2_20210527_aber_cls.kea")
rsgislib.rastergis.populate_rat_with_mode(
in_cls_img,
clumps_img,
out_cols_name="cls_val",
use_no_data=True,
no_data_val=0,
out_no_data=0,
mode_band=1,
rat_band=1,
)
def test_populate_rat_with_prop_valid_pxls(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
in_cls_img = os.path.join(DATA_DIR, "sen2_20210527_aber_cls.kea")
rsgislib.rastergis.populate_rat_with_prop_valid_pxls(
in_cls_img, clumps_img, out_col="cls_val", no_data_val=0, rat_band=1
)
def test_export_col_to_gdal_img(tmp_path):
import rsgislib.rastergis
clumps_img = os.path.join(
CLASSIFICATION_DATA_DIR, "sen2_20210527_aber_clumps_s2means.kea"
)
output_img = os.path.join(tmp_path, "out_img.kea")
rsgislib.rastergis.export_col_to_gdal_img(
clumps_img, output_img, "KEA", rsgislib.TYPE_32FLOAT, "b6Mean", rat_band=1
)
assert os.path.exists(output_img)
def test_export_cols_to_gdal_img(tmp_path):
import rsgislib.rastergis
clumps_img = os.path.join(
CLASSIFICATION_DATA_DIR, "sen2_20210527_aber_clumps_s2means.kea"
)
output_img = os.path.join(tmp_path, "out_img.kea")
rsgislib.rastergis.export_cols_to_gdal_img(
clumps_img,
output_img,
"KEA",
rsgislib.TYPE_32FLOAT,
["b1Mean", "b2Mean", "b3Mean"],
rat_band=1,
)
assert os.path.exists(output_img)
def test_export_rat_cols_to_ascii(tmp_path):
import rsgislib.rastergis
clumps_img = os.path.join(
CLASSIFICATION_DATA_DIR, "sen2_20210527_aber_clumps_s2means.kea"
)
out_file = os.path.join(tmp_path, "out_data.txt")
rsgislib.rastergis.export_rat_cols_to_ascii(
clumps_img, out_file, ["b1Mean", "b2Mean", "b3Mean"], rat_band=1
)
assert os.path.exists(out_file)
def test_export_clumps_to_images(tmp_path):
import rsgislib.rastergis
import glob
clumps_img = os.path.join(RASTERGIS_DATA_DIR, "sen2_grid_clumps.kea")
out_img_base = os.path.join(tmp_path, "out_img_")
rsgislib.rastergis.export_clumps_to_images(
clumps_img, out_img_base, True, "kea", "KEA", rat_band=1
)
assert len(glob.glob("{}*.kea".format(out_img_base))) == 4
def test_get_column_data():
import rsgislib.rastergis
import numpy
ref_clumps_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_attref.kea"
)
hist_col_vals = rsgislib.rastergis.get_column_data(ref_clumps_img, "Histogram")
hist_vals_range_ok = False
if (
(numpy.min(hist_col_vals) >= 0)
and (numpy.max(hist_col_vals) <= 80174)
and (hist_col_vals.shape[0] == 11949)
):
hist_col_vals = True
assert hist_col_vals
def test_set_column_data(tmp_path):
import rsgislib.rastergis
import numpy
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
n_rows = rsgislib.rastergis.get_rat_length(clumps_img)
uid_col = numpy.arange(0, n_rows, 1, dtype=numpy.uint32)
rsgislib.rastergis.set_column_data(clumps_img, "test_col", uid_col)
read_col_vals = rsgislib.rastergis.get_column_data(clumps_img, "test_col")
assert numpy.array_equal(read_col_vals, uid_col)
def test_create_uid_col(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(DATA_DIR, "sen2_20210527_aber_clumps.kea")
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps.kea")
copy2(input_ref_img, clumps_img)
rsgislib.rastergis.create_uid_col(clumps_img)
def test_get_global_class_stats(tmp_path):
import rsgislib.rastergis
input_ref_img = os.path.join(
RASTERGIS_DATA_DIR, "sen2_20210527_aber_clumps_cls_out.kea"
)
clumps_img = os.path.join(tmp_path, "sen2_20210527_aber_clumps_cls_out.kea")
copy2(input_ref_img, clumps_img)
change_feat_vals = list()
change_feat_vals.append(rsgislib.rastergis.ChangeFeats(cls_name="Forest"))
change_feat_vals.append(rsgislib.rastergis.ChangeFeats(cls_name="Grass"))
rsgislib.rastergis.get_global_class_stats(
clumps_img, "OutClassName", ["ndvi_mean"], change_feat_vals
)
# TODO rsgislib.rastergis.str_class_majority
# TODO rsgislib.rastergis.histo_sampling
# TODO rsgislib.rastergis.class_split_fit_hist_gausian_mixture_model
# TODO rsgislib.rastergis.apply_rat_knn
# TODO rsgislib.rastergis.get_global_class_stats
# TODO rsgislib.rastergis.fit_hist_gausian_mixture_model
# TODO rsgislib.rastergis.calc_1d_jm_distance
# TODO rsgislib.rastergis.calc_2d_jm_distance
# TODO rsgislib.rastergis.calc_bhattacharyya_distance
# TODO rsgislib.rastergis.copy_gdal_rat_columns
# TODO rsgislib.rastergis.copy_rat
# TODO rsgislib.rastergis.import_vec_atts
# TODO rsgislib.rastergis.colour_rat_classes
# TODO rsgislib.rastergis.define_class_names
# TODO rsgislib.rastergis.take_random_sample
# TODO rsgislib.rastergis.set_class_names_colours
# TODO rsgislib.rastergis.calc_dist_between_clumps
# TODO rsgislib.rastergis.calc_dist_to_large_clumps
# TODO rsgislib.rastergis.calc_dist_to_classes
# TODO rsgislib.rastergis.identify_small_units